|
| static void | xwos_thd_attr_init (struct xwos_thd_attr *attr) |
| | XWOS API:初始化线程属性结构体
|
| |
| static xwer_t | xwos_thd_init (struct xwos_thd *thd, xwos_thd_d *thdd, const struct xwos_thd_attr *attr, xwos_thd_f thdfunc, void *arg) |
| | XWOS API:静态方式初始化线程
|
| |
| static xwer_t | xwos_thd_grab (xwos_thd_d thdd) |
| | XWOS API:增加线程对象的引用计数
|
| |
| static xwer_t | xwos_thd_put (xwos_thd_d thdd) |
| | XWOS API:减少线程对象的引用计数
|
| |
| static xwer_t | xwos_thd_create (xwos_thd_d *thdd, const struct xwos_thd_attr *attr, xwos_thd_f thdfunc, void *arg) |
| | XWOS API:使用动态申请内存方式创建线程并初始化
|
| |
| static xwer_t | xwos_thd_acquire (xwos_thd_d thdd) |
| | XWOS API:检查线程对象的标签并增加引用计数
|
| |
| static xwer_t | xwos_thd_release (xwos_thd_d thdd) |
| | XWOS API:检查对象的标签并减少引用计数
|
| |
| static xwer_t | xwos_thd_launch (xwos_thd_d thdd, xwos_thd_f thdfunc, void *arg) |
| | XWOS API:加载线程
|
| |
| static xwer_t | xwos_thd_get_attr (xwos_thd_d thdd, struct xwos_thd_attr *attr) |
| | XWOS API:获取线程的属性
|
| |
| static xwer_t | xwos_thd_get_stack_info (xwos_thd_d thdd, struct xwos_thd_stack_info *stack) |
| | XWOS API:获取线程的栈信息
|
| |
| static xwer_t | xwos_thd_get_stack_usage (xwos_thd_d thdd, xwsz_t *usage) |
| | XWOS API:获取线程的栈用量信息
|
| |
| static xwer_t | xwos_thd_intr (xwos_thd_d thdd) |
| | XWOS API:中断线程的阻塞态和睡眠态
|
| |
| static xwer_t | xwos_thd_quit (xwos_thd_d thdd) |
| | XWOS API:通知线程退出
|
| |
| static xwer_t | xwos_thd_join (xwos_thd_d thdd, xwer_t *trc) |
| | XWOS API:等待线程结束,回收线程内存资源,抛出它的返回值
|
| |
| static xwer_t | xwos_thd_stop (xwos_thd_d thdd, xwer_t *trc) |
| | XWOS API:终止线程并等待它退出,回收线程内存资源,并抛出线程的返回值
|
| |
| static xwer_t | xwos_thd_detach (xwos_thd_d thdd) |
| | XWMP API:分离线程
|
| |
| static xwer_t | xwos_thd_migrate (xwos_thd_d thdd, xwid_t dstcpu) |
| | XWOS API:将线程迁移到目标CPU
|
| |
| static xwos_thd_d | xwos_cthd_self (void) |
| | XWOS API:获取当前线程的对象描述符
|
| |
| static void | xwos_cthd_get_attr (struct xwos_thd_attr *attr) |
| | XWOS API:获取线程自身的属性
|
| |
| static void | xwos_cthd_get_stack_info (struct xwos_thd_stack_info *stack) |
| | XWOS API:获取线程自身的栈信息
|
| |
| static xwsz_t | xwos_cthd_get_stack_usage (void) |
| | XWOS API:获取线程自身的栈用量信息
|
| |
| static void | xwos_cthd_yield (void) |
| | XWOS API:当前线程通知调度器重新调度
|
| |
| static void | xwos_cthd_exit (xwer_t rc) |
| | XWOS API:退出当前线程
|
| |
| static bool | xwos_cthd_shld_frz (void) |
| | XWOS API:判断当前线程是否可被冻结
|
| |
| static bool | xwos_cthd_shld_stop (void) |
| | XWOS API:判断当前线程是否可以退出
|
| |
| static bool | xwos_cthd_frz_shld_stop (bool *frozen) |
| | XWOS API:判断当前线程是否可被冻结,如果是,就冻结线程, 之后再判断线程是否可以退出
|
| |
| static xwer_t | xwos_cthd_sleep (xwtm_t dur) |
| | XWOS API:当前线程睡眠一段时间
|
| |
| static xwer_t | xwos_cthd_sleep_to (xwtm_t to) |
| | XWOS API:线程睡眠到一个时间点
|
| |
| static xwer_t | xwos_cthd_sleep_from (xwtm_t *from, xwtm_t dur) |
| | XWOS API:线程从一个时间起点睡眠到另一个时间点
|
| |
| static xwer_t | xwos_cthd_freeze (void) |
| | XWOS API:冻结当前线程
|
| |
| static xwer_t | xwos_thd_set_data (xwos_thd_d thdd, xwsq_t pos, void *data) |
| | XWOS API:设置线程的本地数据指针
|
| |
| static xwer_t | xwos_thd_get_data (xwos_thd_d thdd, xwsq_t pos, void **databuf) |
| | XWOS API:获取线程的本地数据指针
|
| |
| static xwer_t | xwos_cthd_set_data (xwsq_t pos, void *data) |
| | XWOS API:设置当前线程的本地数据指针
|
| |
| static xwer_t | xwos_cthd_get_data (xwsq_t pos, void **databuf) |
| | XWOS API:获取当前线程的本地数据指针
|
| |
技术参考手册: 线程
线程属性
创建或初始化线程之前,需要先描述线程属性 xwos_thd_attr 。 线程属性需要先通过 xwos_thd_attr_init() 初始化后再描述。
已经创建或初始化的线程可以通过 xwos_thd_get_attr() 获取其属性。
线程可以通过 xwos_cthd_get_attr() 获取自身属性。
线程栈信息
已经创建或初始化的线程可以通过 xwos_thd_get_stack_info() 获取其属性。
线程可以通过 xwos_cthd_get_stack_info() 获取自身栈信息。
静态初始化线程
用户可以在编译期预先定义线程对象的结构体、线程栈, 然后运行时通过 xwos_thd_init() 对线程进行初始化。
定义静态线程栈时需要使用 __xwos_thd_stack 修饰,以到达内存对齐的要求。
动态创建线程
用户可以在程序运行时通过 xwos_thd_create() 动态创建线程。
加载线程
若初始化/创建线程时未指定线程函数,就需要通过 xwos_thd_launch() 来加载线程, 线程会在调用 xwos_thd_launch() 的CPU上运行。
中断线程的阻塞态和睡眠态
用户可以通过 xwos_thd_intr() 中断另一个线程的阻塞态和睡眠态。
线程的连接态与分离态
XWOS线程的分离态与连接态是参考 pthread 设计的:
线程的终止
用户可以通过 xwos_thd_quit() 终止另一个正在运行的线程。 之后可以通过 xwos_thd_join() 等待线程结束运行。
xwos_thd_stop() = xwos_thd_quit() + xwos_thd_join()
线程自己的退出
线程自身可以在主函数中 return 或调用 xwos_cthd_exit() 退出。 线程自身可以通过 xwos_cthd_shld_stop() 判断 是否有其他线程对自己调用了 xwos_thd_quit() 。
线程自身的冻结
线程可以通过 xwos_cthd_shld_frz() 判断自身是否 需要 冻结 。 需要冻结时可调用 xwos_cthd_freeze() 进行冻结。
xwos_cthd_frz_shld_stop() 等价于 xwos_cthd_shld_frz() + xwos_cthd_freeze() + xwos_cthd_shld_stop() 。
线程的迁移
多CPU的系统中,可以通过 xwos_thd_migrate() 迁移线程到另一个CPU。
线程自身的睡眠
线程自身的私有数据
XWOS支持 C11 标准之后引入的 _Thread_local 关键字 。 如果使用 C99 以前的标准,用户可以通过 xwos_cthd_set_data() 和 xwos_cthd_get_data() 可设置和获取线程自身私有变量。
用户也可通过 xwos_thd_set_data() 和 xwos_thd_get_data() 设置和获取 其他线程私有变量。
线程对象的生命周期管理
- 通过 对象指针 管理生命周期:
- 通过 对象描述符 管理生命周期:
对象描述符
线程自身可以通过 xwos_cthd_self() 获取自己的 xwos_thd_d 。
◆ XWOS_THD_NILD
◆ __xwos_thd_stack
XWOS API:定义栈内存时的修饰符
在文件 thd.h 第 203 行定义.
◆ XWOS_STACK_SIZE_DEFAULT
| #define XWOS_STACK_SIZE_DEFAULT XWMMCFG_STACK_SIZE_DEFAULT |
◆ XWOS_STACK_SIZE_MIN
| #define XWOS_STACK_SIZE_MIN XWMMCFG_STACK_SIZE_MIN |
◆ XWOS_STACK_GUARD_SIZE_DEFAULT
| #define XWOS_STACK_GUARD_SIZE_DEFAULT XWMMCFG_STACK_GUARD_SIZE_DEFAULT |
◆ XWOS_THD_LOCAL_DATA_NUM
| #define XWOS_THD_LOCAL_DATA_NUM XWOSDL_THD_LOCAL_DATA_NUM |
XWOS API:线程本地数据指针的数量
在文件 thd.h 第 223 行定义.
◆ xwos_thd_f
| typedef xwer_t(* xwos_thd_f) (void *) |
◆ xwos_thd_attr_init()
XWOS API:初始化线程属性结构体
- 参数
-
- 注解
-
线程属性结构体 struct xwos_thd_attr 是对 pthread_attr_t 的简化。
此CAPI功能类似于 pthread_attr_init() 。XWOS的线程属性结构体成员都是基本数据类型, 初始化不会失败,因此此CAPI没有返回值。
XWOS的线程属性结构体对用户透明,用户亦可在定义时直接对结构体成员赋值, 因此XWOS不提供类似于 pthread_attr_setXXX() 与 pthread_attr_getXXX() 的函数组合。
XWOS的线程属性结构体初始化不会额外动态申请内存,因此不需要对 结构体进行销毁,也不存在与 pthread_attr_destroy() 类似的函数。
在文件 thd.h 第 253 行定义.
254{
255 xwosdl_thd_attr_init((struct xwosdl_thd_attr *)attr);
256}
◆ xwos_thd_init()
XWOS API:静态方式初始化线程
- 参数
-
| [in] | thd | 指向构造线程对象内存的指针 |
| [out] | thdd | 指向缓冲区的指针,通过此缓冲区返回线程对象描述符 |
| [in] | attr | 线程属性 |
| [in] | thdfunc | 线程函数的指针 |
| [in] | arg | 线程函数的参数 |
- 返回
- 错误码
- 返回值
-
- 注解
-
此CAPI用于不使用动态内存的方式创建线程。所需内存(线程对象结构体,栈内存数组) 都需要在编译期定义,并且用户需要保证它们的生命周期在线程运行期间始终有效, 通常定义为全局变量。
这种方式创建线程,可在编译期计算出准确的内存使用量, 避免使用动态内存时返回 -ENOMEM 错误, 在一些强调安全的规范中,这是硬性要求。
参数 attr 类似于 pthread_attr_t ,但静态方式初始化线程时不可为空, 必须用来传递预先定义的 线程栈内存 的地址。
栈内存的首地址与大小,必须要满足CPU的ABI规则,例如ARM,就需要8字节对齐, 且大小是8的倍数。如果CPU存在DCache,最好让线程栈对齐到缓存线,可获取最高性能。 因此需要使用 __xwos_thd_stack 修饰线程对象结构体和栈内存数组。
参数 thdfunc 可以为 NULL ,此时只创建线程对象,不开始运行。 直到CPU调用 xwos_thd_launch() 后,线程才开始在此CPU上运行。
在文件 thd.h 第 294 行定义.
297{
298 return xwosdl_thd_init(&thd->
osthd, (xwosdl_thd_d *)thdd,
299 (const struct xwosdl_thd_attr *)attr,
300
301 (xwosdl_thd_f)thdfunc, arg);
302}
◆ xwos_thd_grab()
XWOS API:增加线程对象的引用计数
- 参数
-
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EFAULT | 空指针 |
| -EOBJDEAD | 线程对象无效 |
- 注解
-
此CAPI主要用于管理 静态对象 的引用计数。 若用于 动态对象 ,需要确保对象的指针一定不是野指针。
在文件 thd.h 第 318 行定义.
319{
320 return xwosdl_thd_grab(&thdd.
thd->
osthd);
321}
◆ xwos_thd_put()
XWOS API:减少线程对象的引用计数
- 参数
-
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EFAULT | 空指针 |
| -EOBJDEAD | 线程对象无效 |
- 注解
-
此CAPI主要用于管理 静态对象 的引用计数。 若用于 动态对象 ,需要确保对象的指针一定不是野指针。
在文件 thd.h 第 337 行定义.
338{
339 return xwosdl_thd_put(&thdd.
thd->
osthd);
340}
◆ xwos_thd_create()
XWOS API:使用动态申请内存方式创建线程并初始化
- 参数
-
| [out] | thdd | 指向缓冲区的指针,通过此缓冲区返回线程对象描述符 |
| [in] | attr | 线程属性 |
| [in] | thdfunc | 线程函数的指针 |
| [in] | arg | 线程函数的参数 |
- 返回
- 错误码
- 注解
-
参数 attr 类似于 pthread_attr_t ,可为 NULL ,表示采用默认属性创建线程。
若通过 attr->stack 指定内存作为栈,栈内存的首地址与大小,必须要满足CPU的ABI规则, 例如ARM,就需要8字节对齐,且大小是8的倍数。 如果CPU存在DCache,最好让线程栈对齐到缓存线,可获取最高性能。 因此需要使用 __xwos_thd_stack 修饰线程对象结构体和栈内存数组。 如果栈内存也动态申请,地址对齐问题由操作系统内核处理。
参数 thdfunc 可以为 NULL ,此时只创建线程对象,不开始运行。 直到CPU调用 xwos_thd_launch() 后,线程才开始在此CPU上运行。
在文件 thd.h 第 366 行定义.
369{
370 return xwosdl_thd_create((xwosdl_thd_d *)thdd,
371 (const struct xwosdl_thd_attr *)attr,
372
373 (xwosdl_thd_f)thdfunc, arg);
374}
◆ xwos_thd_acquire()
XWOS API:检查线程对象的标签并增加引用计数
- 参数
-
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EOBJDEAD | 线程对象无效 |
| -EACCES | 对象标签检查失败 |
- 注解
-
在文件 thd.h 第 387 行定义.
388{
389 return xwosdl_thd_acquire(&thdd.
thd->
osthd, thdd.
tik);
390}
◆ xwos_thd_release()
XWOS API:检查对象的标签并减少引用计数
- 参数
-
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EOBJDEAD | 线程对象无效 |
| -EACCES | 对象标签检查失败 |
- 注解
-
在文件 thd.h 第 403 行定义.
404{
405 return xwosdl_thd_release(&thdd.
thd->
osthd, thdd.
tik);
406}
◆ xwos_thd_launch()
XWOS API:加载线程
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [in] | thdfunc | 线程函数的指针 |
| [in] | arg | 线程函数的参数 |
- 返回
- 错误码
- 注解
-
线程会在调用此CAPI的CPU上,以 thdfunc 作为线程函数,arg 作为线程函数参数, 开始运行。
在文件 thd.h 第 422 行定义.
423{
424 return xwosdl_thd_launch(&thdd.
thd->
osthd, thdd.
tik,
425
426 (xwosdl_thd_f)thdfunc, arg);
427}
◆ xwos_thd_get_attr()
XWOS API:获取线程的属性
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [out] | attr | 用于返回线程属性的缓冲区 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EOBJDEAD | 线程对象无效 |
| -EACCES | 对象标签检查失败 |
- 注解
-
在文件 thd.h 第 442 行定义.
443{
444 return xwosdl_thd_get_attr(&thdd.
thd->
osthd, thdd.
tik,
445 (struct xwosdl_thd_attr *)attr);
446}
◆ xwos_thd_get_stack_info()
XWOS API:获取线程的栈信息
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [out] | stack | 用于返回线程栈信息的缓冲区 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EOBJDEAD | 线程对象无效 |
| -EACCES | 对象标签检查失败 |
- 注解
-
<No error
在文件 thd.h 第 460 行定义.
461{
463 struct xwosdl_thd_stack_info info;
464
465 rc = xwosdl_thd_get_stack_info(&thdd.
thd->
osthd, thdd.
tik, &info);
466 if ((
XWOK == rc) && (stack)) {
469 stack->
base = info.base;
470 stack->
size = info.size;
473 stack->
line = info.line;
474 stack->
usage = info.usage;
475 }
476 return rc;
477}
◆ xwos_thd_get_stack_usage()
XWOS API:获取线程的栈用量信息
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [out] | usage | 用于返回线程栈用量信息的缓冲区 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EOBJDEAD | 线程对象无效 |
| -EACCES | 对象标签检查失败 |
- 注解
-
在文件 thd.h 第 491 行定义.
492{
493 return xwosdl_thd_get_stack_usage(&thdd.
thd->
osthd, thdd.
tik, usage);
494}
◆ xwos_thd_intr()
XWOS API:中断线程的阻塞态和睡眠态
- 参数
-
- 返回
- 错误码
- 返回值
-
- 注解
-
- 此CAPI可在 线程 、 中断 、 中断底半部 、 空闲任务 上下文中使用。
- 此CAPI会中断线程的 阻塞状态 和 睡眠状态 , 阻塞和睡眠的函数将以错误码
-EINTR 退出。
- 如果线程的 阻塞状态 是不可被中断的,中断将不会发生。
在文件 thd.h 第 511 行定义.
512{
513 return xwosdl_thd_intr(&thdd.
thd->
osthd, thdd.
tik);
514}
◆ xwos_thd_quit()
XWOS API:通知线程退出
- 参数
-
- 返回
- 错误码
- 返回值
-
- 注解
-
此CAPI用于向另一个子线程 B 设置 退出状态 。
- 此CAPI可在 线程 、 中断 、 中断底半部 、 空闲任务 上下文中使用, 不会等待子线程 B 退出。
- 子线程 B 可以是 Joinable 的,也可以是 Detached 的。
- 此CAPI可被重复调用,子线程 B 退出状态 一旦被设置,不可被清除。
- 此CAPI会中断子线程 B 的 阻塞状态 和 睡眠状态 , 阻塞和睡眠的函数将以错误码
-EINTR 退出。
- 如果子线程 B 的 阻塞状态 是不可被中断的,中断将不会发生。
- 子线程 B 可以通过
xwos_cthd_shld_stop() 检测 退出状态 。
在XWOS-V2.0以前,此CAPI被命名为 xwos_thd_cancel() , 名称类似于 pthread_cacnel() ,但功能差异较大。 为避免迷惑,将其改名为 xwos_thd_quit() 。
在文件 thd.h 第 542 行定义.
543{
544 return xwosdl_thd_quit(&thdd.
thd->
osthd, thdd.
tik);
545}
◆ xwos_thd_join()
XWOS API:等待线程结束,回收线程内存资源,抛出它的返回值
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [out] | trc | 指向缓冲区的指针,通过此缓冲区返回线程的返回值 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EOBJDEAD | 线程对象无效 |
| -EINVAL | 线程不是Joinable的 |
| -EPERM; | 不允许线程join自己 |
| -EALREADY | 线程已被连接 |
- 注解
-
此CAPI类似于POSIX线程库的 pthread_join() 函数,用于等待另一个线程退出。
父线程 A 调用此CAPI时,会阻塞等待子线程 B 退出。 子线程 B 退出后,此CAPI会释放子线程 B 的内存资源,然后唤醒父线程 A , 并通过 trc 将子线程 B 的返回值返回给父线程 A 。 参数 trc 可为 NULL ,表示不需要获取返回值。
- 父线程 A 的阻塞状态是可被中断的;
- 如果子线程 B 已经提前运行至退出,此CAPI会释放子线程 B 的资源, 然后立即将子线程 B 的返回值返回给父线程 A ;
- 此CAPI只能对 Joinable 的子线程 B 使用;
- 对 Detached 的子线程 B 调用此CAPI会得到错误码
-EINVAL ;
- 多个线程对同一个子线程 B 进行
xwos_thd_join() ,会返回错误码 -EALREADY ;
- 父线程 A 对自己调用此CAPI,会返回错误码
-EPERM 。
在文件 thd.h 第 577 行定义.
578{
579 return xwosdl_thd_join(&thdd.
thd->
osthd, thdd.
tik, trc);
580}
◆ xwos_thd_stop()
XWOS API:终止线程并等待它退出,回收线程内存资源,并抛出线程的返回值
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [out] | trc | 指向缓冲区的指针,通过此缓冲区返回线程的返回值 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EOBJDEAD | 线程对象无效 |
| -EINVAL | 线程不是Joinable的 |
| -EPERM; | 不允许线程stop自己 |
| -EALREADY | 线程已连接 |
- 注解
-
此CAPI等价于 xwos_thd_quit() + xwos_thd_join() 。
参数 trc 可为 NULL ,表示不需要获取返回值。
在文件 thd.h 第 600 行定义.
601{
602 return xwosdl_thd_stop(&thdd.
thd->
osthd, thdd.
tik, trc);
603}
◆ xwos_thd_detach()
XWMP API:分离线程
- 参数
-
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EINVAL | 线程已经被连接 |
| -EOBJDEAD | 线程对象无效 |
- 注解
-
此CAPI功能类似于 pthread_detach() ,将线程设置为 分离状态 。 处于 分离状态 的线程退出后,系统会自动回收其内存资源, 不需要另一个线程调用 xwos_thd_join() 来回收其内存资源。
同一个线程对象,可重复调用此CAPI。
在文件 thd.h 第 622 行定义.
623{
624 return xwosdl_thd_detach(&thdd.
thd->
osthd, thdd.
tik);
625}
◆ xwos_thd_migrate()
XWOS API:将线程迁移到目标CPU
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [in] | dstcpu | 目标CPU的ID |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -ENODEV | CPU序号不存在 |
| -EOBJDEAD | 线程对象无效 |
- 注解
-
此CAPI用于将线程 thdd.thd 迁移到另一个CPU dstcpu 上。
此CAPI是异步的,只会发起一个软中断就返回。 线程的迁移由内核在软中断中完成。
在文件 thd.h 第 644 行定义.
645{
646 return xwosdl_thd_migrate(&thdd.
thd->
osthd, thdd.
tik, dstcpu);
647}
◆ xwos_cthd_self()
XWOS API:获取当前线程的对象描述符
- 返回
- 线程对象描述符
- 注解
-
在文件 thd.h 第 656 行定义.
657{
658 xwosdl_thd_d thdd;
659
660 thdd = xwosdl_cthd_self();
662}
◆ xwos_cthd_get_attr()
XWOS API:获取线程自身的属性
- 参数
-
- 注解
-
在文件 thd.h 第 671 行定义.
672{
673
675}
static xwos_thd_d xwos_cthd_self(void)
XWOS API:获取当前线程的对象描述符
static xwer_t xwos_thd_get_attr(xwos_thd_d thdd, struct xwos_thd_attr *attr)
XWOS API:获取线程的属性
◆ xwos_cthd_get_stack_info()
XWOS API:获取线程自身的栈信息
- 参数
-
- 注解
-
在文件 thd.h 第 684 行定义.
685{
686
688}
static xwer_t xwos_thd_get_stack_info(xwos_thd_d thdd, struct xwos_thd_stack_info *stack)
XWOS API:获取线程的栈信息
◆ xwos_cthd_get_stack_usage()
| static xwsz_t xwos_cthd_get_stack_usage |
( |
void |
| ) |
|
|
inlinestatic |
XWOS API:获取线程自身的栈用量信息
- 返回
- 线程栈用量信息
- 注解
-
在文件 thd.h 第 697 行定义.
698{
700
701
703 return usage;
704}
static xwer_t xwos_thd_get_stack_usage(xwos_thd_d thdd, xwsz_t *usage)
XWOS API:获取线程的栈用量信息
◆ xwos_cthd_yield()
| static void xwos_cthd_yield |
( |
void |
| ) |
|
|
inlinestatic |
XWOS API:当前线程通知调度器重新调度
- 注解
-
在文件 thd.h 第 712 行定义.
713{
714 xwosdl_cthd_yield();
715}
◆ xwos_cthd_exit()
| static void xwos_cthd_exit |
( |
xwer_t |
rc | ) |
|
|
inlinestatic |
XWOS API:退出当前线程
- 参数
-
- 注解
-
此CAPI类似于POSIX线程库中的 pthread_exit() ,会立即终止调用线程,并抛出返回值。 线程调用此CAPI后就结束运行,不再返回。
在文件 thd.h 第 727 行定义.
728{
729 xwosdl_cthd_exit(rc);
730}
◆ xwos_cthd_shld_frz()
| static bool xwos_cthd_shld_frz |
( |
void |
| ) |
|
|
inlinestatic |
XWOS API:判断当前线程是否可被冻结
- 返回
- 布尔值
- 返回值
-
- 注解
-
在文件 thd.h 第 741 行定义.
742{
743 return xwosdl_cthd_shld_frz();
744}
◆ xwos_cthd_shld_stop()
| static bool xwos_cthd_shld_stop |
( |
void |
| ) |
|
|
inlinestatic |
XWOS API:判断当前线程是否可以退出
- 返回
- 布尔值
- 返回值
-
- 注解
-
退出的条件由 xwos_thd_quit() 或 xwos_thd_stop() 设置。 此CAPI常用在线程主循环的循环条件中。例如:
{
}
}
static bool xwos_cthd_shld_stop(void)
XWOS API:判断当前线程是否可以退出
在文件 thd.h 第 767 行定义.
768{
769 return xwosdl_cthd_shld_stop();
770}
◆ xwos_cthd_frz_shld_stop()
| static bool xwos_cthd_frz_shld_stop |
( |
bool * |
frozen | ) |
|
|
inlinestatic |
XWOS API:判断当前线程是否可被冻结,如果是,就冻结线程, 之后再判断线程是否可以退出
- 参数
-
| [out] | frozen | 指向缓冲区的指针,通过此缓冲区返回线程是否被冻结过 |
- 返回
- 布尔值
- 返回值
-
- 注解
-
此CAPI等价于 xwos_cthd_shld_frz() + xwos_cthd_freeze() + xwos_cthd_shld_stop()
执行顺序是:
参数 frozen 可以告知调用者线程是否发生过冻结,如果不需要这个信息, 可以将参数 frozen 填写为 NULL 。
例如:
xwer_t thread_main(
void * arg)
{
}
}
static bool xwos_cthd_frz_shld_stop(bool *frozen)
XWOS API:判断当前线程是否可被冻结,如果是,就冻结线程, 之后再判断线程是否可以退出
在文件 thd.h 第 805 行定义.
806{
807 return xwosdl_cthd_frz_shld_stop(frozen);
808}
◆ xwos_cthd_sleep()
XWOS API:当前线程睡眠一段时间
- 参数
-
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EINTR | 睡眠过程被中断 |
| -ETIMEDOUT | 输入的时间为负数 |
| -EDSPMPT | 抢占被关闭 |
| -EDSBH | 中断底半部被关闭 |
- 注解
-
调用此CAPI的线程会睡眠 dur ,线程会在 当前时间点 + dur 时被唤醒。
此CAPI等价于 xwos_cthd_sleep_to(xwtm_ft(dur)) 。
在文件 thd.h 第 827 行定义.
828{
829 return xwosdl_cthd_sleep_to(
xwtm_ft(dur));
830}
static xwtm_t xwtm_ft(xwtm_t dur)
XWOS API:获取当前CPU的未来 系统时间 点
◆ xwos_cthd_sleep_to()
XWOS API:线程睡眠到一个时间点
- 参数
-
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EINTR | 睡眠过程被中断 |
| -ETIMEDOUT | 输入的时间为负数 |
| -EDSPMPT | 抢占被关闭 |
| -EDSBH | 中断底半部被关闭 |
- 注解
-
调用此CAPI的线程,睡眠到未来的某个时间点 to 被唤醒:
to 通常是未来的时间,即 当前系统时间 + delta , 可以使用 xwtm_ft(delta) 表示;
- 如果
to 是过去的时间点,将直接返回 -ETIMEDOUT 。
在文件 thd.h 第 850 行定义.
851{
852 return xwosdl_cthd_sleep_to(to);
853}
◆ xwos_cthd_sleep_from()
XWOS API:线程从一个时间起点睡眠到另一个时间点
- 参数
-
| [in,out] | from | 指向缓冲区的指针,此缓冲区:
- (I) 输入时,作为时间起点
- (O) 输出时,返回线程被唤醒的时间(可作为下一次时间起点,形成精确的周期)
|
| [in] | dur | 期望被唤醒的时间增量(相对于时间原点) |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EINTR | 睡眠过程被中断 |
| -ETIMEDOUT | 时间点是过去 |
| -EDSPMPT | 抢占被关闭 |
| -EDSBH | 中断底半部被关闭 |
- 注解
-
调用此CAPI前,需要先确定一个时间起点,可以通过 xwtm_now() 获取当前的 系统时间 作为起点。通过指针 from 将时间起点的地址传递给函数, dur 表示唤醒时间为 *from + dur 。
当线程被唤醒时,*from + dur 会覆盖到指针 from 指向的地址中, 此时 *from 又可作为下次调用此CAPI的时间起点。 由此,循环调用,可以形成周期为 dur ,更为精确的周期性睡眠唤醒。
在文件 thd.h 第 879 行定义.
880{
881 return xwosdl_cthd_sleep_from(from, dur);
882}
◆ xwos_cthd_freeze()
| static xwer_t xwos_cthd_freeze |
( |
void |
| ) |
|
|
inlinestatic |
XWOS API:冻结当前线程
- 返回
- 错误码
- 返回值
-
- 注解
-
线程的冻结功能,主要用于两个目的:
- 系统进入低功耗前的准备工作;
- 线程从一个CPU迁移到另一个CPU之前的准备工作。
此CAPI由线程自己调用,冻结自身。但线程并不能随时冻结,必须满足下列条件之一:
可以通过 xwos_cthd_shld_frz() 来判断是否可以冻结。
在文件 thd.h 第 905 行定义.
906{
907 return xwosdl_cthd_freeze();
908}
◆ xwos_thd_set_data()
XWOS API:设置线程的本地数据指针
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [in] | pos | 数据存放位置的索引 |
| [in] | data | 数据指针 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EFAULT | 空指针 |
| -ECHRNG | 位置超出范围 |
- 注解
-
在文件 thd.h 第 924 行定义.
925{
926 return xwosdl_thd_set_data(&thdd.
thd->
osthd, thdd.
tik, pos, data);
927}
◆ xwos_thd_get_data()
XWOS API:获取线程的本地数据指针
- 参数
-
| [in] | thdd | 线程对象描述符 |
| [in] | pos | 数据存放位置的索引 |
| [out] | databuf | 指向缓冲区的指针,通过此缓冲区返回数据指针 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EFAULT | 空指针 |
| -ECHRNG | 位置超出范围 |
- 注解
-
在文件 thd.h 第 942 行定义.
943{
944 return xwosdl_thd_get_data(&thdd.
thd->
osthd, thdd.
tik, pos, databuf);
945}
◆ xwos_cthd_set_data()
| static xwer_t xwos_cthd_set_data |
( |
xwsq_t |
pos, |
|
|
void * |
data |
|
) |
| |
|
inlinestatic |
XWOS API:设置当前线程的本地数据指针
- 参数
-
| [in] | pos | 数据存放位置的索引 |
| [in] | data | 数据指针 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EFAULT | 空指针 |
| -ECHRNG | 位置超出范围 |
- 注解
-
在文件 thd.h 第 959 行定义.
960{
961 return xwosdl_cthd_set_data(pos, data);
962}
◆ xwos_cthd_get_data()
| static xwer_t xwos_cthd_get_data |
( |
xwsq_t |
pos, |
|
|
void ** |
databuf |
|
) |
| |
|
inlinestatic |
XWOS API:获取当前线程的本地数据指针
- 参数
-
| [in] | pos | 数据存放位置的索引 |
| [out] | databuf | 指向缓冲区的指针,通过此缓冲区返回数据指针 |
- 返回
- 错误码
- 返回值
-
| XWOK | 没有错误 |
| -EFAULT | 空指针 |
| -ECHRNG | 位置超出范围 |
- 注解
-
在文件 thd.h 第 976 行定义.
977{
978 return xwosdl_cthd_get_data(pos, databuf);
979
980}