2
3
6
7
8
15#include <condition_variable>
29#include <unordered_map>
32#if defined(_MSC_VER
) && (_MSC_VER
< 1900
)
34 #define TIMERTT_ALIGNAS_WORKAROUND(T) __declspec(align(8
))
37 #define TIMERTT_NOEXCEPT
38 #define TIMERTT_HAS_NOEXCEPT 0
40 #define TIMERTT_ALIGNAS_WORKAROUND(T) alignas(T)
41 #define TIMERTT_NOEXCEPT noexcept
42 #define TIMERTT_HAS_NOEXCEPT 1
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#define TIMERTT_VERSION 1002003u
73
74
79
80
81
82
83
87
88
89
90
91
95
96
97
98
99
117
118
119
120
121
125
126
133
134
135
136
137
138
139
140
141
142template<
typename Thread_Safety >
146
147
148
149
150
162
163
164
165
166
182
183
184
185
186
187template<
typename Thread_Safety >
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256template<
typename Actual_Object >
279
280
281
282
283
284template<
typename Thread_Safety >
290
291
315 template<
typename Actual_Object >
355
356
365
366
367
368
369
376
377
378
395
396
428
429
430
431
438 const std::string & what )
440 std::cerr << what << std::endl;
449
450
451
452
469
470
471
472
473
480
481
488
489
490
491
492
503
504
524template<
typename Lambda >
525void invoke_noexcept_code_block(Lambda && lambda)
noexcept
532template<
typename Lambda >
544
545
546
547
548
549
550
551
552
570 using element_type = T;
582 template<
typename... Args>
620
621
622
623
624
625
626
627
628
629
630
631template<
typename Action_Type >
667 m_action = std::move(action);
681
682
683
684
685
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
725 typename Thread_Safety,
726 typename Timer_Action,
727 typename Error_Logger,
728 typename Actor_Exception_Handler >
733 using thread_safety = Thread_Safety;
736 using timer_action = Timer_Action;
740 Error_Logger error_logger,
741 Actor_Exception_Handler exception_handler )
747
748
749
750
751
766
767
768
769
770
774
775
776
777
778
779
790
791
792
793
794
795
806
807
808
809
810
822
823
824
825
826
827
831 inline static unsigned int
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
885 typename Thread_Safety,
886 typename Timer_Action,
887 typename Error_Logger,
888 typename Actor_Exception_Handler >
891 Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
895 Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >;
904 using timer_action =
typename base_type::timer_action;
907 using scoped_timer_object =
913 unsigned int wheel_size,
915 monotonic_clock::duration granularity,
917 Error_Logger error_logger,
919 Actor_Exception_Handler exception_handler )
944
945
946
947
948
949
950
951
952
953 template<
class Duration_1,
class Duration_2 >
965 timer_action action )
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017 template<
class Duration_1,
class Duration_2 >
1029 timer_action action )
1040 "it can't be rescheduled" );
1093
1094
1095 template<
typename Unique_Lock >
1099 Unique_Lock & lock )
1102
1103
1104
1105
1106
1107
1137
1138
1147
1148
1149
1150
1161
1162
1201
1202
1219
1220
1221
1222
1223
1241
1242
1243
1255
1256
1265
1266
1269
1270
1271
1272
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292 template<
class Duration_1,
class Duration_2 >
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335 template<
class Duration >
1346 unsigned int r =
static_cast<
unsigned int >(
1348
1349
1350
1351
1352
1360
1361
1362
1363
1364
1370 unsigned int pause_in_ticks )
const
1378
1379
1380
1381
1382
1407
1408
1424
1425
1426
1427
1428
1429 template<
class Unique_Lock >
1432 Unique_Lock & lock )
1445
1446
1488
1489
1490 template<
class Unique_Lock >
1526 ss << __FILE__ <<
"(" << __LINE__
1527 <<
"): an unknown exception from timer action";
1541
1542
1543
1544
1545
1546
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1647 typename Thread_Safety,
1648 typename Time_Action,
1649 typename Error_Logger,
1650 typename Actor_Exception_Handler >
1653 Thread_Safety, Time_Action, Error_Logger, Actor_Exception_Handler >
1657 Thread_Safety, Time_Action, Error_Logger, Actor_Exception_Handler >;
1667 using timer_action =
typename base_type::timer_action;
1670 using scoped_timer_object =
1683 Error_Logger error_logger,
1685 Actor_Exception_Handler exception_handler )
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713 template<
class Duration_1,
class Duration_2 >
1725 timer_action action )
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780 template<
class Duration_1,
class Duration_2 >
1792 timer_action action )
1803 "it can't be rescheduled" );
1866
1867
1868
1869
1870 template<
typename Unique_Lock >
1874 Unique_Lock & lock )
1887
1888
1889
1890
1894 return nullptr ==
m_head;
1898
1899
1900
1901
1909
1910
1940
1941
1958
1959
1960
1961
1962
1972
1973
1974
1981
1982
1985
1986
1987
1988
1998
1999
2000
2001
2002
2003
2004
2049
2050
2067
2068
2069
2070
2071
2112
2113
2114
2115
2116 template<
class Unique_Lock >
2152 ss << __FILE__ <<
"(" << __LINE__
2153 <<
"): an unknown exception from timer action";
2167
2168
2169
2170
2171
2172
2208
2209
2210
2211
2212
2213
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2263 typename Thread_Safety,
2264 typename Timer_Action,
2265 typename Error_Logger,
2266 typename Actor_Exception_Handler >
2269 Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >
2273 Thread_Safety, Timer_Action, Error_Logger, Actor_Exception_Handler >;
2283 using timer_action =
typename base_type::timer_action;
2286 using scoped_timer_object =
2292 std::size_t initial_heap_capacity,
2294 Error_Logger error_logger,
2296 Actor_Exception_Handler exception_handler )
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325 template<
class Duration_1,
class Duration_2 >
2337 timer_action action )
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393 template<
class Duration_1,
class Duration_2 >
2405 timer_action action )
2416 "it can't be rescheduled" );
2488
2489
2490
2491
2492
2493
2494
2495
2496 template<
typename Unique_Lock >
2500 Unique_Lock & lock )
2536
2537
2538
2539
2547
2548
2549
2550
2577
2578
2579
2587
2588
2619
2620
2621
2622
2623
2633
2634
2635
2642
2643
2646
2647
2648
2649
2658 template<
class Unique_Lock >
2685 ss << __FILE__ <<
"(" << __LINE__
2686 <<
"): an unknown exception from timer action";
2696
2697
2698
2708
2709
2790
2791
2798
2799
2807
2808
2809
2810
2811
2812
2817
2818
2819
2820
2821
2822
2843
2844
2845
2846
2847
2848
2880
2881
2882
2883
2884
2895
2896
2897
2920
2921
2926 throw std::runtime_error(
"timer thread is not started" );
2933 m_condition.notify_one();
2938
2939
2940
2941
2942
2946
2947
2948
2949
2950
2953
2954
2955
2956
2957
2962
2963
2964
2965
2966
2967
2968
2969
2970template<
typename Thread_Safety,
typename Consumer >
2976
2977
2978
2979
2980
2989
2990
2991
2992
2993
3002
3003
3004
3005
3006
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3033 :
protected mixin_selector<
typename Engine::thread_safety, Consumer >::type
3034 ,
public Engine::defaults_type
3038 typename Engine::thread_safety, Consumer >::type;
3042
3043
3044
3045
3046
3047 using thread_safety =
typename Engine::thread_safety;
3050 using timer_action =
typename Engine::timer_action;
3053 using scoped_timer_object =
typename Engine::scoped_timer_object;
3057
3058
3059
3063 template<
typename... Args >
3071
3072
3073
3074
3083
3084
3085
3086
3087
3088
3089 template<
class Duration_1 >
3097 timer_action action )
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137 template<
class Duration_1 >
3145 timer_action action )
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168 template<
class Duration_1 >
3172 scoped_timer_object & timer,
3176 timer_action action )
3183
3184
3185
3186
3187
3188
3189
3190 template<
class Duration_1 >
3196 timer_action action )
3207
3208
3209
3210
3211
3212
3213
3214 template<
class Duration_1,
class Duration_2 >
3226 timer_action action )
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269 template<
class Duration_1,
class Duration_2 >
3281 timer_action action )
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307 template<
class Duration_1,
class Duration_2 >
3311 scoped_timer_object & timer,
3319 timer_action action )
3326
3327
3328
3329
3330
3331
3332
3333
3334 template<
class Duration_1,
class Duration_2 >
3344 timer_action action )
3353 timer_holder timer )
3362
3363
3364
3368 scoped_timer_object & timer )
3374
3375
3376
3377
3378
3379
3380
3381
3391
3392
3393
3394
3395
3414
3415
3416
3417
3418
3419
3420
3421template<
typename Engine >
3432 template<
typename... Args >
3458
3459
3460
3474
3475
3476
3477
3478
3479 template<
typename Duration >
3505
3506
3507
3508
3509
3510
3511
3512template<
typename Engine >
3523 template<
typename... Args >
3532
3533
3541
3542
3572
3573
3601
3602
3603
3607
3608
3627
3628
3629
3630
3631
3636 typename base_type::lock_guard & lock )
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3676 typename Timer_Action,
3677 typename Error_Logger,
3678 typename Actor_Exception_Handler >
3686 Actor_Exception_Handler > >
3694 Actor_Exception_Handler > >;
3709 unsigned int wheel_size,
3711 monotonic_clock::duration granularity )
3722 unsigned int wheel_size,
3724 monotonic_clock::duration granularity,
3726 Error_Logger error_logger,
3728 Actor_Exception_Handler exception_handler )
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3766 typename Thread_Safety,
3777 Actor_Exception_Handler > >
3786 Actor_Exception_Handler > >;
3801 unsigned int wheel_size,
3803 monotonic_clock::duration granularity )
3814 unsigned int wheel_size,
3816 monotonic_clock::duration granularity,
3818 Error_Logger error_logger,
3820 Actor_Exception_Handler exception_handler )
3833
3834
3835
3836
3837
3838
3839using default_timer_wheel_thread =
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3868 typename Timer_Action,
3869 typename Error_Logger,
3870 typename Actor_Exception_Handler >
3878 Actor_Exception_Handler > >
3886 Actor_Exception_Handler > >;
3895 Error_Logger error_logger,
3896 Actor_Exception_Handler actor_exception_handler )
3906
3907
3908
3909
3910
3911
3912using default_timer_list_thread =
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3947 typename Thread_Safety,
3958 Actor_Exception_Handler > >
3966 Actor_Exception_Handler > >;
3975 Error_Logger error_logger,
3976 Actor_Exception_Handler actor_exception_handler )
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4005 typename Timer_Action,
4006 typename Error_Logger,
4007 typename Actor_Exception_Handler >
4015 Actor_Exception_Handler > >
4024 Actor_Exception_Handler > >;
4029
4030
4031
4042 std::size_t initial_heap_capacity )
4052 std::size_t initial_heap_capacity,
4054 Error_Logger error_logger,
4056 Actor_Exception_Handler exception_handler )
4068
4069
4070
4071
4072
4073
4074using default_timer_heap_thread =
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4106 typename Thread_Safety,
4117 Actor_Exception_Handler > >
4126 Actor_Exception_Handler > >;
4131
4132
4133
4144 std::size_t initial_heap_capacity )
4154 std::size_t initial_heap_capacity,
4156 Error_Logger error_logger,
4158 Actor_Exception_Handler exception_handler )
#define TIMERTT_HAS_NOEXCEPT
#define TIMERTT_ALIGNAS_WORKAROUND(T)
Helper class for indication of long-lived reference via its type.
An indentificator for the timer.
An interface for collector of elapsed timers.
A base class for timer identificator.
An actual implementation of timer_manager.
virtual timer_thread_stats_t query_stats() override
Get statistics for run-time monitoring.
virtual void schedule_anonymous(const std::type_index &type_index, const mbox_t &mbox, const message_ref_t &msg, std::chrono::steady_clock::duration pause, std::chrono::steady_clock::duration period) override
Push anonymous delayed/periodic message to the timer queue.
actual_manager_t(std::unique_ptr< Timer_Manager > manager, outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > collector)
Initializing constructor.
outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > m_collector
virtual bool empty() override
virtual void process_expired_timers() override
Translation of expired timers into message sends.
virtual timer_id_t schedule(const std::type_index &type_index, const mbox_t &mbox, const message_ref_t &msg, std::chrono::steady_clock::duration pause, std::chrono::steady_clock::duration period) override
Push delayed/periodic message to the timer queue.
std::unique_ptr< Timer_Manager > m_manager
virtual std::chrono::steady_clock::duration timeout_before_nearest_timer(std::chrono::steady_clock::duration default_timer) override
Calculate time before the nearest timer (if any).
An actual implementation of timer thread.
std::unique_ptr< Timer_Thread > m_thread
virtual timer_thread_stats_t query_stats() override
Get statistics for run-time monitoring.
virtual void start() override
Launch timer.
actual_thread_t(std::unique_ptr< Timer_Thread > thread)
Initializing constructor.
virtual void finish() override
Finish timer and wait for full stop.
virtual timer_id_t schedule(const std::type_index &type_index, const mbox_t &mbox, const message_ref_t &msg, std::chrono::steady_clock::duration pause, std::chrono::steady_clock::duration period) override
Push delayed/periodic message to the timer queue.
virtual void schedule_anonymous(const std::type_index &type_index, const mbox_t &mbox, const message_ref_t &msg, std::chrono::steady_clock::duration pause, std::chrono::steady_clock::duration period) override
Push anonymous delayed/periodic message to the timer queue.
An actual implementation of timer interface.
timer_holder_t & timer_holder() noexcept
actual_timer_t(Timer *thread)
Initialized constructor.
virtual void release() noexcept override
Release the timer event.
timer_holder_t m_timer
Underlying timer object reference.
virtual ~actual_timer_t() noexcept override
virtual bool is_active() const noexcept override
Is this timer event is active?
Timer * m_thread
Timer thread for the timer.
A functor to be used as timer action in implementation of timer thread.
void operator()() noexcept
std::type_index m_type_index
timer_action_for_timer_manager_t(outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > collector, std::type_index type_index, mbox_t mbox, message_ref_t msg)
outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > m_collector
A functor to be used as timer action in implementation of timer thread.
timer_action_for_timer_thread_t(std::type_index type_index, mbox_t mbox, message_ref_t msg)
std::type_index m_type_index
void operator()() noexcept
A implementation of basic methods for timer managers and timer threads.
void deactivate(timer_holder timer)
Deactivate timer and remove it from the list.
void activate(Duration_1 pause, timer_action action)
Activate timer and schedule it for execution.
void activate(timer_holder timer, Duration_1 pause, Duration_2 period, timer_action action)
Activate timer and schedule it for execution.
void reschedule(timer_holder timer, Duration_1 pause, timer_action action)
Perform an attempt to reschedule a timer.
void activate(scoped_timer_object &timer, Duration_1 pause, timer_action action)
Activate a scoped timer and schedule it for execution.
void deactivate(scoped_timer_object &timer)
Deactivate timer and remove it from the list.
timer_quantities get_timer_quantities()
Count of timers of various types.
void reschedule(timer_holder timer, Duration_1 pause, Duration_2 period, timer_action action)
Perform an attempt to reschedule a timer.
basic_methods_impl_mixin(Args &&... args)
Constructor with all parameters.
bool empty()
Check for emptiness.
Engine m_engine
Actual timer engine instance.
void activate(Duration_1 pause, Duration_2 period, timer_action action)
Activate timer and schedule it for execution.
void activate(scoped_timer_object &timer, Duration_1 pause, Duration_2 period, timer_action action)
Activate a scoped timer and schedule it for execution.
timer_holder allocate()
Allocate of new timer object.
void activate(timer_holder timer, Duration_1 pause, timer_action action)
Activate timer and schedule it for execution.
A special storage to be used for holding non-default constructible objects which are created by deman...
pointer get() const TIMERTT_NOEXCEPT
~buffer_allocated_object()
buffer_allocated_object(const buffer_allocated_object &)=delete
TIMERTT_ALIGNAS_WORKAROUND(T) std bool allocated_
reference operator*() const TIMERTT_NOEXCEPT
pointer operator->() const TIMERTT_NOEXCEPT
void allocate(Args &&...args)
void destroy_if_allocated()
operator bool() const TIMERTT_NOEXCEPT
buffer_allocated_object(buffer_allocated_object &&)=delete
buffer_allocated_object() TIMERTT_NOEXCEPT=default
A common part for all timer engines.
timer_quantities m_timer_quantities
Quantities of timers of various types.
void inc_timer_count(timer_kind kind)
Helper method for increment the count of timers of the specific type.
Actor_Exception_Handler m_exception_handler
Exception handler.
void reset_timer_count()
Helper method for reseting quantities of timers to zero.
void dec_timer_count(timer_kind kind)
Helper method for decrement the count of timers of the specific type.
timer_quantities get_timer_quantities() const
Get the quantities of timers of various types.
Error_Logger m_error_logger
Error logger.
engine_common(Error_Logger error_logger, Actor_Exception_Handler exception_handler)
Initializing constructor.
Template-based implementation of timer manager.
void process_expired_timers()
Perform processing of expired timers.
void reset()
Reset all timers and return manager to the initial state.
monotonic_clock::duration timeout_before_nearest_timer(Duration default_timeout)
Get the sleeping time before the earlist timer expiration.
manager_impl_template(Args &&... args)
Constructor with all parameters.
std::tuple< bool, monotonic_clock::time_point > nearest_time_point()
Get the time for next process_expired_timers invocation.
Template-based implementation of timer thread.
bool m_shutdown
Shutdown flag.
void shutdown()
Initiate shutdown for the timer thread without waiting for completion.
void sleep_for_next_event(typename base_type::lock_guard &lock)
Waiting for next event to process.
void shutdown_and_join()
Initiate shutdown and wait for completion.
void start()
Start timer thread.
void join()
Wait for completion of timer thread.
~thread_impl_template()
Destructor.
thread_impl_template(Args &&... args)
Constructor with all parameters.
A special wrapper around actual std::unique_lock.
lock_guard(thread_mixin &self)
std::unique_lock< std::mutex > m_lock
std::unique_lock< std::mutex > & actual_lock()
Accessor for actual std::unique_lock object.
A special wrapper around actual std::unique_lock.
std::unique_lock< std::mutex > m_lock
lock_guard(thread_safe_manager_mixin &self)
A empty class for an object's lock emulation.
lock_guard(thread_unsafe_manager_mixin &)
default_timer_action_type m_action
void assign(default_timer_action_type &&action)
timer_action_holder(timer_action_holder &&)=delete
timer_action_holder()=default
timer_action_holder(const timer_action_holder &)=delete
A special storage for holding timer actions.
void assign(Action_Type &&action)
buffer_allocated_object< Action_Type > m_action
timer_action_holder(timer_action_holder &&)=delete
timer_action_holder()=default
timer_action_holder(const timer_action_holder &)=delete
An engine for timer heap mechanism.
timer_type * m_timer_in_processing
Timer which is currently in processing.
void clear_all()
Clear all timer demands.
void deactivate(timer_object_holder< Thread_Safety > timer)
Deactivate timer and remove it from the list.
void heap_swap(timer_type *a, timer_type *b)
Swap two heap nodes.
static void ensure_timer_deactivated(const timer_type *timer)
Hard check for deactivation state of the timer.
void execute_timer_in_processing(Unique_Lock &lock) TIMERTT_NOEXCEPT
Execute the current timer.
void process_expired_timers(Unique_Lock &lock)
Process all expired timers from the heap.
timer_object_holder< Thread_Safety > allocate()
Create timer to be activated later.
std::vector< timer_type * > m_heap
Array for holding heap data structure.
timer_type * heap_item(std::size_t position) const
Get timer by it index.
bool heap_empty() const
Is heap data structure empty?
timer_heap_engine_defaults defaults_type
Type with default parameters for this engine.
bool empty() const
Is empty timer list?
monotonic_clock::time_point nearest_time_point() const
Get time point of the next timer.
bool reschedule(timer_object_holder< Thread_Safety > timer, Duration_1 pause, Duration_2 period, timer_action action)
Perform an attempt to reschedule a timer.
void heap_add(timer_type *timer)
Add new timer to the heap data structure.
timer_heap_engine(std::size_t initial_heap_capacity, Error_Logger error_logger, Actor_Exception_Handler exception_handler)
Constructor with all parameters.
timer_type * heap_head() const
Get the minimal timer.
bool activate(timer_object_holder< Thread_Safety > timer, Duration_1 pause, Duration_2 period, timer_action action)
Activate timer and schedule it for execution.
void heap_remove(timer_type *timer)
Remove timer from the heap data structure.
An engine for timer list mechanism.
void exec_actions(Unique_Lock &lock, timer_type *head) TIMERTT_NOEXCEPT
Execute all active timers in the sublist.
void clear_all()
Deactivate all timers and cleanup internal data structures.
static void ensure_timer_deactivated(const timer_type *timer)
Hard check for deactivation state of the timer.
timer_list_engine()
Default constructor.
bool activate(timer_object_holder< Thread_Safety > timer, Duration_1 pause, Duration_2 period, timer_action action)
Activate timer and schedule it for execution.
void process_expired_timers(Unique_Lock &lock)
Build sublist of elapsed timers and process them all.
monotonic_clock::time_point nearest_time_point() const
Get time point of the next timer.
bool reschedule(timer_object_holder< Thread_Safety > timer, Duration_1 pause, Duration_2 period, timer_action action)
Perform an attempt to reschedule a timer.
timer_list_engine(Error_Logger error_logger, Actor_Exception_Handler exception_handler)
Constructor with all parameters.
bool empty() const
Is empty timer list?
timer_type * m_head
Head of the list of timers.
timer_list_engine_defaults defaults_type
Type with default parameters for this engine.
void deactivate(timer_object_holder< Thread_Safety > timer)
Deactivate timer and remove it from the list.
void remove_timer_from_list(timer_type *timer)
Remove the timer from the list.
timer_type * make_exec_list()
Build sublist of elapsed timers.
timer_object_holder< Thread_Safety > allocate()
Create timer to be activated later.
void utilize_exec_list(timer_type *head)
Process list of elapsed timers after execution of its actions.
timer_type * m_tail
Tail of the list of timers.
void insert_timer_to_list(timer_type *timer)
Insert timer to the list.
A engine for timer wheel mechanism.
timer_object_holder< Thread_Safety > allocate()
Create timer to be activated later.
monotonic_clock::time_point nearest_time_point() const
Get time point of the next timer.
void insert_demand_to_wheel(timer_type *wheel_timer)
Insert timer to the wheel.
unsigned int m_current_position
Index of the current position in the wheel.
void process_expired_timers(Unique_Lock &lock)
Build sublist of elapsed timers and process them all.
~timer_wheel_engine()
Destructor.
bool m_current_tick_processed
Has the current tick been processed?
timer_wheel_engine(unsigned int wheel_size, monotonic_clock::duration granularity, Error_Logger error_logger, Actor_Exception_Handler exception_handler)
Constructor with all parameters.
bool empty() const
Is empty timer list?
unsigned int duration_to_ticks(Duration d) const
Converion of duration to number of time steps.
bool activate(timer_object_holder< Thread_Safety > timer, Duration_1 pause, Duration_2 period, timer_action action)
Activate timer and schedule it for execution.
std::vector< wheel_item > m_wheel
The wheel data.
const monotonic_clock::duration m_granularity
Granularity of one time step.
void process_current_wheel_position(Unique_Lock &lock)
Detect elapsed timers for the current time step and process them all.
void clear_all()
Deactivate all timers and cleanup internal data structures.
void remove_timer_from_wheel(timer_type *wheel_timer)
Remove timer from the timer_wheel.
monotonic_clock::time_point m_current_tick_border
Right border of the current tick.
static void ensure_timer_deactivated(const timer_type *timer)
Hard check for deactivation state of the timer.
bool reschedule(timer_object_holder< Thread_Safety > timer, Duration_1 pause, Duration_2 period, timer_action action)
Perform an attempt to reschedule a timer.
void perform_insertion_info_wheel(timer_type *wheel_timer, Duration_1 pause, Duration_2 period)
Perform insertion of a timer into wheel data structure.
void utilize_exec_list(timer_type *head)
Process list of elapsed timers after execution of its actions.
const unsigned int m_wheel_size
Size of the wheel.
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.
timer_type * make_exec_list()
Make list of elapsed timers to be executed.
void deactivate(timer_object_holder< Thread_Safety > timer)
Deactivate timer and remove it from the wheel.
void exec_actions(Unique_Lock &lock, timer_type *head) TIMERTT_NOEXCEPT
Execute all active timers from the list.
A special wrapper to be used to hold an actual timer object which is not allocated dynamically.
scoped_timer_object_holder(scoped_timer_object_holder &&)=delete
scoped_timer_object_holder(const scoped_timer_object_holder &)=delete
scoped_timer_object_holder()
A timer heap manager template.
timer_heap_manager_template()
Default constructor.
timer_heap_manager_template(std::size_t initial_heap_capacity, Error_Logger error_logger, Actor_Exception_Handler exception_handler)
Constructor with all parameters.
timer_heap_manager_template(std::size_t initial_heap_capacity)
Constructor to specify initial capacity of heap-array.
A timer heap thread template.
timer_heap_thread_template(std::size_t initial_heap_capacity)
Constructor to specify initial capacity of heap-array.
timer_heap_thread_template(std::size_t initial_heap_capacity, Error_Logger error_logger, Actor_Exception_Handler exception_handler)
Constructor with all parameters.
timer_heap_thread_template()
Default constructor.
A timer list thread template.
timer_list_manager_template()
Default constructor.
timer_list_manager_template(Error_Logger error_logger, Actor_Exception_Handler actor_exception_handler)
Constructor with all parameters.
A timer list thread template.
timer_list_thread_template(Error_Logger error_logger, Actor_Exception_Handler actor_exception_handler)
Constructor with all parameters.
timer_list_thread_template()
Default constructor.
An intrusive smart pointer to timer demand.
~timer_object_holder()
Destructor.
void swap(timer_object_holder &o)
Swap values.
timer_object_holder(timer_object< Thread_Safety > *t)
Constructor for a raw pointer.
void reset()
Drop controlled object.
void dismiss_object()
Decrement reference count to object and delete it if needed.
void take_object()
Increment reference count to object if it's not null.
timer_object_holder(timer_object_holder &&o)
Move constructor.
timer_object< Thread_Safety > * m_timer
Timer controlled by a smart pointer.
timer_object_holder()
Default constructor.
timer_object_holder & operator=(const timer_object_holder &o)
Copy operator.
timer_object< Thread_Safety > * get() const
timer_object_holder(const timer_object_holder &o)
Copy constructor.
timer_object_holder & operator=(timer_object_holder &&o)
Move operator.
operator bool() const
Is this a null pointer?
timer_object_holder(scoped_timer_object_holder< Actual_Object > &scoped)
Constructor for the case when timer object is a scoped timer.
A timer wheel manager template.
timer_wheel_manager_template(unsigned int wheel_size, monotonic_clock::duration granularity)
Constructor with wheel size and granularity parameters.
timer_wheel_manager_template(unsigned int wheel_size, monotonic_clock::duration granularity, Error_Logger error_logger, Actor_Exception_Handler exception_handler)
Constructor with all parameters.
timer_wheel_manager_template()
Default constructor.
A timer wheel thread template.
timer_wheel_thread_template(unsigned int wheel_size, monotonic_clock::duration granularity, Error_Logger error_logger, Actor_Exception_Handler exception_handler)
Constructor with all parameters.
timer_wheel_thread_template(unsigned int wheel_size, monotonic_clock::duration granularity)
Constructor with wheel size and granularity parameters.
timer_wheel_thread_template()
Default constructor.
#define SO_5_LOG_ERROR(logger, var_name)
A special macro for helping error logging.
Timers implementation details.
exception_handler_for_timertt_t create_exception_handler_for_timertt_thread(const error_logger_shptr_t &logger)
exception_handler_for_timertt_t create_exception_handler_for_timertt_manager(const error_logger_shptr_t &logger)
error_logger_for_timertt_t create_error_logger_for_timertt(const error_logger_shptr_t &logger)
Private part of message limit implementation.
SO_5_FUNC timer_manager_unique_ptr_t create_timer_heap_manager(error_logger_shptr_t logger, outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > collector, std::size_t initial_heap_capacity)
Create timer manager based on timer_heap mechanism.
SO_5_FUNC timer_thread_unique_ptr_t create_timer_wheel_thread(error_logger_shptr_t logger)
Create timer thread based on timer_wheel mechanism.
SO_5_FUNC timer_thread_unique_ptr_t create_timer_heap_thread(error_logger_shptr_t logger, std::size_t initial_heap_capacity)
Create timer thread based on timer_heap mechanism.
SO_5_FUNC timer_thread_unique_ptr_t create_timer_list_thread(error_logger_shptr_t logger)
Create timer thread based on timer_list mechanism.
SO_5_FUNC timer_manager_unique_ptr_t create_timer_list_manager(error_logger_shptr_t logger, outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > collector)
Create timer thread based on timer_list mechanism.
SO_5_FUNC timer_manager_unique_ptr_t create_timer_wheel_manager(error_logger_shptr_t logger, outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > collector)
Create timer manager based on timer_wheel mechanism.
SO_5_FUNC timer_manager_unique_ptr_t create_timer_wheel_manager(error_logger_shptr_t logger, outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > collector, unsigned int wheel_size, std::chrono::steady_clock::duration granuality)
Create timer manager based on timer_wheel mechanism.
SO_5_FUNC timer_manager_unique_ptr_t create_timer_heap_manager(error_logger_shptr_t logger, outliving_reference_t< timer_manager_t::elapsed_timers_collector_t > collector)
Create timer manager based on timer_heap mechanism.
SO_5_FUNC timer_thread_unique_ptr_t create_timer_wheel_thread(error_logger_shptr_t logger, unsigned int wheel_size, std::chrono::steady_clock::duration granuality)
Create timer thread based on timer_wheel mechanism.
SO_5_FUNC timer_thread_unique_ptr_t create_timer_heap_thread(error_logger_shptr_t logger)
Create timer thread based on timer_heap mechanism.
An internal namespace with implementation details.
timer_kind
Type of the timer (single-shot or periodic).
@ single_shot
Timer is a single-shot timer.
@ periodic
Timer is a periodic timer.
timer_status
Status of timer.
@ deactivated
Timer is deactivated.
@ wait_for_execution
Timer is in execution list and is waiting for execution.
@ wait_for_deactivation
Timer must be deactivated after processing of execution list.
@ active
Timer is activated.
Top-level project's namespace.
std::function< void() > default_timer_action_type
Defaulf type of timer action.
std::chrono::steady_clock monotonic_clock
Type of clock used by all threads.
Statistics for run-time monitoring.
Class of default handler for exceptions thrown from timer actors.
void operator()(const std::exception &)
Handles exception.
Class of default error logger.
void operator()(const std::string &what)
Logs error message to std::cerr.
Indicator that an engine will be owned by timer manager.
Indicator that an engine will be owned by timer thread.
A type-container for types of engine-consumers.
A selector of actual mixin type for timer manager or timer thread.
A mixin which must be used as base class for timer threads.
void ensure_started()
Checks that timer thread is really started.
std::shared_ptr< std::thread > m_thread
Underlying thread object.
std::condition_variable m_condition
Condition variable for waiting for next event.
void notify()
Sends notification to timer thread.
std::mutex m_lock
Timer thread's lock.
A mixin which must be used as base class for thread-safe timer managers.
std::mutex m_lock
Timer manager's lock.
A mixin which must be used as base class for not-thread-safe timer managers.
bool deactivated() const
Is timer deactivated.
static const std::size_t deactivation_indicator
A special value which means that timer is deactivated.
timer_action_holder< timer_action > m_action
Timer action.
std::size_t m_position
Position in the heap-array.
monotonic_clock::duration m_period
Period in ticks.
timer_kind kind() const
Detect type of timer (single-shot or periodic).
void deactivate()
Set deactivation indicator on.
bool single_shot() const
Is this is single shot timer?
monotonic_clock::time_point m_when
Time of execution for this timer.
Container for static method with default values for timer_heap engine.
static std::size_t default_initial_heap_capacity()
Default initial capacity of heap-array.
timer_action_holder< timer_action > m_action
Timer action.
monotonic_clock::time_point m_when
Time of execution for this timer.
timer_type * m_prev
Previous demand in the list.
threading_traits< Thread_Safety >::status_holder_type m_status
Status of the timer.
timer_kind kind() const
Detect type of timer (single-shot or periodic).
monotonic_clock::duration m_period
Period in ticks.
timer_type * m_next
Next demand in the list.
Container for static method with default values for timer_list engine.
threading_traits< Thread_Safety >::status_holder_type m_status
Status of the timer.
timer_type * m_next
Next demand in the list.
unsigned int m_position
Position in the wheel.
timer_kind kind() const
Detect type of the timer (single-shot or periodic).
unsigned int m_full_rolls_left
Full rolls of wheel before execution of demand.
timer_action_holder< timer_action > m_action
Timer action.
unsigned int m_period
Period in ticks.
timer_type * m_prev
Previous demand in the list.
timer_type * m_tail
Tail of the demand's list.
timer_type * m_head
Head of the demand's list.
Container for static method with default values for timer_wheel engine.
static unsigned int default_wheel_size()
Default wheel size.
static monotonic_clock::duration default_granularity()
Default tick duration.
Indicator for thread-safe implemetation.
Indicator for not-thread-safe implementation.
Container for thread safety flags.
std::atomic_uint reference_counter_type
Type for reference counters.
std::atomic< details::timer_status > status_holder_type
Type for holding timer status inside a timer object.
details::timer_status status_holder_type
Type for holding timer status inside a timer object.
unsigned int reference_counter_type
Type for reference counters.
Container for thread-safety-specific type declarations.
Base type for timer demands.
static void decrement_references(timer_object *t)
static void increment_references(timer_object *t)
Increment reference counter for the demand.
threading_traits< Thread_Safety >::reference_counter_type m_references
Reference counter for the demand.
timer_object()
Deafault constructor.
Information about quantities of various timer types.
std::size_t m_periodic_count
Quantity of periodic timers.
std::size_t m_single_shot_count
Quantity of single-shot timers.