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 <errno.h>
33 #include <securec.h>
34 #include <limits.h>
35 #include "pthread.h"
36 #include "los_config.h"
37 #include "los_debug.h"
38
pthread_attr_init(pthread_attr_t * attr)39 int pthread_attr_init(pthread_attr_t *attr)
40 {
41 if (attr == NULL) {
42 return EINVAL;
43 }
44
45 attr->detachstate = PTHREAD_CREATE_JOINABLE;
46 attr->schedpolicy = SCHED_RR;
47 attr->schedparam.sched_priority = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO;
48 attr->inheritsched = PTHREAD_INHERIT_SCHED;
49 attr->scope = PTHREAD_SCOPE_SYSTEM;
50 attr->stackaddr_set = 0;
51 attr->stackaddr = NULL;
52 attr->stacksize_set = 1;
53 attr->stacksize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
54
55 return 0;
56 }
57
pthread_attr_destroy(pthread_attr_t * attr)58 int pthread_attr_destroy(pthread_attr_t *attr)
59 {
60 if (attr == NULL) {
61 return EINVAL;
62 }
63
64 (void)memset_s(attr, sizeof(pthread_attr_t), 0, sizeof(pthread_attr_t));
65
66 return 0;
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 0;
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 0;
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_SYSTEM) {
97 attr->scope = (unsigned int)scope;
98 return 0;
99 }
100
101 if (scope == PTHREAD_SCOPE_PROCESS) {
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 0;
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 0;
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 0;
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 0;
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 0;
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 < LOS_TASK_PRIORITY_HIGHEST) ||
166 (param->sched_priority >= LOS_TASK_PRIORITY_LOWEST)) {
167 return ENOTSUP;
168 }
169
170 attr->schedparam = *param;
171
172 return 0;
173 }
174
pthread_attr_getschedparam(const pthread_attr_t * attr,struct sched_param * param)175 int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
176 {
177 if ((attr == NULL) || (param == NULL)) {
178 return EINVAL;
179 }
180
181 *param = attr->schedparam;
182
183 return 0;
184 }
185
186 /*
187 * Set starting address of stack. Whether this is at the start or end of
188 * the memory block allocated for the stack depends on whether the stack
189 * grows up or down.
190 */
pthread_attr_setstackaddr(pthread_attr_t * attr,void * stackAddr)191 int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackAddr)
192 {
193 if (attr == NULL) {
194 return EINVAL;
195 }
196
197 attr->stackaddr_set = 1;
198 attr->stackaddr = stackAddr;
199
200 return 0;
201 }
202
pthread_attr_getstackaddr(const pthread_attr_t * attr,void ** stackAddr)203 int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackAddr)
204 {
205 if (((attr != NULL) && (stackAddr != NULL)) && attr->stackaddr_set) {
206 *stackAddr = attr->stackaddr;
207 return 0;
208 }
209
210 return EINVAL; /* Stack address not set, return EINVAL. */
211 }
212
pthread_attr_setstacksize(pthread_attr_t * attr,size_t stackSize)213 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stackSize)
214 {
215 /* Reject inadequate stack sizes */
216 if ((attr == NULL) || (stackSize < PTHREAD_STACK_MIN)) {
217 return EINVAL;
218 }
219
220 attr->stacksize_set = 1;
221 attr->stacksize = stackSize;
222 return 0;
223 }
224
pthread_attr_setstack(pthread_attr_t * attr,void * stackAddr,size_t stackSize)225 int pthread_attr_setstack(pthread_attr_t *attr, void *stackAddr, size_t stackSize)
226 {
227 if ((attr == NULL) || (stackAddr == NULL) || (stackSize < PTHREAD_STACK_MIN)) {
228 return EINVAL;
229 }
230
231 attr->stacksize_set = 1;
232 attr->stacksize = stackSize;
233 attr->stackaddr_set = 1;
234 attr->stackaddr = stackAddr;
235 return 0;
236 }
237
pthread_attr_getstack(const pthread_attr_t * attr,void ** stackAddr,size_t * stackSize)238 int pthread_attr_getstack(const pthread_attr_t *attr, void **stackAddr, size_t *stackSize)
239 {
240 if ((attr == NULL) || (stackAddr == NULL) || (stackSize == NULL) ||
241 !attr->stacksize_set || !attr->stackaddr_set) {
242 return EINVAL;
243 }
244
245 *stackAddr = attr->stackaddr;
246 *stackSize = attr->stacksize;
247 return 0;
248 }
249
pthread_attr_getstacksize(const pthread_attr_t * attr,size_t * stackSize)250 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stackSize)
251 {
252 /* Reject attempts to get a stack size when one has not been set. */
253 if ((attr == NULL) || (stackSize == NULL) || (!attr->stacksize_set)) {
254 return EINVAL;
255 }
256
257 *stackSize = attr->stacksize;
258
259 return 0;
260 }
261
sched_get_priority_min(int policy)262 int sched_get_priority_min(int policy)
263 {
264 if (policy != SCHED_RR) {
265 errno = EINVAL;
266 return -1;
267 }
268
269 return LOS_TASK_PRIORITY_LOWEST;
270 }
271
sched_get_priority_max(int policy)272 int sched_get_priority_max(int policy)
273 {
274 if (policy != SCHED_RR) {
275 errno = EINVAL;
276 return -1;
277 }
278
279 return LOS_TASK_PRIORITY_HIGHEST;
280 }
281