CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::utils::Timer Class Reference

Multiplexing timer: schedule many periodic timers and consume their expiries from one queue. More...

#include <Utils.h>

Collaboration diagram for cmlabs::utils::Timer:
[legend]

Public Member Functions

 Timer ()
 ~Timer ()
bool addTimer (uint32 id, uint32 interval, uint64 start=0, uint64 end=0)
 Register a periodic timer.
bool triggerTimer (uint32 id)
 Manually inject an expiry for timer id, as if it had fired now.
bool waitForTimer (uint32 timeout, uint32 &id, uint64 &time)
 Wait for the next expiry of any registered timer.
bool removeTimer (uint32 id)
 Unregister a timer and cancel its OS timer.

Static Public Attributes

static std::map< uint32, Timer * > * timers = NULL
 Global registry mapping globalID to live Timer instances, used by the OS callbacks.

Detailed Description

Multiplexing timer: schedule many periodic timers and consume their expiries from one queue.

Each addTimer() registers an OS timer (timer queue / dispatch source / POSIX timer). Expiries are pushed into an internal trigger queue which a consumer drains via waitForTimer(), giving a select()-like wait over any number of timers from a single thread.

Definition at line 643 of file Utils.h.

Constructor & Destructor Documentation

◆ Timer()

cmlabs::utils::Timer::Timer ( )

Definition at line 1642 of file Utils.cpp.

References timers.

Referenced by addTimer().

◆ ~Timer()

cmlabs::utils::Timer::~Timer ( )

Definition at line 1651 of file Utils.cpp.

References removeTimer(), and timers.

Member Function Documentation

◆ addTimer()

bool cmlabs::utils::Timer::addTimer ( uint32 id,
uint32 interval,
uint64 start = 0,
uint64 end = 0 )

Register a periodic timer.

Parameters
idCaller-chosen id reported back by waitForTimer().
intervalPeriod in milliseconds.
startOptional absolute start time (ms epoch); 0 = now.
endOptional absolute end time after which the timer stops; 0 = never.
Returns
true on success.

Definition at line 1672 of file Utils.cpp.

References cmlabs::utils::TimerSchedule::end, cmlabs::GetTimeNow(), cmlabs::utils::TimerSchedule::globalID, cmlabs::utils::TimerSchedule::handle, cmlabs::utils::TimerSchedule::id, cmlabs::utils::TimerSchedule::interval, cmlabs::utils::TimerSchedule::start, Timer(), cmlabs::utils::TimerCallback(), timers, and triggerTimer().

Referenced by cmlabs::utils::UnitTest_Timer().

◆ removeTimer()

bool cmlabs::utils::Timer::removeTimer ( uint32 id)

Unregister a timer and cancel its OS timer.

Parameters
idTimer id.
Returns
true on success.

Definition at line 1769 of file Utils.cpp.

References cmlabs::utils::TimerSchedule::handle.

Referenced by triggerTimer(), and ~Timer().

◆ triggerTimer()

bool cmlabs::utils::Timer::triggerTimer ( uint32 id)

Manually inject an expiry for timer id, as if it had fired now.

Returns
true on success.

Definition at line 1796 of file Utils.cpp.

References cmlabs::utils::TimerSchedule::end, cmlabs::GetTimeNow(), cmlabs::utils::TimerSchedule::id, cmlabs::utils::TimerTrigger::id, cmlabs::utils::TimerSchedule::interval, removeTimer(), and cmlabs::utils::TimerTrigger::time.

Referenced by addTimer(), and cmlabs::utils::TimerCallback().

◆ waitForTimer()

bool cmlabs::utils::Timer::waitForTimer ( uint32 timeout,
uint32 & id,
uint64 & time )

Wait for the next expiry of any registered timer.

Parameters
timeoutMax wait in milliseconds.
[out]idReceives the id of the timer that fired.
[out]timeReceives the expiry timestamp (ms).
Returns
true when a trigger was consumed, false on timeout.

Definition at line 1818 of file Utils.cpp.

References cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), cmlabs::utils::TimerTrigger::id, and cmlabs::utils::TimerTrigger::time.

Referenced by cmlabs::utils::UnitTest_Timer().

Member Data Documentation

◆ timers

std::map< uint32, Timer * > * cmlabs::utils::Timer::timers = NULL
static

Global registry mapping globalID to live Timer instances, used by the OS callbacks.

Definition at line 646 of file Utils.h.

Referenced by addTimer(), cmlabs::ClearUtilsMaps(), Timer(), cmlabs::utils::TimerCallback(), and ~Timer().


The documentation for this class was generated from the following files:
  • /home/ubuntu/c/partner/psyclone2/CMSDK/include/Utils.h
  • /home/ubuntu/c/partner/psyclone2/CMSDK/src/Utils.cpp