更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
pthread_attr.c 文件参考

浏览源代码.

函数

int pthread_attr_init (pthread_attr_t *attr)
 
int pthread_attr_destroy (pthread_attr_t *attr)
 
int pthread_attr_setdetachstate (pthread_attr_t *attr, int detachState)
 设置分离状态(分离和联合) 如果创建分离线程 (PTHREAD_CREATE_DETACHED),则该线程一退出,便可重用其线程 ID 和其他资源。 更多...
 
int pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachState)
 获取分离状态 更多...
 
int pthread_attr_setscope (pthread_attr_t *attr, int scope)
 设置线程的竞争范围(PTHREAD_SCOPE_SYSTEM 或 PTHREAD_SCOPE_PROCESS)。 更多...
 
int pthread_attr_getscope (const pthread_attr_t *attr, int *scope)
 获取线程的竞争范围 更多...
 
int pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit)
 设置继承的调度策略, PTHREAD_INHERIT_SCHED 表示新建的线程将继承创建者线程中定义的调度策略。 更多...
 
int pthread_attr_getinheritsched (const pthread_attr_t *attr, int *inherit)
 获取继承的调度策略 更多...
 
int pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy)
 设置调度策略,POSIX 标准指定 SCHED_FIFO(先入先出)、SCHED_RR(抢占)或 SCHED_OTHER(实现定义的方法)的调度策略属性。 更多...
 
int pthread_attr_getschedpolicy (const pthread_attr_t *attr, int *policy)
 获取调度策略 更多...
 
int pthread_attr_setschedparam (pthread_attr_t *attr, const struct sched_param *param)
 设置线程属性对象的调度参数属性,调度参数是在 param 结构中定义的。仅支持优先级参数。新创建的线程使用此优先级运行。 更多...
 
int pthread_attr_getschedparam (const pthread_attr_t *attr, struct sched_param *param)
 获取线程属性对象的调度参数属性 更多...
 
int pthread_attr_setstackaddr (pthread_attr_t *attr, void *stackAddr)
 
int pthread_attr_getstackaddr (const pthread_attr_t *attr, void **stackAddr)
 获取栈起始地址 更多...
 
int pthread_attr_setstacksize (pthread_attr_t *attr, size_t stackSize)
 设置栈大小 更多...
 
int pthread_attr_getstacksize (const pthread_attr_t *attr, size_t *stackSize)
 获取栈大小 更多...
 
int pthread_attr_setaffinity_np (pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset)
 
int pthread_attr_getaffinity_np (const pthread_attr_t *attr, size_t cpusetsize, cpu_set_t *cpuset)
 

函数说明

◆ pthread_attr_destroy()

int pthread_attr_destroy ( pthread_attr_t *  attr)

在文件 pthread_attr.c59 行定义.

60{
61 if (attr == NULL) {
62 return EINVAL;
63 }
64
65 /* Nothing to do here... */
66 return ENOERR;
67}

◆ pthread_attr_getaffinity_np()

int pthread_attr_getaffinity_np ( const pthread_attr_t *  attr,
size_t  cpusetsize,
cpu_set_t *  cpuset 
)

在文件 pthread_attr.c269 行定义.

270{
271#ifdef LOSCFG_KERNEL_SMP
272 if ((attr == NULL) || (cpuset == NULL) || (cpusetsize != sizeof(cpu_set_t))) {
273 return EINVAL;
274 }
275
276 *cpuset = attr->cpuset;
277#endif
278
279 return ENOERR;
280}

◆ pthread_attr_getdetachstate()

int pthread_attr_getdetachstate ( const pthread_attr_t *  attr,
int detachState 
)

获取分离状态

在文件 pthread_attr.c79 行定义.

80{
81 if ((attr == NULL) || (detachState == NULL)) {
82 return EINVAL;
83 }
84
85 *detachState = (int)attr->detachstate;
86
87 return ENOERR;
88}
ARG_NUM_3 int

◆ pthread_attr_getinheritsched()

int pthread_attr_getinheritsched ( const pthread_attr_t *  attr,
int inherit 
)

获取继承的调度策略

在文件 pthread_attr.c132 行定义.

133{
134 if ((attr == NULL) || (inherit == NULL)) {
135 return EINVAL;
136 }
137
138 *inherit = (int)attr->inheritsched;
139
140 return ENOERR;
141}

◆ pthread_attr_getschedparam()

