|
VLink
2.0.0
A high-performance communication middleware
|
Periodic or one-shot timer that fires on an associated MessageLoop thread.
More...
#include <timer.h>
Public Types | |
| using | Callback = MoveFunction< void()> |
| Callback signature invoked on every tick. More... | |
Public Member Functions | |
| Timer () | |
| Constructs a detached timer with a 1000 ms interval and no callback. More... | |
| Timer (class MessageLoop *message_loop) | |
Constructs a timer attached to message_loop with the default interval and no callback. More... | |
| Timer (class MessageLoop *message_loop, uint32_t interval_ms, int32_t loop_count=kInfinite, Callback &&callback=nullptr) | |
| Constructs a fully configured timer attached to a loop. More... | |
| Timer (uint32_t interval_ms, int32_t loop_count=kInfinite, Callback &&callback=nullptr) | |
| Constructs a detached timer with explicit interval and loop count. More... | |
| ~Timer () | |
Destructor. Calls stop() and detaches the timer. More... | |
| bool | is_active () const |
| Reports whether the timer is currently armed. More... | |
| bool | is_strict () const |
| Reports whether strict catch-up mode is enabled. More... | |
| uint32_t | get_interval () const |
| Returns the configured tick interval in milliseconds. More... | |
| int32_t | get_loop_count () const |
| Returns the configured total tick count. More... | |
| int32_t | get_remain_loop_count () const |
| Returns the number of ticks remaining before automatic stop. More... | |
| uint64_t | get_invoke_count () const |
Returns the number of ticks dispatched since the last start() or restart(). More... | |
| uint16_t | get_priority () const |
Returns the dispatch priority used with kPriorityType loops. More... | |
| class MessageLoop * | get_message_loop () const |
| Returns the loop the timer is currently attached to. More... | |
| bool | attach (class MessageLoop *message_loop) |
Attaches the timer to message_loop. More... | |
| bool | detach () |
| Detaches the timer from its loop and stops it first. More... | |
| void | start (Callback &&callback=nullptr) |
| Arms the timer and starts dispatching ticks. More... | |
| void | restart () |
| Resets the tick count and re-arms the timer as if newly started. More... | |
| void | stop () |
| Disarms the timer without destroying it. More... | |
| void | set_strict (bool strict) |
| Enables or disables strict catch-up firing. More... | |
| void | set_interval (uint32_t interval_ms) |
| Updates the tick interval. More... | |
| void | set_loop_count (int32_t loop_count) |
| Updates the total tick count. More... | |
| void | set_callback (Callback &&callback) |
| Replaces the callback dispatched on every tick. More... | |
| void | set_priority (uint16_t priority) |
| Updates the dispatch priority. More... | |
Static Public Member Functions | |
| static bool | call_once (class MessageLoop *message_loop, uint32_t interval_ms, Callback &&callback, uint16_t priority=0) |
| Posts a fire-and-forget one-shot timer to a loop. More... | |
Static Public Attributes | |
| static constexpr int | kInfinite {-1} |
Sentinel loop_count value meaning fire forever. More... | |
Friends | |
| class | MessageLoop |
Periodic or one-shot timer that fires on an associated MessageLoop thread.
Owns an internal implementation that integrates with the loop's tick scheduling so the callback runs in-line with every other task posted to the loop.
| using vlink::Timer::Callback = MoveFunction<void()> |
Callback signature invoked on every tick.
|
explicit |
Constructs a detached timer with a 1000 ms interval and no callback.
Use attach() and set_callback() before start().
|
explicit |
Constructs a timer attached to message_loop with the default interval and no callback.
| message_loop | Loop that will dispatch ticks. |
|
explicit |
Constructs a fully configured timer attached to a loop.
| message_loop | Loop that will dispatch ticks. |
| interval_ms | Tick interval in milliseconds; 0 clamps to kMinInterval. |
| loop_count | Number of ticks to fire; kInfinite repeats forever. Default: kInfinite. |
| callback | Callback to install. Default: empty. |
|
explicit |
Constructs a detached timer with explicit interval and loop count.
| interval_ms | Tick interval in milliseconds; 0 clamps to kMinInterval. |
| loop_count | Number of ticks to fire; kInfinite repeats forever. Default: kInfinite. |
| callback | Callback to install. Default: empty. |
| vlink::Timer::~Timer | ( | ) |
Destructor. Calls stop() and detaches the timer.
| bool vlink::Timer::attach | ( | class MessageLoop * | message_loop | ) |
Attaches the timer to message_loop.
Must be called before start() when no loop was provided at construction. A null pointer logs a fatal error and throws. Re-attaching detaches from any previous loop.
| message_loop | Loop to attach to. |
true on success.
|
static |
Posts a fire-and-forget one-shot timer to a loop.
The timer fires exactly once interval_ms milliseconds later and is then destroyed automatically. Convenient when lifetime management of a long-lived Timer object would be inconvenient.
| message_loop | Loop that will dispatch the tick. |
| interval_ms | Delay in milliseconds. |
| callback | Callback to invoke. |
| priority | Dispatch priority; 0 keeps the default. Default: 0. |
true on success. | bool vlink::Timer::detach | ( | ) |
Detaches the timer from its loop and stops it first.
true on success. | uint32_t vlink::Timer::get_interval | ( | ) | const |
Returns the configured tick interval in milliseconds.
| uint64_t vlink::Timer::get_invoke_count | ( | ) | const |
| int32_t vlink::Timer::get_loop_count | ( | ) | const |
Returns the configured total tick count.
kInfinite for indefinite repetition. | class MessageLoop* vlink::Timer::get_message_loop | ( | ) | const |
Returns the loop the timer is currently attached to.
nullptr when detached. | uint16_t vlink::Timer::get_priority | ( | ) | const |
Returns the dispatch priority used with kPriorityType loops.
| int32_t vlink::Timer::get_remain_loop_count | ( | ) | const |
Returns the number of ticks remaining before automatic stop.
kInfinite when repeating forever. | bool vlink::Timer::is_active | ( | ) | const |
Reports whether the timer is currently armed.
true when the timer is running. | bool vlink::Timer::is_strict | ( | ) | const |
Reports whether strict catch-up mode is enabled.
true when missed ticks are fired back-to-back. | void vlink::Timer::restart | ( | ) |
Resets the tick count and re-arms the timer as if newly started.
| void vlink::Timer::set_callback | ( | Callback && | callback | ) |
Replaces the callback dispatched on every tick.
| callback | New callback. |
| void vlink::Timer::set_interval | ( | uint32_t | interval_ms | ) |
Updates the tick interval.
Takes effect immediately for an active timer: the processed tick count is recomputed against the existing start time and the loop is woken. interval_ms == 0 clamps the internal interval to kMinInterval (10000 ns = 10 us).
| interval_ms | New interval in milliseconds. |
| void vlink::Timer::set_loop_count | ( | int32_t | loop_count | ) |
Updates the total tick count.
| loop_count | New loop count; kInfinite for indefinite repetition. |
| void vlink::Timer::set_priority | ( | uint16_t | priority | ) |
Updates the dispatch priority.
Only effective when the associated loop is of type kPriorityType.
| priority | Priority value; higher fires sooner. |
| void vlink::Timer::set_strict | ( | bool | strict | ) |
Enables or disables strict catch-up firing.
| strict | true to fire missed ticks back-to-back. Default state is false. |
| void vlink::Timer::start | ( | Callback && | callback = nullptr | ) |
Arms the timer and starts dispatching ticks.
When callback is non-null it replaces the existing callback. The first tick fires after one full interval, not immediately. Starting a timer with no callback is a no-op for dispatch purposes.
| callback | Optional callback replacement. Default: keep the existing callback. |
| void vlink::Timer::stop | ( | ) |
|
friend |
|
staticconstexpr |
Sentinel loop_count value meaning fire forever.