Mercury Currency Engine
Classes | Public Member Functions | List of all members
mce::timer_service Struct Reference

#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.
 

Detailed Description

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);

Constructor & Destructor Documentation

◆ ~timer_service()

mce::timer_service::~timer_service ( )
inline

Shutdown and join with asynchronous timer service if shutdown() has not been previously called.

Member Function Documentation

◆ clear()

void mce::timer_service::clear ( )
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.

◆ remove()

bool mce::timer_service::remove ( timer_id  id)
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.

Returns
true if timer was found and removed, else returns false.

◆ timer() [1/3]

template<typename THUNK >
timer_id mce::timer_service::timer ( const mce::duration &  d,
THUNK &&  timeout_handler 
)
inline

start timer

Parameters
timeoutthe duration till the timeout_handler should be executed
timeout_handlera Callable accepting no arguments
Returns
a timer id object

◆ timer() [2/3]

template<typename THUNK >
timer_id mce::timer_service::timer ( const mce::time_point &  timeout,
THUNK &&  timeout_handler 
)
inline

start timer

Parameters
timeoutthe time when the timeout_handler should be executed
timeout_handlera Callable accepting no arguments
Returns
a timer id object

◆ timer() [3/3]

template<typename THUNK >
timer_id mce::timer_service::timer ( const time_unit  u,
size_t  count,
THUNK &&  timeout_handler 
)
inline

start timer

Parameters
time_unitthe unit of time measurement to be used
countthe time_unit count till the timeout_handler should be executed
timeout_handlera Callable accepting no arguments
Returns
a timer id object

The documentation for this struct was generated from the following file: