class Timer
Defined at line 37 of file ../../src/media/audio/lib/clock/timer.h
A Timer is built around one core operation, `SleepUntil`, which puts a thread to sleep until a
deadline is reached. Threads can be interrupted by two signals: a "shutdown" bit which signals
that the thread should exit, and an "event" bit which signals that new work has arrived.
Typically this is used in a loop:
```
for (;;) {
auto wake_reason = timer_->SleepUntil(DeadlineForNextScheduledJob());
if (wake_reason.shutdown_set) {
timer->Stop();
return;
}
if (wake_reason.event_set) {
// check for new work
}
if (wake_reason.deadline_expired) {
// do next scheduled job
}
}
```
This is an abstract class so we can provide implementations that use real and synthetic clocks.
Public Methods
void SetEventBit ()
Interrupts the timer by setting a generic "event" bit. If a thread is blocked in SleepUntil,
that thread is woken immediately. Otherwise the next SleepUntil call will return immediately.
Implementations must be safe to call from any thread.
void SetShutdownBit ()
Interrupts the timer by setting a "shutdown" bit. If a thread is blocked in SleepUntil, that
thread is woken immediately. Otherwise the next SleepUntil call will return immediately.
Implementations must be safe to call from any thread.
WakeReason SleepUntil (zx::time deadline)
Sleeps until the given `deadline`, relative to the system monotonic clock, or until interrupted
by SetShutdownBit or SetEventBit. Returns the reason for waking.
Just before returning, SleepUntil atomically clears the event bit. This gives the SetEventBit
method "at least once" semantics: after SetEventBit is called, at least one future SleepUntil
call must return with `event_set = true`. If SetEventBit happens concurrently with SleepUntil,
it is unspecified whether the concurrent SleepUntil will recognize the event.
The shutdown bit is not cleared: once set, it persists indefinitely.
Implementations must be safe to call from any thread, however it must be called by at most one
thread at a time.
void Stop ()
Declares that there will not be any further calls to SleepUntil. Implementations must be safe
to call from any thread, however in practice this is normally called by the thread which loops
on SleepUntil, as illustrated in the class comments.
void Timer ()
Defined at line 39 of file ../../src/media/audio/lib/clock/timer.h
void ~Timer ()
Defined at line 40 of file ../../src/media/audio/lib/clock/timer.h
void Timer (const Timer & )
Defined at line 41 of file ../../src/media/audio/lib/clock/timer.h
Timer & operator= (const Timer & )
Defined at line 42 of file ../../src/media/audio/lib/clock/timer.h
void Timer (Timer && )
Defined at line 43 of file ../../src/media/audio/lib/clock/timer.h
Timer & operator= (Timer && )
Defined at line 44 of file ../../src/media/audio/lib/clock/timer.h