更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
pthread_attr.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "pthread.h"
33#include "pprivate.h"
34//https://docs.oracle.com/cd/E19253-01/819-7051/6n919hpac/index.html
35//线程属性初始化
36int pthread_attr_init(pthread_attr_t *attr)
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}
58
59int pthread_attr_destroy(pthread_attr_t *attr)
60{
61 if (attr == NULL) {
62 return EINVAL;
63 }
64
65 /* Nothing to do here... */
66 return ENOERR;
67}
68///设置分离状态(分离和联合) 如果创建分离线程 (PTHREAD_CREATE_DETACHED),则该线程一退出,便可重用其线程 ID 和其他资源。
69int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachState)
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}
78///获取分离状态
79int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachState)
80{
81 if ((attr == NULL) || (detachState == NULL)) {
82 return EINVAL;
83 }
84
85 *detachState = (int)attr->detachstate;
86
87 return ENOERR;
88}
89///设置线程的竞争范围(PTHREAD_SCOPE_SYSTEM 或 PTHREAD_SCOPE_PROCESS)。
90//使用 PTHREAD_SCOPE_SYSTEM 时,此线程将与系统中的所有线程进行竞争。
91//使用 PTHREAD_SCOPE_PROCESS 时,此线程将与进程中的其他线程进行竞争。
92int pthread_attr_setscope(pthread_attr_t *attr, int scope)
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}
109///获取线程的竞争范围
110int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
111{
112 if ((attr == NULL) || (scope == NULL)) {
113 return EINVAL;
114 }
115
116 *scope = (int)attr->scope;
117
118 return ENOERR;
119}
120///设置继承的调度策略, PTHREAD_INHERIT_SCHED 表示新建的线程将继承创建者线程中定义的调度策略。
121//将忽略在 pthread_create() 调用中定义的所有调度属性。如果使用缺省值 PTHREAD_EXPLICIT_SCHED,则将使用 pthread_create() 调用中的属性
122int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
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}
131///获取继承的调度策略
132int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
133{
134 if ((attr == NULL) || (inherit == NULL)) {
135 return EINVAL;
136 }
137
138 *inherit = (int)attr->inheritsched;
139
140 return ENOERR;
141}
142///设置调度策略,POSIX 标准指定 SCHED_FIFO(先入先出)、SCHED_RR(抢占)或 SCHED_OTHER(实现定义的方法)的调度策略属性。
143int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
144{
145 if ((attr != NULL) && (policy == SCHED_RR)) {
146 attr->schedpolicy = SCHED_RR;
147 return ENOERR;
148 }
149
150 return EINVAL;
151}
152///获取调度策略
153int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
154{
155 if ((attr == NULL) || (policy == NULL)) {
156 return EINVAL;
157 }
158
159 *policy = (int)attr->schedpolicy;
160
161 return ENOERR;
162}
163///设置线程属性对象的调度参数属性,调度参数是在 param 结构中定义的。仅支持优先级参数。新创建的线程使用此优先级运行。
164int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
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}
176///获取线程属性对象的调度参数属性
177int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
178{
179 if ((attr == NULL) || (param == NULL)) {
180 return EINVAL;
181 }
182
183 *param = attr->schedparam;
184
185 return ENOERR;
186}
187
188/*
189 * Set starting address of stack. Whether this is at the start or end of
190 * the memory block allocated for the stack depends on whether the stack
191 * grows up or down.
192 */
193//设置栈起始地址,在开始还是结束为堆栈分配的内存块取决于堆栈是向上增长还是向下增长。
194int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackAddr)
195{
196 if (attr == NULL) {
197 return EINVAL;
198 }
199
200 attr->stackaddr_set = 1;
201 attr->stackaddr = stackAddr;
202
203 return ENOERR;
204}
205///获取栈起始地址
206int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackAddr)
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}
215///设置栈大小
216int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stackSize)
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}
228///获取栈大小
229int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stackSize)
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}
240
241/*
242 * Set the cpu affinity mask
243 *///cpu集可以认为是一个掩码,每个设置的位都对应一个可以合法调度的 cpu,而未设置的位则对应一个不可调度的 CPU。
244int pthread_attr_setaffinity_np(pthread_attr_t* attr, size_t cpusetsize, const cpu_set_t* cpuset)
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}
265
266/*
267 * Get the cpu affinity mask
268 */
269int pthread_attr_getaffinity_np(const pthread_attr_t* attr, size_t cpusetsize, cpu_set_t* cpuset)
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}
281
macro EXC_SP_SET stackSize
Definition: asm.h:50
unsigned int UINT32
Definition: los_typedef.h:57
int pthread_attr_init(pthread_attr_t *attr)
Definition: pthread_attr.c:36
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
获取继承的调度策略
Definition: pthread_attr.c:132
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stackSize)
获取栈大小
Definition: pthread_attr.c:229
int pthread_attr_getaffinity_np(const pthread_attr_t *attr, size_t cpusetsize, cpu_set_t *cpuset)
Definition: pthread_attr.c:269
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stackSize)
设置栈大小
Definition: pthread_attr.c:216
int pthread_attr_setscope(pthread_attr_t *attr, int scope)
设置线程的竞争范围(PTHREAD_SCOPE_SYSTEM 或 PTHREAD_SCOPE_PROCESS)。
Definition: pthread_attr.c:92
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
设置继承的调度策略, PTHREAD_INHERIT_SCHED 表示新建的线程将继承创建者线程中定义的调度策略。
Definition: pthread_attr.c:122
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachState)
获取分离状态
Definition: pthread_attr.c:79
int pthread_attr_destroy(pthread_attr_t *attr)
Definition: pthread_attr.c:59
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
设置线程属性对象的调度参数属性,调度参数是在 param 结构中定义的。仅支持优先级参数。新创建的线程使用此优先级运行。
Definition: pthread_attr.c:164
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackAddr)
Definition: pthread_attr.c:194
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
设置调度策略,POSIX 标准指定 SCHED_FIFO(先入先出)、SCHED_RR(抢占)或 SCHED_OTHER(实现定义的方法)的调度策略属性。
Definition: pthread_attr.c:143
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachState)
设置分离状态(分离和联合) 如果创建分离线程 (PTHREAD_CREATE_DETACHED),则该线程一退出,便可重用其线程 ID 和其他资源。
Definition: pthread_attr.c:69
int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackAddr)
获取栈起始地址
Definition: pthread_attr.c:206
int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
获取线程的竞争范围
Definition: pthread_attr.c:110
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
获取调度策略
Definition: pthread_attr.c:153
int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset)
Definition: pthread_attr.c:244
int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
获取线程属性对象的调度参数属性
Definition: pthread_attr.c:177
ARG_NUM_3 int