2
3
6
7
8
9
10
11
13#include <so_5/details/abort_on_fatal_error.hpp>
15#include <so_5/impl/mbox_iface_for_timers.hpp>
17#include <so_5/timers.hpp>
19#include <so_5/3rd_party/timertt/all.hpp>
31
32
33
34
35
36
37
38
39
40
41template<
class Timer >
47 typename Timer::thread_safety >;
86
87
98
99
100
101
102
103
112 std::type_index type_index,
123 ::so_5::impl::mbox_iface_for_timers_t{ m_mbox }
124 .deliver_message_from_timer( m_type_index, m_msg );
132
133
134
135
136
137
138
139template<
class Timer_Thread >
148 std::unique_ptr< Timer_Thread > thread )
166 const std::type_index & type_index,
168 const message_ref_t & msg,
169 std::chrono::steady_clock::duration pause,
170 std::chrono::steady_clock::duration period )
override
184 const std::type_index & type_index,
186 const message_ref_t & msg,
187 std::chrono::steady_clock::duration pause,
188 std::chrono::steady_clock::duration period )
override
215
216
217
218
219
220
233 std::type_index type_index,
245 m_collector.get().accept( m_type_index, m_mbox, m_msg );
253
254
255
256
257
258
259
260template<
class Timer_Manager >
269 std::unique_ptr< Timer_Manager > manager,
292 const std::type_index & type_index,
294 const message_ref_t & msg,
295 std::chrono::steady_clock::duration pause,
296 std::chrono::steady_clock::duration period )
override
311 const std::type_index & type_index,
313 const message_ref_t & msg,
314 std::chrono::steady_clock::duration pause,
315 std::chrono::steady_clock::duration period )
override
351
352
353
354
355
362#if defined(__clang__
)
363#pragma clang diagnostic push
364#pragma clang diagnostic ignored "-Wmissing-prototypes"
375 return [logger](
const std::string & msg ) {
377 stream <<
"error inside timer_thread: " << msg;
382#if defined(__clang__
)
383#pragma clang diagnostic pop
390
391
392
393
394
402#if defined(__clang__
)
403#pragma clang diagnostic push
404#pragma clang diagnostic ignored "-Wmissing-prototypes"
411 return [logger](
const std::exception & x ) {
412 so_5::details::abort_on_fatal_error( [&] {
414 stream <<
"exception has been thrown and caught inside "
415 "timer_thread, application will be aborted. "
416 "Exception: " << x.what();
426 return [logger](
const std::exception & x ) {
427 so_5::details::abort_on_fatal_error( [&] {
429 stream <<
"exception has been thrown and caught inside "
430 "timer_manager, application will be aborted. "
431 "Exception: " << x.what();
437#if defined(__clang__
)
438#pragma clang diagnostic pop
442
443
444
484
485
493 using timertt_thread_t = timers_details::timer_wheel_thread_t;
495 return create_timer_wheel_thread(
497 timertt_thread_t::default_wheel_size(),
498 timertt_thread_t::default_granularity() );
507 using timertt_thread_t = timers_details::timer_wheel_thread_t;
510 std::unique_ptr< timertt_thread_t > thread(
511 new timertt_thread_t(
514 create_error_logger_for_timertt( logger ),
515 create_exception_handler_for_timertt_thread( logger ) ) );
517 return timer_thread_unique_ptr_t(
518 new actual_thread_t< timertt_thread_t >( std::move( thread ) ) );
525 using timertt_thread_t = timers_details::timer_heap_thread_t;
527 return create_timer_heap_thread(
529 timertt_thread_t::default_initial_heap_capacity() );
537 using timertt_thread_t = timers_details::timer_heap_thread_t;
540 std::unique_ptr< timertt_thread_t > thread(
541 new timertt_thread_t(
542 initial_heap_capacity,
543 create_error_logger_for_timertt( logger ),
544 create_exception_handler_for_timertt_thread( logger ) ) );
546 return timer_thread_unique_ptr_t(
547 new actual_thread_t< timertt_thread_t >( std::move( thread ) ) );
554 using timertt_thread_t = timers_details::timer_list_thread_t;
557 std::unique_ptr< timertt_thread_t > thread(
558 new timertt_thread_t(
559 create_error_logger_for_timertt( logger ),
560 create_exception_handler_for_timertt_thread( logger ) ) );
562 return timer_thread_unique_ptr_t(
563 new actual_thread_t< timertt_thread_t >( std::move( thread ) ) );
572 using timertt_manager_t = timers_details::timer_wheel_manager_t;
574 return create_timer_wheel_manager(
577 timertt_manager_t::default_wheel_size(),
578 timertt_manager_t::default_granularity() );
589 using timertt_manager_t = timers_details::timer_wheel_manager_t;
592 auto manager = std::make_unique< timertt_manager_t >(
595 create_error_logger_for_timertt( logger ),
596 create_exception_handler_for_timertt_manager( logger ) );
598 return std::make_unique< actual_manager_t< timertt_manager_t > >(
599 std::move( manager ),
609 using timertt_manager_t = timers_details::timer_heap_manager_t;
611 return create_timer_heap_manager(
614 timertt_manager_t::default_initial_heap_capacity() );
624 using timertt_manager_t = timers_details::timer_heap_manager_t;
627 auto manager = std::make_unique< timertt_manager_t >(
628 initial_heap_capacity,
629 create_error_logger_for_timertt( logger ),
630 create_exception_handler_for_timertt_manager( logger ) );
632 return std::make_unique< actual_manager_t< timertt_manager_t > >(
633 std::move( manager ),
643 using timertt_manager_t = timers_details::timer_list_manager_t;
646 auto manager = std::make_unique< timertt_manager_t >(
647 create_error_logger_for_timertt( logger ),
648 create_exception_handler_for_timertt_manager( logger ) );
650 return std::make_unique< actual_manager_t< timertt_manager_t > >(
651 std::move( manager ),
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
An intrusive smart pointer to timer demand.
#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.
Top-level project's namespace.
Statistics for run-time monitoring.