libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
timer.cpp
1#include "timer.hpp"
2
3#include "lockfree_list.hpp"
4
5using namespace LibXR;
6
7void Timer::Start(TimerHandle handle) { handle->data_.enable_ = true; }
8
9void Timer::Stop(TimerHandle handle) { handle->data_.enable_ = false; }
10
11void Timer::SetCycle(TimerHandle handle, uint32_t cycle)
12{
13 ASSERT(cycle > 0);
14 handle->data_.cycle_ = cycle;
15}
16
18{
20 while (true)
21 {
23 Thread::SleepUntil(time, 1);
24 }
25}
26
28{
29 ASSERT(!handle->next_);
30
32 {
34#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
35#else
36 thread_handle_.Create<void*>(nullptr, RefreshThreadFunction, "libxr_timer_task",
38#endif
39 }
40 list_->Add(*handle);
41}
42
44{
45 // 第一次 Add() 才创建任务列表和管理线程;在此之前没有任务可刷新。
46 // The first Add() creates the task list and the management thread; before that there
47 // is no task to refresh.
49 {
50 return;
51 }
52
53 auto fun = [](ControlBlock& block)
54 {
55 if (!block.enable_)
56 {
57 return ErrorCode::OK;
58 }
59
60 block.count_++;
61
62 if (block.count_ >= block.cycle_)
63 {
64 block.count_ = 0;
65 block.Run();
66 }
67
68 return ErrorCode::OK;
69 };
70
72}
std::atomic< BaseNode * > next_
指向下一个节点的原子指针 / Atomic pointer to the next node.
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Data data_
存储的数据 / The stored data.
链表实现,用于存储和管理数据节点。 A linked list implementation for storing and managing data nodes.
ErrorCode Foreach(Func func)
遍历链表中的每个节点,并应用回调函数。 Iterates over each node in the list and applies a callback function.
void Add(BaseNode &data)
向链表添加一个节点。 Adds a node to the linked list.
毫秒时间戳 / Millisecond timestamp
static void SleepUntil(MillisecondTimestamp &last_wakeup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
Definition thread.cpp:23
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:35
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:74
控制块类,存储任务信息 Control block class for storing task information
Definition timer.hpp:32
static Thread thread_handle_
定时器管理线程 / Timer management thread
Definition timer.hpp:167
static uint32_t stack_depth_
线程栈深度 / Thread stack depth
Definition timer.hpp:171
static LibXR::Thread::Priority priority_
线程优先级 / Thread priority
Definition timer.hpp:169
static void Refresh()
刷新定时任务状态 Refreshes the state of periodic tasks
Definition timer.cpp:43
static void SetCycle(TimerHandle handle, uint32_t cycle)
设置定时任务的周期 Sets the cycle of a periodic task
Definition timer.cpp:11
static void Start(TimerHandle handle)
启动定时任务 Starts a periodic task
Definition timer.cpp:7
static void Add(TimerHandle handle)
添加定时任务 Adds a periodic task
Definition timer.cpp:27
static LibXR::LockFreeList * list_
定时任务列表 / List of registered tasks
Definition timer.hpp:164
static void RefreshThreadFunction(void *unused)
定时器管理线程函数 Timer management thread function
Definition timer.cpp:17
static void Stop(TimerHandle handle)
停止定时任务 Stops a periodic task
Definition timer.cpp:9
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
@ OK
操作成功 / Operation successful