|
| template<typename ArgType > |
| static TimerHandle | CreateTask (void(*fun)(ArgType), ArgType arg, uint32_t cycle) |
| | 创建定时任务 Creates a periodic task
|
| |
| static void | Start (TimerHandle handle) |
| | 启动定时任务 Starts a periodic task
|
| |
| static void | Stop (TimerHandle handle) |
| | 停止定时任务 Stops a periodic task
|
| |
| static void | SetCycle (TimerHandle handle, uint32_t cycle) |
| | 设置定时任务的周期 Sets the cycle of a periodic task
|
| |
| static void | RefreshThreadFunction (void *unused) |
| | 定时器管理线程函数 Timer management thread function
|
| |
| static void | Add (TimerHandle handle) |
| | 添加定时任务 Adds a periodic task
|
| |
| static void | Refresh () |
| | 刷新定时任务状态 Refreshes the state of periodic tasks
|
| |
|
static void | RefreshTimerInIdle () |
| | 在空闲时刷新定时器 Refreshes the timer during idle time
|
| |
定时器类,实现周期性任务调度 Timer class for scheduling periodic tasks
该定时器支持多任务调度,可用于执行周期性任务,如定时回调函数调用。 Timer 提供任务创建、启动、停止、删除及周期调整等功能,并使用 Thread::SleepUntil 实现精确调度。
This timer supports multi-task scheduling and can be used to execute periodic tasks, such as invoking callback functions at regular intervals. It provides task creation, start, stop, delete, and cycle adjustment functionalities, utilizing Thread::SleepUntil for precise scheduling.
Definition at line 24 of file timer.hpp.
template<typename ArgType >
| static TimerHandle LibXR::Timer::CreateTask |
( |
void(* | fun )(ArgType), |
|
|
ArgType | arg, |
|
|
uint32_t | cycle ) |
|
inlinestaticnodiscard |
创建定时任务 Creates a periodic task
- Template Parameters
-
| ArgType | 任务参数类型 / Type of task argument |
- Parameters
-
| fun | 定时执行的任务函数 / Function to execute periodically |
| arg | 任务参数 / Argument for the function |
| cycle | 任务周期(毫秒) / Task execution cycle (milliseconds) |
- Returns
- 任务句柄 / TimerHandle pointing to the created task
该方法创建一个新的周期性任务,任务将在 cycle 毫秒的周期内运行。 若 cycle 小于等于 0,则会触发 ASSERT 断言。
This method creates a new periodic task that runs in a cycle of cycle milliseconds. If cycle is less than or equal to 0, it triggers an ASSERT assertion.
Definition at line 67 of file timer.hpp.
69 {
70 ASSERT(cycle > 0);
71
72 typedef struct
73 {
75 ArgType arg;
76 void (*fun)(ArgType);
77 } Data;
78
79 Data* data = new Data;
80 data->fun = fun;
81 data->arg = arg;
82
83 data->ctrl_block.data_.handle = data;
84 data->ctrl_block.data_.fun_ = [](void* arg)
85 {
86 Data* data = reinterpret_cast<Data*>(arg);
87 data->fun(data->arg);
88 };
89 data->ctrl_block.data_.count_ = 0;
90 data->ctrl_block.data_.cycle_ = cycle;
91 data->ctrl_block.data_.enable_ = false;
92
93 return &data->ctrl_block;
94 }
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
刷新定时任务状态 Refreshes the state of periodic tasks
该方法遍历任务列表,并检查任务是否应当运行。 若任务启用,并且 count_ 达到 cycle_,则执行任务并重置计数器。 第一次 Add() 之前任务列表尚未创建,此时直接返回。
This method iterates through the task list and checks whether a task should run. If a task is enabled and its count_ reaches cycle_, the task is executed and the counter is reset. Before the first Add() the task list does not exist yet, and the method returns at once.
Definition at line 43 of file timer.cpp.
44{
45
46
47
49 {
50 return;
51 }
52
53 auto fun = [](ControlBlock& block)
54 {
55 if (!block.enable_)
56 {
58 }
59
60 block.count_++;
61
62 if (block.count_ >= block.cycle_)
63 {
64 block.count_ = 0;
65 block.Run();
66 }
67
69 };
70
72}
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
@ OK
操作成功 / Operation successful