VLink  2.0.0
A high-performance communication middleware
vlink::DeadlineTimer Class Referencefinal

Cache-line aligned atomic deadline counter with millisecond / microsecond / nanosecond precision. More...

#include <deadline_timer.h>

Collaboration diagram for vlink::DeadlineTimer:

Public Types

using Accuracy = ElapsedTimer::Accuracy
 Precision unit alias inherited from ElapsedTimer::Accuracy. More...
 

Public Member Functions

 DeadlineTimer () noexcept
 Constructs an invalid timer with no deadline set. More...
 
 DeadlineTimer (int64_t interval, Accuracy accuracy=ElapsedTimer::kMilli) noexcept
 Constructs a timer that expires interval units in the future. More...
 
 DeadlineTimer (const DeadlineTimer &other) noexcept
 Copy constructor; copies the stored deadline and accuracy. More...
 
 DeadlineTimer (DeadlineTimer &&other) noexcept
 Move constructor; equivalent to copy because the timer holds only POD state. More...
 
 ~DeadlineTimer () noexcept
 Destructor. More...
 
DeadlineTimeroperator= (const DeadlineTimer &other) noexcept
 Copy assignment; replaces the stored deadline and accuracy. More...
 
DeadlineTimeroperator= (DeadlineTimer &&other) noexcept
 Move assignment; equivalent to copy assignment for this POD-state type. More...
 
void set_deadline (int64_t interval) noexcept
 Sets the deadline interval units after the current monotonic timestamp. More...
 
void set_deadline_abs (uint64_t abs_deadline) noexcept
 Sets an absolute deadline timestamp directly. More...
 
void reset () noexcept
 Resets the timer to the invalid state. More...
 
uint64_t deadline () const noexcept
 Returns the stored absolute deadline timestamp. More...
 
int64_t remaining_time () const noexcept
 Returns the time remaining until the deadline. More...
 
bool has_expired () const noexcept
 Reports whether the current time has passed the stored deadline. More...
 
bool is_valid () const noexcept
 Reports whether a deadline has been set. More...
 
Accuracy get_accuracy () const noexcept
 Returns the precision configured for this timer. More...
 

Detailed Description

Cache-line aligned atomic deadline counter with millisecond / microsecond / nanosecond precision.

Reuses ElapsedTimer::Accuracy for time precision. Reads and writes go through a single std::atomic<uint64_t> aligned to 64 bytes to avoid false sharing when several timers share a structure. An invalid (unset) timer stores 0 and never reports expiry.

Member Typedef Documentation

◆ Accuracy

Constructor & Destructor Documentation

◆ DeadlineTimer() [1/4]

vlink::DeadlineTimer::DeadlineTimer ( )
noexcept

Constructs an invalid timer with no deadline set.

is_valid returns false and has_expired returns false until set_deadline or set_deadline_abs is called.

◆ DeadlineTimer() [2/4]

vlink::DeadlineTimer::DeadlineTimer ( int64_t  interval,
Accuracy  accuracy = ElapsedTimer::kMilli 
)
explicitnoexcept

Constructs a timer that expires interval units in the future.

The absolute deadline is computed as the current monotonic timestamp plus interval. A non-positive interval leaves the timer invalid.

Parameters
intervalDuration until expiry in accuracy units.
accuracyPrecision of stored timestamps. Default: ElapsedTimer::kMilli.

◆ DeadlineTimer() [3/4]

vlink::DeadlineTimer::DeadlineTimer ( const DeadlineTimer other)
noexcept

Copy constructor; copies the stored deadline and accuracy.

Parameters
otherSource timer.

◆ DeadlineTimer() [4/4]

vlink::DeadlineTimer::DeadlineTimer ( DeadlineTimer &&  other)
noexcept

Move constructor; equivalent to copy because the timer holds only POD state.

Parameters
otherSource timer.

◆ ~DeadlineTimer()

vlink::DeadlineTimer::~DeadlineTimer ( )
noexcept

Destructor.

Member Function Documentation

◆ deadline()

uint64_t vlink::DeadlineTimer::deadline ( ) const
noexcept

Returns the stored absolute deadline timestamp.

Returns
Deadline value in the configured accuracy unit. 0 indicates an invalid timer.

◆ get_accuracy()

Accuracy vlink::DeadlineTimer::get_accuracy ( ) const
noexcept

Returns the precision configured for this timer.

Returns
Accuracy unit chosen at construction.

◆ has_expired()

bool vlink::DeadlineTimer::has_expired ( ) const
noexcept

Reports whether the current time has passed the stored deadline.

Returns
true when expired; always false for invalid timers.

◆ is_valid()

bool vlink::DeadlineTimer::is_valid ( ) const
noexcept

Reports whether a deadline has been set.

Returns
true when deadline() is non-zero.

◆ operator=() [1/2]

DeadlineTimer& vlink::DeadlineTimer::operator= ( const DeadlineTimer other)
noexcept

Copy assignment; replaces the stored deadline and accuracy.

Parameters
otherSource timer.
Returns
Reference to *this.

◆ operator=() [2/2]

DeadlineTimer& vlink::DeadlineTimer::operator= ( DeadlineTimer &&  other)
noexcept

Move assignment; equivalent to copy assignment for this POD-state type.

Parameters
otherSource timer.
Returns
Reference to *this.

◆ remaining_time()

int64_t vlink::DeadlineTimer::remaining_time ( ) const
noexcept

Returns the time remaining until the deadline.

Computed as (deadline - current_cpu_timestamp); clamped to 0 once the deadline has been reached or when the timer is invalid.

Returns
Remaining time in the configured accuracy unit; 0 when invalid or expired.

◆ reset()

void vlink::DeadlineTimer::reset ( )
noexcept

Resets the timer to the invalid state.

After reset, is_valid returns false and has_expired returns false.

◆ set_deadline()

void vlink::DeadlineTimer::set_deadline ( int64_t  interval)
noexcept

Sets the deadline interval units after the current monotonic timestamp.

A non-positive interval clears the timer (equivalent to reset).

Parameters
intervalDuration until expiry in the configured accuracy unit.

◆ set_deadline_abs()

void vlink::DeadlineTimer::set_deadline_abs ( uint64_t  abs_deadline)
noexcept

Sets an absolute deadline timestamp directly.

abs_deadline must be expressed in the same unit and time base as the configured accuracy; obtain it via ElapsedTimer::get_cpu_timestamp(accuracy).

Parameters
abs_deadlineAbsolute monotonic deadline.

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