Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
CScheduler Class Reference

Simple class for background tasks that should be run periodically or once "after a while". More...

#include <scheduler.h>

Collaboration diagram for CScheduler:
[legend]

Public Types

typedef std::function< void()> Function
 
typedef std::function< bool()> Predicate
 

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.
 
void scheduleFromNow (Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
 Call f once after the delta has passed.
 
void scheduleEvery (Predicate p, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
 Repeat p until it return false.
 
void MockForward (std::chrono::seconds delta_seconds) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
 Mock the scheduler to fast forward in time.
 
void serviceQueue () EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
 Services the queue 'forever'.
 
void stop () EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
 Tell any threads running serviceQueue to stop as soon as the current task is done.
 
void StopWhenDrained () EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
 Tell any threads running serviceQueue to stop when there is no work left to be done.
 
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.
 
bool AreThreadsServicingQueue () const EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
 Returns true if there are threads actively running in serviceQueue()
 

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
 

Detailed Description

Simple class for background tasks that should be run periodically or once "after a while".

Usage:

CScheduler* s = new CScheduler();

... 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 41 of file scheduler.h.

Member Typedef Documentation

◆ Function

typedef std::function<void()> CScheduler::Function

Definition at line 48 of file scheduler.h.

◆ Predicate

Definition at line 49 of file scheduler.h.

Constructor & Destructor Documentation

◆ CScheduler()

CScheduler::CScheduler ( )

Definition at line 14 of file scheduler.cpp.

◆ ~CScheduler()

CScheduler::~CScheduler ( )

Definition at line 16 of file scheduler.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ AreThreadsServicingQueue()

bool CScheduler::AreThreadsServicingQueue ( ) const

Returns true if there are threads actively running in serviceQueue()

Definition at line 131 of file scheduler.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getQueueInfo()

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 120 of file scheduler.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GUARDED_BY() [1/4]

std::multimap< std::chrono::steady_clock::time_point, Function > taskQueue CScheduler::GUARDED_BY ( newTaskMutex  )
private

◆ GUARDED_BY() [2/4]

int nThreadsServicingQueue CScheduler::GUARDED_BY ( newTaskMutex  )
inlineprivate

Definition at line 125 of file scheduler.h.

◆ GUARDED_BY() [3/4]

bool stopRequested CScheduler::GUARDED_BY ( newTaskMutex  )
inlineprivate

Definition at line 126 of file scheduler.h.

◆ GUARDED_BY() [4/4]

bool stopWhenEmpty CScheduler::GUARDED_BY ( newTaskMutex  )
inlineprivate

Definition at line 127 of file scheduler.h.

◆ MockForward()

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 83 of file scheduler.cpp.

Here is the call graph for this function:

◆ schedule()

void CScheduler::schedule ( CScheduler::Function  f,
std::chrono::steady_clock::time_point  t 
)

Call func at/after time t.

Definition at line 74 of file scheduler.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scheduleEvery()

void CScheduler::scheduleEvery ( CScheduler::Predicate  p,
std::chrono::milliseconds  delta 
)

Repeat p until it return false.

First run is after delta has passed once.

The timing is not exact: Every time p is finished, it is rescheduled to run again after delta. If you need more accurate scheduling, don't use this method.

Definition at line 114 of file scheduler.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scheduleFromNow()

void CScheduler::scheduleFromNow ( Function  f,
std::chrono::milliseconds  delta 
)
inline

Call f once after the delta has passed.

Definition at line 56 of file scheduler.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ serviceQueue()

void CScheduler::serviceQueue ( )

Services the queue 'forever'.

Should be run in a thread.

Definition at line 23 of file scheduler.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shouldStop()

bool CScheduler::shouldStop ( ) const
inlineprivate

Definition at line 128 of file scheduler.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void CScheduler::stop ( )
inline

Tell any threads running serviceQueue to stop as soon as the current task is done.

Definition at line 88 of file scheduler.h.

Here is the call graph for this function:

◆ StopWhenDrained()

void CScheduler::StopWhenDrained ( )
inline

Tell any threads running serviceQueue to stop when there is no work left to be done.

Definition at line 100 of file scheduler.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_service_thread

std::thread CScheduler::m_service_thread

Definition at line 46 of file scheduler.h.

◆ newTaskMutex

Mutex CScheduler::newTaskMutex
mutableprivate

Definition at line 121 of file scheduler.h.

◆ newTaskScheduled

std::condition_variable CScheduler::newTaskScheduled
private

Definition at line 122 of file scheduler.h.


The documentation for this class was generated from the following files: