AccurateWaiter allows threaded applications to have threads wait for a precise amount of time, without sending the CPU into a permanent spinlock.

Dependents:   AccurateWaiter-Test

Embed: (wiki syntax)

« Back to documentation index

AccurateWaiter Class Reference

AccurateWaiter Class Reference

--------------- AccurateWaiter --------------- ...your order, sir? More...

#include <AccurateWaiter.h>

Public Member Functions

TickerDataClock & clock ()
 Get Mbed's C++ Clock object representing the us ticker.
void wait_for (std::chrono::microseconds duration)
 Wait for an exact amount of time.
template<typename _Rep , typename _Period >
void wait_for (std::chrono::duration< _Rep, _Period > duration)
 Wait for an exact amount of time.
void wait_until (TickerDataClock::time_point timePoint)
 Wait until a specific us timestamp.

Detailed Description

--------------- AccurateWaiter --------------- ...your order, sir?

AccurateWaiter allows threaded applications to have threads wait for a precise amount of time, without sending the CPU into a permanent spinlock.

In standard Mbed OS, certain types of threaded applications are limited by the precision of ThisThread::sleep_for(), which only allows sleeping for a whole number of milliseconds. This limitation comes from the underlying RTX RTOS, which uses a 1 ms scheduling interrupt.

What a lot of people don't know is that there is actually a way around this limitation, using the RTOS's EventFlags objects. Unlike most other RTOS objects, EventFlags are usable from interrupts. By configuring the Mbed us ticker interrupt to set EventFlags, a waiting thread can be woken immediately after an exact number of ticks on the us ticker.

The result is something that combines the best features of wait_us() and ThisThread::sleep_for(). Other threads may run during the wait period (like ThisThread::sleep_for()), but the wait time can be specified to the microsecond (like wait_us()). The only downside is a bit more overhead when starting the wait operation (to trigger the timer interrupt).

I tested this code on my NUCLEO_F429ZI board, and found that it was able to handle any time duration from 1us-1s accurately, with exactly 14us of delay between the set time and the time the wait function returns. Overhead of calling the function also is around the 15-25us range.

Note 1: Since it uses the Mbed us ticker, this class allows waiting for >250,000 years before rollover.

Note 2: Each AccurateWaiter object may only be safely used by one thread at a time.

Definition at line 47 of file AccurateWaiter.h.


Member Function Documentation

TickerDataClock& clock (  )

Get Mbed's C++ Clock object representing the us ticker.

Use this object to get time_points for wait_until()

Returns:

Definition at line 64 of file AccurateWaiter.h.

void wait_for ( std::chrono::microseconds  duration )

Wait for an exact amount of time.

Other threads will be allowed to run during the wait period. When the timer expires, the waiting thread will be run immediately, preempting the current running thread, as long as a few things are true:

  • The waiting thread has higher thread priority than the currently running thread
  • Interrupts are not disabled

Definition at line 20 of file AccurateWaiter.cpp.

void wait_for ( std::chrono::duration< _Rep, _Period >  duration )

Wait for an exact amount of time.

Overload that casts to us.

Other threads will be allowed to run during the wait period. When the timer expires, the waiting thread will be run immediately, preempting the current running thread, as long as a few things are true:

  • The waiting thread has higher thread priority than the currently running thread
  • Interrupts are not disabled

Definition at line 91 of file AccurateWaiter.h.

void wait_until ( TickerDataClock::time_point  timePoint )

Wait until a specific us timestamp.

Other threads will be allowed to run during the wait period. When the timer expires, the waiting thread will be run immediately, preempting the current running thread, as long as a few things are true:

  • The waiting thread has higher thread priority than the currently running thread
  • Interrupts are not disabled

Definition at line 29 of file AccurateWaiter.cpp.