int pthread_attr_getschedparam ( const pthread_attr_t *  attr,
struct sched_param *  param 
)

获取线程属性对象的调度参数属性

在文件 pthread_attr.c177 行定义.

178{
179 if ((attr == NULL) || (param == NULL)) {
180 return EINVAL;
181 }
182
183 *param = attr->schedparam;
184
185 return ENOERR;
186}

◆ pthread_attr_getschedpolicy()

int pthread_attr_getschedpolicy ( const pthread_attr_t *  attr,
int policy 
)

获取调度策略

在文件 pthread_attr.c153 行定义.

154{
155 if ((attr == NULL) || (policy == NULL)) {
156 return EINVAL;
157 }
158
159 *policy = (int)attr->schedpolicy;
160
161 return ENOERR;
162}

◆ pthread_attr_getscope()

int pthread_attr_getscope ( const pthread_attr_t *  attr,
int scope 
)

获取线程的竞争范围

在文件 pthread_attr.c110 行定义.

111{
112 if ((attr == NULL) || (scope == NULL)) {
113 return EINVAL;
114 }
115
116 *scope = (int)attr->scope;
117
118 return ENOERR;
119}

◆ pthread_attr_getstackaddr()

int pthread_attr_getstackaddr ( const pthread_attr_t *  attr,
void **  stackAddr 
)

获取栈起始地址

在文件 pthread_attr.c206 行定义.

207{
208 if (((attr != NULL) && (stackAddr != NULL)) && attr->stackaddr_set) {
209 *stackAddr = attr->stackaddr;
210 return ENOERR;
211 }
212
213 return EINVAL; /* Stack address not set, return EINVAL. */
214}

◆ pthread_attr_getstacksize()

int pthread_attr_getstacksize ( const pthread_attr_t *  attr,
size_t stackSize 
)

获取栈大小

在文件 pthread_attr.c229 行定义.

230{
231 /* Reject attempts to get a stack size when one has not been set. */
232 if ((attr == NULL) || (stackSize == NULL) || (!attr->stacksize_set)) {
233 return EINVAL;
234 }
235
236 *stackSize = attr->stacksize;
237
238 return ENOERR;
239}
macro EXC_SP_SET stackSize
Definition: asm.h:50

◆ pthread_attr_init()

int pthread_attr_init ( pthread_attr_t *  attr)

在文件 pthread_attr.c36 行定义.

37{
38 if (attr == NULL) {
39 return EINVAL;
40 }
41
42 attr->detachstate = PTHREAD_CREATE_JOINABLE;//
43 attr->schedpolicy = SCHED_RR;//抢占式调度
44 attr->schedparam.sched_priority = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO;//调度优先级
45 attr->inheritsched = PTHREAD_INHERIT_SCHED;//继承调度
46 attr->scope = PTHREAD_SCOPE_PROCESS;//进程范围
47 attr->stackaddr_set = 0; //暂未内核栈开始地址
48 attr->stackaddr = NULL;//内核栈地址
49 attr->stacksize_set = 1;//已设置内核栈大小
50 attr->stacksize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;//默认16K
51
52#ifdef LOSCFG_KERNEL_SMP
53 attr->cpuset.__bits[0] = 0;
54#endif
55
56 return ENOERR;
57}
这是这个函数的调用关系图:

◆ pthread_attr_setaffinity_np()

int pthread_attr_setaffinity_np ( pthread_attr_t *  attr,
size_t  cpusetsize,
const cpu_set_t *  cpuset 
)

cpu_set_t这个结构体的理解类似于select中的fd_set,可以理解为cpu集,也是通过约定好的宏来进行清除、设置以及判断

在文件 pthread_attr.c244 行定义.

245{
246#ifdef LOSCFG_KERNEL_SMP
247 if (attr == NULL) {
248 return EINVAL;
249 }
250
251 if ((cpuset == NULL) || (cpusetsize == 0)) {
252 attr->cpuset.__bits[0] = 0;
253 return ENOERR;
254 }
255///cpu_set_t这个结构体的理解类似于select中的fd_set,可以理解为cpu集,也是通过约定好的宏来进行清除、设置以及判断
256 if ((cpusetsize != sizeof(cpu_set_t)) || (cpuset->__bits[0] > LOSCFG_KERNEL_CPU_MASK)) {
257 return EINVAL;
258 }
259
260 attr->cpuset = *cpuset;
261#endif
262
263 return ENOERR;
264}

