PRCYCoin  2.0.0.7rc1
P2P Digital Currency
scheduler.h
Go to the documentation of this file.
1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Copyright (c) 2015-2018 The PIVX developers
3 // Copyright (c) 2018-2020 The DAPS Project developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef BITCOIN_SCHEDULER_H
8 #define BITCOIN_SCHEDULER_H
9 
10 //
11 // NOTE:
12 // boost::thread / boost::function / boost::chrono should be ported to
13 // std::thread / std::function / std::chrono when we support C++11.
14 //
15 #include <boost/function.hpp>
16 #include <boost/chrono/chrono.hpp>
17 #include <boost/thread.hpp>
18 #include <map>
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(boost::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 class CScheduler {
38 public:
39  CScheduler();
40 
41  ~CScheduler();
42 
43  typedef boost::function<void(void)> Function;
44 
45  // Call func at/after time t
46  void schedule(Function f, boost::chrono::system_clock::time_point t);
47 
48  // Convenience method: call f once deltaSeconds from now
49  void scheduleFromNow(Function f, int64_t deltaSeconds);
50 
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 deltaSeconds);
57 
58  // To keep things as simple as possible, there is no unschedule.
59  // Services the queue 'forever'. Should be run in a thread,
60  // and interrupted using boost::interrupt_thread
61  void serviceQueue();
62 
63  // Tell any threads running serviceQueue to stop as soon as they're
64  // done servicing whatever task they're currently servicing (drain=false)
65  // or when there is no work left to be done (drain=true)
66  void stop(bool drain = false);
67 
68  // Returns number of tasks waiting to be serviced,
69  // and first and last task times
70  size_t getQueueInfo(boost::chrono::system_clock::time_point &first,
71  boost::chrono::system_clock::time_point &last) const;
72 
73 private:
74  std::multimap <boost::chrono::system_clock::time_point, Function> taskQueue;
75  boost::condition_variable newTaskScheduled;
76  mutable boost::mutex newTaskMutex;
80 
81  bool shouldStop() { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
82 };
83 
84 #endif
CScheduler
Definition: scheduler.h:37
CScheduler::shouldStop
bool shouldStop()
Definition: scheduler.h:81
CScheduler::CScheduler
CScheduler()
Definition: scheduler.cpp:14
CScheduler::scheduleFromNow
void scheduleFromNow(Function f, int64_t deltaSeconds)
Definition: scheduler.cpp:86
CScheduler::stopRequested
bool stopRequested
Definition: scheduler.h:78
CScheduler::~CScheduler
~CScheduler()
Definition: scheduler.cpp:17
CScheduler::newTaskScheduled
boost::condition_variable newTaskScheduled
Definition: scheduler.h:75
CScheduler::getQueueInfo
size_t getQueueInfo(boost::chrono::system_clock::time_point &first, boost::chrono::system_clock::time_point &last) const
Definition: scheduler.cpp:99
CScheduler::serviceQueue
void serviceQueue()
Definition: scheduler.cpp:21
CScheduler::nThreadsServicingQueue
int nThreadsServicingQueue
Definition: scheduler.h:77
CScheduler::taskQueue
std::multimap< boost::chrono::system_clock::time_point, Function > taskQueue
Definition: scheduler.h:74
CScheduler::stopWhenEmpty
bool stopWhenEmpty
Definition: scheduler.h:79
CScheduler::newTaskMutex
boost::mutex newTaskMutex
Definition: scheduler.h:76
CScheduler::Function
boost::function< void(void)> Function
Definition: scheduler.h:43
CScheduler::schedule
void schedule(Function f, boost::chrono::system_clock::time_point t)
Definition: scheduler.cpp:78
CScheduler::scheduleEvery
void scheduleEvery(Function f, int64_t deltaSeconds)
Definition: scheduler.cpp:95
CScheduler::stop
void stop(bool drain=false)
Definition: scheduler.cpp:67