libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Timer Class Reference

定时器类,实现周期性任务调度 Timer class for scheduling periodic tasks More...

#include <timer.hpp>

Collaboration diagram for LibXR::Timer:

Data Structures

class  ControlBlock
 控制块类,存储任务信息 Control block class for storing task information More...
 

Public Types

typedef LibXR::List::Node< ControlBlock > * TimerHandle
 定时器任务句柄 Timer task handle
 

Static Public Member Functions

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 *)
 定时器管理线程函数 Timer management thread function
 
static void Remove (TimerHandle handle)
 删除定时任务 Removes a periodic task
 
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
 

Static Public Attributes

static LibXR::Listlist_ = nullptr
 定时任务列表 List of registered tasks
 
static Thread thread_handle_
 定时器管理线程 Timer management thread
 
static LibXR::Thread::Priority priority_
 线程优先级 Thread priority
 
static uint32_t stack_depth_
 线程栈深度 Thread stack depth
 

Detailed Description

定时器类,实现周期性任务调度 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 26 of file timer.hpp.

Member Typedef Documentation

◆ TimerHandle

定时器任务句柄 Timer task handle

Definition at line 50 of file timer.hpp.

Member Function Documentation

◆ Add()

static void LibXR::Timer::Add ( TimerHandle  handle)
inlinestatic

添加定时任务 Adds a periodic task

Parameters
handle任务句柄 Timer handle to add

Definition at line 169 of file timer.hpp.

