Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Timer

Table of Contents

Overview

...

Expand
titleWallTime
Code Block
TIMERVAL WallTime()

Returns the current value of the wall clock.

...

The following functions can be used to determine the type of a timer.

Expand
titleIsSetTimerCurTimers
Code Block
intvoid IsSetTimer(TIMERVAL timerCurTimers(TIMERVAL* pSetTimer, TIMERVAL* pWallTime)
   where:
      TIMERVAL* timerpSetTimer = Pointer to Timer value

Checks if the passed timer is a set interval timer value. If so, this function returns non-zero; otherwise it returns zero.

Expand
titleIsWallTime
Code Block
int IsWallTime(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer value

Checks if the passed timer is a wall clock timer value. If so, this function returns non-zero; otherwise it returns zero.

...

output set interval time
      TIMERVAL* pWallTime = Pointer to output wall clock time

This function fills the passed timers with the current values of each type of timer. This function is provided to guarantee the timers are always sampled in the same order in situations where the values of both current timers are required.

Timer Type Detection

The following functions can be used to determine the type of a timer.

Expand
titleIsSetTimer
Code Block
int IsSetTimer(TIMERVAL ResetTimer()

Returns a value indicating that the timer is reset.

Timer Type Conversion

...

timer)
   where:
      TIMERVAL timer = Timer value

Checks if the passed timer is a set interval timer value. If so, this function returns non-zero; otherwise it returns zero.

Expand
titleIsWallTime
Code Block
int IsWallTime(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer value

Checks if the passed timer is a wall clock timer value. If so, this function returns non-zero; otherwise it returns zero.

Timer Reset

The following functions can be used to initialize or reset a timer.

Expand
titleResetTimer
Code Block
TIMERVAL ResetTimer()

Returns a value indicating that the timer is reset.

Timer Type Conversion

The following functions can be used to convert between timer types.

Expand
titleSetTimerToWallTime
Code Block
TIMERVAL value = SetTimerToWallTime(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer value

Converts a set interval timer to a wall clock time by offsetting the current wall time by the amount of time elapsed since (or until) the supplied set interval timer. Note this may differ from the wall time that would have been current at the moment that the set interval timer was set.

If the passed value is already a wall clock time, then it is returned unchanged.

Expand
titleWallTimeToSetTimer
Code Block
TIMERVAL value = WallTimeToSetTimer(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer value

Converts a wall clock timer to a set interval timer by offsetting the current set interval clock time by the amount of time elapsed since (or until) the supplied wall time. Note this may differ from the set interval timer value that would have been current at the moment that the wall clock value was retrieved.

If the passed value is already a set interval timer, then it is returned unchanged.

Expand
titleTimerValToFileTime
Code Block
TIMERVAL value = TimerValToFileTime(TIMERVAL timer, LPFILETIME pFileTime)
   where:
      TIMERVAL timer = Timer value
      LPFILETIME pFileTime = Pointer to target FILETIME structure

Converts a timer to a FILETIME structure. If the supplied timer is a set interval timer, then it is first converted to a wall clock time before converting to a FILETIME.

Expand
titleWallTimeFileTimeToTimerVal
Code Block
TIMERVAL value = WallTimeFileTimeToTimerVal(timerLPFILETIME pFileTime)
   where:
      TIMERVAL timer = Timer value

Converts a set interval timer to a wall clock time adjusting the current wall time by the amount of time elapsed since (or until) the passed set interval timer. Note this may differ from the wall time that would have been current at the moment that the set interval timer was set.

If the passed value is already a wall clock time then it is returned unchanged
LPFILETIME pFileTime = Pointer to target FILETIME structure

Converts a FILETIME to a wall clock timer.

Setting Timers

The following functions can be used to create a timer that will expire at some time in the future.

...

Expand
titleAddUTimer, AddMTimer, & AddSTimer
Code Block
TIMERVAL value = AddUTimer(TIMERVAL timer, int64_t us)
   where:
      TIMERVAL timer = Existing timer
      int64_t us = Timeout value (us)

TIMERVAL value = AddMTimer(TIMERVAL timer, int64_t ms)
   where:
      TIMERVAL timer = Existing timer
      int64_t ms = Timeout value (ms)

TIMERVAL value = AddSTimer(timer,TIMERVAL timer, int64_t sec)
   where:
      TIMERVAL timer = Existing timer
      int64_t sec = Timeout value (sec)

Adds additional time to an existing timer and returns the new timer value. This function can be used to allow a timer to expire on fixed intervals without concern for delays between when the timer actually expires and when the timer is checked.

Code Block
// Set a timer to expire after 100 ms
TIMERVAL timer = SetMTimer(100);

// Oversleep past the timer's expiration
Sleep(115);

// Reset the timer so that it will now expire 200 ms
// after it was originally set (not 100 ms from now)
timer = AddMTimer(timer, 100);

This function works with both wall clock timers and set interval timers. If the timer value passed to the AddXTimer(..) function has not previously been set then the returned timer will be a set interval timer set to expire after the specified timeout.

...

Expand
titleExpUTimer, ExpMTimer, & ExpSTimer
Code Block
int value = ExpUTimer(TIMERVAL timer, int64_t us)
   where:
      TIMERVAL timer = Timer to test
      int64_t us     = Timeout value (us)

int value = ExpMTimer(TIMERVAL timer, int64_t ms)
   where:
      TIMERVAL timer = Timer to test
      int64_t ms     = Timeout value (ms)

int value = ExpSTimer(TIMERVAL timer, int64_t sec)
   where:
      TIMERVAL timer = Timer to test
      int64_t sec    = Timeout value (sec)

Tests if a timer has expired i.e. the time elapsed since a timer was set is greater than or equal to a timeout. If so, this function returns non-zero. Zero is returned if the timeout hasn’t been reached or if the timer has not been set.

This function works with both wall clock timers and set interval timers and is typically used in conjunction with the StartTimer() or WallTime() methods:

Code Block
// Get the current time
TIMERVAL timer = StartTimer();

// Do some work
...

// Check if more than 100 ms has elapsed
int tstatus = ElapMTimer(timer, 100);
Expand
titleActUTimer, ActMTimer, & ActSTimer
Code Block
int value = ActUTimer(TIMERVAL timer, int64_t us)
   where:
      TIMERVAL timer = Timer to test
      int64_t us     = Timeout value (us)

int value = ActMTimer(TIMERVAL timer, int64_t ms)
   where:
      TIMERVAL timer = Timer to test
      int64_t ms     = Timeout value (ms)

int value = ActSTimer(TIMERVAL timer, int64_t sec)
   where:
      TIMERVAL timer = Timer to test
      int64_t sec    = Timeout value (sec)

Tests if a timer is still active i.e. the time elapsed since a timer was set is less than a timeout. If so, this function returns non-zero. Zero is returned if the timeout has already been reached or if the timer has not been set.

This function works with both wall clock timers and set interval timers and is typically used in conjunction with the StartTimer() or WallTime() methods:

Code Block
// Get the current time
TIMERVAL timer = StartTimer();

// Do some work
...

// Check if less than 100 ms has elapsed
int tstatus = ActMTimer(timer, 100);

...

Expand
titleElapUTimer, ElapMTimer, & ElapSTimer
Code Block
int64_t elap = ElapUTimer(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer to test

int64_t elap = ElapMTimer(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer to test

int64_t elap = ElapSTimer(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer to test

Calculates the amount of time elapsed since a timer value, returned in units of full microseconds, milliseconds, or seconds. These functions will return zero for the first full unit of time after a timer is set, then count +1 for each successive time unit thereafter.

Code Block
// Get the current time
TIMERVAL timer = StartTimer();
// For the next 1 second, ElapSTimer(..) will return 0
// For the 1 second after that, RemSTimer(..) will return 1
// For the 1 second after that, RemSTimer(..) will return 2
// etc.

These functions work with both wall clock timers and set interval timers.

If the timer is reset, then these functions will return zero.

...

Expand
titleRemUTimer, RemMTimer, & RemSTimer
Code Block
int64_t rem = RemUTimer(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer to test

int64_t rem = RemMTimer(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer to test

int64_t rem = RemSTimer(TIMERVAL timer)
   where:
      TIMERVAL timer = Timer to test

Calculates the amount of time remaining until a timer value, returned in units of full microseconds, milliseconds, or seconds. These functions will count down as the current time approaches the timer value, reaching zero at the instant that the current time equals the timer value. For the time unit after the timer expires (e.g. the first second), the return value will remain zero, and after that continue to count negative.

Code Block
// Set a timer for 3 seconds in the future
TIMERVAL timer = SetSTimer(3);
// For the next 1 second, RemSTimer(..) will return 3
// For the 1 second after that, RemSTimer(..) will return 2
// For the 1 second after that, RemSTimer(..) will return 1
// At this moment the timer expires
// For the next 1 second, RemSTimer(..) will return 0
// For the next 1 second, RemSTimer(..) will return -1
// For the next 1 second, RemSTimer(..) will return -2
// etc.

These functions work with both wall clock timers and set interval timers.

If the timer is reset, then these functions will return zero.

...

Expand
titleDiffUTimer, DiffMTimer, & DiffSTimer
Code Block
int64_t delta = DiffUTimer(TIMERVAL timer1, TIMERVAL timer2)
   where:
      TIMERVAL timer1  = First timer
      TIMERVAL timer2  = Second timer

int64_t delta = DiffMTimer(TIMERVAL timer1, TIMERVAL timer2)
   where:
      TIMERVAL timer1  = First timer
      TIMERVAL timer2  = Second timer

int64_t delta = DiffSTimer(TIMERVAL timer1, TIMERVAL timer2)
   where:
      TIMERVAL timer1  = First timer
      TIMERVAL timer2  = Second timer

Calculates the difference between two timers (timer1 - timer2), returned in units of full microseconds, milliseconds, or seconds.

These functions work with both wall clock timers and set interval timers. If the two timers are of different types, the functions convert the set interval timer to a wall clock timer before calculating the difference.

If either timer is reset, then these functions will return zero.

...