SObjectizer  5.7
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | List of all members
timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler > Class Template Reference

A engine for timer wheel mechanism. More...

#include <all.hpp>

Inheritance diagram for timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >:
timertt::details::engine_common< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >

Classes

struct  timer_type
 Type of wheel timer. More...
 
struct  wheel_item
 Type of wheel's item. More...
 

Public Types

using defaults_type = timer_wheel_engine_defaults
 Type with default parameters for this engine. More...
 
using timer_action = typename base_type::timer_action
 Alias for timer_action type. More...
 
using scoped_timer_object = scoped_timer_object_holder< timer_type >
 Alias for scoped timer object. More...
 
- Public Types inherited from timertt::details::engine_common< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
using thread_safety = Thread_Safety
 Indicator of thread-safety. More...
 
using timer_action = Timer_Action
 Alias for Timer_Action. More...
 

Public Member Functions

 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...
 
- Public Member Functions inherited from timertt::details::engine_common< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
 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...
 

Private Types

using base_type = engine_common< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
 An alias for base class. More...
 

Private Member Functions

template<class Duration_1 , class Duration_2 >
void perform_insertion_info_wheel (timer_type *wheel_timer, Duration_1 pause, Duration_2 period)
 Perform insertion of a timer into wheel data structure. More...
 
template<class Duration >
unsigned int duration_to_ticks (Duration d) const
 Converion of duration to number of time steps. More...
 
void set_position_in_the_wheel (timer_type *wheel_timer, unsigned int pause_in_ticks) const
 Calculate and fill up wheel position for the timer. More...
 
void insert_demand_to_wheel (timer_type *wheel_timer)
 Insert timer to the wheel. More...
 
void remove_timer_from_wheel (timer_type *wheel_timer)
 Remove timer from the timer_wheel. More...
 
template<class Unique_Lock >
void process_current_wheel_position (Unique_Lock &lock)
 Detect elapsed timers for the current time step and process them all. More...
 
timer_typemake_exec_list ()
 Make list of elapsed timers to be executed. More...
 
template<class Unique_Lock >
void exec_actions (Unique_Lock &lock, timer_type *head) TIMERTT_NOEXCEPT
 Execute all active timers from the list. More...
 
void utilize_exec_list (timer_type *head)
 Process list of elapsed timers after execution of its actions. More...
 

Static Private Member Functions

static void ensure_timer_deactivated (const timer_type *timer)
 Hard check for deactivation state of the timer. More...
 

Private Attributes

Object's attributes.
const unsigned int m_wheel_size
 Size of the wheel. More...
 
const monotonic_clock::duration m_granularity
 Granularity of one time step. More...
 
unsigned int m_current_position = 0
 Index of the current position in the wheel. More...
 
monotonic_clock::time_point m_current_tick_border
 Right border of the current tick. More...
 
bool m_current_tick_processed = false
 Has the current tick been processed? More...
 
std::vector< wheel_itemm_wheel
 The wheel data. More...
 

Additional Inherited Members

- Protected Member Functions inherited from timertt::details::engine_common< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
void inc_timer_count (timer_kind kind)
 Helper method for increment the count of timers of the specific type. More...
 
void dec_timer_count (timer_kind kind)
 Helper method for decrement the count of timers of the specific type. More...
 
void reset_timer_count ()
 Helper method for reseting quantities of timers to zero. More...
 
- Protected Attributes inherited from timertt::details::engine_common< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
Error_Logger m_error_logger
 Error logger. More...
 
Actor_Exception_Handler m_exception_handler
 Exception handler. More...
 
timer_quantities m_timer_quantities
 Quantities of timers of various types. More...
 

Detailed Description

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_SafetyThread-safety indicator. Must be timertt::thread_safety::unsafe or timertt::thread_safety::safe.
Timer_Actiontype of functor to perform an user-defined action when timer expires. This must be Moveable and MoveConstructible type.
Error_Loggertype of logger for errors detected during timer thread execution. Interface for error logger is defined by default_error_logger class.
Actor_Exception_Handlertype of handler for dealing with exceptions thrown from timer actors. Interface for exception handler is defined by default_actor_exception_handler.

Member Typedef Documentation

◆ base_type

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
using timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::base_type = engine_common< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
private

An alias for base class.

◆ defaults_type

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
using timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::defaults_type = timer_wheel_engine_defaults

Type with default parameters for this engine.

◆ scoped_timer_object

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
using timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::scoped_timer_object = scoped_timer_object_holder< timer_type >

Alias for scoped timer object.

◆ timer_action

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
using timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::timer_action = typename base_type::timer_action

Alias for timer_action type.

Constructor & Destructor Documentation

◆ timer_wheel_engine()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::timer_wheel_engine ( unsigned int  wheel_size,
monotonic_clock::duration  granularity,
Error_Logger  error_logger,
Actor_Exception_Handler  exception_handler 
)
inline

Constructor with all parameters.

Parameters
wheel_sizeSize of the wheel.
granularitySize of time step for the timer_wheel.
error_loggerAn error logger for timer thread.
exception_handlerAn actor exception handler for timer thread.

◆ ~timer_wheel_engine()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::~timer_wheel_engine ( )
inline

Destructor.

Member Function Documentation

