6#include "libxr_mem.hpp"
7#include "libxr_system.hpp"
8#include "libxr_time.hpp"
67 template <
typename ArgType>
68 void Create(ArgType arg,
void (*function)(ArgType arg),
const char* name,
72 pthread_attr_init(&attr);
73 ConfigureAttributes(attr, stack_depth, priority);
89 ThreadBlock(
decltype(function) fun, ArgType arg,
const char* name)
90 : fun_(fun), arg_(arg)
92 std::memset(name_, 0,
sizeof(name_));
95 std::strncpy(name_, name,
sizeof(name_) - 1);
106 static void* Port(
void* arg)
108 ThreadBlock* block =
static_cast<ThreadBlock*
>(arg);
110 if (block->name_[0] !=
'\0')
112 pthread_setname_np(pthread_self(), block->name_);
115 block->fun_(block->arg_);
117 return static_cast<void*
>(
nullptr);
120 decltype(function) fun_;
125 auto block =
new ThreadBlock(function, arg, name);
128 int ans = pthread_create(&this->
thread_handle_, &attr, ThreadBlock::Port, block);
132 XR_LOG_WARN(
"Failed to create thread: %s (%s), retrying with default attributes.",
133 name, strerror(ans));
136 ans = pthread_create(&this->
thread_handle_,
nullptr, ThreadBlock::Port, block);
140 XR_LOG_ERROR(
"Failed to create thread: %s (%s)", name, strerror(ans));
145 pthread_attr_destroy(&attr);
167 static void Sleep(uint32_t milliseconds);
197 static void ConfigureAttributes(pthread_attr_t& attr,
size_t stack_depth,
200 const size_t stack_size =
201 LibXR::max(
static_cast<size_t>(PTHREAD_STACK_MIN), stack_depth);
202 pthread_attr_setstacksize(&attr, stack_size);
203 ConfigureScheduling(attr, priority);
206 static void ConfigureScheduling(pthread_attr_t& attr,
Thread::Priority priority)
208 const int min_priority = sched_get_priority_min(SCHED_FIFO);
209 const int max_priority = sched_get_priority_max(SCHED_FIFO);
213 "SCHED_FIFO not supported or insufficient range. Using default policy.");
214 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
218 pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
219 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
221 struct sched_param sp;
223 sp.sched_priority = min_priority +
static_cast<int>(priority);
225 if (pthread_attr_setschedparam(&attr, &sp) == 0)
230 XR_LOG_WARN(
"Failed to set thread priority. Falling back to default policy.");
231 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
232 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
毫秒时间戳 / Millisecond timestamp
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Thread(libxr_thread_handle handle)
通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle
Thread()
默认构造函数,初始化空线程 Default constructor initializing an empty thread
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Priority
线程优先级枚举 Enumeration for thread priorities
@ NUMBER
优先级数量 Number of priority levels
@ REALTIME
实时优先级 Realtime priority
@ IDLE
空闲优先级 Idle priority
@ MEDIUM
中等优先级 Medium priority
libxr_thread_handle thread_handle_
POSIX 线程句柄 POSIX thread handle.
static void SleepUntil(MillisecondTimestamp &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
void Create(ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
创建新线程 Creates a new thread
ErrorCode Join()
Waits for this POSIX thread to terminate and releases its resources.
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
static void Yield()
让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
constexpr auto max(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最大值