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#include "logger.hpp"
6
7namespace LibXR
8{
9
14class Thread
15{
16 public:
21 enum class Priority : uint8_t
22 {
23 IDLE,
24 LOW,
25 MEDIUM,
26 HIGH,
27 REALTIME,
28 NUMBER,
29 };
30
35 Thread() {};
36
43
64 template <typename ArgType>
65 void Create(ArgType arg, void (*function)(ArgType arg), const char *name,
67 {
70
71 // 线程栈大小设定,确保不小于 1
72 if (stack_depth > 256)
73 {
75 }
76 else
77 {
79 }
80
85 class ThreadBlock
86 {
87 public:
95 ThreadBlock(typeof(function) fun, ArgType arg, const char *name)
96 : fun_(fun),
97 arg_(arg),
98 name_(reinterpret_cast<char *>(malloc(strlen(name) + 1)))
99 {
100 strcpy(name_, name);
101 }
102
110 static void *Port(void *arg)
111 {
112 ThreadBlock *block = static_cast<ThreadBlock *>(arg);
113 volatile const char *thread_name = block->name_;
114 block->fun_(block->arg_);
115
116 UNUSED(thread_name);
117 return static_cast<void *>(nullptr);
118 }
119
120 typeof(function) fun_;
121 ArgType arg_;
122 char *name_;
123 };
124
125 auto block = new ThreadBlock(function, arg, name);
126
127 // 优先尝试设置 SCHED_FIFO 和线程优先级
130 bool scheduling_set = false;
131
132 if (max_priority - min_priority >= static_cast<int>(Priority::REALTIME))
133 {
136
137 struct sched_param sp;
138 memset(&sp, 0, sizeof(sp));
139 sp.sched_priority = min_priority + static_cast<int>(priority);
140
142 {
143 scheduling_set = true;
144 }
145 else
146 {
147 XR_LOG_WARN("Failed to set thread priority. Falling back to default policy.");
150 }
151 }
152 else
153 {
154 XR_LOG_WARN(
155 "SCHED_FIFO not supported or insufficient range. Using default policy.");
157 }
158
159 // 创建线程
160 if (auto ans = pthread_create(&this->thread_handle_, &attr, block->Port, block) != 0)
161 {
162 XR_LOG_WARN("Failed to create thread: %s (%s), Falling back to default policy.",
163 name, strerror(ans));
166 if (auto ans =
167 pthread_create(&this->thread_handle_, &attr, block->Port, block) != 0)
168 {
169 XR_LOG_ERROR("Failed to create thread: %s (%s)", ans);
170 }
171 }
172
174 }
175
181 static Thread Current(void);
182
189
196
204
209 static void Yield();
210
216 operator libxr_thread_handle() { return thread_handle_; }
217
218 private:
220};
221
222} // namespace LibXR
线程管理类,封装 FreeRTOS 任务创建和调度 Thread management class encapsulating FreeRTOS task creation and scheduling
Definition thread.hpp:15
Thread(libxr_thread_handle handle)
通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle
Definition thread.hpp:42
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
Thread()
默认构造函数,初始化空线程 Default constructor initializing an empty thread
Definition thread.hpp:35
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
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:65
static void Yield()
让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
static void SleepUntil(TimestampMS &last_waskup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
static Thread Current(void)
获取当前线程对象 Gets the current thread object
表示毫秒级时间戳的类。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
计算两个数的最小值