◆ activate()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Duration_1 , class Duration_2 >
bool timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::activate ( timer_object_holder< Thread_Safety >  timer,
Duration_1  pause,
Duration_2  period,
timer_action  action 
)
inline

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::exceptionIf timer thread is not started.
std::exceptionIf timer is already activated.
Template Parameters
Duration_1actual type which represents time duration.
Duration_2actual type which represents time duration.
Parameters
timerTimer to be activated.
pausePause for timer execution.
periodRepetition period. If Duration_2::zero() == period then timer will be single-shot.
actionAction for the timer.

◆ allocate()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
timer_object_holder< Thread_Safety > timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::allocate ( )
inline

Create timer to be activated later.

◆ clear_all()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::clear_all ( )
inline

Deactivate all timers and cleanup internal data structures.

◆ deactivate()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::deactivate ( timer_object_holder< Thread_Safety >  timer)
inline

Deactivate timer and remove it from the wheel.

◆ duration_to_ticks()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Duration >
unsigned int timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::duration_to_ticks ( Duration  d) const
inlineprivate

Converion of duration to number of time steps.

Note
This implementation performs rounding up for duration values. For example if granularity is 10ms and duration is 15ms then result will be 2 time steps.
Never return 0. If duration is less then granularity (even after rounding up) the value 1 will be returned. E.g. timer will be scheduled for the next time step.
Template Parameters
Durationactual type for duration representation.
Parameters
dTime duration to be converted in time steps count.

◆ empty()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
bool timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::empty ( ) const
inline

Is empty timer list?

◆ ensure_timer_deactivated()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
static void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::ensure_timer_deactivated ( const timer_type timer)
inlinestaticprivate

Hard check for deactivation state of the timer.

Exceptions
std::runtimer_errorif timer is not deactivated.

◆ exec_actions()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Unique_Lock >
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::exec_actions ( Unique_Lock &  lock,
timer_type head 
)
inlineprivate

Execute all active timers from the list.

Parameters
lockObject lock. This lock will be unlocked before execution of actions and locked back after.
headHead of execution list. Cannot be nullptr.

◆ insert_demand_to_wheel()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::insert_demand_to_wheel ( timer_type wheel_timer)
inlineprivate

Insert timer to the wheel.

If there is a non-empty timer list for the timer wheel position the wheel_timer will be added to the end of that list.

◆ make_exec_list()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
timer_type* timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::make_exec_list ( )
inlineprivate

Make list of elapsed timers to be executed.

◆ nearest_time_point()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
monotonic_clock::time_point timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::nearest_time_point ( ) const
inline

Get time point of the next timer.

Attention
Must be called only when !empty().

◆ perform_insertion_info_wheel()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Duration_1 , class Duration_2 >
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::perform_insertion_info_wheel ( timer_type wheel_timer,
Duration_1  pause,
Duration_2  period 
)
inlineprivate

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_timerTimer to be inserted.
pausePause for timer execution.
periodRepetition period. If Duration_2::zero() == period then timer will be single-shot.

◆ process_current_wheel_position()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Unique_Lock >
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::process_current_wheel_position ( Unique_Lock &  lock)
inlineprivate

Detect elapsed timers for the current time step and process them all.

Object lock will be unlocked and then locked back.

◆ process_expired_timers()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<typename Unique_Lock >
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::process_expired_timers ( Unique_Lock &  lock)
inline

Build sublist of elapsed timers and process them all.

Parameters
lockObject's lock.

◆ remove_timer_from_wheel()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::remove_timer_from_wheel ( timer_type wheel_timer)
inlineprivate

Remove timer from the timer_wheel.

◆ reschedule()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
template<class Duration_1 , class Duration_2 >
bool timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::reschedule ( timer_object_holder< Thread_Safety >  timer,
Duration_1  pause,
Duration_2  period,
timer_action  action 
)
inline

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::exceptionIf timer thread is not started.
std::exceptionIf timer is in processing right now.
Template Parameters
Duration_1actual type which represents time duration.
Duration_2actual type which represents time duration.
Since
v.1.2.1
Parameters
timerTimer to be rescheduled. Must be in activated or deactivated state.
pausePause for timer execution.
periodRepetition period. If Duration_2::zero() == period then timer will be single-shot.
actionAction for the timer.

◆ set_position_in_the_wheel()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::set_position_in_the_wheel ( timer_type wheel_timer,
unsigned int  pause_in_ticks 
) const
inlineprivate

Calculate and fill up wheel position for the timer.

timer_type::m_position and timer_type::m_full_rolls_left will be set for wheel_timer.

Parameters
wheel_timerTimer to modify.
pause_in_ticksTimeout for the timer is time steps.

◆ utilize_exec_list()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::utilize_exec_list ( timer_type head)
inlineprivate

Process list of elapsed timers after execution of its actions.

Active periodic timers will be rescheduled. All other timers will be deactivated and removed.

Parameters
headHead of execution list. Cannot be null.

Member Data Documentation

◆ m_current_position

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
unsigned int timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::m_current_position = 0
private

Index of the current position in the wheel.

◆ m_current_tick_border

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
monotonic_clock::time_point timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::m_current_tick_border
private

Right border of the current tick.

This is the time point at which new tick must be started.

◆ m_current_tick_processed

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
bool timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::m_current_tick_processed = false
private

Has the current tick been processed?

◆ m_granularity

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
const monotonic_clock::duration timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::m_granularity
private

Granularity of one time step.

◆ m_wheel

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
std::vector< wheel_item > timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::m_wheel
private

The wheel data.

◆ m_wheel_size

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
const unsigned int timertt::details::timer_wheel_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::m_wheel_size
private

Size of the wheel.


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