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{
35public:
36 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
97private:
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{
119private:
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
129public:
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
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:34
bool stopWhenEmpty GUARDED_BY(newTaskMutex)
Definition: scheduler.h:103
void serviceQueue()
Services the queue 'forever'.
Definition: scheduler.cpp:26
bool stopRequested GUARDED_BY(newTaskMutex)
Definition: scheduler.h:102
std::multimap< std::chrono::system_clock::time_point, Function > taskQueue GUARDED_BY(newTaskMutex)
void scheduleFromNow(Function f, std::chrono::milliseconds delta)
Call f once after the delta has passed.
Definition: scheduler.h:47
void schedule(Function f, std::chrono::system_clock::time_point t)
Call func at/after time t.
Definition: scheduler.cpp:75
int nThreadsServicingQueue GUARDED_BY(newTaskMutex)
Definition: scheduler.h:101
void scheduleEvery(Function f, std::chrono::milliseconds delta)
Repeat f until the scheduler is stopped.
Definition: scheduler.cpp:112
void StopWhenDrained()
Tell any threads running serviceQueue to stop when there is no work left to be done.
Definition: scheduler.h:80
std::function< void()> Function
Definition: scheduler.h:41
void MockForward(std::chrono::seconds delta_seconds)
Mock the scheduler to fast forward in time.
Definition: scheduler.cpp:84
std::thread m_service_thread
Definition: scheduler.h:39
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:117
bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex)
Definition: scheduler.h:104
void stop()
Tell any threads running serviceQueue to stop as soon as the current task is done.
Definition: scheduler.h:73
bool AreThreadsServicingQueue() const
Returns true if there are threads actively running in serviceQueue()
Definition: scheduler.cpp:129
std::condition_variable newTaskScheduled
Definition: scheduler.h:99
Mutex newTaskMutex
Definition: scheduler.h:98
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:118
void EmptyQueue()
Processes all remaining queue members on the calling thread, blocking until queue is empty Must be ca...
Definition: scheduler.cpp:191
void AddToProcessQueue(std::function< void()> func)
Add a callback to be executed.
Definition: scheduler.cpp:180
std::list< std::function< void()> > m_callbacks_pending GUARDED_BY(m_cs_callbacks_pending)
bool m_are_callbacks_running GUARDED_BY(m_cs_callbacks_pending)
RecursiveMutex m_cs_callbacks_pending
Definition: scheduler.h:122
SingleThreadedSchedulerClient(CScheduler *pschedulerIn)
Definition: scheduler.h:130
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:270
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49