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
35
pthread_attr_init(pthread_attr_t * attr)36 int 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;
51
52 #ifdef LOSCFG_KERNEL_SMP
53 attr->cpuset.__bits[0] = 0;
54 #endif
55
56 return ENOERR;
57 }
58
pthread_attr_destroy(pthread_attr_t * attr)59 int 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_attr_setdetachstate(pthread_attr_t * attr,int detachState)69 int 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
pthread_attr_getdetachstate(const pthread_attr_t * attr,int * detachState)79 int 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_attr_setscope(pthread_attr_t * attr,int scope)90 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
91 {
92 if (attr == NULL) {
93 return EINVAL;
94 }
95
96 if (scope == PTHREAD_SCOPE_PROCESS) {
97 attr->scope = (unsigned int)scope;
98 return ENOERR;
99 }
100
101 if (scope == PTHREAD_SCOPE_SYSTEM) {
102 return ENOTSUP;
103 }
104
105 return EINVAL;
106 }
107
pthread_attr_getscope(const pthread_attr_t * attr,int * scope)108 int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
109 {
110 if ((attr == NULL) || (scope == NULL)) {
111 return EINVAL;
112 }
113
114 *scope = (int)attr->scope;
115
116 return ENOERR;
117 }
118
pthread_attr_setinheritsched(pthread_attr_t * attr,int inherit)119 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
120 {
121 if ((attr != NULL) && ((inherit == PTHREAD_INHERIT_SCHED) || (inherit == PTHREAD_EXPLICIT_SCHED))) {
122 attr->inheritsched = (UINT32)inherit;
123 return ENOERR;
124 }
125
126 return EINVAL;
127 }
128
pthread_attr_getinheritsched(const pthread_attr_t * attr,int * inherit)129 int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
130 {
131 if ((attr == NULL) || (inherit == NULL)) {
132 return EINVAL;
133 }
134
135 *inherit = (int)attr->inheritsched;
136
137 return ENOERR;
138 }
139
pthread_attr_setschedpolicy(pthread_attr_t * attr,int policy)140 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
141 {
142 if ((attr != NULL) && (policy == SCHED_RR)) {
143 attr->schedpolicy = SCHED_RR;
144 return ENOERR;
145 }
146
147 return EINVAL;
148 }
149
pthread_attr_getschedpolicy(const pthread_attr_t * attr,int * policy)150 int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
151 {
152 if ((attr == NULL) || (policy == NULL)) {
153 return EINVAL;
154 }
155
156 *policy = (int)attr->schedpolicy;
157
158 return ENOERR;
159 }
160
pthread_attr_setschedparam(pthread_attr_t * attr,const struct sched_param * param)161 int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
162 {
163 if ((attr == NULL) || (param == NULL)) {
164 return EINVAL;
165 } else if ((param->sched_priority < 0) || (param->sched_priority > OS_TASK_PRIORITY_LOWEST)) {
166 return ENOTSUP;
167 }
168
169 attr->schedparam = *param;
170
171 return ENOERR;
172 }
173
pthread_attr_getschedparam(const pthread_attr_t * attr,struct sched_param * param)174 int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
175 {
176 if ((attr == NULL) || (param == NULL)) {
177 return EINVAL;
178 }
179
180 *param = attr->schedparam;
181
182 return ENOERR;
183 }
184
185 /*
186 * Set starting address of stack. Whether this is at the start or end of
187 * the memory block allocated for the stack depends on whether the stack
188 * grows up or down.
189 */
pthread_attr_setstackaddr(pthread_attr_t * attr,void * stackAddr)190 int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackAddr)
191 {
192 if (attr == NULL) {
193 return EINVAL;
194 }
195
196 attr->stackaddr_set = 1;
197 attr->stackaddr = stackAddr;
198
199 return ENOERR;
200 }
201
pthread_attr_getstackaddr(const pthread_attr_t * attr,void ** stackAddr)202 int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackAddr)
203 {
204 if (((attr != NULL) && (stackAddr != NULL)) && attr->stackaddr_set) {
205 *stackAddr = attr->stackaddr;
206 return ENOERR;
207 }
208
209 return EINVAL; /* Stack address not set, return EINVAL. */
210 }
211
pthread_attr_setstacksize(pthread_attr_t * attr,size_t stackSize)212 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stackSize)
213 {
214 /* Reject inadequate stack sizes */
215 if ((attr == NULL) || (stackSize < PTHREAD_STACK_MIN)) {
216 return EINVAL;
217 }
218
219 attr->stacksize_set = 1;
220 attr->stacksize = stackSize;
221
222 return ENOERR;
223 }
224
pthread_attr_getstacksize(const pthread_attr_t * attr,size_t * stackSize)225 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stackSize)
226 {
227 /* Reject attempts to get a stack size when one has not been set. */
228 if ((attr == NULL) || (stackSize == NULL) || (!attr->stacksize_set)) {
229 return EINVAL;
230 }
231
232 *stackSize = attr->stacksize;
233
234 return ENOERR;
235 }
236
237 /*
238 * Set the cpu affinity mask
239 */
pthread_attr_setaffinity_np(pthread_attr_t * attr,size_t cpusetsize,const cpu_set_t * cpuset)240 int pthread_attr_setaffinity_np(pthread_attr_t* attr, size_t cpusetsize, const cpu_set_t* cpuset)
241 {
242 #ifdef LOSCFG_KERNEL_SMP
243 if (attr == NULL) {
244 return EINVAL;
245 }
246
247 if ((cpuset == NULL) || (cpusetsize == 0)) {
248 attr->cpuset.__bits[0] = 0;
249 return ENOERR;
250 }
251
252 if ((cpusetsize != sizeof(cpu_set_t)) || (cpuset->__bits[0] > LOSCFG_KERNEL_CPU_MASK)) {
253 return EINVAL;
254 }
255
256 attr->cpuset = *cpuset;
257 #endif
258
259 return ENOERR;
260 }
261
262 /*
263 * Get the cpu affinity mask
264 */
pthread_attr_getaffinity_np(const pthread_attr_t * attr,size_t cpusetsize,cpu_set_t * cpuset)265 int pthread_attr_getaffinity_np(const pthread_attr_t* attr, size_t cpusetsize, cpu_set_t* cpuset)
266 {
267 #ifdef LOSCFG_KERNEL_SMP
268 if ((attr == NULL) || (cpuset == NULL) || (cpusetsize != sizeof(cpu_set_t))) {
269 return EINVAL;
270 }
271
272 *cpuset = attr->cpuset;
273 #endif
274
275 return ENOERR;
276 }
277
278