time_wheel内部实现结构体
More...
|
|
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_node * | m_worldnodehead = nullptr |
|
wheel_node * | m_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 |
time_wheel内部实现结构体
管理多层级时间轮、定时器线程、回调队列等核心状态
- Note
- 使用PIMPL模式隐藏实现细节
◆ addtimer()
| int64_t ngl::tools::time_wheel::impl_time_wheel::addtimer |
( |
const wheel_parm & | apram | ) |
|
|
inline |
添加定时器
创建定时器节点并调度到时间轮中
- Parameters
-
- Returns
- 定时器ID,失败返回-1
◆ finish_node()
| void ngl::tools::time_wheel::impl_time_wheel::finish_node |
( |
wheel_node * | anode | ) |
|
|
inline |
完成定时器节点处理
从定时器映射中移除并投递到回调队列
- Parameters
-
◆ 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
-
◆ 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
-
- Return values
-
| true | 至少有一个定时器被成功调度 |
| false | 调度失败 |
The documentation for this struct was generated from the following file: