Mercury Currency Engine
|
#include <timer.hpp>
Classes | |
struct | timer_id |
Public Member Functions | |
timer_service () | |
default constructor | |
~timer_service () | |
void | start () |
start timer service on current thread | |
void | ready () |
blocks until service is running | |
void | shutdown () |
inform service to shutdown and join with service | |
template<typename THUNK > | |
timer_id | timer (const mce::time_point &timeout, THUNK &&timeout_handler) |
start timer More... | |
template<typename THUNK > | |
timer_id | timer (const mce::duration &d, THUNK &&timeout_handler) |
start timer More... | |
template<typename THUNK > | |
timer_id | timer (const time_unit u, size_t count, THUNK &&timeout_handler) |
start timer More... | |
bool | running (timer_id id) |
return true if timer is running, else false | |
bool | remove (timer_id id) |
remove a running timer More... | |
void | clear () |
remove all pending timers More... | |
size_t | count () |
Return the number of running timers. | |
A tiny asynchronous timer service implementation. This service is not designed to work inside of coroutines and is unsafe to do so, it will almost certainly cause deadlock. To safely interact with coroutines, extra work must be done so that the timeout_handler executes threadsafe code (which can include rescheduling a coroutine or notifying a coroutine via a channel).
Start the service: mce::timer_service my_timer_service; std::thread thd([&]{ my_timer_service.start(); }).detach(); my_timer_service.ready(); // block until started
Usage is as simple as: mce::timer_id tid = my_timer_service.timer(mce::time_unit::microsecond, microsecs_till_timeout, my_timeout_handler, false);
The timer can be synchronously removed (if it is not already executing) with: my_timer_service.remove(tid);
|
inline |
Shutdown and join with asynchronous timer service if shutdown() has not been previously called.
|
inline |
remove all pending timers
WARNING: This is a very, very dangerous operation. Any timeout handler which expects to be called and has no handling for being destroyed early may cause problems in code.
|
inline |
remove a running timer
If an enqueued timer is found, it is removed. If the target timer is currently* executing, this function will block and not return until the timeout handler completes, ensuring the target timer does not exist by the time this function returns.
|
inline |
start timer
timeout | the duration till the timeout_handler should be executed |
timeout_handler | a Callable accepting no arguments |
|
inline |
start timer
timeout | the time when the timeout_handler should be executed |
timeout_handler | a Callable accepting no arguments |
|
inline |
start timer
time_unit | the unit of time measurement to be used |
count | the time_unit count till the timeout_handler should be executed |
timeout_handler | a Callable accepting no arguments |