VLink  2.0.0
A high-performance communication middleware
vlink::Timer类 参考final

Periodic or one-shot timer that fires on an associated MessageLoop thread. 更多...

#include <timer.h>

vlink::Timer 的协作图:

Public 类型

using Callback = MoveFunction< void()>
 Callback signature invoked on every tick. 更多...
 

Public 成员函数

 Timer ()
 Constructs a detached timer with a 1000 ms interval and no callback. 更多...
 
 Timer (class MessageLoop *message_loop)
 Constructs a timer attached to message_loop with the default interval and no callback. 更多...
 
 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. 更多...
 
 Timer (uint32_t interval_ms, int32_t loop_count=kInfinite, Callback &&callback=nullptr)
 Constructs a detached timer with explicit interval and loop count. 更多...
 
 ~Timer ()
 Destructor. Calls stop() and detaches the timer. 更多...
 
bool is_active () const
 Reports whether the timer is currently armed. 更多...
 
bool is_strict () const
 Reports whether strict catch-up mode is enabled. 更多...
 
uint32_t get_interval () const
 Returns the configured tick interval in milliseconds. 更多...
 
int32_t get_loop_count () const
 Returns the configured total tick count. 更多...
 
int32_t get_remain_loop_count () const
 Returns the number of ticks remaining before automatic stop. 更多...
 
uint64_t get_invoke_count () const
 Returns the number of ticks dispatched since the last start() or restart(). 更多...
 
uint16_t get_priority () const
 Returns the dispatch priority used with kPriorityType loops. 更多...
 
class MessageLoopget_message_loop () const
 Returns the loop the timer is currently attached to. 更多...
 
bool attach (class MessageLoop *message_loop)
 Attaches the timer to message_loop. 更多...
 
bool detach ()
 Detaches the timer from its loop and stops it first. 更多...
 
void start (Callback &&callback=nullptr)
 Arms the timer and starts dispatching ticks. 更多...
 
void restart ()
 Resets the tick count and re-arms the timer as if newly started. 更多...
 
void stop ()
 Disarms the timer without destroying it. 更多...
 
void set_strict (bool strict)
 Enables or disables strict catch-up firing. 更多...
 
void set_interval (uint32_t interval_ms)
 Updates the tick interval. 更多...
 
void set_loop_count (int32_t loop_count)
 Updates the total tick count. 更多...
 
void set_callback (Callback &&callback)
 Replaces the callback dispatched on every tick. 更多...
 
void set_priority (uint16_t priority)
 Updates the dispatch priority. 更多...
 

静态 Public 成员函数

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. 更多...
 

静态 Public 属性

static constexpr int kInfinite {-1}
 Sentinel loop_count value meaning fire forever. 更多...
 

友元

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.

成员类型定义说明

◆ Callback

Callback signature invoked on every tick.

构造及析构函数说明

◆ Timer() [1/4]

vlink::Timer::Timer ( )
explicit

Constructs a detached timer with a 1000 ms interval and no callback.

Use attach() and set_callback() before start().

◆ Timer() [2/4]

vlink::Timer::Timer ( class MessageLoop message_loop)
explicit

Constructs a timer attached to message_loop with the default interval and no callback.

参数
message_loopLoop that will dispatch ticks.

◆ Timer() [3/4]

vlink::Timer::Timer ( class MessageLoop message_loop,
uint32_t  interval_ms,
int32_t  loop_count = kInfinite,
Callback &&  callback = nullptr 
)
explicit

Constructs a fully configured timer attached to a loop.

参数
message_loopLoop that will dispatch ticks.
interval_msTick interval in milliseconds; 0 clamps to kMinInterval.
loop_countNumber of ticks to fire; kInfinite repeats forever. Default: kInfinite.
callbackCallback to install. Default: empty.

◆ Timer() [4/4]

vlink::Timer::Timer ( uint32_t  interval_ms,
int32_t  loop_count = kInfinite,
Callback &&  callback = nullptr 
)
explicit

