|
| timer_wheel_engine (unsigned int wheel_size, monotonic_clock::duration granularity, Error_Logger error_logger, Actor_Exception_Handler exception_handler) |
| Constructor with all parameters. More...
|
|
| ~timer_wheel_engine () |
| Destructor. More...
|
|
timer_object_holder< Thread_Safety > | allocate () |
| Create timer to be activated later. More...
|
|
template<class Duration_1 , class Duration_2 > |
bool | activate (timer_object_holder< Thread_Safety > timer, Duration_1 pause, Duration_2 period, timer_action action) |
| Activate timer and schedule it for execution. More...
|
|
template<class Duration_1 , class Duration_2 > |
bool | reschedule (timer_object_holder< Thread_Safety > timer, Duration_1 pause, Duration_2 period, timer_action action) |
| Perform an attempt to reschedule a timer. More...
|
|
void | deactivate (timer_object_holder< Thread_Safety > timer) |
| Deactivate timer and remove it from the wheel. More...
|
|
template<typename Unique_Lock > |
void | process_expired_timers (Unique_Lock &lock) |
| Build sublist of elapsed timers and process them all. More...
|
|
bool | empty () const |
| Is empty timer list? More...
|
|
monotonic_clock::time_point | nearest_time_point () const |
| Get time point of the next timer. More...
|
|
void | clear_all () |
| Deactivate all timers and cleanup internal data structures. More...
|
|
| engine_common (Error_Logger error_logger, Actor_Exception_Handler exception_handler) |
| Initializing constructor. More...
|
|
timer_quantities | get_timer_quantities () const |
| Get the quantities of timers of various types. More...
|
|
template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
class timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
A engine for timer wheel mechanism.
This class uses timer_wheel mechanism to work with timers. This mechanism is efficient for working with big amount of timers. But it requires that timer thread is working always, even in case when there is no timers. Another price for timer_wheel is the granularity of timer steps.
Timer wheel data structure consists from one fixed size vector (the wheel) and several double-linked list (one list for every wheel slot).
At the start of next time step thread switches to next wheel position and handles timers from this position list. After processing all elapsed single-shot timers will be removed and deactivated, and all elapsed periodic timers will be rescheduled for the new time steps.
- Note
- At the beginnig of time step thread detects elapsed timers, then unblocks object mutex and calls timer actors for those timers. It means that actors call call timer thread object. And there won't be frequent mutex locking/unlocking operations for building and processing list of elapsed timers. This allows to process millions of timer actor per second.
- Template Parameters
-
Thread_Safety | Thread-safety indicator. Must be timertt::thread_safety::unsafe or timertt::thread_safety::safe. |
Timer_Action | type of functor to perform an user-defined action when timer expires. This must be Moveable and MoveConstructible type. |
Error_Logger | type of logger for errors detected during timer thread execution. Interface for error logger is defined by default_error_logger class. |
Actor_Exception_Handler | type of handler for dealing with exceptions thrown from timer actors. Interface for exception handler is defined by default_actor_exception_handler. |
template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Duration_1 , class Duration_2 >
Activate timer and schedule it for execution.
- Returns
- Value true is returned only when the first timer is added to the empty wheel.
- Exceptions
-
std::exception | If timer thread is not started. |
std::exception | If timer is already activated. |
- Template Parameters
-
Duration_1 | actual type which represents time duration. |
Duration_2 | actual type which represents time duration. |
- Parameters
-
timer | Timer to be activated. |
pause | Pause for timer execution. |
period | Repetition period. If Duration_2::zero() == period then timer will be single-shot. |
action | Action for the timer. |
template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Duration_1 , class Duration_2 >
Perform insertion of a timer into wheel data structure.
This method added to remove the duplication of code in activate() and reschedule() methods.
- Note
- This method doesn't change reference count to timer object.
- Since
- v.1.2.1
- Parameters
-
wheel_timer | Timer to be inserted. |
pause | Pause for timer execution. |
period | Repetition period. If Duration_2::zero() == period then timer will be single-shot. |
template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Unique_Lock >
Detect elapsed timers for the current time step and process them all.
Object lock will be unlocked and then locked back.
template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Duration_1 , class Duration_2 >
Perform an attempt to reschedule a timer.
Before v.1.2.1 there was just one way to reschedule a timer: method deactivate() must be called first and then method activate() must be called for the same timer. This approach is not fast because in the case of thread-safe engines it requires two operations on a mutex.
Since v.1.2.1 there is a reschedule() method which does deactivation of a timer (if it is active) and then new activation of this timer. All actions are performed by using just one operation on a mutex.
- Note
- This operation can fail if the timer to be rescheduled is in processing. Because of that it is recommended to use such operation for timer_managers only. But even with timer_managers this operation should be used with care.
- Attention
- It move operator for a timer_action throws then timer will be deactivated. The state for a timer_action itself will be unknown.
- Exceptions
-
std::exception | If timer thread is not started. |
std::exception | If timer is in processing right now. |
- Template Parameters
-
Duration_1 | actual type which represents time duration. |
Duration_2 | actual type which represents time duration. |
- Since
- v.1.2.1
- Parameters
-
timer | Timer to be rescheduled. Must be in activated or deactivated state. |
pause | Pause for timer execution. |
period | Repetition period. If Duration_2::zero() == period then timer will be single-shot. |
action | Action for the timer. |