Raven Core  3.0.0
P2P Digital Currency
scheduler.h
Go to the documentation of this file.
1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_SCHEDULER_H
7 #define RAVEN_SCHEDULER_H
8 
9 //
10 // NOTE:
11 // boost::thread / boost::chrono should be ported to std::thread / std::chrono
12 // when we support C++11.
13 //
14 #include <boost/chrono/chrono.hpp>
15 #include <boost/thread.hpp>
16 #include <map>
17 
18 #include "sync.h"
19 
20 //
21 // Simple class for background tasks that should be run
22 // periodically or once "after a while"
23 //
24 // Usage:
25 //
26 // CScheduler* s = new CScheduler();
27 // s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { }
28 // s->scheduleFromNow(std::bind(Class::func, this, argument), 3);
29 // boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, s));
30 //
31 // ... then at program shutdown, clean up the thread running serviceQueue:
32 // t->interrupt();
33 // t->join();
34 // delete t;
35 // delete s; // Must be done after thread is interrupted/joined.
36 //
37 
39 {
40 public:
41  CScheduler();
42  ~CScheduler();
43 
44  typedef std::function<void(void)> Function;
45 
46  // Call func at/after time t
47  void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now());
48 
49  // Convenience method: call f once deltaSeconds from now
50  void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
51 
52  // Another convenience method: call f approximately
53  // every deltaSeconds forever, starting deltaSeconds from now.
54  // To be more precise: every time f is finished, it
55  // is rescheduled to run deltaSeconds later. If you
56  // need more accurate scheduling, don't use this method.
57  void scheduleEvery(Function f, int64_t deltaMilliSeconds);
58 
59  // To keep things as simple as possible, there is no unschedule.
60 
61  // Services the queue 'forever'. Should be run in a thread,
62  // and interrupted using boost::interrupt_thread
63  void serviceQueue();
64 
65  // Tell any threads running serviceQueue to stop as soon as they're
66  // done servicing whatever task they're currently servicing (drain=false)
67  // or when there is no work left to be done (drain=true)
68  void stop(bool drain=false);
69 
70  // Returns number of tasks waiting to be serviced,
71  // and first and last task times
72  size_t getQueueInfo(boost::chrono::system_clock::time_point &first,
73  boost::chrono::system_clock::time_point &last) const;
74 
75  // Returns true if there are threads actively running in serviceQueue()
76  bool AreThreadsServicingQueue() const;
77 
78 private:
79  std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
80  boost::condition_variable newTaskScheduled;
81  mutable boost::mutex newTaskMutex;
85  bool shouldStop() const { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
86 };
87 
95 private:
97 
99  std::list<std::function<void (void)>> m_callbacks_pending;
100  bool m_are_callbacks_running = false;
101 
102  void MaybeScheduleProcessQueue();
103  void ProcessQueue();
104 
105 public:
106  explicit SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {}
107  void AddToProcessQueue(std::function<void (void)> func);
108 
109  // Processes all remaining queue members on the calling thread, blocking until queue is empty
110  // Must be called after the CScheduler has no remaining processing threads!
111  void EmptyQueue();
112 };
113 
114 #endif
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:94
bool stopWhenEmpty
Definition: scheduler.h:84
std::multimap< boost::chrono::system_clock::time_point, Function > taskQueue
Definition: scheduler.h:79
bool shouldStop() const
Definition: scheduler.h:85
void scheduleEvery(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:127
void scheduleFromNow(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:116
void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now())
Definition: scheduler.cpp:107
void stop(bool drain=false)
Definition: scheduler.cpp:95
std::function< void(void)> Function
Definition: scheduler.h:44
SingleThreadedSchedulerClient(CScheduler *pschedulerIn)
Definition: scheduler.h:106
boost::condition_variable newTaskScheduled
Definition: scheduler.h:80
void serviceQueue()
Definition: scheduler.cpp:34
boost::mutex newTaskMutex
Definition: scheduler.h:81
std::list< std::function< void(void)> > m_callbacks_pending
Definition: scheduler.h:99
bool stopRequested
Definition: scheduler.h:83
CCriticalSection m_cs_callbacks_pending
Definition: scheduler.h:98
bool AreThreadsServicingQueue() const
Definition: scheduler.cpp:144
int nThreadsServicingQueue
Definition: scheduler.h:82
size_t getQueueInfo(boost::chrono::system_clock::time_point &first, boost::chrono::system_clock::time_point &last) const
Definition: scheduler.cpp:132
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Definition: sync.h:92