Constructs a detached timer with explicit interval and loop count.

参数
interval_msTick interval in milliseconds; 0 clamps to kMinInterval.
loop_countNumber of ticks to fire; kInfinite repeats forever. Default: kInfinite.
callbackCallback to install. Default: empty.

◆ ~Timer()

vlink::Timer::~Timer ( )

Destructor. Calls stop() and detaches the timer.

成员函数说明

◆ attach()

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_loopLoop to attach to.
返回
true on success.

◆ call_once()

static bool vlink::Timer::call_once ( class MessageLoop message_loop,
uint32_t  interval_ms,
Callback &&  callback,
uint16_t  priority = 0 
)
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_loopLoop that will dispatch the tick.
interval_msDelay in milliseconds.
callbackCallback to invoke.
priorityDispatch priority; 0 keeps the default. Default: 0.
返回
true on success.

◆ detach()

bool vlink::Timer::detach ( )

Detaches the timer from its loop and stops it first.

返回
true on success.

◆ get_interval()

uint32_t vlink::Timer::get_interval ( ) const

Returns the configured tick interval in milliseconds.

返回
Interval.

◆ get_invoke_count()

uint64_t vlink::Timer::get_invoke_count ( ) const

Returns the number of ticks dispatched since the last start() or restart().

返回
Dispatched tick count.

◆ get_loop_count()

int32_t vlink::Timer::get_loop_count ( ) const

Returns the configured total tick count.

返回
Loop count; kInfinite for indefinite repetition.

◆ get_message_loop()

class MessageLoop* vlink::Timer::get_message_loop ( ) const

Returns the loop the timer is currently attached to.

返回
Loop pointer; nullptr when detached.

◆ get_priority()

uint16_t vlink::Timer::get_priority ( ) const

Returns the dispatch priority used with kPriorityType loops.

返回
Priority value.

◆ get_remain_loop_count()

int32_t vlink::Timer::get_remain_loop_count ( ) const

Returns the number of ticks remaining before automatic stop.

返回
Remaining tick count; kInfinite when repeating forever.

◆ is_active()

bool vlink::Timer::is_active ( ) const

Reports whether the timer is currently armed.

返回
true when the timer is running.

◆ is_strict()

bool vlink::Timer::is_strict ( ) const

Reports whether strict catch-up mode is enabled.

返回
true when missed ticks are fired back-to-back.

◆ restart()

void vlink::Timer::restart ( )

Resets the tick count and re-arms the timer as if newly started.

◆ set_callback()

void vlink::Timer::set_callback ( Callback &&  callback)

Replaces the callback dispatched on every tick.

参数
callbackNew callback.

◆ set_interval()

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_msNew interval in milliseconds.

◆ set_loop_count()

void vlink::Timer::set_loop_count ( int32_t  loop_count)

Updates the total tick count.

参数
loop_countNew loop count; kInfinite for indefinite repetition.

◆ set_priority()

void vlink::Timer::set_priority ( uint16_t  priority)

Updates the dispatch priority.

Only effective when the associated loop is of type kPriorityType.

参数
priorityPriority value; higher fires sooner.

◆ set_strict()

void vlink::Timer::set_strict ( bool  strict)

Enables or disables strict catch-up firing.

参数
stricttrue to fire missed ticks back-to-back. Default state is false.

◆ start()

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.

参数
callbackOptional callback replacement. Default: keep the existing callback.

◆ stop()

void vlink::Timer::stop ( )

Disarms the timer without destroying it.

After stop() the timer can be re-armed by another start().

友元及相关函数文档

◆ MessageLoop

friend class MessageLoop
friend

类成员变量说明

◆ kInfinite

constexpr int vlink::Timer::kInfinite {-1}
staticconstexpr

Sentinel loop_count value meaning fire forever.


该类的文档由以下文件生成: