XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
DThd.hxx
浏览该文件的文档.
1
13#ifndef __xwos_cxx_DThd_hxx__
14#define __xwos_cxx_DThd_hxx__
15
16#include <xwos/osal/thd.hxx>
17#include <xwos/cxx/Cpu.hxx>
18
19namespace xwos {
20
34template<xwid_t TCpu = 0>
35class DThd
36{
37 private:
41 public:
51 DThd(const char * name, xwsz_t stack_size,
52 xwsz_t stack_guard_size = XWOS_STACK_GUARD_SIZE_DEFAULT,
53 xwpr_t priority = XWOS_SKD_PRIORITY_RT_MIN, bool detached = false,
54 bool privileged = true)
55 : mThdDesc{ nullptr, 0 }
56 {
57 struct xwos_thd_attr attr({
58 .name = name,
59 .stack = (xwstk_t *)NULL,
60 .stack_size = stack_size,
61 .stack_guard_size = stack_guard_size,
62 .priority = priority,
63 .detached = detached,
64 .privileged = privileged,
65 });
66 mCtorRc = xwos_thd_create(&mThdDesc, &attr, nullptr, nullptr);
67 }
82 void launch()
83 {
84 xwer_t rc;
85
86 if (TCpu == Cpu::getLocalCpuId()) {
88 } else {
89 rc = -EHOSTUNREACH;
90 }
91 return rc;
92 }
104 {
105 return xwos_thd_get_stack_info(mThdDesc, stack);
106 }
119 {
120 return xwos_thd_join(mThdDesc, trc);
121 }
126 {
127 return xwos_thd_stop(mThdDesc, trc);
128 }
136 struct xwos_thd * getXwosObj() { return mThdDesc.thd; }
137
138 /* 生命周期管理 */
142 protected:
143 /* 线程主函数 */
147 virtual xwer_t thdMainFunction() { return XWOK; }
148
149 /* 只能在当前线程函数中调用的API */
153 void yield()
154 {
156 }
161 void exit(xwer_t rc)
162 {
163 xwos_cthd_exit(rc);
164 }
169 {
170 return xwos_cthd_shld_frz();
171 }
176 {
177 return xwos_cthd_shld_stop();
178 }
184 {
185 return xwos_cthd_sleep(dur);
186 }
192 {
193 return xwos_cthd_sleep_to(to);
194 }
203 {
204 return xwos_cthd_sleep_from(from, dur);
205 }
210 {
211 return xwos_cthd_freeze();
212 }
213
214 private:
215 static xwer_t sThdMainFunction(DThd * thd) { return thd->thdMainFunction(); }
216};
217
222} // namespace xwos
223
224#endif /* xwos/cxx/DThd.hxx */
xwos::Cpu
static xwid_t getLocalCpuId()
获取当前CPU的ID
Definition Cpu.hxx:64
动态线程
Definition DThd.hxx:36
xwos_thd_d mThdDesc
Definition DThd.hxx:38
xwer_t sleep(xwtm_t dur)
当前线程睡眠一段时间
Definition DThd.hxx:183
virtual xwer_t thdMainFunction()
线程主函数,用户需要重新实现此函数
Definition DThd.hxx:147
struct xwos_thd * getXwosObj()
获取XWOS对象指针
Definition DThd.hxx:136
xwer_t quit()
通知线程退出
Definition DThd.hxx:114
xwer_t sleepTo(xwtm_t to)
当前线程睡眠到一个时间点
Definition DThd.hxx:191
void launch()
加载线程
Definition DThd.hxx:82
static xwer_t sThdMainFunction(DThd *thd)
Definition DThd.hxx:215
xwer_t getStackInfo(struct xwos_thd_stack_info *stack)
获取当前线程的栈信息
Definition DThd.hxx:103
xwer_t getDThdCtorRc()
获取动态线程构造的结果
Definition DThd.hxx:98
virtual ~DThd()
动态线程析构函数
Definition DThd.hxx:71
bool shouldFreeze()
判断当前线程是否可被冻结
Definition DThd.hxx:168
xwer_t join(xwer_t *trc)
等待线程结束并获取它的返回值
Definition DThd.hxx:118
xwer_t release()
Definition DThd.hxx:140
xwer_t mCtorRc
Definition DThd.hxx:39
void exit(xwer_t rc)
退出当前线程
Definition DThd.hxx:161
xwer_t freeze(void)
冻结当前线程
Definition DThd.hxx:209
xwer_t acquire()
Definition DThd.hxx:139
xwer_t intr()
中断线程的阻塞态和睡眠态
Definition DThd.hxx:110
xwer_t stop(xwer_t *trc)
通知线程退出,等待线程结束并获取它的返回值
Definition DThd.hxx:125
xwer_t detach()
终止线程并等待它退出
Definition DThd.hxx:132
void yield()
当前线程通知调度器重新调度
Definition DThd.hxx:153
xwer_t sleepFrom(xwtm_t *from, xwtm_t dur)
当前线程从一个时间起点睡眠到另一个时间点
Definition DThd.hxx:202
bool shouldStop()
判断当前线程是否可以退出
Definition DThd.hxx:175
DThd(const char *name, 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 DThd.hxx:51
#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
#define NULL
Definition type.h:28
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_release(xwos_thd_d thdd)
XWOS API:检查对象的标签并减少引用计数
Definition thd.h:403
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_cthd_sleep_from(xwtm_t *from, xwtm_t dur)
XWOS API:线程从一个时间起点睡眠到另一个时间点
Definition thd.h:879
static xwer_t xwos_thd_acquire(xwos_thd_d thdd)
XWOS API:检查线程对象的标签并增加引用计数
Definition thd.h:387
#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_create(xwos_thd_d *thdd, const struct xwos_thd_attr *attr, xwos_thd_f thdfunc, void *arg)
XWOS API:使用动态申请内存方式创建线程并初始化
Definition thd.h:366
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_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++操作系统抽象层:线程