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 <pthread.h>
34 #include <limits.h>
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 return 0;
53 }
54
pthread_attr_destroy(pthread_attr_t * attr)55 int pthread_attr_destroy(pthread_attr_t *attr)
56 {
57 if (attr == NULL) {
58 return EINVAL;
59 }
60
61 /* Nothing to do here... */
62 return 0;
63 }
64
pthread_attr_setdetachstate(pthread_attr_t * attr,int detachState)65 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachState)
66 {
67 if ((attr != NULL) && ((detachState == PTHREAD_CREATE_JOINABLE) || (detachState == PTHREAD_CREATE_DETACHED))) {
68 attr->detachstate = (UINT32)detachState;
69 return 0;
70 }
71
72 return EINVAL;
73 }
74
pthread_attr_getdetachstate(const pthread_attr_t * attr,int * detachState)75 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachState)
76 {
77 if ((attr == NULL) || (detachState == NULL)) {
78 return EINVAL;
79 }
80
81 *detachState = (int)attr->detachstate;
82
83 return 0;
84 }
85
pthread_attr_setscope(pthread_attr_t * attr,int scope)86 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
87 {
88 if (attr == NULL) {
89 return EINVAL;
90 }
91
92 if (scope == PTHREAD_SCOPE_PROCESS) {
93 attr->scope = (unsigned int)scope;
94 return 0;
95 }
96
97 if (scope == PTHREAD_SCOPE_SYSTEM) {
98 return ENOTSUP;
99 }
100
101 return EINVAL;
102 }
103
pthread_attr_getscope(const pthread_attr_t * attr,int * scope)104 int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
105 {
106 if ((attr == NULL) || (scope == NULL)) {
107 return EINVAL;
108 }
109
110 *scope = (int)attr->scope;
111
112 return 0;
113 }
114
pthread_attr_setinheritsched(pthread_attr_t * attr,int inherit)115 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
116 {
117 if ((attr != NULL) && ((inherit == PTHREAD_INHERIT_SCHED) || (inherit == PTHREAD_EXPLICIT_SCHED))) {
118 attr->inheritsched = (UINT32)inherit;
119 return 0;
120 }
121
122 return EINVAL;
123 }
124
pthread_attr_getinheritsched(const pthread_attr_t * attr,int * inherit)125 int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
126 {
127 if ((attr == NULL) || (inherit == NULL)) {
128 return EINVAL;
129 }
130
131 *inherit = (int)attr->inheritsched;
132
133 return 0;
134 }
135
pthread_attr_setschedpolicy(pthread_attr_t * attr,int policy)136 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
137 {
138 if ((attr != NULL) && (policy == SCHED_RR)) {
139 attr->schedpolicy = SCHED_RR;
140 return 0;
141 }
142
143 return EINVAL;
144 }
145
pthread_attr_getschedpolicy(const pthread_attr_t * attr,int * policy)146 int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
147 {
148 if ((attr == NULL) || (policy == NULL)) {
149 return EINVAL;
150 }
151
152 *policy = (int)attr->schedpolicy;
153
154 return 0;
155 }
156
pthread_attr_setschedparam(pthread_attr_t * attr,const struct sched_param * param)157 int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
158 {
159 if ((attr == NULL) || (param == NULL)) {
160 return EINVAL;
161 } else if ((param->sched_priority < OS_TASK_PRIORITY_HIGHEST) ||
162 (param->sched_priority >= OS_TASK_PRIORITY_LOWEST)) {
163 return ENOTSUP;
164 }
165
166 attr->schedparam = *param;
167
168 return 0;
169 }
170
pthread_attr_getschedparam(const pthread_attr_t * attr,struct sched_param * param)171 int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
172 {
173 if ((attr == NULL) || (param == NULL)) {
174 return EINVAL;
175 }
176
177 *param = attr->schedparam;
178
179 return 0;
180 }
181
182 /*
183 * Set starting address of stack. Whether this is at the start or end of
184 * the memory block allocated for the stack depends on whether the stack
185 * grows up or down.
186 */
pthread_attr_setstackaddr(pthread_attr_t * attr,void * stackAddr)187 int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackAddr)
188 {
189 if (attr == NULL) {
190 return EINVAL;
191 }
192
193 attr->stackaddr_set = 1;
194 attr->stackaddr = stackAddr;
195
196 return 0;
197 }
198
pthread_attr_getstackaddr(const pthread_attr_t * attr,void ** stackAddr)199 int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackAddr)
200 {
201 if (((attr != NULL) && (stackAddr != NULL)) && attr->stackaddr_set) {
202 *stackAddr = attr->stackaddr;
203 return 0;
204 }
205
206 return EINVAL; /* Stack address not set, return EINVAL. */
207 }
208
pthread_attr_setstacksize(pthread_attr_t * attr,size_t stackSize)209 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stackSize)
210 {
211 /* Reject inadequate stack sizes */
212 if ((attr == NULL) || (stackSize < PTHREAD_STACK_MIN)) {
213 return EINVAL;
214 }
215
216 attr->stacksize_set = 1;
217 attr->stacksize = stackSize;
218
219 return 0;
220 }
221
pthread_attr_getstacksize(const pthread_attr_t * attr,size_t * stackSize)222 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stackSize)
223 {
224 /* Reject attempts to get a stack size when one has not been set. */
225 if ((attr == NULL) || (stackSize == NULL) || (!attr->stacksize_set)) {
226 return EINVAL;
227 }
228
229 *stackSize = attr->stacksize;
230
231 return 0;
232 }
233