libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
timer.cpp
1#include "timer.hpp"
2#include "lockfree_list.hpp"
3
4using namespace LibXR;
5
6void Timer::Start(TimerHandle handle) { handle->data_.enable_ = true; }
7
8void Timer::Stop(TimerHandle handle) { handle->data_.enable_ = false; }
9
10void Timer::SetCycle(TimerHandle handle, uint32_t cycle)
11{
12 ASSERT(cycle > 0);
13 handle->data_.cycle_ = cycle;
14}
15
17{
19 while (true)
20 {
22 Thread::SleepUntil(time, 1);
23 }
24}
25
27{
28 ASSERT(!handle->next_);
29
31 {
33#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
34#else
35 thread_handle_.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task",
37#endif
38 }
39 list_->Add(*handle);
40}
41
43{
45 {
47
48#ifndef LIBXR_NOT_SUPPORT_MUTI_THREAD
49
50 auto thread_handle = Thread();
51 thread_handle.Create<void *>(nullptr, RefreshThreadFunction, "libxr_timer_task", 512,
53#endif
54 }
55
56 auto fun = [](ControlBlock &block)
57 {
58 if (!block.enable_)
59 {
60 return ErrorCode::OK;
61 }
62
63 block.count_++;
64
65 if (block.count_ >= block.cycle_)
66 {
67 block.count_ = 0;
68 block.Run();
69 }
70
71 return ErrorCode::OK;
72 };
73
75}
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:14
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:64
控制块类,存储任务信息 Control block class for storing task information
Definition timer.hpp:32
static Thread thread_handle_
定时器管理线程 Timer management thread
Definition timer.hpp:161
static uint32_t stack_depth_
线程栈深度 Thread stack depth
Definition timer.hpp:164
static LibXR::Thread::Priority priority_
线程优先级 Thread priority
Definition timer.hpp:163
static void RefreshThreadFunction(void *)
定时器管理线程函数 Timer management thread function
Definition timer.cpp:16
static void Refresh()
刷新定时任务状态 Refreshes the state of periodic tasks
Definition timer.cpp:42
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