Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_shield::TimerScheduler Class Reference

Scheduler that manages timer execution. More...

#include <TimerScheduler.hpp>

Public Types

using clock = detail::TimerClock
 

Public Member Functions

 TimerScheduler ()
 
 ~TimerScheduler ()
 
 TimerScheduler (const TimerScheduler &)=delete
 
TimerScheduleroperator= (const TimerScheduler &)=delete
 
 TimerScheduler (TimerScheduler &&)=delete
 
TimerScheduleroperator= (TimerScheduler &&)=delete
 
void run ()
 Starts a dedicated worker thread that processes timers.
 
void stop ()
 Requests the worker thread to stop and waits for it to exit.
 
void process ()
 Processes all timers that are ready to fire at the moment of the call.
 
void update ()
 Alias for process() for compatibility with update-based loops.
 
std::size_t active_timer_count_for_testing ()
 Returns number of timer states that are still alive.
 

Private Member Functions

timer_state_ptr create_timer_state ()
 
void destroy_timer_state (const timer_state_ptr &state)
 
void start_timer (const timer_state_ptr &state, clock::time_point when)
 
void stop_timer (const timer_state_ptr &state)
 
void worker_loop ()
 
void collect_due_timers_locked (std::vector< detail::DueTimer > &due, clock::time_point now)
 
void execute_due_timers (std::vector< detail::DueTimer > &due)
 
void finalize_timer (const detail::DueTimer &due_timer)
 

Private Attributes

std::mutex m_mutex
 
std::condition_variable m_cv
 
std::thread m_thread
 
bool m_is_worker_running {false}
 
bool m_stop_requested {false}
 
std::priority_queue< detail::ScheduledTimer, std::vector< detail::ScheduledTimer >, detail::ScheduledComparatorm_queue
 
std::unordered_map< std::size_t, std::weak_ptr< detail::TimerState > > m_timers
 
std::size_t m_next_id {1}
 

Friends

class Timer
 

Detailed Description

Scheduler that manages timer execution.

Definition at line 114 of file TimerScheduler.hpp.

Member Typedef Documentation

◆ clock

Constructor & Destructor Documentation

◆ TimerScheduler() [1/3]

time_shield::TimerScheduler::TimerScheduler ( )
inlinedefault

◆ ~TimerScheduler()

time_shield::TimerScheduler::~TimerScheduler ( )
inline

Definition at line 251 of file TimerScheduler.hpp.

◆ TimerScheduler() [2/3]

time_shield::TimerScheduler::TimerScheduler ( const TimerScheduler & )
delete

◆ TimerScheduler() [3/3]

time_shield::TimerScheduler::TimerScheduler ( TimerScheduler && )
delete

Member Function Documentation

◆ active_timer_count_for_testing()

std::size_t time_shield::TimerScheduler::active_timer_count_for_testing ( )
inline

Returns number of timer states that are still alive.

Method is intended for tests to verify resource cleanup.

Definition at line 341 of file TimerScheduler.hpp.

◆ collect_due_timers_locked()

void time_shield::TimerScheduler::collect_due_timers_locked ( std::vector< detail::DueTimer > & due,
clock::time_point now )
inlineprivate

Definition at line 435 of file TimerScheduler.hpp.

◆ create_timer_state()

timer_state_ptr time_shield::TimerScheduler::create_timer_state ( )
inlineprivate

Definition at line 352 of file TimerScheduler.hpp.

◆ destroy_timer_state()

void time_shield::TimerScheduler::destroy_timer_state ( const timer_state_ptr & state)
inlineprivate

Definition at line 361 of file TimerScheduler.hpp.

◆ execute_due_timers()

void time_shield::TimerScheduler::execute_due_timers ( std::vector< detail::DueTimer > & due)
inlineprivate

Definition at line 466 of file TimerScheduler.hpp.

◆ finalize_timer()

void time_shield::TimerScheduler::finalize_timer ( const detail::DueTimer & due_timer)
inlineprivate

Definition at line 485 of file TimerScheduler.hpp.

◆ operator=() [1/2]

TimerScheduler & time_shield::TimerScheduler::operator= ( const TimerScheduler & )
delete

◆ operator=() [2/2]

TimerScheduler & time_shield::TimerScheduler::operator= ( TimerScheduler && )
delete

◆ process()

void time_shield::TimerScheduler::process ( )
inline

Processes all timers that are ready to fire at the moment of the call.

The method is non-blocking: it does not wait for future timers. It must not be called while the worker thread started by run() is active.

Definition at line 326 of file TimerScheduler.hpp.

◆ run()

void time_shield::TimerScheduler::run ( )
inline

Starts a dedicated worker thread that processes timers.

This method is non-blocking. It spawns a background thread that waits for timers to fire and executes their callbacks. While the worker thread is active, manual processing via process() or update() must not be used.

Definition at line 266 of file TimerScheduler.hpp.

◆ start_timer()

void time_shield::TimerScheduler::start_timer ( const timer_state_ptr & state,
clock::time_point when )
inlineprivate

Definition at line 378 of file TimerScheduler.hpp.

◆ stop()

void time_shield::TimerScheduler::stop ( )
inline

Requests the worker thread to stop and waits for it to exit.

Definition at line 276 of file TimerScheduler.hpp.

◆ stop_timer()

void time_shield::TimerScheduler::stop_timer ( const timer_state_ptr & state)
inlineprivate

Definition at line 389 of file TimerScheduler.hpp.

◆ update()

void time_shield::TimerScheduler::update ( )
inline

Alias for process() for compatibility with update-based loops.

Definition at line 337 of file TimerScheduler.hpp.

◆ worker_loop()

void time_shield::TimerScheduler::worker_loop ( )
inlineprivate

Definition at line 399 of file TimerScheduler.hpp.

Friends And Related Symbol Documentation

◆ Timer

friend class Timer
friend

Definition at line 153 of file TimerScheduler.hpp.

Member Data Documentation

◆ m_cv

std::condition_variable time_shield::TimerScheduler::m_cv
private

Definition at line 166 of file TimerScheduler.hpp.

◆ m_is_worker_running

bool time_shield::TimerScheduler::m_is_worker_running {false}
private

Definition at line 168 of file TimerScheduler.hpp.

◆ m_mutex

std::mutex time_shield::TimerScheduler::m_mutex
private

Definition at line 165 of file TimerScheduler.hpp.

◆ m_next_id

std::size_t time_shield::TimerScheduler::m_next_id {1}
private

Definition at line 172 of file TimerScheduler.hpp.

◆ m_queue

std::priority_queue<detail::ScheduledTimer, std::vector<detail::ScheduledTimer>, detail::ScheduledComparator> time_shield::TimerScheduler::m_queue
private

Definition at line 170 of file TimerScheduler.hpp.

◆ m_stop_requested

bool time_shield::TimerScheduler::m_stop_requested {false}
private

Definition at line 169 of file TimerScheduler.hpp.

◆ m_thread

std::thread time_shield::TimerScheduler::m_thread
private

Definition at line 167 of file TimerScheduler.hpp.

◆ m_timers

std::unordered_map<std::size_t, std::weak_ptr<detail::TimerState> > time_shield::TimerScheduler::m_timers
private

Definition at line 171 of file TimerScheduler.hpp.


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