diff options
author | Matt Corallo <git@bluematt.me> | 2017-04-10 14:55:49 -0400 |
---|---|---|
committer | Matt Corallo <git@bluematt.me> | 2017-07-07 11:33:18 -0400 |
commit | 08096bbbc6d6fef86943ca8ce5e6de18744d58ea (patch) | |
tree | b1199ad23ceb259db07d8f3fb3b6dbe4c996fefa /src/scheduler.h | |
parent | 2fbf2dbe151e135586cc1bb05b891f2c8ab6c817 (diff) |
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.
Diffstat (limited to 'src/scheduler.h')
-rw-r--r-- | src/scheduler.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/scheduler.h b/src/scheduler.h index 5da6e6f69f..82036afdf0 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -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 |