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)
93 std::memset(name_, 0,
sizeof(name_));
96 std::strncpy(name_, name,
sizeof(name_) - 1);
107 static void *Port(
void *arg)
109 ThreadBlock *block =
static_cast<ThreadBlock *
>(arg);
111 if (block->name_[0] !=
'\0')
113 pthread_setname_np(pthread_self(), block->name_);
116 block->fun_(block->arg_);
118 return static_cast<void *
>(
nullptr);
121 decltype(function) fun_;
126 auto block =
new ThreadBlock(function, arg, name);
129 int ans = pthread_create(&this->
thread_handle_, &attr, ThreadBlock::Port, block);
133 XR_LOG_WARN(
"Failed to create thread: %s (%s), retrying with default attributes.",
134 name, strerror(ans));
137 ans = pthread_create(&this->
thread_handle_,
nullptr, ThreadBlock::Port, block);
141 XR_LOG_ERROR(
"Failed to create thread: %s (%s)", name, strerror(ans));
146 pthread_attr_destroy(&attr);
168 static void Sleep(uint32_t milliseconds);
192 static void ConfigureAttributes(pthread_attr_t& attr,
size_t stack_depth,
195 const size_t stack_size =
LibXR::max(
static_cast<size_t>(PTHREAD_STACK_MIN), stack_depth);
196 pthread_attr_setstacksize(&attr, stack_size);
197 ConfigureScheduling(attr, priority);
200 static void ConfigureScheduling(pthread_attr_t& attr,
Thread::Priority priority)
202 const int min_priority = sched_get_priority_min(SCHED_FIFO);
203 const int max_priority = sched_get_priority_max(SCHED_FIFO);
207 "SCHED_FIFO not supported or insufficient range. Using default policy.");
208 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
212 pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
213 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
215 struct sched_param sp;
217 sp.sched_priority = min_priority +
static_cast<int>(priority);
219 if (pthread_attr_setschedparam(&attr, &sp) == 0)
224 XR_LOG_WARN(
"Failed to set thread priority. Falling back to default policy.");
225 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
226 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
表示毫秒级时间戳的类。Class representing a timestamp in milliseconds.
线程管理类,封装 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
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(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最大值