◆ pthread_attr_setdetachstate()

int pthread_attr_setdetachstate ( pthread_attr_t *  attr,
int  detachState 
)

设置分离状态(分离和联合) 如果创建分离线程 (PTHREAD_CREATE_DETACHED),则该线程一退出,便可重用其线程 ID 和其他资源。

在文件 pthread_attr.c69 行定义.

70{
71 if ((attr != NULL) && ((detachState == PTHREAD_CREATE_JOINABLE) || (detachState == PTHREAD_CREATE_DETACHED))) {
72 attr->detachstate = (UINT32)detachState;
73 return ENOERR;
74 }
75
76 return EINVAL;
77}
unsigned int UINT32
Definition: los_typedef.h:57

◆ pthread_attr_setinheritsched()

int pthread_attr_setinheritsched ( pthread_attr_t *  attr,
int  inherit 
)

设置继承的调度策略, PTHREAD_INHERIT_SCHED 表示新建的线程将继承创建者线程中定义的调度策略。

在文件 pthread_attr.c122 行定义.

123{
124 if ((attr != NULL) && ((inherit == PTHREAD_INHERIT_SCHED) || (inherit == PTHREAD_EXPLICIT_SCHED))) {
125 attr->inheritsched = (UINT32)inherit;
126 return ENOERR;
127 }
128
129 return EINVAL;
130}

◆ pthread_attr_setschedparam()

int pthread_attr_setschedparam ( pthread_attr_t *  attr,
const struct sched_param *  param 
)

设置线程属性对象的调度参数属性,调度参数是在 param 结构中定义的。仅支持优先级参数。新创建的线程使用此优先级运行。

在文件 pthread_attr.c164 行定义.

165{
166 if ((attr == NULL) || (param == NULL)) {
167 return EINVAL;
168 } else if ((param->sched_priority < 0) || (param->sched_priority > OS_TASK_PRIORITY_LOWEST)) {
169 return ENOTSUP;
170 }
171
172 attr->schedparam = *param;
173
174 return ENOERR;
175}

◆ pthread_attr_setschedpolicy()

int pthread_attr_setschedpolicy ( pthread_attr_t *  attr,
int  policy 
)

设置调度策略,POSIX 标准指定 SCHED_FIFO(先入先出)、SCHED_RR(抢占)或 SCHED_OTHER(实现定义的方法)的调度策略属性。

在文件 pthread_attr.c143 行定义.

144{
145 if ((attr != NULL) && (policy == SCHED_RR)) {
146 attr->schedpolicy = SCHED_RR;
147 return ENOERR;
148 }
149
150 return EINVAL;
151}

◆ pthread_attr_setscope()

int pthread_attr_setscope ( pthread_attr_t *  attr,
int  scope 
)

设置线程的竞争范围(PTHREAD_SCOPE_SYSTEM 或 PTHREAD_SCOPE_PROCESS)。

在文件 pthread_attr.c92 行定义.

93{
94 if (attr == NULL) {
95 return EINVAL;
96 }
97
98 if (scope == PTHREAD_SCOPE_PROCESS) {
99 attr->scope = (unsigned int)scope;
100 return ENOERR;
101 }
102
103 if (scope == PTHREAD_SCOPE_SYSTEM) {
104 return ENOTSUP;
105 }
106
107 return EINVAL;
108}

◆ pthread_attr_setstackaddr()

int pthread_attr_setstackaddr ( pthread_attr_t *  attr,
void stackAddr 
)

在文件 pthread_attr.c194 行定义.

195{
196 if (attr == NULL) {
197 return EINVAL;
198 }
199
200 attr->stackaddr_set = 1;
201 attr->stackaddr = stackAddr;
202
203 return ENOERR;
204}

◆ pthread_attr_setstacksize()

int pthread_attr_setstacksize ( pthread_attr_t *  attr,
size_t  stackSize 
)

设置栈大小

在文件 pthread_attr.c216 行定义.

217{
218 /* Reject inadequate stack sizes *///拒绝不合适的堆栈尺寸
219 if ((attr == NULL) || (stackSize < PTHREAD_STACK_MIN)) {//size 不应小于系统定义的最小栈大小
220 return EINVAL;
221 }
222
223 attr->stacksize_set = 1;
224 attr->stacksize = stackSize;
225
226 return ENOERR;
227}
这是这个函数的调用关系图: