Bitcoin Core  0.19.99
P2P Digital Currency
scheduler.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 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.
4 
5 #ifndef BITCOIN_SCHEDULER_H
6 #define BITCOIN_SCHEDULER_H
7 
8 //
9 // NOTE:
10 // boost::thread / boost::chrono should be ported to std::thread / std::chrono
11 // when we support C++11.
12 //
13 #include <boost/chrono/chrono.hpp>
14 #include <boost/thread.hpp>
15 #include <map>
16 
17 #include <sync.h>
18 
19 //
20 // Simple class for background tasks that should be run
21 // periodically or once "after a while"
22 //
23 // Usage:
24 //
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(std::bind(CScheduler::serviceQueue, s));
29 //
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.
35 //
36 
38 {
39 public:
40  CScheduler();
41  ~CScheduler();
42 
43  typedef std::function<void()> Function;
44 
45  // Call func at/after time t
46  void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now());
47 
48  // Convenience method: call f once deltaMilliSeconds from now
49  void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
50 
51  // Another convenience method: call f approximately
52  // every deltaMilliSeconds forever, starting deltaMilliSeconds from now.
53  // To be more precise: every time f is finished, it
54  // is rescheduled to run deltaMilliSeconds later. If you
55  // need more accurate scheduling, don't use this method.
56  void scheduleEvery(Function f, int64_t deltaMilliSeconds);
57 
63  void MockForward(boost::chrono::seconds delta_seconds);
64 
65  // To keep things as simple as possible, there is no unschedule.
66 
67  // Services the queue 'forever'. Should be run in a thread,
68  // and interrupted using boost::interrupt_thread
69  void serviceQueue();
70 
71  // Tell any threads running serviceQueue to stop as soon as they're
72  // done servicing whatever task they're currently servicing (drain=false)
73  // or when there is no work left to be done (drain=true)
74  void stop(bool drain=false);
75 
76  // Returns number of tasks waiting to be serviced,
77  // and first and last task times
80 
81  // Returns true if there are threads actively running in serviceQueue()
82  bool AreThreadsServicingQueue() const;
83 
84 private:
85  std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
86  boost::condition_variable newTaskScheduled;
87  mutable boost::mutex newTaskMutex;
91  bool shouldStop() const { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
92 };
93 
105 private:
107 
109  std::list<std::function<void ()>> m_callbacks_pending GUARDED_BY(m_cs_callbacks_pending);
110  bool m_are_callbacks_running GUARDED_BY(m_cs_callbacks_pending) = false;
111 
112  void MaybeScheduleProcessQueue();
113  void ProcessQueue();
114 
115 public:
116  explicit SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {}
117 
124  void AddToProcessQueue(std::function<void ()> func);
125 
126  // Processes all remaining queue members on the calling thread, blocking until queue is empty
127  // Must be called after the CScheduler has no remaining processing threads!
128  void EmptyQueue();
129 
130  size_t CallbacksPending();
131 };
132 
133 #endif
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:104
bool stopWhenEmpty
Definition: scheduler.h:90
std::multimap< boost::chrono::system_clock::time_point, Function > taskQueue
Definition: scheduler.h:85
bool shouldStop() const
Definition: scheduler.h:91
void scheduleEvery(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:145
std::function< void()> Function
Definition: scheduler.h:43
void scheduleFromNow(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:112
void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now())
Definition: scheduler.cpp:103
void stop(bool drain=false)
Definition: scheduler.cpp:91
RecursiveMutex m_cs_callbacks_pending
Definition: scheduler.h:108
SingleThreadedSchedulerClient(CScheduler *pschedulerIn)
Definition: scheduler.h:116
boost::condition_variable newTaskScheduled
Definition: scheduler.h:86
void serviceQueue()
Definition: scheduler.cpp:32
void MockForward(boost::chrono::seconds delta_seconds)
Mock the scheduler to fast forward in time.
Definition: scheduler.cpp:117
boost::mutex newTaskMutex
Definition: scheduler.h:87
bool stopRequested
Definition: scheduler.h:89
bool AreThreadsServicingQueue() const
Definition: scheduler.cpp:162
clock::time_point time_point
Definition: bench.h:51
#define GUARDED_BY(x)
Definition: threadsafety.h:38
int nThreadsServicingQueue
Definition: scheduler.h:88
size_t getQueueInfo(boost::chrono::system_clock::time_point &first, boost::chrono::system_clock::time_point &last) const
Definition: scheduler.cpp:150