Support more than one CScheduler thread for serial clients
[bitcoinplatinum.git] / src / scheduler.h
blob82036afdf02225185d2510a66a0597eb11414313
1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_SCHEDULER_H
6 #define BITCOIN_SCHEDULER_H
8 //
9 // NOTE:
10 // boost::thread / boost::chrono should be ported to std::thread / std::chrono
11 // when we support C++11.
13 #include <boost/chrono/chrono.hpp>
14 #include <boost/thread.hpp>
15 #include <map>
17 #include "sync.h"
20 // Simple class for background tasks that should be run
21 // periodically or once "after a while"
23 // Usage:
25 // CScheduler* s = new CScheduler();
26 // s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { }
27 // s->scheduleFromNow(std::bind(Class::func, this, argument), 3);
28 // boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, s));
30 // ... then at program shutdown, clean up the thread running serviceQueue:
31 // t->interrupt();
32 // t->join();
33 // delete t;
34 // delete s; // Must be done after thread is interrupted/joined.
37 class CScheduler
39 public:
40 CScheduler();
41 ~CScheduler();
43 typedef std::function<void(void)> Function;
45 // Call func at/after time t
46 void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now());
48 // Convenience method: call f once deltaSeconds from now
49 void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
51 // Another convenience method: call f approximately
52 // every deltaSeconds forever, starting deltaSeconds from now.
53 // To be more precise: every time f is finished, it
54 // is rescheduled to run deltaSeconds later. If you
55 // need more accurate scheduling, don't use this method.
56 void scheduleEvery(Function f, int64_t deltaMilliSeconds);
58 // To keep things as simple as possible, there is no unschedule.
60 // Services the queue 'forever'. Should be run in a thread,
61 // and interrupted using boost::interrupt_thread
62 void serviceQueue();
64 // Tell any threads running serviceQueue to stop as soon as they're
65 // done servicing whatever task they're currently servicing (drain=false)
66 // or when there is no work left to be done (drain=true)
67 void stop(bool drain=false);
69 // Returns number of tasks waiting to be serviced,
70 // and first and last task times
71 size_t getQueueInfo(boost::chrono::system_clock::time_point &first,
72 boost::chrono::system_clock::time_point &last) const;
74 private:
75 std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
76 boost::condition_variable newTaskScheduled;
77 mutable boost::mutex newTaskMutex;
78 int nThreadsServicingQueue;
79 bool stopRequested;
80 bool stopWhenEmpty;
81 bool shouldStop() { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
84 /**
85 * Class used by CScheduler clients which may schedule multiple jobs
86 * which are required to be run serially. Does not require such jobs
87 * to be executed on the same thread, but no two jobs will be executed
88 * at the same time.
90 class SingleThreadedSchedulerClient {
91 private:
92 CScheduler *m_pscheduler;
94 CCriticalSection m_cs_callbacks_pending;
95 std::list<std::function<void (void)>> m_callbacks_pending;
96 bool m_are_callbacks_running = false;
98 void MaybeScheduleProcessQueue();
99 void ProcessQueue();
101 public:
102 SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {}
103 void AddToProcessQueue(std::function<void (void)> func);
106 #endif