170 {
171 ASSERT(!handle->next_);
172
174 {
176#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
177#else
178 thread_handle_.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task",
180#endif
181 }
182 list_->Add(*handle);
183 }
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
Definition list.hpp:23
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
Definition list.hpp:143
void Create(ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
创建新线程 Creates a new thread
Definition thread.hpp:64
static Thread thread_handle_
定时器管理线程 Timer management thread
Definition timer.hpp:240
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:243
static LibXR::List * list_
定时任务列表 List of registered tasks
Definition timer.hpp:238
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:242
static void RefreshThreadFunction(void *)
定时器管理线程函数 Timer management thread function
Definition timer.hpp:135

◆ CreateTask()

template<typename ArgType >
static TimerHandle LibXR::Timer::CreateTask ( void(*)(ArgType fun,
ArgType  arg,
uint32_t  cycle 
)
inlinestatic

创建定时任务 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 69 of file timer.hpp.

70 {
71 ASSERT(cycle > 0);
72
73 typedef struct
74 {
76 ArgType arg;
77 void (*fun)(ArgType);
78 } Data;
79
80 Data *data = new Data;
81 data->fun = fun;
82 data->arg = arg;
83
84 data->ctrl_block.data_.handle = data;
85 data->ctrl_block.data_.fun_ = [](void *arg)
86 {
87 Data *data = reinterpret_cast<Data *>(arg);
88 data->fun(data->arg);
89 };
90 data->ctrl_block.data_.count_ = 0;
91 data->ctrl_block.data_.cycle_ = cycle;
92 data->ctrl_block.data_.enable_ = false;
93
94 return &data->ctrl_block;
95 }
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Definition list.hpp:60
Data data_
存储的数据。 The stored data.
Definition list.hpp:111
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ Refresh()

static void LibXR::Timer::Refresh ( )
inlinestatic

刷新定时任务状态 Refreshes the state of periodic tasks

该方法遍历任务列表,并检查任务是否应当运行。 若任务启用,并且 count_ 达到 cycle_,则执行任务并重置计数器。

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.

Definition at line 197 of file timer.hpp.

198 {
200 {
202
203#ifndef LIBXR_NOT_SUPPORT_MUTI_THREAD
204
205 auto thread_handle = Thread();
206 thread_handle.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task",
208#endif
209 }
210
211 auto fun = [](ControlBlock &block)
212 {
213 if (!block.enable_)
214 {
215 return ErrorCode::OK;
216 }
217
218 block.count_++;
219
220 if (block.count_ >= block.cycle_)
221 {
222 block.count_ = 0;
223 block.Run();
224 }
225
226 return ErrorCode::OK;
227 };
228
229 list_->Foreach<ControlBlock>(fun);
230 }
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
Definition list.hpp:214
@ HIGH
高优先级 High priority

◆ RefreshThreadFunction()

static void LibXR::Timer::RefreshThreadFunction ( void )
inlinestatic

定时器管理线程函数 Timer management thread function

Parameters
unused未使用参数 Unused parameter

该线程持续运行,定期刷新任务列表,并确保任务按时执行。 Thread::SleepUntil 方式用于精确调度。

This thread runs continuously, periodically refreshing the task list and ensuring timely task execution. Thread::SleepUntil is used for precise scheduling.

Definition at line 135 of file timer.hpp.

136 {
137 TimestampMS time = Thread::GetTime();
138 while (true)
139 {
142 }
143 }
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:16
static void SleepUntil(TimestampMS &last_waskup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
Definition thread.cpp:11
static void Refresh()
刷新定时任务状态 Refreshes the state of periodic tasks
Definition timer.hpp:197

◆ RefreshTimerInIdle()

void LibXR::Timer::RefreshTimerInIdle ( )
static

在空闲时刷新定时器 Refreshes the timer during idle time

Definition at line 16 of file libxr_system.cpp.

16 {
17 static bool in_timer = false;
18 if (in_timer) {
19 return;
20 }
21
23
25 return;
26 }
27
28 in_timer = true;
31 in_timer = false;
32}
static TimestampMS GetMilliseconds()
获取当前时间的毫秒级时间戳。 Gets the current timestamp in milliseconds.
Definition timebase.hpp:63

◆ Remove()

static void LibXR::Timer::Remove ( TimerHandle  handle)
inlinestatic

删除定时任务 Removes a periodic task

Parameters
handle任务句柄 Timer handle to remove

该方法删除已注册的定时任务,并确保任务列表完整性。 若 handle->next_ 为空,则会触发 ASSERT 断言。

This method removes a registered periodic task while ensuring the integrity of the task list. If handle->next_ is null, it triggers an ASSERT assertion.

Definition at line 158 of file timer.hpp.

159 {
160 ASSERT(handle->next_);
161 list_->Delete(*handle);
162 }
ErrorCode Delete(BaseNode &data) noexcept
从链表中删除指定的节点。 Deletes a specified node from the linked list.
Definition list.hpp:181

◆ SetCycle()

static void LibXR::Timer::SetCycle ( TimerHandle  handle,
uint32_t  cycle 
)
inlinestatic

设置定时任务的周期 Sets the cycle of a periodic task

Parameters
handle任务句柄 Timer handle to modify
cycle任务周期(毫秒) New cycle time (milliseconds)

Definition at line 117 of file timer.hpp.

118 {
119 handle->data_.cycle_ = cycle;
120 }

◆ Start()

static void LibXR::Timer::Start ( TimerHandle  handle)
inlinestatic

启动定时任务 Starts a periodic task

Parameters
handle任务句柄 Timer handle to start

Definition at line 102 of file timer.hpp.

102{ handle->data_.enable_ = true; }

◆ Stop()

static void LibXR::Timer::Stop ( TimerHandle  handle)
inlinestatic

停止定时任务 Stops a periodic task

Parameters
handle任务句柄 Timer handle to stop

Definition at line 109 of file timer.hpp.

109{ handle->data_.enable_ = false; }

Field Documentation

◆ list_

LibXR::List* LibXR::Timer::list_ = nullptr
inlinestatic

定时任务列表 List of registered tasks

Definition at line 238 of file timer.hpp.

◆ priority_

LibXR::Thread::Priority LibXR::Timer::priority_
inlinestatic

线程优先级 Thread priority

Definition at line 242 of file timer.hpp.

◆ stack_depth_

uint32_t LibXR::Timer::stack_depth_
inlinestatic

线程栈深度 Thread stack depth

Definition at line 243 of file timer.hpp.

◆ thread_handle_

Thread LibXR::Timer::thread_handle_
inlinestatic

定时器管理线程 Timer management thread

Definition at line 240 of file timer.hpp.


The documentation for this class was generated from the following files: