libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Thread Class Reference

线程管理类,封装 FreeRTOS 任务创建和调度 Thread management class encapsulating FreeRTOS task creation and scheduling More...

#include <thread.hpp>

Public Types

enum class  Priority : uint8_t {
  IDLE = 0 , LOW = LIBXR_PRIORITY_STEP * 1 , MEDIUM = LIBXR_PRIORITY_STEP * 2 , HIGH = LIBXR_PRIORITY_STEP * 3 ,
  REALTIME = LIBXR_PRIORITY_STEP * 4 , NUMBER = 5 , IDLE , LOW ,
  MEDIUM , HIGH , REALTIME , NUMBER ,
  IDLE = 0 , LOW = 0 , MEDIUM = 0 , HIGH = 0 ,
  REALTIME = 0 , NUMBER = 1 , IDLE = 0 , LOW = 0 ,
  MEDIUM = 0 , HIGH = 0 , REALTIME = 0 , NUMBER = 1 ,
  IDLE , LOW , MEDIUM , HIGH ,
  REALTIME , NUMBER
}
 线程优先级枚举 Enumeration for thread priorities More...
 
enum class  Priority : uint8_t {
  IDLE = 0 , LOW = LIBXR_PRIORITY_STEP * 1 , MEDIUM = LIBXR_PRIORITY_STEP * 2 , HIGH = LIBXR_PRIORITY_STEP * 3 ,
  REALTIME = LIBXR_PRIORITY_STEP * 4 , NUMBER = 5 , IDLE , LOW ,
  MEDIUM , HIGH , REALTIME , NUMBER ,
  IDLE = 0 , LOW = 0 , MEDIUM = 0 , HIGH = 0 ,
  REALTIME = 0 , NUMBER = 1 , IDLE = 0 , LOW = 0 ,
  MEDIUM = 0 , HIGH = 0 , REALTIME = 0 , NUMBER = 1 ,
  IDLE , LOW , MEDIUM , HIGH ,
  REALTIME , NUMBER
}
 线程优先级枚举 Enumeration for thread priorities More...
 
enum class  Priority {
  IDLE = 0 , LOW = LIBXR_PRIORITY_STEP * 1 , MEDIUM = LIBXR_PRIORITY_STEP * 2 , HIGH = LIBXR_PRIORITY_STEP * 3 ,
  REALTIME = LIBXR_PRIORITY_STEP * 4 , NUMBER = 5 , IDLE , LOW ,
  MEDIUM , HIGH , REALTIME , NUMBER ,
  IDLE = 0 , LOW = 0 , MEDIUM = 0 , HIGH = 0 ,
  REALTIME = 0 , NUMBER = 1 , IDLE = 0 , LOW = 0 ,
  MEDIUM = 0 , HIGH = 0 , REALTIME = 0 , NUMBER = 1 ,
  IDLE , LOW , MEDIUM , HIGH ,
  REALTIME , NUMBER
}
 线程优先级枚举 Enumeration for thread priorities More...
 
enum class  Priority {
  IDLE = 0 , LOW = LIBXR_PRIORITY_STEP * 1 , MEDIUM = LIBXR_PRIORITY_STEP * 2 , HIGH = LIBXR_PRIORITY_STEP * 3 ,
  REALTIME = LIBXR_PRIORITY_STEP * 4 , NUMBER = 5 , IDLE , LOW ,
  MEDIUM , HIGH , REALTIME , NUMBER ,
  IDLE = 0 , LOW = 0 , MEDIUM = 0 , HIGH = 0 ,
  REALTIME = 0 , NUMBER = 1 , IDLE = 0 , LOW = 0 ,
  MEDIUM = 0 , HIGH = 0 , REALTIME = 0 , NUMBER = 1 ,
  IDLE , LOW , MEDIUM , HIGH ,
  REALTIME , NUMBER
}
 线程优先级枚举 Enumeration for thread priorities More...
 
