Bitcoin Core 28.99.0
P2P Digital Currency
|
Simple class for background tasks that should be run periodically or once "after a while". More...
#include <scheduler.h>
Public Types | |
typedef std::function< void()> | Function |
Public Member Functions | |
CScheduler () | |
~CScheduler () | |
void | schedule (Function f, std::chrono::steady_clock::time_point t) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Call func at/after time t. More... | |
void | scheduleFromNow (Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Call f once after the delta has passed. More... | |
void | scheduleEvery (Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Repeat f until the scheduler is stopped. More... | |
void | MockForward (std::chrono::seconds delta_seconds) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Mock the scheduler to fast forward in time. More... | |
void | serviceQueue () EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Services the queue 'forever'. More... | |
void | stop () EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Tell any threads running serviceQueue to stop as soon as the current task is done. More... | |
void | StopWhenDrained () EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Tell any threads running serviceQueue to stop when there is no work left to be done. More... | |
size_t | getQueueInfo (std::chrono::steady_clock::time_point &first, std::chrono::steady_clock::time_point &last) const EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Returns number of tasks waiting to be serviced, and first and last task times. More... | |
bool | AreThreadsServicingQueue () const EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex) |
Returns true if there are threads actively running in serviceQueue() More... | |
Public Attributes | |
std::thread | m_service_thread |
Private Member Functions | |
std::multimap< std::chrono::steady_clock::time_point, Function > taskQueue | GUARDED_BY (newTaskMutex) |
int nThreadsServicingQueue | GUARDED_BY (newTaskMutex) |
bool stopRequested | GUARDED_BY (newTaskMutex) |
bool stopWhenEmpty | GUARDED_BY (newTaskMutex) |
bool | shouldStop () const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex) |
Private Attributes | |
Mutex | newTaskMutex |
std::condition_variable | newTaskScheduled |
Simple class for background tasks that should be run periodically or once "after a while".
Usage:
CScheduler* s = new CScheduler(); s->scheduleFromNow(doSomething, std::chrono::milliseconds{11}); // Assuming a: void doSomething() { } s->scheduleFromNow([=] { this->func(argument); }, std::chrono::milliseconds{3}); std::thread* t = new std::thread([&] { s->serviceQueue(); });
... then at program shutdown, make sure to call stop() to clean up the thread(s) running serviceQueue: s->stop(); t->join(); delete t; delete s; // Must be done after thread is interrupted/joined.
Definition at line 39 of file scheduler.h.
typedef std::function<void()> CScheduler::Function |
Definition at line 47 of file scheduler.h.
|
default |
CScheduler::~CScheduler | ( | ) |
bool CScheduler::AreThreadsServicingQueue | ( | ) | const |
Returns true if there are threads actively running in serviceQueue()
Definition at line 125 of file scheduler.cpp.
size_t CScheduler::getQueueInfo | ( | std::chrono::steady_clock::time_point & | first, |
std::chrono::steady_clock::time_point & | last | ||
) | const |
Returns number of tasks waiting to be serviced, and first and last task times.
Definition at line 113 of file scheduler.cpp.
|
private |
|
inlineprivate |
Definition at line 108 of file scheduler.h.
|
inlineprivate |
Definition at line 109 of file scheduler.h.
|
inlineprivate |
Definition at line 110 of file scheduler.h.
void CScheduler::MockForward | ( | std::chrono::seconds | delta_seconds | ) |
Mock the scheduler to fast forward in time.
Iterates through items on taskQueue and reschedules them to be delta_seconds sooner.
Definition at line 80 of file scheduler.cpp.
void CScheduler::schedule | ( | CScheduler::Function | f, |
std::chrono::steady_clock::time_point | t | ||
) |
Call func at/after time t.
Definition at line 71 of file scheduler.cpp.
void CScheduler::scheduleEvery | ( | CScheduler::Function | f, |
std::chrono::milliseconds | delta | ||
) |
Repeat f until the scheduler is stopped.
First run is after delta has passed once.
The timing is not exact: Every time f is finished, it is rescheduled to run again after delta. If you need more accurate scheduling, don't use this method.
Definition at line 108 of file scheduler.cpp.
|
inline |
Call f once after the delta has passed.
Definition at line 53 of file scheduler.h.
void CScheduler::serviceQueue | ( | ) |
Services the queue 'forever'.
Should be run in a thread.
Definition at line 23 of file scheduler.cpp.
|
inlineprivate |
|
inline |
Tell any threads running serviceQueue to stop as soon as the current task is done.
Definition at line 79 of file scheduler.h.
|
inline |
Tell any threads running serviceQueue to stop when there is no work left to be done.
Definition at line 86 of file scheduler.h.
std::thread CScheduler::m_service_thread |
Definition at line 45 of file scheduler.h.
|
mutableprivate |
Definition at line 105 of file scheduler.h.
|
private |
Definition at line 106 of file scheduler.h.