mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
Support more than one CScheduler thread for serial clients
This will be used by CValidationInterface soon. This requires a bit of work as we need to ensure that most of our callbacks happen in-order (to avoid synchronization issues in wallet) - we keep our own internal queue and push things onto it, scheduling a queue-draining function immediately upon new callbacks.
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
#include <boost/thread.hpp>
|
||||
#include <map>
|
||||
|
||||
#include "sync.h"
|
||||
|
||||
//
|
||||
// Simple class for background tasks that should be run
|
||||
// periodically or once "after a while"
|
||||
@@ -79,4 +81,26 @@ private:
|
||||
bool shouldStop() { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
|
||||
};
|
||||
|
||||
/**
|
||||
* Class used by CScheduler clients which may schedule multiple jobs
|
||||
* which are required to be run serially. Does not require such jobs
|
||||
* to be executed on the same thread, but no two jobs will be executed
|
||||
* at the same time.
|
||||
*/
|
||||
class SingleThreadedSchedulerClient {
|
||||
private:
|
||||
CScheduler *m_pscheduler;
|
||||
|
||||
CCriticalSection m_cs_callbacks_pending;
|
||||
std::list<std::function<void (void)>> m_callbacks_pending;
|
||||
bool m_are_callbacks_running = false;
|
||||
|
||||
void MaybeScheduleProcessQueue();
|
||||
void ProcessQueue();
|
||||
|
||||
public:
|
||||
SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {}
|
||||
void AddToProcessQueue(std::function<void (void)> func);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user