7#include "libxr_mem.hpp"
8#include "libxr_system.hpp"
9#include "libxr_time.hpp"
46 Thread(libxr_thread_handle handle) : thread_handle_(handle) {};
73 template <
typename ArgType>
74 void Create(ArgType arg,
void (*function)(ArgType arg),
const char* name,
78 pthread_attr_init(&attr);
79 ConfigureAttributes(attr, stack_depth, priority);
86 ThreadBlock(
decltype(function) fun, ArgType arg,
const char* name)
87 : fun_(fun), arg_(arg)
89 std::memset(name_, 0,
sizeof(name_));
92 std::strncpy(name_, name,
sizeof(name_) - 1);
98 static void* Port(
void* arg)
100 ThreadBlock* block =
static_cast<ThreadBlock*
>(arg);
102 if (block->name_[0] !=
'\0')
104 pthread_setname_np(pthread_self(), block->name_);
107 block->fun_(block->arg_);
109 return static_cast<void*
>(
nullptr);
112 decltype(function) fun_;
117 auto block =
new ThreadBlock(function, arg, name);
120 int ans = pthread_create(&this->thread_handle_, &attr, ThreadBlock::Port, block);
124 if (FirstDefaultAttributeRetry())
127 "Failed to create thread: %s (%s), retrying with default attributes. Later "
128 "threads that fail the same way retry without this warning.",
129 name, strerror(ans));
133 ans = pthread_create(&this->thread_handle_,
nullptr, ThreadBlock::Port, block);
137 XR_LOG_ERROR(
"Failed to create thread: %s (%s)", name, strerror(ans));
142 pthread_attr_destroy(&attr);
164 static void Sleep(uint32_t milliseconds);
191 operator libxr_thread_handle() {
return thread_handle_; }
200 static bool FirstDefaultAttributeRetry()
202 static std::atomic_flag retried = ATOMIC_FLAG_INIT;
203 return !retried.test_and_set(std::memory_order_relaxed);
206 static void ConfigureAttributes(pthread_attr_t& attr,
size_t stack_depth,
209 const size_t stack_size =
210 LibXR::max(
static_cast<size_t>(PTHREAD_STACK_MIN), stack_depth);
211 pthread_attr_setstacksize(&attr, stack_size);
212 ConfigureScheduling(attr, priority);
215 static void ConfigureScheduling(pthread_attr_t& attr,
Thread::Priority priority)
217 const int min_priority = sched_get_priority_min(SCHED_FIFO);
218 const int max_priority = sched_get_priority_max(SCHED_FIFO);
222 "SCHED_FIFO not supported or insufficient range. Using default policy.");
223 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
227 pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
228 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
230 struct sched_param sp;
232 sp.sched_priority = min_priority +
static_cast<int>(priority);
234 if (pthread_attr_setschedparam(&attr, &sp) == 0)
239 XR_LOG_WARN(
"Failed to set thread priority. Falling back to default policy.");
240 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
241 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
244 libxr_thread_handle thread_handle_;
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
static void SleepUntil(MillisecondTimestamp &last_wakeup_time, uint32_t time_to_sleep)
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
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
@ HIGH
高优先级 / High priority
@ MEDIUM
中等优先级 / Medium priority
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
LibXR 命名空间 / LibXR namespace.
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
constexpr auto max(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最大值 / Computes the maximum of two numbers