Bitcoin ABC  0.24.7
P2P Digital Currency
scheduler.h
Go to the documentation of this file.
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.
4 
5 #ifndef BITCOIN_SCHEDULER_H
6 #define BITCOIN_SCHEDULER_H
7 
8 #include <sync.h>
9 
10 #include <condition_variable>
11 #include <functional>
12 #include <list>
13 #include <map>
14 
35 class CScheduler {
36 public:
37  CScheduler();
38  ~CScheduler();
39 
40  typedef std::function<void()> Function;
41  typedef std::function<bool()> Predicate;
42 
44  void schedule(Function f, std::chrono::system_clock::time_point t);
45 
47  void scheduleFromNow(Function f, std::chrono::milliseconds delta) {
48  schedule(std::move(f), std::chrono::system_clock::now() + delta);
49  }
50 
58  void scheduleEvery(Predicate p, std::chrono::milliseconds delta);
59 
65  void MockForward(std::chrono::seconds delta_seconds);
66 
71  void serviceQueue();
72 
77  void stop() {
78  WITH_LOCK(newTaskMutex, stopRequested = true);
79  newTaskScheduled.notify_all();
80  }
81 
86  void StopWhenDrained() {
87  WITH_LOCK(newTaskMutex, stopWhenEmpty = true);
88  newTaskScheduled.notify_all();
89  }
90 
95  size_t getQueueInfo(std::chrono::system_clock::time_point &first,
96  std::chrono::system_clock::time_point &last) const;
97 
99  bool AreThreadsServicingQueue() const;
100 
101 private:
103  std::condition_variable newTaskScheduled;
104  std::multimap<std::chrono::system_clock::time_point, Function>
105  taskQueue GUARDED_BY(newTaskMutex);
106  int nThreadsServicingQueue GUARDED_BY(newTaskMutex){0};
107  bool stopRequested GUARDED_BY(newTaskMutex){false};
108  bool stopWhenEmpty GUARDED_BY(newTaskMutex){false};
110  return stopRequested || (stopWhenEmpty && taskQueue.empty());
111  }
112 };
113 
125 private:
127 
129  std::list<std::function<void()>>
130  m_callbacks_pending GUARDED_BY(m_cs_callbacks_pending);
131  bool m_are_callbacks_running GUARDED_BY(m_cs_callbacks_pending) = false;
132 
134  void ProcessQueue();
135 
136 public:
138  : m_pscheduler(pschedulerIn) {}
139 
146  void AddToProcessQueue(std::function<void()> func);
147 
153  void EmptyQueue();
154 
155  size_t CallbacksPending();
156 };
157 
158 #endif // BITCOIN_SCHEDULER_H
CScheduler::newTaskScheduled
std::condition_variable newTaskScheduled
Definition: scheduler.h:103
CScheduler::GUARDED_BY
int nThreadsServicingQueue GUARDED_BY(newTaskMutex)
Definition: scheduler.h:106
CScheduler
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:35
CScheduler::MockForward
void MockForward(std::chrono::seconds delta_seconds)
Mock the scheduler to fast forward in time.
Definition: scheduler.cpp:81
SingleThreadedSchedulerClient::m_pscheduler
CScheduler * m_pscheduler
Definition: scheduler.h:126
CScheduler::GUARDED_BY
bool stopWhenEmpty GUARDED_BY(newTaskMutex)
Definition: scheduler.h:108
CScheduler::newTaskMutex
Mutex newTaskMutex
Definition: scheduler.h:102
sync.h
CScheduler::CScheduler
CScheduler()
Definition: scheduler.cpp:12
SingleThreadedSchedulerClient::CallbacksPending
size_t CallbacksPending()
Definition: scheduler.cpp:208
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:272
AnnotatedMixin< std::mutex >
SingleThreadedSchedulerClient::ProcessQueue
void ProcessQueue()
Definition: scheduler.cpp:152
CScheduler::~CScheduler
~CScheduler()
Definition: scheduler.cpp:14
CScheduler::Predicate
std::function< bool()> Predicate
Definition: scheduler.h:41
CScheduler::serviceQueue
void serviceQueue()
Services the queue 'forever'.
Definition: scheduler.cpp:21
CScheduler::scheduleEvery
void scheduleEvery(Predicate p, std::chrono::milliseconds delta)
Repeat p until it return false.
Definition: scheduler.cpp:112
SingleThreadedSchedulerClient::SingleThreadedSchedulerClient
SingleThreadedSchedulerClient(CScheduler *pschedulerIn)
Definition: scheduler.h:137
CScheduler::getQueueInfo
size_t getQueueInfo(std::chrono::system_clock::time_point &first, std::chrono::system_clock::time_point &last) const
Returns number of tasks waiting to be serviced, and first and last task times.
Definition: scheduler.cpp:118
SingleThreadedSchedulerClient::GUARDED_BY
std::list< std::function< void()> > m_callbacks_pending GUARDED_BY(m_cs_callbacks_pending)
CScheduler::GUARDED_BY
std::multimap< std::chrono::system_clock::time_point, Function > taskQueue GUARDED_BY(newTaskMutex)
CScheduler::Function
std::function< void()> Function
Definition: scheduler.h:40
CScheduler::GUARDED_BY
bool stopRequested GUARDED_BY(newTaskMutex)
Definition: scheduler.h:107
CScheduler::stop
void stop()
Tell any threads running serviceQueue to stop as soon as the current task is done.
Definition: scheduler.h:77
SingleThreadedSchedulerClient::m_cs_callbacks_pending
RecursiveMutex m_cs_callbacks_pending
Definition: scheduler.h:128
SingleThreadedSchedulerClient::EmptyQueue
void EmptyQueue()
Processes all remaining queue members on the calling thread, blocking until queue is empty.
Definition: scheduler.cpp:198
CScheduler::StopWhenDrained
void StopWhenDrained()
Tell any threads running serviceQueue to stop when there is no work left to be done.
Definition: scheduler.h:86
SingleThreadedSchedulerClient::AddToProcessQueue
void AddToProcessQueue(std::function< void()> func)
Add a callback to be executed.
Definition: scheduler.cpp:187
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
CScheduler::shouldStop
bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex)
Definition: scheduler.h:109
SingleThreadedSchedulerClient::MaybeScheduleProcessQueue
void MaybeScheduleProcessQueue()
Definition: scheduler.cpp:134
CScheduler::scheduleFromNow
void scheduleFromNow(Function f, std::chrono::milliseconds delta)
Call f once after the delta has passed.
Definition: scheduler.h:47
SingleThreadedSchedulerClient
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:124
CScheduler::AreThreadsServicingQueue
bool AreThreadsServicingQueue() const
Returns true if there are threads actively running in serviceQueue()
Definition: scheduler.cpp:129
CScheduler::schedule
void schedule(Function f, std::chrono::system_clock::time_point t)
Call func at/after time t.
Definition: scheduler.cpp:72