Motion Master
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
mm::core::timer::MainTimer Class Reference

Cross-platform high-resolution periodic timer. More...

#include <timer.h>

Public Member Functions

void wait ()
 Waits for the next timer tick to occur. More...
 
uint64_t getTickCount () const
 Returns the current tick count. More...
 
uint64_t getCycleCount () const
 Returns the number of completed wait cycles. More...
 
uint64_t getCycleMissedTickCount () const
 Returns the number of missed timer ticks in the current cycle. More...
 
int64_t getElapsedTime () const
 Returns the total elapsed time since the timer started. More...
 
int64_t getWaitIntervalUs () const
 Returns the interval in microseconds between two consecutive wait() calls. More...
 

Static Public Member Functions

static MainTimergetInstance ()
 Returns the singleton instance of the MainTimer. More...
 
static void notifyAllListeners ()
 Notifies all listeners (waiting threads) about a timer tick. More...
 

Static Public Attributes

static unsigned int DELAY_MS {1}
 Static delay in milliseconds between each timer tick. More...
 
static std::mutex listenerMx {}
 Static mutex protecting the static condition variable for notifying all listener threads. More...
 
static std::condition_variable listenerCv {}
 Static condition variable used to notify all listener threads of timer ticks. More...
 

Detailed Description

Cross-platform high-resolution periodic timer.

The MainTimer class implements a singleton timer that generates periodic high-resolution timer events. It is designed to be thread-safe and allows other components in the application to synchronize with or wait for timer ticks.

This class supports both Windows and POSIX-compliant systems, providing a consistent interface for time-based operations across platforms.

Member Function Documentation

◆ getCycleCount()

uint64_t mm::core::timer::MainTimer::getCycleCount ( ) const

Returns the number of completed wait cycles.

A cycle is recorded each time wait() completes successfully.

Returns
Completed cycle count as an unsigned 64-bit integer.

◆ getCycleMissedTickCount()

uint64_t mm::core::timer::MainTimer::getCycleMissedTickCount ( ) const

Returns the number of missed timer ticks in the current cycle.

Indicates how many ticks were missed due to delays in processing.

Returns
Number of missed ticks as an unsigned 64-bit integer.

◆ getElapsedTime()

int64_t mm::core::timer::MainTimer::getElapsedTime ( ) const

Returns the total elapsed time since the timer started.

Uses a high-resolution clock to calculate the elapsed time in microseconds.

Returns
Elapsed time in microseconds as a signed 64-bit integer.

◆ getInstance()

MainTimer & mm::core::timer::MainTimer::getInstance ( )
static

Returns the singleton instance of the MainTimer.

Ensures only one instance of MainTimer exists throughout the application's lifetime. Initializes the timer on the first call.

Returns
Reference to the singleton MainTimer instance.

◆ getTickCount()

uint64_t mm::core::timer::MainTimer::getTickCount ( ) const

Returns the current tick count.

Tick count increments with each timer tick and reflects the total number of ticks since the timer was started.

Returns
The current tick count as an unsigned 64-bit integer.

◆ getWaitIntervalUs()

int64_t mm::core::timer::MainTimer::getWaitIntervalUs ( ) const

Returns the interval in microseconds between two consecutive wait() calls.

Returns
The interval in microseconds as a signed 64-bit integer.

◆ notifyAllListeners()

void mm::core::timer::MainTimer::notifyAllListeners ( )
static

Notifies all listeners (waiting threads) about a timer tick.

Broadcasts a signal using the condition variable to wake any threads waiting for a tick.

◆ wait()

void mm::core::timer::MainTimer::wait ( )

Waits for the next timer tick to occur.

Blocks the current thread until a tick is received. Tracks missed ticks and updates internal state accordingly.

Member Data Documentation

◆ DELAY_MS

unsigned int mm::core::timer::MainTimer::DELAY_MS {1}
static

Static delay in milliseconds between each timer tick.

◆ listenerCv

std::condition_variable mm::core::timer::MainTimer::listenerCv {}
static

Static condition variable used to notify all listener threads of timer ticks.

This condition variable works together with the static mutex listenerMx to safely synchronize notification and waiting of multiple listener threads.

◆ listenerMx

std::mutex mm::core::timer::MainTimer::listenerMx {}
static

Static mutex protecting the static condition variable for notifying all listener threads.

This mutex must be locked whenever waiting on or notifying the static condition variable to ensure thread-safe synchronization across all listeners.


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