libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
timer.hpp
1#pragma once
2
3#include "libxr_def.hpp"
4#include "lockfree_list.hpp"
5#include "thread.hpp"
6
7namespace LibXR
8{
9
24class Timer
25{
26 public:
32 {
33 public:
38 void Run() { fun_(handle); }
39
40 void (*fun_)(void *);
41 void *handle;
42 uint32_t cycle_;
43 uint32_t count_;
44 bool enable_;
45 };
46
49
66 template <typename ArgType>
67 [[nodiscard]] static TimerHandle CreateTask(void (*fun)(ArgType), ArgType arg,
68 uint32_t cycle)
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 }
95
101 static void Start(TimerHandle handle);
102
108 static void Stop(TimerHandle handle);
109
116 static void SetCycle(TimerHandle handle, uint32_t cycle);
117
131 static void RefreshThreadFunction(void *);
132
138 static void Add(TimerHandle handle);
139
152 static void Refresh();
157 static void RefreshTimerInIdle();
158
159 static inline LibXR::LockFreeList *list_ = nullptr;
160
161 static inline Thread thread_handle_;
162
164 static inline uint32_t stack_depth_;
165};
166
167} // namespace LibXR
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:14
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:21
控制块类,存储任务信息 Control block class for storing task information
Definition timer.hpp:32
void Run()
运行定时任务 Runs the scheduled task
Definition timer.hpp:38
uint32_t cycle_
任务周期(单位:毫秒) Task cycle (unit: milliseconds)
Definition timer.hpp:42
void * handle
任务句柄 Handle to the task
Definition timer.hpp:41
uint32_t count_
计数器 Counter
Definition timer.hpp:43
bool enable_
任务是否启用 Flag indicating whether the task is enabled
Definition timer.hpp:44
void(* fun_)(void *)
任务执行函数 Function pointer to the task
Definition timer.hpp:40
定时器类,实现周期性任务调度 Timer class for scheduling periodic tasks
Definition timer.hpp:25
static Thread thread_handle_
定时器管理线程 Timer management thread
Definition timer.hpp:161
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:164
static void RefreshTimerInIdle()
在空闲时刷新定时器 Refreshes the timer during idle time
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:163
static void RefreshThreadFunction(void *)
定时器管理线程函数 Timer management thread function
Definition timer.cpp:16
LibXR::LockFreeList::Node< ControlBlock > * TimerHandle
定时器任务句柄 Timer task handle
Definition timer.hpp:48
static void Refresh()
刷新定时任务状态 Refreshes the state of periodic tasks
Definition timer.cpp:42
static TimerHandle CreateTask(void(*fun)(ArgType), ArgType arg, uint32_t cycle)
创建定时任务 Creates a periodic task
Definition timer.hpp:67
static void SetCycle(TimerHandle handle, uint32_t cycle)
设置定时任务的周期 Sets the cycle of a periodic task
Definition timer.cpp:10
static void Start(TimerHandle handle)
启动定时任务 Starts a periodic task
Definition timer.cpp:6
static void Add(TimerHandle handle)
添加定时任务 Adds a periodic task
Definition timer.cpp:26
static LibXR::LockFreeList * list_
定时任务列表 List of registered tasks
Definition timer.hpp:159
static void Stop(TimerHandle handle)
停止定时任务 Stops a periodic task
Definition timer.cpp:8
LibXR 命名空间
Definition ch32_gpio.hpp:9