Class astutedds::dcps::TimerService

ClassList > astutedds > dcps > TimerService

More...

  • #include <timer_service.hpp>

Public Functions

Type Name
TimerService ()
TimerService (const TimerService &) = delete
bool cancel (TimerId id)
bool is_running () const
True if the background thread is running. Primarily for tests.
TimerService & operator= (const TimerService &) = delete
bool reset (TimerId id, std::chrono::nanoseconds new_delay)
TimerId schedule (std::chrono::nanoseconds delay, std::function< void()> callback)
TimerId schedule_periodic (std::chrono::nanoseconds period, std::function< void()> callback)
void start ()
void stop ()
std::size_t timer_count () const
~TimerService ()

Detailed Description

Cooperative min-heap timer scheduler with a single background thread. See file-level comment for the threading contract.

Public Functions Documentation

function TimerService [1/2]

astutedds::dcps::TimerService::TimerService () 

function TimerService [2/2]

astutedds::dcps::TimerService::TimerService (
    const TimerService &
) = delete

function cancel

bool astutedds::dcps::TimerService::cancel (
    TimerId id
) 

Cancel a timer. For one-shot timers, the callback is skipped if it has not already fired. For periodic timers, no further fires occur but a currently-executing callback runs to completion. Returns true if the timer was found and cancelled, false if it had already fired / been cancelled / never existed.


function is_running

True if the background thread is running. Primarily for tests.

bool astutedds::dcps::TimerService::is_running () const


function operator=

TimerService & astutedds::dcps::TimerService::operator= (
    const TimerService &
) = delete

function reset

bool astutedds::dcps::TimerService::reset (
    TimerId id,
    std::chrono::nanoseconds new_delay
) 

Reset a timer's next fire time to new_delay from now. Preserves the callback and (for periodic timers) the period. A one-shot timer that has already fired cannot be reset — returns false. Useful for deadline QoS: "we just saw the event we were watching for; push the expiry back to full period from now".


function schedule

TimerId astutedds::dcps::TimerService::schedule (
    std::chrono::nanoseconds delay,
    std::function< void()> callback
) 

Schedule a one-shot timer. The callback is invoked once, from the timer thread, approximately delay from the moment of this call. delay may be zero (fires ASAP) or negative (clamped to zero). Returns an opaque handle valid until the callback fires or the timer is cancelled.


function schedule_periodic

TimerId astutedds::dcps::TimerService::schedule_periodic (
    std::chrono::nanoseconds period,
    std::function< void()> callback
) 

Schedule a periodic timer. The first fire occurs after period; each subsequent fire is drift-corrected relative to the previous scheduled fire so long-running callbacks catch up rather than accumulate lag. period must be > 0; a zero or negative period is rejected (returns INVALID_TIMER_ID).


function start

void astutedds::dcps::TimerService::start () 

Start the background thread. Idempotent — calling start twice is a no-op. Must be called before schedule() actually delivers callbacks; schedule() before start() is allowed and queues the timers, but callbacks fire once start() is invoked.


function stop

void astutedds::dcps::TimerService::stop () 

Stop the background thread. All pending timers are silently dropped (their callbacks do NOT fire). If a callback is currently executing, stop() waits for it to return before joining the thread.


function timer_count

std::size_t astutedds::dcps::TimerService::timer_count () const

Number of timers currently scheduled (excluding cancelled ones). Primarily for tests.


function ~TimerService

astutedds::dcps::TimerService::~TimerService () 


The documentation for this class was generated from the following file include/astutedds/dcps/timer_service.hpp