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

O(1) hashed-timing-wheel scheduler backed by an internal worker thread. 更多...

#include <wheel_timer.h>

vlink::WheelTimer 的协作图:

Public 类型

using Key = int64_t
 Opaque handle returned by add() and accepted by remove(). 更多...
 
using Callback = Function< void(Key)>
 Callback signature invoked when a timer expires. 更多...
 

Public 成员函数

 WheelTimer (uint32_t slots, uint32_t interval_ms)
 Constructs the wheel with the given resolution and capacity. 更多...
 
 ~WheelTimer ()
 Destructor. Calls stop() when the wheel is still running. 更多...
 
void start ()
 Starts the worker thread and begins advancing the wheel cursor. 更多...
 
void stop ()
 Stops the wheel and joins the worker thread. 更多...
 
void pause ()
 Temporarily suspends timer dispatch without joining the worker thread. 更多...
 
void resume ()
 Resumes a paused wheel; a no-op when not paused. 更多...
 
void wakeup ()
 Wakes the worker thread early when it is sleeping between ticks. 更多...
 
bool is_running () const
 Reports whether the wheel is currently running. 更多...
 
Key add (uint32_t timeout_ms, Callback &&callback, uint32_t repeat_ms=0)
 Inserts a new timer into the wheel. 更多...
 
bool remove (Key key)
 Removes a timer before its callback runs. 更多...
 
uint32_t get_remaining_time (Key key) const
 Returns the approximate remaining time before a timer fires. 更多...
 
void set_catchup_limit (uint32_t max_slots_to_catch_up)
 Caps the number of catch-up slots processed in a single tick iteration. 更多...
 

详细描述

O(1) hashed-timing-wheel scheduler backed by an internal worker thread.

Owns a fixed-size slot array and advances a cursor every interval_ms milliseconds.

成员类型定义说明

◆ Callback

Callback signature invoked when a timer expires.

The Key argument lets a single lambda manage multiple timers.

◆ Key

using vlink::WheelTimer::Key = int64_t

Opaque handle returned by add() and accepted by remove().

构造及析构函数说明

◆ WheelTimer()

vlink::WheelTimer::WheelTimer ( uint32_t  slots,
uint32_t  interval_ms 
)
explicit

Constructs the wheel with the given resolution and capacity.

Both slots and interval_ms must be greater than zero; invalid values log a fatal error and throw. Call start() to begin advancing the wheel.

参数
slotsNumber of buckets in the wheel. Larger values shorten the round counter for long timeouts.
interval_msTick duration in milliseconds; sets the resolution of every timer.

◆ ~WheelTimer()

vlink::WheelTimer::~WheelTimer ( )

Destructor. Calls stop() when the wheel is still running.

成员函数说明

◆ add()

Key vlink::WheelTimer::add ( uint32_t  timeout_ms,
Callback &&  callback,
uint32_t  repeat_ms = 0 
)

Inserts a new timer into the wheel.

The callback runs on the worker thread after timeout_ms milliseconds. When repeat_ms is non-zero the timer is re-armed at every expiry with repeat_ms as the next timeout.

参数
timeout_msInitial delay in milliseconds (rounded up to a slot boundary).
callbackFunction invoked on expiry.
repeat_msRe-arm interval in milliseconds; 0 selects one-shot. Default: 0.
返回
Unique key identifying this timer entry, or -1 on invalid input or key allocation failure.

◆ get_remaining_time()

uint32_t vlink::WheelTimer::get_remaining_time ( Key  key) const

Returns the approximate remaining time before a timer fires.

The value is rounded to the wheel's tick resolution.

参数
keyKey returned by add().
返回
Estimated remaining time in milliseconds, or 0 when key is unknown.

◆ is_running()

bool vlink::WheelTimer::is_running ( ) const

Reports whether the wheel is currently running.

返回
true between start() and stop().

◆ pause()

void vlink::WheelTimer::pause ( )

Temporarily suspends timer dispatch without joining the worker thread.

The worker remains alive but the cursor stops advancing. Use resume() to continue.

◆ remove()

bool vlink::WheelTimer::remove ( Key  key)

Removes a timer before its callback runs.

参数
keyKey returned by add().
返回
true when the timer existed and was removed.

◆ resume()

void vlink::WheelTimer::resume ( )

Resumes a paused wheel; a no-op when not paused.

◆ set_catchup_limit()

void vlink::WheelTimer::set_catchup_limit ( uint32_t  max_slots_to_catch_up)

Caps the number of catch-up slots processed in a single tick iteration.

Prevents a single tick from blocking the worker for an unbounded duration when the wheel falls behind (for example, after the system wakes from sleep).

参数
max_slots_to_catch_upMaximum slots processed per tick cycle.

◆ start()

void vlink::WheelTimer::start ( )

Starts the worker thread and begins advancing the wheel cursor.

◆ stop()

void vlink::WheelTimer::stop ( )

Stops the wheel and joins the worker thread.

Pending timers do not fire after stop() returns.

◆ wakeup()

void vlink::WheelTimer::wakeup ( )

Wakes the worker thread early when it is sleeping between ticks.

Useful after inserting a very short timeout that should fire immediately.


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