ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
ngl::tools::time_wheel::impl_time_wheel Struct Reference

time_wheel内部实现结构体 More...

Public Member Functions

 impl_time_wheel (time_wheel *atwheel, const time_wheel_config &aconfig, bool aisthreadcallback)
wheel_nodedetach_callback_nodes ()
void append_callback_nodes (wheel_node *ahead, bool anotify_worker)
void clear_callback_nodes ()
void remove (wheel_node *anode)
 释放定时器节点链表
void finish_node (wheel_node *anode)
 完成定时器节点处理
int64_t all_slot_ms () const
int64_t duration_ms () const
bool schedule_locked (wheel_node *apnode)
 调度定时器节点(需持锁)
void removetimer (int64_t atimerid)
void push (wheel_node *apnode)
void stop ()
void run ()
 时间轮主循环线程
void runcallback ()
 回调工作线程
std::shared_ptr< wheel_nodepop_node ()
 从回调队列中弹出一个到期定时器节点
int64_t addtimer (const wheel_parm &apram)
 添加定时器

Static Public Member Functions

static wheel_nodecallback_tail (wheel_node *ahead)

Public Attributes

time_wheel_config m_config
int64_t m_server_start_ms = 0
int64_t m_current_ms = 0
int64_t m_sched_start_ms = 0
int64_t m_sched_current_ms = 0
std::vector< std::shared_ptr< wheel > > m_wheel
std::unique_ptr< std::jthread > m_thread = nullptr
std::shared_mutex m_mutex
bool m_isthreadcallback = false
std::unique_ptr< std::jthread > m_threadcallback = nullptr
std::shared_mutex m_mutexcallback
ngl::tools::sem m_sem
wheel_nodem_worldnodehead = nullptr
wheel_nodem_worldnodetail = nullptr
int64_t m_timerid = 1
std::unordered_map< int64_t, std::shared_ptr< std::atomic_bool > > m_timer
time_wheel * m_twheel = nullptr
std::atomic_bool m_stop = false

Detailed Description

time_wheel内部实现结构体

管理多层级时间轮、定时器线程、回调队列等核心状态

Note
使用PIMPL模式隐藏实现细节

Member Function Documentation

◆ addtimer()

int64_t ngl::tools::time_wheel::impl_time_wheel::addtimer ( const wheel_parm & apram)
inline

添加定时器

创建定时器节点并调度到时间轮中

Parameters
apram定时器参数
Returns
定时器ID,失败返回-1

◆ finish_node()

void ngl::tools::time_wheel::impl_time_wheel::finish_node ( wheel_node * anode)
inline

完成定时器节点处理

从定时器映射中移除并投递到回调队列

Parameters
anode定时器节点

◆ pop_node()

std::shared_ptr< wheel_node > ngl::tools::time_wheel::impl_time_wheel::pop_node ( )
inline

从回调队列中弹出一个到期定时器节点

非阻塞方式,跳过已取消的节点

Returns
定时器节点的共享指针,队列为空时返回nullptr

◆ remove()

void ngl::tools::time_wheel::impl_time_wheel::remove ( wheel_node * anode)
inline

释放定时器节点链表

遍历链表释放每个节点并从定时器映射中移除

Parameters
anode链表头节点

◆ run()

void ngl::tools::time_wheel::impl_time_wheel::run ( )
inline

时间轮主循环线程

按精度间隔推进时间轮,处理到期定时器

Note
使用steady_clock确保时间推进不受系统时间调整影响

◆ runcallback()

void ngl::tools::time_wheel::impl_time_wheel::runcallback ( )
inline

回调工作线程

从回调队列中取出到期定时器并执行用户回调函数

Note
独立于时间轮线程,避免回调执行阻塞时间推进

◆ schedule_locked()

bool ngl::tools::time_wheel::impl_time_wheel::schedule_locked ( wheel_node * apnode)
inline

调度定时器节点(需持锁)

将节点插入合适的时间轮层级,处理已到期和超范围的定时器

Parameters
apnode待调度的定时器节点链表
Return values
true至少有一个定时器被成功调度
false调度失败

The documentation for this struct was generated from the following file: