Bitcoin Core  22.99.0
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 #include <condition_variable>
9 #include <functional>
10 #include <list>
11 #include <map>
12 #include <thread>
13 
14 #include <sync.h>
15 
34 {
35 public:
36  CScheduler();
37  ~CScheduler();
38 
39  std::thread m_service_thread;
40 
41  typedef std::function<void()> Function;
42 
44  void schedule(Function f, std::chrono::system_clock::time_point t);
45 
47  void scheduleFromNow(Function f, std::chrono::milliseconds delta)
48  {
49  schedule(std::move(f), std::chrono::system_clock::now() + delta);
50  }
51 
58  void scheduleEvery(Function f, std::chrono::milliseconds delta);
59 
65  void MockForward(std::chrono::seconds delta_seconds);
66 
70  void serviceQueue();
71 
73  void stop()
74  {
75  WITH_LOCK(newTaskMutex, stopRequested = true);
76  newTaskScheduled.notify_all();
77  if (m_service_thread.joinable()) m_service_thread.join();
78  }
81  {
82  WITH_LOCK(newTaskMutex, stopWhenEmpty = true);
83  newTaskScheduled.notify_all();
84  if (m_service_thread.joinable()) m_service_thread.join();
85  }
86 
91  size_t getQueueInfo(std::chrono::system_clock::time_point& first,
92  std::chrono::system_clock::time_point& last) const;
93 
95  bool AreThreadsServicingQueue() const;
96 
97 private:
99  std::condition_variable newTaskScheduled;
100  std::multimap<std::chrono::system_clock::time_point, Function> taskQueue GUARDED_BY(newTaskMutex);
101  int nThreadsServicingQueue GUARDED_BY(newTaskMutex){0};
102  bool stopRequested GUARDED_BY(newTaskMutex){false};
103  bool stopWhenEmpty GUARDED_BY(newTaskMutex){false};
104  bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex) { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
105 };
106 
118 {
119 private:
121 
123  std::list<std::function<void()>> m_callbacks_pending GUARDED_BY(m_cs_callbacks_pending);
124  bool m_are_callbacks_running GUARDED_BY(m_cs_callbacks_pending) = false;
125 
127  void ProcessQueue();
128 
129 public:
130  explicit SingleThreadedSchedulerClient(CScheduler* pschedulerIn) : m_pscheduler(pschedulerIn) {}
131 
138  void AddToProcessQueue(std::function<void()> func);
139 
144  void EmptyQueue();
145 
146  size_t CallbacksPending();
147 };
148 
149 #endif
CScheduler::newTaskScheduled
std::condition_variable newTaskScheduled
Definition: scheduler.h:99
CScheduler::GUARDED_BY
int nThreadsServicingQueue GUARDED_BY(newTaskMutex)
Definition: scheduler.h:101
CScheduler
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:33
CScheduler::MockForward
void MockForward(std::chrono::seconds delta_seconds)
Mock the scheduler to fast forward in time.
Definition: scheduler.cpp:82
SingleThreadedSchedulerClient::m_pscheduler
CScheduler * m_pscheduler
Definition: scheduler.h:120
CScheduler::GUARDED_BY
bool stopWhenEmpty GUARDED_BY(newTaskMutex)
Definition: scheduler.h:103
CScheduler::newTaskMutex
Mutex newTaskMutex
Definition: scheduler.h:98
sync.h
CScheduler::CScheduler
CScheduler()
Definition: scheduler.cpp:14
SingleThreadedSchedulerClient::CallbacksPending
size_t CallbacksPending()
Definition: scheduler.cpp:200
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:270
AnnotatedMixin< std::mutex >
SingleThreadedSchedulerClient::ProcessQueue
void ProcessQueue()
Definition: scheduler.cpp:147
CScheduler::~CScheduler
~CScheduler()
Definition: scheduler.cpp:18
CScheduler::serviceQueue
void serviceQueue()
Services the queue 'forever'.
Definition: scheduler.cpp:25
SingleThreadedSchedulerClient::SingleThreadedSchedulerClient
SingleThreadedSchedulerClient(CScheduler *pschedulerIn)
Definition: scheduler.h:130
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:115
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:41
CScheduler::GUARDED_BY
bool stopRequested GUARDED_BY(newTaskMutex)
Definition: scheduler.h:102
CScheduler::stop
void stop()
Tell any threads running serviceQueue to stop as soon as the current task is done.
Definition: scheduler.h:73
CScheduler::scheduleEvery
void scheduleEvery(Function f, std::chrono::milliseconds delta)
Repeat f until the scheduler is stopped.
Definition: scheduler.cpp:110
CScheduler::m_service_thread
std::thread m_service_thread
Definition: scheduler.h:39
SingleThreadedSchedulerClient::m_cs_callbacks_pending
RecursiveMutex m_cs_callbacks_pending
Definition: scheduler.h:122
SingleThreadedSchedulerClient::EmptyQueue
void EmptyQueue()
Processes all remaining queue members on the calling thread, blocking until queue is empty Must be ca...
Definition: scheduler.cpp:189
CScheduler::StopWhenDrained
void StopWhenDrained()
Tell any threads running serviceQueue to stop when there is no work left to be done.
Definition: scheduler.h:80
SingleThreadedSchedulerClient::AddToProcessQueue
void AddToProcessQueue(std::function< void()> func)
Add a callback to be executed.
Definition: scheduler.cpp:178
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CScheduler::shouldStop
bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex)
Definition: scheduler.h:104
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:117
CScheduler::AreThreadsServicingQueue
bool AreThreadsServicingQueue() const
Returns true if there are threads actively running in serviceQueue()
Definition: scheduler.cpp:127
CScheduler::schedule
void schedule(Function f, std::chrono::system_clock::time_point t)
Call func at/after time t.
Definition: scheduler.cpp:73