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{
13class Thread
14{
15 public:
20 enum class Priority : uint8_t
21 {
22 IDLE,
23 LOW,
24 MEDIUM,
25 HIGH,
26 REALTIME,
27 NUMBER,
28 };
29
34 Thread() {};
35
41 Thread(libxr_thread_handle handle) : thread_handle_(handle) {};
42
63 template <typename ArgType>
64 void Create(ArgType arg, void (*function)(ArgType arg), const char *name,
65 size_t stack_depth, Thread::Priority priority)
66 {
67 pthread_attr_t attr;
68 pthread_attr_init(&attr);
69
70 // 线程栈大小设定,至少满足 PTHREAD_STACK_MIN
71 size_t stack_size = LibXR::max(static_cast<size_t>(PTHREAD_STACK_MIN), stack_depth);
72 pthread_attr_setstacksize(&attr, stack_size);
73
78 class ThreadBlock
79 {
80 public:
88 ThreadBlock(decltype(function) fun, ArgType arg, const char *name)
89 : fun_(fun),
90 arg_(arg),
91 name_(reinterpret_cast<char *>(malloc(strlen(name) + 1)))
92 {
93 strcpy(name_, name);
94 }
95
96 ~ThreadBlock() { free(name_); }
97
105 static void *Port(void *arg)
106 {
107 ThreadBlock *block = static_cast<ThreadBlock *>(arg);
108 volatile const char *thread_name = block->name_;
109 block->fun_(block->arg_);
110
111 UNUSED(thread_name);
112 delete block;
113 return static_cast<void *>(nullptr);
114 }
115
116 decltype(function) fun_;
117 ArgType arg_;
118 char *name_;
119 };
120
121 auto block = new ThreadBlock(function, arg, name);
122
123 // 优先尝试设置 SCHED_FIFO 和线程优先级
124 int min_priority = sched_get_priority_min(SCHED_FIFO);
125 int max_priority = sched_get_priority_max(SCHED_FIFO);
126 bool scheduling_set = false;
127
128 UNUSED(scheduling_set);
129
130 if (max_priority - min_priority >= static_cast<int>(Priority::REALTIME))
131 {
132 pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
133 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
134
135 struct sched_param sp;
136 memset(&sp, 0, sizeof(sp));
137 sp.sched_priority = min_priority + static_cast<int>(priority);
138
139 if (pthread_attr_setschedparam(&attr, &sp) == 0)
140 {
141 scheduling_set = true;
142 }
143 else
144 {
145 XR_LOG_WARN("Failed to set thread priority. Falling back to default policy.");
146 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
147 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
148 }
149 }
150 else
151 {
152 XR_LOG_WARN(
153 "SCHED_FIFO not supported or insufficient range. Using default policy.");
154 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
155 }
156
157 // 创建线程
158 int ans = pthread_create(&this->thread_handle_, &attr, block->Port, block);
159 if (ans != 0)
160 {
161 XR_LOG_WARN("Failed to create thread: %s (%s), Falling back to default policy.",
162 name, strerror(ans));
163 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
164 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
165 ans = pthread_create(&this->thread_handle_, &attr, block->Port, block);
166 if (ans != 0)
167 {
168 XR_LOG_ERROR("Failed to create thread: %s (%s)", name, strerror(ans));
169 delete block;
170 }
171 }
172
173 pthread_attr_destroy(&attr);
174 }
175
181 static Thread Current(void);
182
188 static uint32_t GetTime();
189
195 static void Sleep(uint32_t milliseconds);
196
203 static void SleepUntil(TimestampMS &last_waskup_time, uint32_t time_to_sleep);
204
209 static void Yield();
210
216 operator libxr_thread_handle() { return thread_handle_; }
217
218 private:
219 libxr_thread_handle thread_handle_;
220};
221
222} // namespace LibXR
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:14
Thread(libxr_thread_handle handle)
通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle
Definition thread.hpp:41
Thread()
默认构造函数,初始化空线程 Default constructor initializing an empty thread
Definition thread.hpp:34
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition thread.cpp:39
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:21
@ 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_
POSIX 线程句柄 POSIX thread handle.
Definition thread.hpp:219
static Thread Current(void)
获取当前线程对象 Gets the current thread object
Definition thread.cpp:14
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:16
static void Yield()
让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
Definition thread.cpp:47
static void SleepUntil(TimestampMS &last_waskup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
Definition thread.cpp:23
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
LibXR 命名空间
Definition ch32_gpio.hpp:9
constexpr auto max(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最大值