libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
thread.hpp
1#pragma once
2
3#include "libxr_system.hpp"
4#include "libxr_time.hpp"
5
6#define LIBXR_PRIORITY_STEP ((configMAX_PRIORITIES - 1) / 5)
7
8namespace LibXR
9{
14class Thread
15{
16 public:
21 enum class Priority : uint8_t
22 {
23 IDLE = 0,
24 LOW = LIBXR_PRIORITY_STEP * 1,
25 MEDIUM = LIBXR_PRIORITY_STEP * 2,
26 HIGH = LIBXR_PRIORITY_STEP * 3,
27 REALTIME = LIBXR_PRIORITY_STEP * 4,
28 NUMBER = 5
29 };
30
35 Thread() {};
36
43
63 template <typename ArgType>
64 void Create(ArgType arg, void (*function)(ArgType arg), const char *name,
66 {
67 ASSERT(configMAX_PRIORITIES >= 6);
68
69 class ThreadBlock
70 {
71 public:
72 ThreadBlock(typeof(function) fun, ArgType arg) : fun_(fun), arg_(arg) {}
73
74 static void Port(void *arg)
75 {
76 ThreadBlock *block = static_cast<ThreadBlock *>(arg);
77 block->fun_(block->arg_);
78 }
79
80 typeof(function) fun_;
81 ArgType arg_;
82 };
83
84 auto block = new ThreadBlock(function, arg);
85
86 auto ans = xTaskCreate(block->Port, name, stack_depth, block,
87 static_cast<uint32_t>(priority), &(this->thread_handle_));
88 UNUSED(ans);
89 UNUSED(block);
90 ASSERT(ans == pdPASS);
91 }
92
98 static Thread Current(void);
99
105 static uint32_t GetTime();
106
112 static void Sleep(uint32_t milliseconds);
113
121
126 static void Yield();
127
133 operator libxr_thread_handle() { return thread_handle_; }
134
135 private:
137};
138} // namespace LibXR
线程管理类,封装 FreeRTOS 任务创建和调度 Thread management class encapsulating FreeRTOS task creation and scheduling
Definition thread.hpp:15
Thread(libxr_thread_handle handle)
通过 FreeRTOS 线程句柄创建线程对象 Constructor to create a thread object from a FreeRTOS thread handle
Definition thread.hpp:42
Thread()
默认构造函数,初始化空线程 Default constructor initializing an empty thread
Definition thread.hpp:35
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:16
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:22
@ NUMBER
优先级数量 Number of priority levels
@ LOW
低优先级 Low priority
@ REALTIME
实时优先级 Realtime priority
@ IDLE
空闲优先级 Idle priority
@ HIGH
高优先级 High priority
@ MEDIUM
中等优先级 Medium priority
libxr_thread_handle thread_handle_
FreeRTOS 线程句柄 FreeRTOS thread handle.
Definition thread.hpp:136
static Thread Current(void)
获取当前线程对象 Gets the current thread object
Definition thread.cpp:7
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 void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Definition thread.cpp:9
static void Yield()
让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
Definition thread.cpp:18
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
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
LibXR Color Control Library / LibXR终端颜色控制库
TaskHandle_t libxr_thread_handle
线程句柄类型定义 Thread handle type definition
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值