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

An engine for timer heap mechanism. More...

#include <all.hpp>

Inheritance diagram for timertt::details::timer_heap_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 heap timer. More...
 

Public Types

typedef timer_heap_engine_defaults defaults_type
 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_heap_engine (std::size_t initial_heap_capacity, Error_Logger error_logger, Actor_Exception_Handler exception_handler)
 Constructor with all parameters. More...
 
 ~timer_heap_engine ()
 
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 list. More...
 
template<typename Unique_Lock >
void process_expired_timers (Unique_Lock &lock)
 Process all expired timers from the heap. 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 ()
 Clear all timer demands. 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 Unique_Lock >
void execute_timer_in_processing (Unique_Lock &lock)
 Execute the current timer. More...
 
Methods for work with heap data structure.
bool heap_empty () const
 Is heap data structure empty? More...
 
timer_typeheap_head () const
 Get the minimal timer. More...
 
void heap_add (timer_type *timer)
 Add new timer to the heap data structure. More...
 
void heap_remove (timer_type *timer)
 Remove timer from the heap data structure. More...
 
void heap_swap (timer_type *a, timer_type *b)
 Swap two heap nodes. More...
 
timer_typeheap_item (std::size_t position) const
 Get timer by it index. 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.
std::vector< timer_type *> m_heap
 Array for holding heap data structure. More...
 
timer_typem_timer_in_processing = nullptr
 Timer which is currently in processing. 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_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >

An engine for timer heap mechanism.

This timer engine uses timer mechanism based on heap data structure. The timer with the earlier time point is on the top of the heap. When this timer elapsed and removed next timer with the eralier time point is going to the top of the heap.

This implementation uses array-based binary heap. The array is growing as necessary to hold all the timers. The initial size of that array can be specified in the constructor.

Note
Unlike timer_wheel and timer_list threads this thread unlock and lock its mutex for processing every timers. It means that processing speed of this thread will be slower then for timer_wheel or timer_list threads. But this type of thread doesn't consume resources when there is no timers (unlike timer_wheel thread). And has very efficient activation and deactivation procedures (unlike timer_list thread).
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_heap_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>
typedef timer_heap_engine_defaults timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::defaults_type

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_heap_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_heap_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_heap_engine()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::timer_heap_engine ( std::size_t  initial_heap_capacity,
Error_Logger  error_logger,
Actor_Exception_Handler  exception_handler 
)
inline

Constructor with all parameters.

Parameters
initial_heap_capacityAn initial size for heap array.
error_loggerAn error logger for timer thread.
exception_handlerAn actor exception handler for timer thread.

◆ ~timer_heap_engine()

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

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_heap_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
true is new timer is a timer on the top of the heap (has earlier expiration time).
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_heap_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_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::clear_all ( )
inline

Clear all timer demands.

◆ deactivate()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_heap_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 list.

Parameters
timerTimer to be deactivated.

◆ empty()

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

Is empty timer list?

Returns
true if there is no more timers.

◆ ensure_timer_deactivated()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
static void timertt::details::timer_heap_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.

◆ execute_timer_in_processing()

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

Execute the current timer.

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

◆ heap_add()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::heap_add ( timer_type timer)
inlineprivate

Add new timer to the heap data structure.

◆ heap_empty()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
bool timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::heap_empty ( ) const
inlineprivate

Is heap data structure empty?

◆ heap_head()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
timer_type* timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::heap_head ( ) const
inlineprivate

Get the minimal timer.

Attention
This method must be called only on non-empty heap.

◆ heap_item()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
timer_type* timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::heap_item ( std::size_t  position) const
inlineprivate

Get timer by it index.

This accessor work with respect that positions are started from 1.

◆ heap_remove()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::heap_remove ( timer_type timer)
inlineprivate

Remove timer from the heap data structure.

◆ heap_swap()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
void timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::heap_swap ( timer_type a,
timer_type b 
)
inlineprivate

Swap two heap nodes.

◆ nearest_time_point()

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
monotonic_clock::time_point timertt::details::timer_heap_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().

◆ 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_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::process_expired_timers ( Unique_Lock &  lock)
inline

Process all expired timers from the heap.

Note
time_point for timers expiration detection is got only once at the begining on the method.
lock unlocked and then locked back for every timer action execution.
Parameters
lockObject's lock.

◆ 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_heap_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.
pausePause for timer execution.
periodRepetition period. If Duration_2::zero() == period then timer will be single-shot.
actionAction for the timer.

Member Data Documentation

◆ m_heap

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
std::vector< timer_type * > timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::m_heap
private

Array for holding heap data structure.

◆ m_timer_in_processing

template<typename Thread_Safety, typename Timer_Action, typename Error_Logger, typename Actor_Exception_Handler>
timer_type* timertt::details::timer_heap_engine< Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >::m_timer_in_processing = nullptr
private

Timer which is currently in processing.


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