5 #ifndef BITCOIN_SCHEDULER_H
6 #define BITCOIN_SCHEDULER_H
13 #include <condition_variable>
56 schedule(std::move(f), std::chrono::steady_clock::now() + delta);
73 void MockForward(std::chrono::seconds delta_seconds);
108 size_t getQueueInfo(std::chrono::steady_clock::time_point &first,
109 std::chrono::steady_clock::time_point &last)
const;
117 std::multimap<std::chrono::steady_clock::time_point, Function>
123 return stopRequested || (stopWhenEmpty && taskQueue.empty());
142 std::list<std::function<void()>>
Simple class for background tasks that should be run periodically or once "after a while".
bool stopWhenEmpty GUARDED_BY(newTaskMutex)
void serviceQueue()
Services the queue 'forever'.
std::function< bool()> Predicate
bool stopRequested GUARDED_BY(newTaskMutex)
size_t 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.
void scheduleFromNow(Function f, std::chrono::milliseconds delta)
Call f once after the delta has passed.
void schedule(Function f, std::chrono::steady_clock::time_point t)
Call func at/after time t.
std::multimap< std::chrono::steady_clock::time_point, Function > taskQueue GUARDED_BY(newTaskMutex)
int nThreadsServicingQueue GUARDED_BY(newTaskMutex)
void StopWhenDrained()
Tell any threads running serviceQueue to stop when there is no work left to be done.
std::function< void()> Function
void MockForward(std::chrono::seconds delta_seconds)
Mock the scheduler to fast forward in time.
std::thread m_service_thread
bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex)
void stop()
Tell any threads running serviceQueue to stop as soon as the current task is done.
void scheduleEvery(Predicate p, std::chrono::milliseconds delta)
Repeat p until it return false.
bool AreThreadsServicingQueue() const
Returns true if there are threads actively running in serviceQueue()
std::condition_variable newTaskScheduled
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
void EmptyQueue()
Processes all remaining queue members on the calling thread, blocking until queue is empty.
std::list< std::function< void()> > m_callbacks_pending GUARDED_BY(m_cs_callbacks_pending)
void MaybeScheduleProcessQueue()
void AddToProcessQueue(std::function< void()> func)
Add a callback to be executed.
bool m_are_callbacks_running GUARDED_BY(m_cs_callbacks_pending)
size_t CallbacksPending()
SingleThreadedSchedulerClient(CScheduler &scheduler LIFETIMEBOUND)
RecursiveMutex m_cs_callbacks_pending
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define EXCLUSIVE_LOCKS_REQUIRED(...)