libxr  1.0
Want to be the best embedded framework
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{
46 {
48
49#ifndef LIBXR_NOT_SUPPORT_MUTI_THREAD
50
51 auto thread_handle = Thread();
52 thread_handle.Create<void*>(nullptr, RefreshThreadFunction, "libxr_timer_task", 512,
54#endif
55 }
56
57 auto fun = [](ControlBlock& block)
58 {
59 if (!block.enable_)
60 {
61 return ErrorCode::OK;
62 }
63
64 block.count_++;
65
66 if (block.count_ >= block.cycle_)
67 {
68 block.count_ = 0;
69 block.Run();
70 }
71
72 return ErrorCode::OK;
73 };
74
76}
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.
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:16
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:41
@ HIGH
高优先级 High priority
static void SleepUntil(MillisecondTimestamp &last_waskup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
Definition thread.cpp:24
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:66
控制块类,存储任务信息 Control block class for storing task information
Definition timer.hpp:32
static Thread thread_handle_
定时器管理线程 Timer management thread
Definition timer.hpp:165
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:169
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:167
static void RefreshThreadFunction(void *)
定时器管理线程函数 Timer management thread function
Definition timer.cpp:17
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:162
static void Stop(TimerHandle handle)
停止定时任务 Stops a periodic task
Definition timer.cpp:9
LibXR 命名空间
Definition ch32_can.hpp:14