XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
SThd.hxx
浏览该文件的文档.
1
13#ifndef __xwos_cxx_SThd_hxx__
14#define __xwos_cxx_SThd_hxx__
15
16#include <xwos/osal/thd.hxx>
17#include <xwos/cxx/Cpu.hxx>
18
19namespace xwos {
20
38template<xwid_t TCpu = 0UL>
39class SThd
40{
41 private:
43 struct xwos_thd mThd;
45 public:
56 SThd(const char * name, xwstk_t * stack, xwsz_t stack_size,
57 xwsz_t stack_guard_size = XWOS_STACK_GUARD_SIZE_DEFAULT,
58 xwpr_t priority = XWOS_SKD_PRIORITY_RT_MIN, bool detached = false,
59 bool privileged = true)
60 : mThdDesc{ nullptr, 0 }
61 {
62 struct xwos_thd_attr attr({
63 .name = name,
64 .stack = stack,
65 .stack_size = stack_size,
66 .stack_guard_size = stack_guard_size,
67 .priority = priority,
68 .detached = detached,
69 .privileged = privileged,
70 });
71 xwos_thd_init(&mThd, &mThdDesc, &attr, nullptr, nullptr);
72 }
88 {
89 xwer_t rc;
90
91 if (TCpu == Cpu::getLocalCpuId()) {
93 } else {
94 rc = -EHOSTUNREACH;
95 }
96 return rc;
97 }
103 {
104 return xwos_thd_get_stack_info(mThdDesc, stack);
105 }
118 {
119 return xwos_thd_join(mThdDesc, trc);
120 }
125 {
126 return xwos_thd_stop(mThdDesc, trc);
127 }
135 struct xwos_thd * getXwosObj() { return mThdDesc.thd; }
136
137 /* 生命周期管理 */
141 protected:
142 /* 线程主函数 */
146 virtual xwer_t thdMainFunction() { return XWOK; }
147
148 /* 只能在当前线程函数中调用的API */
152 void yield()
153 {
155 }
160 void exit(xwer_t rc)
161 {
162 xwos_cthd_exit(rc);
163 }
168 {
169 return xwos_cthd_shld_frz();
170 }
175 {
176 return xwos_cthd_shld_stop();
177 }
183 {
184 return xwos_cthd_sleep(dur);
185 }
191 {
192 return xwos_cthd_sleep_to(to);
193 }
202 {
203 return xwos_cthd_sleep_from(from, dur);
204 }
209 {
210 return xwos_cthd_freeze();
211 }
212
213 private:
214 static xwer_t sThdMainFunction(SThd * thd) { return thd->thdMainFunction(); }
215};
216
221} // namespace xwos
222
223#endif /* xwos/cxx/SThd.hxx */
xwos::Cpu
static xwid_t getLocalCpuId()
获取当前CPU的ID
Definition Cpu.hxx:64
静态线程
Definition SThd.hxx:40
struct xwos_thd * getXwosObj()
获取XWOS对象指针
Definition SThd.hxx:135
xwer_t freeze(void)
冻结当前线程
Definition SThd.hxx:208
bool shouldFreeze()
判断当前线程是否可被冻结
Definition SThd.hxx:167
struct xwos_thd mThd
Definition SThd.hxx:43
static xwer_t sThdMainFunction(SThd *thd)
Definition SThd.hxx:214
xwer_t intr()
中断线程的阻塞态和睡眠态
Definition SThd.hxx:109
xwer_t getStackInfo(struct xwos_thd_stack_info *stack)
获取当前线程的栈信息
Definition SThd.hxx:102
xwer_t grab()
Definition SThd.hxx:138
virtual xwer_t thdMainFunction()
线程主函数,用户需要重新实现此函数
Definition SThd.hxx:146
xwer_t launch()
加载线程
Definition SThd.hxx:87
void exit(xwer_t rc)
退出当前线程
Definition SThd.hxx:160
xwer_t sleepTo(xwtm_t to)
当前线程睡眠到一个时间点
Definition SThd.hxx:190
xwer_t sleepFrom(xwtm_t *from, xwtm_t dur)
当前线程从一个时间起点睡眠到另一个时间点
Definition SThd.hxx:201
xwer_t sleep(xwtm_t dur)
当前线程睡眠一段时间
Definition SThd.hxx:182
xwos_thd_d mThdDesc
Definition SThd.hxx:42
virtual ~SThd()
静态线程析构函数
Definition SThd.hxx:76
xwer_t put()
Definition SThd.hxx:139
xwer_t quit()
通知线程退出
Definition SThd.hxx:113
SThd(const char *name, xwstk_t *stack, xwsz_t stack_size, xwsz_t stack_guard_size=XWOS_STACK_GUARD_SIZE_DEFAULT, xwpr_t priority=XWOS_SKD_PRIORITY_RT_MIN, bool detached=false, bool privileged=true)
静态线程构造函数
Definition SThd.hxx:56
bool shouldStop()
判断当前线程是否可以退出
Definition SThd.hxx:174
void yield()
当前线程通知调度器重新调度
Definition SThd.hxx:152
xwer_t join(xwer_t *trc)
等待线程结束并获取它的返回值
Definition SThd.hxx:117
xwer_t stop(xwer_t *trc)
通知线程退出,等待线程结束并获取它的返回值
Definition SThd.hxx:124
xwer_t detach()
终止线程并等待它退出
Definition SThd.hxx:131
#define EHOSTUNREACH
Host is unreachable
Definition errno.h:131
#define XWOK
No error
Definition errno.h:182
xws64_t xwtm_t
XWOS系统时间 (有符号)
Definition type.h:742
signed long xwer_t
Definition type.h:554
xws32_t xwpr_t
Definition type.h:534
xwptr_t xwstk_t
Definition type.h:392
unsigned long xwsz_t
Definition type.h:339
#define XWOS_SKD_PRIORITY_RT_MIN
XWOS API:最小实时优先级
Definition skd.h:91
static xwer_t xwos_cthd_sleep_to(xwtm_t to)
XWOS API:线程睡眠到一个时间点
Definition thd.h:850
static xwer_t xwos_thd_grab(xwos_thd_d thdd)
XWOS API:增加线程对象的引用计数
Definition thd.h:318
static xwer_t xwos_thd_stop(xwos_thd_d thdd, xwer_t *trc)
XWOS API:终止线程并等待它退出,回收线程内存资源,并抛出线程的返回值
Definition thd.h:600
static bool xwos_cthd_shld_stop(void)
XWOS API:判断当前线程是否可以退出
Definition thd.h:767
static xwer_t xwos_thd_intr(xwos_thd_d thdd)
XWOS API:中断线程的阻塞态和睡眠态
Definition thd.h:511
static xwer_t xwos_thd_put(xwos_thd_d thdd)
XWOS API:减少线程对象的引用计数
Definition thd.h:337
static xwer_t xwos_cthd_sleep_from(xwtm_t *from, xwtm_t dur)
XWOS API:线程从一个时间起点睡眠到另一个时间点
Definition thd.h:879
#define XWOS_STACK_GUARD_SIZE_DEFAULT
XWOS API:默认的栈警戒线
Definition thd.h:218
static xwer_t xwos_cthd_freeze(void)
XWOS API:冻结当前线程
Definition thd.h:905
xwer_t(* xwos_thd_f)(void *)
XWOS API:线程函数指针类型
Definition thd.h:153
static xwer_t xwos_thd_get_stack_info(xwos_thd_d thdd, struct xwos_thd_stack_info *stack)
XWOS API:获取线程的栈信息
Definition thd.h:460
static bool xwos_cthd_shld_frz(void)
XWOS API:判断当前线程是否可被冻结
Definition thd.h:741
static xwer_t xwos_thd_detach(xwos_thd_d thdd)
XWMP API:分离线程
Definition thd.h:622
static xwer_t xwos_thd_quit(xwos_thd_d thdd)
XWOS API:通知线程退出
Definition thd.h:542
static void xwos_cthd_yield(void)
XWOS API:当前线程通知调度器重新调度
Definition thd.h:712
static void xwos_cthd_exit(xwer_t rc)
XWOS API:退出当前线程
Definition thd.h:727
static xwer_t xwos_cthd_sleep(xwtm_t dur)
XWOS API:当前线程睡眠一段时间
Definition thd.h:827
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:静态方式初始化线程
Definition thd.h:294
static xwer_t xwos_thd_join(xwos_thd_d thdd, xwer_t *trc)
XWOS API:等待线程结束,回收线程内存资源,抛出它的返回值
Definition thd.h:577
static xwer_t xwos_thd_launch(xwos_thd_d thdd, xwos_thd_f thdfunc, void *arg)
XWOS API:加载线程
Definition thd.h:422
Definition Bmp.hxx:21
XWOS API:线程属性
Definition thd.h:171
XWOS API:线程对象描述符
Definition thd.h:158
struct xwos_thd * thd
Definition thd.h:159
XWOS API:线程栈信息
Definition thd.h:189
XWOS API:线程对象
Definition thd.h:146
C++操作系统抽象层:线程