enum class  Priority : uint8_t {
  IDLE = 0 , LOW = LIBXR_PRIORITY_STEP * 1 , MEDIUM = LIBXR_PRIORITY_STEP * 2 , HIGH = LIBXR_PRIORITY_STEP * 3 ,
  REALTIME = LIBXR_PRIORITY_STEP * 4 , NUMBER = 5 , IDLE , LOW ,
  MEDIUM , HIGH , REALTIME , NUMBER ,
  IDLE = 0 , LOW = 0 , MEDIUM = 0 , HIGH = 0 ,
  REALTIME = 0 , NUMBER = 1 , IDLE = 0 , LOW = 0 ,
  MEDIUM = 0 , HIGH = 0 , REALTIME = 0 , NUMBER = 1 ,
  IDLE , LOW , MEDIUM , HIGH ,
  REALTIME , NUMBER
}
 线程优先级枚举 Enumeration for thread priorities More...
 

Public Member Functions

 Thread ()
 默认构造函数,初始化空线程 Default constructor initializing an empty thread
 
 Thread (libxr_thread_handle handle)
 通过 FreeRTOS 线程句柄创建线程对象 Constructor to create a thread object from a FreeRTOS thread handle
 
template<typename ArgType >
void Create (ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
 创建新线程 Creates a new thread
 
 operator libxr_thread_handle ()
 线程对象转换为 FreeRTOS 线程句柄 Converts the thread object to a FreeRTOS thread handle
 
 Thread ()
 默认构造函数,初始化空线程 Default constructor initializing an empty thread
 
 Thread (libxr_thread_handle handle)
 通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle
 
template<typename ArgType >
void Create (ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
 创建新线程 Creates a new thread
 
 operator libxr_thread_handle ()
 线程对象转换为 POSIX 线程句柄 Converts the thread object to a POSIX thread handle
 
 Thread ()
 默认构造函数,初始化空线程对象 Default constructor initializing an empty thread object
 
 Thread (libxr_thread_handle handle)
 通过线程句柄创建线程对象 Constructor to create a thread object from a thread handle
 
template<typename ArgType >
void Create (ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
 创建并执行新线程 Creates and executes a new thread
 
 operator libxr_thread_handle ()
 线程对象转换为线程句柄 Converts the thread object to a thread handle
 
 Thread ()
 默认构造函数,初始化空线程对象 Default constructor initializing an empty thread object
 
 Thread (libxr_thread_handle handle)
 通过线程句柄创建线程对象 Constructor to create a thread object from a thread handle
 
template<typename ArgType >
void Create (ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
 创建并执行新线程 Creates and executes a new thread
 
 operator libxr_thread_handle ()
 线程对象转换为线程句柄 Converts the thread object to a thread handle
 
 Thread ()
 默认构造函数,初始化空线程 Default constructor initializing an empty thread
 
 Thread (libxr_thread_handle handle)
 通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle
 
template<typename ArgType >
void Create (ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
 创建新线程 Creates a new thread
 
 operator libxr_thread_handle ()
 线程对象转换为 POSIX 线程句柄 Converts the thread object to a POSIX thread handle
 

Static Public Member Functions

static Thread Current (void)
 获取当前线程对象 Gets the current thread object
 
static uint32_t GetTime ()
 获取当前系统时间(毫秒) Gets the current system time in milliseconds
 
static void Sleep (uint32_t milliseconds)
 让线程进入休眠状态 Puts the thread to sleep
 
static void SleepUntil (TimestampMS &last_waskup_time, uint32_t time_to_sleep)
 让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
 
static void Yield ()
 让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
 
static Thread Current (void)
 获取当前线程对象 Gets the current thread object
 
static uint32_t GetTime ()
 获取当前系统时间(毫秒) Gets the current system time in milliseconds
 
static void Sleep (uint32_t milliseconds)
 让线程进入休眠状态 Puts the thread to sleep
 
static void SleepUntil (TimestampMS &last_waskup_time, uint32_t time_to_sleep)
 让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
 
static void Yield ()
 让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
 
static Thread Current (void)
 获取当前线程对象 Gets the current thread object
 
static uint32_t GetTime ()
 获取当前系统时间(毫秒) Gets the current system time in milliseconds
 
static void Sleep (uint32_t milliseconds)
 让线程进入休眠状态 Puts the thread to sleep
 
static void SleepUntil (TimestampMS &last_waskup_time, uint32_t time_to_sleep)
 让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
 
static void Yield ()
 让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
 
static Thread Current (void)
 获取当前线程对象 Gets the current thread object
 
static uint32_t GetTime ()
 获取当前系统时间(毫秒) Gets the current system time in milliseconds
 
static void Sleep (uint32_t milliseconds)
 让线程进入休眠状态 Puts the thread to sleep
 
static void SleepUntil (TimestampMS &last_waskup_time, uint32_t time_to_sleep)
 让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
 
static void Yield ()
 让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
 
static Thread Current (void)
 获取当前线程对象 Gets the current thread object
 
static uint32_t GetTime ()
 获取当前系统时间(毫秒) Gets the current system time in milliseconds
 
static void Sleep (uint32_t milliseconds)
 让线程进入休眠状态 Puts the thread to sleep
 
static void SleepUntil (TimestampMS &last_waskup_time, uint32_t time_to_sleep)
 让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
 
static void Yield ()
 让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
 

Private Attributes

libxr_thread_handle thread_handle_
 FreeRTOS 线程句柄 FreeRTOS thread handle.
 

Detailed Description

线程管理类,封装 FreeRTOS 任务创建和调度 Thread management class encapsulating FreeRTOS task creation and scheduling

线程管理类,提供线程的创建、调度和时间管理功能 Thread management class that provides thread creation, scheduling, and time management

线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling

Definition at line 14 of file thread.hpp.

Member Enumeration Documentation

◆ Priority [1/5]

线程优先级枚举 Enumeration for thread priorities

Enumerator
IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

Definition at line 21 of file thread.hpp.

22 {
23 IDLE = 0,
24 LOW = LIBXR_PRIORITY_STEP * 1,
25 MEDIUM = LIBXR_PRIORITY_STEP * 2,
26 HIGH = LIBXR_PRIORITY_STEP * 3,
27 REALTIME = LIBXR_PRIORITY_STEP * 4,
28 NUMBER = 5
29 };
@ NUMBER
优先级数量 Number of priority levels
@ LOW
低优先级 Low priority
@ REALTIME
实时优先级 Realtime priority
@ IDLE
空闲优先级 Idle priority
@ HIGH
高优先级 High priority
@ MEDIUM
中等优先级 Medium priority

◆ Priority [2/5]

线程优先级枚举 Enumeration for thread priorities

Enumerator
IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

Definition at line 20 of file thread.hpp.

21 {
22 IDLE,
23 LOW,
24 MEDIUM,
25 HIGH,
26 REALTIME,
27 NUMBER,
28 };

◆ Priority [3/5]

线程优先级枚举 Enumeration for thread priorities

Enumerator
IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

Definition at line 22 of file thread.hpp.

23 {
24 IDLE = 0,
25 LOW = 0,
26 MEDIUM = 0,
27 HIGH = 0,
28 REALTIME = 0,
29 NUMBER = 1,
30 };

◆ Priority [4/5]

线程优先级枚举 Enumeration for thread priorities

Enumerator
IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

Definition at line 22 of file thread.hpp.

23 {
24 IDLE = 0,
25 LOW = 0,
26 MEDIUM = 0,
27 HIGH = 0,
28 REALTIME = 0,
29 NUMBER = 1,
30 };

◆ Priority [5/5]

线程优先级枚举 Enumeration for thread priorities

Enumerator
IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

IDLE 

空闲优先级 Idle priority

LOW 

低优先级 Low priority

MEDIUM 

中等优先级 Medium priority

HIGH 

高优先级 High priority

REALTIME 

实时优先级 Realtime priority

NUMBER 

优先级数量 Number of priority levels

Definition at line 21 of file thread.hpp.

22 {
23 IDLE,
24 LOW,
25 MEDIUM,
26 HIGH,
27 REALTIME,
28 NUMBER,
29 };

Constructor & Destructor Documentation

◆ Thread() [1/10]

LibXR::Thread::Thread ( )
inline

默认构造函数,初始化空线程 Default constructor initializing an empty thread

Definition at line 35 of file thread.hpp.

35{};

◆ Thread() [2/10]

LibXR::Thread::Thread ( libxr_thread_handle  handle)
inline

通过 FreeRTOS 线程句柄创建线程对象 Constructor to create a thread object from a FreeRTOS thread handle

Parameters
handleFreeRTOS 线程句柄 FreeRTOS thread handle

Definition at line 42 of file thread.hpp.

42: thread_handle_(handle) {};
libxr_thread_handle thread_handle_
FreeRTOS 线程句柄 FreeRTOS thread handle.
Definition thread.hpp:136

◆ Thread() [3/10]

LibXR::Thread::Thread ( )
inline

默认构造函数,初始化空线程 Default constructor initializing an empty thread

Definition at line 34 of file thread.hpp.

34{};

◆ Thread() [4/10]

LibXR::Thread::Thread ( libxr_thread_handle  handle)
inline

通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle

Parameters
handlePOSIX 线程句柄 POSIX thread handle

Definition at line 41 of file thread.hpp.

41: thread_handle_(handle) {};

◆ Thread() [5/10]

LibXR::Thread::Thread ( )
inline

默认构造函数,初始化空线程对象 Default constructor initializing an empty thread object

Definition at line 36 of file thread.hpp.

36{};

◆ Thread() [6/10]

LibXR::Thread::Thread ( libxr_thread_handle  handle)
inline

通过线程句柄创建线程对象 Constructor to create a thread object from a thread handle

Parameters
handle线程句柄 Thread handle

Definition at line 43 of file thread.hpp.

43: thread_handle_(handle) {};

◆ Thread() [7/10]

LibXR::Thread::Thread ( )
inline

默认构造函数,初始化空线程对象 Default constructor initializing an empty thread object

Definition at line 36 of file thread.hpp.

36{};

◆ Thread() [8/10]

LibXR::Thread::Thread ( libxr_thread_handle  handle)
inline

通过线程句柄创建线程对象 Constructor to create a thread object from a thread handle

Parameters
handle线程句柄 Thread handle

Definition at line 43 of file thread.hpp.

43: thread_handle_(handle) {};

◆ Thread() [9/10]

LibXR::Thread::Thread ( )
inline

默认构造函数,初始化空线程 Default constructor initializing an empty thread

Definition at line 35 of file thread.hpp.

35{};

◆ Thread() [10/10]

LibXR::Thread::Thread ( libxr_thread_handle  handle)
inline

通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle

Parameters
handlePOSIX 线程句柄 POSIX thread handle

Definition at line 42 of file thread.hpp.

42: thread_handle_(handle) {};

Member Function Documentation

◆ Create() [1/5]

template<typename ArgType >
void LibXR::Thread::Create ( ArgType  arg,
void(*)(ArgType arg)  function,
const char name,
size_t  stack_depth,
Thread::Priority  priority 
)
inline

创建新线程 Creates a new thread

Template Parameters
ArgType线程函数的参数类型 The type of argument for the thread function
Parameters
arg线程函数的参数 Argument for the thread function
function线程执行的函数 Function executed by the thread
name线程名称 Thread name
stack_depth线程栈大小(字节) Stack size of the thread (bytes)
priority线程优先级 Thread priority

该方法基于 FreeRTOS xTaskCreate() 创建新线程,执行 function 并传递 arg 作为参数。 线程优先级 priority 必须符合 FreeRTOS 配置的 configMAX_PRIORITIES 约束。

This method creates a new thread using FreeRTOS xTaskCreate(), executing function with arg as the argument. The thread priority priority must adhere to FreeRTOS configuration constraints defined by configMAX_PRIORITIES.

Definition at line 64 of file thread.hpp.

66 {
67 ASSERT(configMAX_PRIORITIES >= 6);
68
69 class ThreadBlock
70 {
71 public:
72 ThreadBlock(typeof(function) fun, ArgType arg) : fun_(fun), arg_(arg) {}
73
74 static void Port(void *arg)
75 {
76 ThreadBlock *block = static_cast<ThreadBlock *>(arg);
77 block->fun_(block->arg_);
78 }
79
80 typeof(function) fun_;
81 ArgType arg_;
82 };
83
84 auto block = new ThreadBlock(function, arg);
85
86 auto ans = xTaskCreate(block->Port, name, stack_depth, block,
87 static_cast<uint32_t>(priority), &(this->thread_handle_));
88 UNUSED(ans);
89 UNUSED(block);
90 ASSERT(ans == pdPASS);
91 }
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ Create() [2/5]

template<typename ArgType >
void LibXR::Thread::Create ( ArgType  arg,
void(*)(ArgType arg)  function,
const char name,
size_t  stack_depth,
Thread::Priority  priority 
)
inline

创建新线程 Creates a new thread

Template Parameters
ArgType线程函数的参数类型 The type of argument for the thread function
Parameters
arg线程函数的参数 Argument for the thread function
function线程执行的函数 Function executed by the thread
name线程名称 Thread name
stack_depth线程栈大小(字节) Stack size of the thread (bytes)
priority线程优先级 Thread priority

该方法基于 POSIX pthread_create() 创建新线程,执行 function 并传递 arg 作为参数。 线程栈大小 stack_depth 需要进行调整以符合 POSIX 线程的栈管理方式。 如果系统支持 SCHED_RR 调度策略,则设置线程优先级。

This method creates a new thread using POSIX pthread_create(), executing function with arg as the argument. The stack_depth needs adjustment for POSIX thread stack management. If the system supports SCHED_RR scheduling, thread priority is set accordingly.

线程数据封装类 Thread data encapsulation class

构造函数,存储线程相关数据 Constructor to store thread-related data

Parameters
fun线程执行的函数 Function executed by the thread
arg线程参数 Thread argument
name线程名称 Thread name

线程入口函数,执行用户定义的线程函数 Thread entry function that executes the user-defined function

Parameters
arg线程参数 Thread argument
Returns
返回值始终为 nullptr The return value is always nullptr

< 线程执行的函数 Function executed by the thread

< 线程函数的参数 Argument passed to the thread function

< 线程名称 Thread name

Definition at line 64 of file thread.hpp.

66 {
69
70 // 线程栈大小设定,确保不小于 1
71 if (stack_depth > 256)
72 {
74 }
75 else
76 {
78 }
79
84 class ThreadBlock
85 {
86 public:
94 ThreadBlock(typeof(function) fun, ArgType arg, const char *name)
95 : fun_(fun),
96 arg_(arg),
97 name_(reinterpret_cast<char *>(malloc(strlen(name) + 1)))
98 {
99 strcpy(name_, name);
100 }
101
109 static void *Port(void *arg)
110 {
111 ThreadBlock *block = static_cast<ThreadBlock *>(arg);
112 volatile const char *thread_name = block->name_;
113 block->fun_(block->arg_);
114
115 UNUSED(thread_name);
116 return static_cast<void *>(nullptr);
117 }
118
119 typeof(function) fun_;
120 ArgType arg_;
121 char *name_;
122 };
123
124 auto block = new ThreadBlock(function, arg, name);
125
126 // 优先尝试设置 SCHED_FIFO 和线程优先级
129 bool scheduling_set = false;
130
131 if (max_priority - min_priority >= static_cast<int>(Priority::REALTIME))
132 {
135
136 struct sched_param sp;
137 memset(&sp, 0, sizeof(sp));
138 sp.sched_priority = min_priority + static_cast<int>(priority);
139
141 {
142 scheduling_set = true;
143 }
144 else
145 {
146 XR_LOG_WARN("Failed to set thread priority. Falling back to default policy.");
149 }
150 }
151 else
152 {
153 XR_LOG_WARN(
154 "SCHED_FIFO not supported or insufficient range. Using default policy.");
156 }
157
158 // 创建线程
159 if (auto ans = pthread_create(&this->thread_handle_, &attr, block->Port, block) != 0)
160 {
161 XR_LOG_WARN("Failed to create thread: %s (%s), Falling back to default policy.",
162 name, strerror(ans));
165 if (auto ans =
166 pthread_create(&this->thread_handle_, &attr, block->Port, block) != 0)
167 {
168 XR_LOG_ERROR("Failed to create thread: %s (%s)", ans);
169 }
170 }
171
173 }

◆ Create() [3/5]

template<typename ArgType >
void LibXR::Thread::Create ( ArgType  arg,
void(*)(ArgType arg)  function,
const char name,
size_t  stack_depth,
Thread::Priority  priority 
)
inline

创建并执行新线程 Creates and executes a new thread

Template Parameters
ArgType线程函数的参数类型 The type of argument for the thread function
Parameters
arg线程函数的参数 Argument for the thread function
function线程执行的函数 Function executed by the thread
name线程名称(未使用) Thread name (unused)
stack_depth线程栈大小(未使用) Stack size of the thread (unused)
priority线程优先级(未使用) Thread priority (unused)

此函数是一个简化版本的线程创建,仅执行 function(arg),并且只允许创建一次。 由于 namestack_depthpriority 未实际使用,主要用于测试或占位用途。

This function is a simplified version of thread creation, executing only function(arg), and it ensures that the thread is created only once. The parameters name, stack_depth, and priority are unused and mainly serve as placeholders or for testing purposes.

Definition at line 65 of file thread.hpp.

67 {
68 UNUSED(name);
69 UNUSED(stack_depth);
70 UNUSED(priority);
71
72 static bool created = false;
73 ASSERT(created == false);
74 created = true;
75 UNUSED(created);
76
77 function(arg);
78 }

◆ Create() [4/5]

template<typename ArgType >
void LibXR::Thread::Create ( ArgType  arg,
void(*)(ArgType arg)  function,
const char name,
size_t  stack_depth,
Thread::Priority  priority 
)
inline

创建并执行新线程 Creates and executes a new thread

Template Parameters
ArgType线程函数的参数类型 The type of argument for the thread function
Parameters
arg线程函数的参数 Argument for the thread function
function线程执行的函数 Function executed by the thread
name线程名称(未使用) Thread name (unused)
stack_depth线程栈大小(未使用) Stack size of the thread (unused)
priority线程优先级(未使用) Thread priority (unused)

此函数是一个简化版本的线程创建,仅执行 function(arg),并且只允许创建一次。 由于 namestack_depthpriority 未实际使用,主要用于测试或占位用途。

This function is a simplified version of thread creation, executing only function(arg), and it ensures that the thread is created only once. The parameters name, stack_depth, and priority are unused and mainly serve as placeholders or for testing purposes.

Definition at line 65 of file thread.hpp.

67 {
68 UNUSED(name);
69 UNUSED(stack_depth);
70 UNUSED(priority);
71
72 static bool created = false;
73 ASSERT(created == false);
74 created = true;
75 UNUSED(created);
76
77 function(arg);
78 }

◆ Create() [5/5]

template<typename ArgType >
void LibXR::Thread::Create ( ArgType  arg,
void(*)(ArgType arg)  function,
const char name,
size_t  stack_depth,
Thread::Priority  priority 
)
inline

创建新线程 Creates a new thread

Template Parameters
ArgType线程函数的参数类型 The type of argument for the thread function
Parameters
arg线程函数的参数 Argument for the thread function
function线程执行的函数 Function executed by the thread
name线程名称 Thread name
stack_depth线程栈大小(字节) Stack size of the thread (bytes)
priority线程优先级 Thread priority

该方法基于 POSIX pthread_create() 创建新线程,执行 function 并传递 arg 作为参数。 线程栈大小 stack_depth 需要进行调整以符合 POSIX 线程的栈管理方式。 如果系统支持 SCHED_RR 调度策略,则设置线程优先级。

This method creates a new thread using POSIX pthread_create(), executing function with arg as the argument. The stack_depth needs adjustment for POSIX thread stack management. If the system supports SCHED_RR scheduling, thread priority is set accordingly.

线程数据封装类 Thread data encapsulation class

构造函数,存储线程相关数据 Constructor to store thread-related data

Parameters
fun线程执行的函数 Function executed by the thread
arg线程参数 Thread argument
name线程名称 Thread name

线程入口函数,执行用户定义的线程函数 Thread entry function that executes the user-defined function

Parameters
arg线程参数 Thread argument
Returns
返回值始终为 nullptr The return value is always nullptr

< 线程执行的函数 Function executed by the thread

< 线程函数的参数 Argument passed to the thread function

< 线程名称 Thread name

Definition at line 65 of file thread.hpp.

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 }

◆ Current() [1/5]

Thread Thread::Current ( void  )
static

获取当前线程对象 Gets the current thread object

Returns
当前线程对象 The current thread object

Definition at line 7 of file thread.cpp.

Thread()
默认构造函数,初始化空线程 Default constructor initializing an empty thread
Definition thread.hpp:35

◆ Current() [2/5]

static Thread LibXR::Thread::Current ( void  )
static

获取当前线程对象 Gets the current thread object

Returns
当前线程对象 The current thread object

◆ Current() [3/5]

static Thread LibXR::Thread::Current ( void  )
static

获取当前线程对象 Gets the current thread object

Returns
当前线程对象 The current thread object

◆ Current() [4/5]

static Thread LibXR::Thread::Current ( void  )
static

获取当前线程对象 Gets the current thread object

Returns
当前线程对象 The current thread object

◆ Current() [5/5]

static Thread LibXR::Thread::Current ( void  )
static

获取当前线程对象 Gets the current thread object

Returns
当前线程对象 The current thread object

◆ GetTime() [1/5]

uint32_t Thread::GetTime ( )
static

获取当前系统时间(毫秒) Gets the current system time in milliseconds

Returns
当前时间(毫秒) Current time in milliseconds

Definition at line 16 of file thread.cpp.

16{ return xTaskGetTickCount(); }

◆ GetTime() [2/5]

static uint32_t LibXR::Thread::GetTime ( )
static

获取当前系统时间(毫秒) Gets the current system time in milliseconds

Returns
当前时间(毫秒) Current time in milliseconds

◆ GetTime() [3/5]

static uint32_t LibXR::Thread::GetTime ( )
static

获取当前系统时间(毫秒) Gets the current system time in milliseconds

Returns
当前时间(毫秒) Current time in milliseconds

◆ GetTime() [4/5]

static uint32_t LibXR::Thread::GetTime ( )
static

获取当前系统时间(毫秒) Gets the current system time in milliseconds

Returns
当前时间(毫秒) Current time in milliseconds

◆ GetTime() [5/5]

static uint32_t LibXR::Thread::GetTime ( )
static

获取当前系统时间(毫秒) Gets the current system time in milliseconds

Returns
当前时间(毫秒) Current time in milliseconds

◆ operator libxr_thread_handle() [1/5]

LibXR::Thread::operator libxr_thread_handle ( )
inline

线程对象转换为 FreeRTOS 线程句柄 Converts the thread object to a FreeRTOS thread handle

Returns
FreeRTOS 线程句柄 FreeRTOS thread handle

Definition at line 133 of file thread.hpp.

133{ return thread_handle_; }

◆ operator libxr_thread_handle() [2/5]

LibXR::Thread::operator libxr_thread_handle ( )
inline

线程对象转换为 POSIX 线程句柄 Converts the thread object to a POSIX thread handle

Returns
POSIX 线程句柄 POSIX thread handle

Definition at line 215 of file thread.hpp.

215{ return thread_handle_; }

◆ operator libxr_thread_handle() [3/5]

LibXR::Thread::operator libxr_thread_handle ( )
inline

线程对象转换为线程句柄 Converts the thread object to a thread handle

Returns
线程句柄 Thread handle

Definition at line 120 of file thread.hpp.

120{ return thread_handle_; }

◆ operator libxr_thread_handle() [4/5]

LibXR::Thread::operator libxr_thread_handle ( )
inline

线程对象转换为线程句柄 Converts the thread object to a thread handle

Returns
线程句柄 Thread handle

Definition at line 120 of file thread.hpp.

120{ return thread_handle_; }

◆ operator libxr_thread_handle() [5/5]

LibXR::Thread::operator libxr_thread_handle ( )
inline

线程对象转换为 POSIX 线程句柄 Converts the thread object to a POSIX thread handle

Returns
POSIX 线程句柄 POSIX thread handle

Definition at line 216 of file thread.hpp.

216{ return thread_handle_; }

◆ Sleep() [1/5]

void Thread::Sleep ( uint32_t  milliseconds)
static

让线程进入休眠状态 Puts the thread to sleep

Parameters
milliseconds休眠时间(毫秒) Sleep duration in milliseconds

Definition at line 9 of file thread.cpp.

◆ Sleep() [2/5]

static void LibXR::Thread::Sleep ( uint32_t  milliseconds)
static

让线程进入休眠状态 Puts the thread to sleep

Parameters
milliseconds休眠时间(毫秒) Sleep duration in milliseconds

◆ Sleep() [3/5]

static void LibXR::Thread::Sleep ( uint32_t  milliseconds)
static

让线程进入休眠状态 Puts the thread to sleep

Parameters
milliseconds休眠时间(毫秒) Sleep duration in milliseconds

◆ Sleep() [4/5]

static void LibXR::Thread::Sleep ( uint32_t  milliseconds)
static

让线程进入休眠状态 Puts the thread to sleep

Parameters
milliseconds休眠时间(毫秒) Sleep duration in milliseconds

◆ Sleep() [5/5]

static void LibXR::Thread::Sleep ( uint32_t  milliseconds)
static

让线程进入休眠状态 Puts the thread to sleep

Parameters
milliseconds休眠时间(毫秒) Sleep duration in milliseconds

◆ SleepUntil() [1/5]

void Thread::SleepUntil ( TimestampMS last_waskup_time,
uint32_t  time_to_sleep 
)
static

让线程休眠直到指定时间点 Puts the thread to sleep until a specified time

Parameters
last_waskup_time上次唤醒时间 Last wake-up time
time_to_sleep休眠时长(毫秒) Sleep duration in milliseconds

Definition at line 11 of file thread.cpp.

11 {
12 vTaskDelayUntil(reinterpret_cast<uint32_t *>(&last_waskup_time),
14}

◆ SleepUntil() [2/5]

static void LibXR::Thread::SleepUntil ( TimestampMS last_waskup_time,
uint32_t  time_to_sleep 
)
static

让线程休眠直到指定时间点 Puts the thread to sleep until a specified time

Parameters
last_waskup_time上次唤醒时间 Last wake-up time
time_to_sleep休眠时长(毫秒) Sleep duration in milliseconds

◆ SleepUntil() [3/5]

static void LibXR::Thread::SleepUntil ( TimestampMS last_waskup_time,
uint32_t  time_to_sleep 
)
static

让线程休眠直到指定时间点 Puts the thread to sleep until a specified time

Parameters
last_waskup_time上次唤醒时间 Last wake-up time
time_to_sleep休眠时长(毫秒) Sleep duration in milliseconds

◆ SleepUntil() [4/5]

static void LibXR::Thread::SleepUntil ( TimestampMS last_waskup_time,
uint32_t  time_to_sleep 
)
static

让线程休眠直到指定时间点 Puts the thread to sleep until a specified time

Parameters
last_waskup_time上次唤醒时间 Last wake-up time
time_to_sleep休眠时长(毫秒) Sleep duration in milliseconds

◆ SleepUntil() [5/5]

static void LibXR::Thread::SleepUntil ( TimestampMS last_waskup_time,
uint32_t  time_to_sleep 
)
static

让线程休眠直到指定时间点 Puts the thread to sleep until a specified time

Parameters
last_waskup_time上次唤醒时间 Last wake-up time
time_to_sleep休眠时长(毫秒) Sleep duration in milliseconds

◆ Yield()

void Thread::Yield ( )
static

让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run

Definition at line 18 of file thread.cpp.

18{ portYIELD(); } // NOLINT

Field Documentation

◆ thread_handle_

libxr_thread_handle LibXR::Thread::thread_handle_
private

FreeRTOS 线程句柄 FreeRTOS thread handle.

线程句柄 Thread handle

POSIX 线程句柄 POSIX thread handle.

Definition at line 136 of file thread.hpp.


The documentation for this class was generated from the following files: