pthread: General pre-cleanup (style, typos etc)
[dragonfly.git] / lib / libthread_xu / thread / thr_attr.c
1 /*
2  * Copyright (c) 2003 Craig Rodrigues <rodrigc@attbi.com>.
3  * Copyright (c) 2002,2003 Alexey Zelkin <phantom@FreeBSD.org>
4  * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.
5  * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
6  * Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice(s), this list of conditions and the following disclaimer
14  *    unmodified other than the allowable addition of one or more
15  *    copyright notices.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice(s), this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "namespace.h"
35 #include <machine/tls.h>
36 #include <errno.h>
37 #include <pthread.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <pthread_np.h>
41 #include "un-namespace.h"
42
43 #include "thr_private.h"
44
45 /* Default thread attributes. */
46 struct pthread_attr _pthread_attr_default = {
47         .sched_policy = SCHED_OTHER,
48         .sched_inherit = 0,
49         .prio = THR_DEFAULT_PRIORITY,
50         .suspend = THR_CREATE_RUNNING,
51         .flags = 0,
52         .stackaddr_attr = NULL,
53         .stacksize_attr = THR_STACK_DEFAULT,
54         .guardsize_attr = 0
55 };
56
57 int
58 _pthread_attr_destroy(pthread_attr_t *attr)
59 {
60         int     ret;
61
62         /* Check for invalid arguments: */
63         if (attr == NULL || *attr == NULL)
64                 /* Invalid argument: */
65                 ret = EINVAL;
66         else {
67                 /* Free the memory allocated to the attribute object: */
68                 free(*attr);
69
70                 /*
71                  * Leave the attribute pointer NULL now that the memory
72                  * has been freed:
73                  */
74                 *attr = NULL;
75                 ret = 0;
76         }
77         return(ret);
78 }
79
80 __strong_reference(_pthread_attr_destroy, pthread_attr_destroy);
81
82 int
83 _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst)
84 {
85         struct pthread *curthread;
86         struct pthread_attr attr;
87         int     ret;
88
89         if (pid == NULL || dst == NULL || *dst == NULL)
90                 return (EINVAL);
91
92         curthread = tls_get_curthread();
93         if ((ret = _thr_ref_add(curthread, pid, /*include dead*/0)) != 0)
94                 return (ret);
95         attr = pid->attr;
96         if (pid->tlflags & TLFLAGS_DETACHED)
97                 attr.flags |= PTHREAD_DETACHED;
98         _thr_ref_delete(curthread, pid);
99         memcpy(*dst, &attr, sizeof(struct pthread_attr));
100
101         return (0);
102 }
103
104 __strong_reference(_pthread_attr_get_np, pthread_attr_get_np);
105
106 int
107 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
108 {
109         int     ret;
110
111         /* Check for invalid arguments: */
112         if (attr == NULL || *attr == NULL || detachstate == NULL)
113                 ret = EINVAL;
114         else {
115                 /* Check if the detached flag is set: */
116                 if ((*attr)->flags & PTHREAD_DETACHED)
117                         /* Return detached: */
118                         *detachstate = PTHREAD_CREATE_DETACHED;
119                 else
120                         /* Return joinable: */
121                         *detachstate = PTHREAD_CREATE_JOINABLE;
122                 ret = 0;
123         }
124         return(ret);
125 }
126
127 __strong_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
128
129 int
130 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
131 {
132         int     ret;
133
134         /* Check for invalid arguments: */
135         if (attr == NULL || *attr == NULL || guardsize == NULL)
136                 ret = EINVAL;
137         else {
138                 /* Return the guard size: */
139                 *guardsize = (*attr)->guardsize_attr;
140                 ret = 0;
141         }
142         return(ret);
143 }
144
145 __strong_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
146
147 int
148 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
149 {
150         int ret = 0;
151
152         if ((attr == NULL) || (*attr == NULL))
153                 ret = EINVAL;
154         else
155                 *sched_inherit = (*attr)->sched_inherit;
156
157         return(ret);
158 }
159
160 __strong_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
161
162 int
163 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
164 {
165         int ret = 0;
166
167         if ((attr == NULL) || (*attr == NULL) || (param == NULL))
168                 ret = EINVAL;
169         else
170                 param->sched_priority = (*attr)->prio;
171
172         return(ret);
173 }
174
175 __strong_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
176
177 int
178 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
179 {
180         int ret = 0;
181
182         if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
183                 ret = EINVAL;
184         else
185                 *policy = (*attr)->sched_policy;
186
187         return(ret);
188 }
189
190 __strong_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
191
192 int
193 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
194 {
195         int ret = 0;
196
197         if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
198                 /* Return an invalid argument: */
199                 ret = EINVAL;
200
201         else
202                 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
203                     PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
204
205         return(ret);
206 }
207
208 __strong_reference(_pthread_attr_getscope, pthread_attr_getscope);
209
210 int
211 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
212                        void ** __restrict stackaddr,
213                        size_t * __restrict stacksize)
214 {
215         int     ret;
216
217         /* Check for invalid arguments: */
218         if (attr == NULL || *attr == NULL || stackaddr == NULL
219             || stacksize == NULL )
220                 ret = EINVAL;
221         else {
222                 /* Return the stack address and size */
223                 *stackaddr = (*attr)->stackaddr_attr;
224                 *stacksize = (*attr)->stacksize_attr;
225                 ret = 0;
226         }
227         return(ret);
228 }
229
230 __strong_reference(_pthread_attr_getstack, pthread_attr_getstack);
231
232 int
233 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
234 {
235         int     ret;
236
237         /* Check for invalid arguments: */
238         if (attr == NULL || *attr == NULL || stackaddr == NULL)
239                 ret = EINVAL;
240         else {
241                 /* Return the stack address: */
242                 *stackaddr = (*attr)->stackaddr_attr;
243                 ret = 0;
244         }
245         return(ret);
246 }
247
248 __strong_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
249
250 int
251 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
252 {
253         int     ret;
254
255         /* Check for invalid arguments: */
256         if (attr == NULL || *attr == NULL || stacksize  == NULL)
257                 ret = EINVAL;
258         else {
259                 /* Return the stack size: */
260                 *stacksize = (*attr)->stacksize_attr;
261                 ret = 0;
262         }
263         return(ret);
264 }
265
266 __strong_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
267
268 int
269 _pthread_attr_init(pthread_attr_t *attr)
270 {
271         int     ret;
272         pthread_attr_t  pattr;
273
274         /* Allocate memory for the attribute object: */
275         if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
276                 /* Insufficient memory: */
277                 ret = ENOMEM;
278         else {
279                 /* Initialise the attribute object with the defaults: */
280                 memcpy(pattr, &_pthread_attr_default,
281                     sizeof(struct pthread_attr));
282
283                 /* Return a pointer to the attribute object: */
284                 *attr = pattr;
285                 ret = 0;
286         }
287         return(ret);
288 }
289
290 __strong_reference(_pthread_attr_init, pthread_attr_init);
291
292 int
293 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
294 {
295         int     ret;
296
297         if (attr == NULL || *attr == NULL) {
298                 ret = EINVAL;
299         } else {
300                 (*attr)->suspend = THR_CREATE_SUSPENDED;
301                 ret = 0;
302         }
303         return(ret);
304 }
305
306 __strong_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
307
308 int
309 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
310 {
311         int     ret;
312
313         /* Check for invalid arguments: */
314         if (attr == NULL || *attr == NULL ||
315             (detachstate != PTHREAD_CREATE_DETACHED &&
316             detachstate != PTHREAD_CREATE_JOINABLE))
317                 ret = EINVAL;
318         else {
319                 /* Check if detached state: */
320                 if (detachstate == PTHREAD_CREATE_DETACHED)
321                         /* Set the detached flag: */
322                         (*attr)->flags |= PTHREAD_DETACHED;
323                 else
324                         /* Reset the detached flag: */
325                         (*attr)->flags &= ~PTHREAD_DETACHED;
326                 ret = 0;
327         }
328         return(ret);
329 }
330
331 __strong_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
332
333 int
334 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
335 {
336         int     ret;
337
338         /* Check for invalid arguments. */
339         if (attr == NULL || *attr == NULL)
340                 ret = EINVAL;
341         else {
342                 /* Save the stack size. */
343                 (*attr)->guardsize_attr = guardsize;
344                 ret = 0;
345         }
346         return(ret);
347 }
348
349 __strong_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
350
351 int
352 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
353 {
354         int ret = 0;
355
356         if ((attr == NULL) || (*attr == NULL))
357                 ret = EINVAL;
358         else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
359                  sched_inherit != PTHREAD_EXPLICIT_SCHED)
360                 ret = EINVAL;
361         else
362                 (*attr)->sched_inherit = sched_inherit;
363
364         return(ret);
365 }
366
367 __strong_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
368
369 int
370 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
371 {
372         int ret = 0;
373
374         if ((attr == NULL) || (*attr == NULL))
375                 ret = EINVAL;
376         else if (param == NULL) {
377                 ret = ENOTSUP;
378         } else {
379                 int minv = sched_get_priority_min((*attr)->sched_policy);
380                 int maxv = sched_get_priority_max((*attr)->sched_policy);
381                 if (minv == -1 || maxv == -1 ||
382                     param->sched_priority < minv ||
383                     param->sched_priority > maxv) {
384                         ret = ENOTSUP;
385                 } else {
386                         (*attr)->prio = param->sched_priority;
387                 }
388         }
389         return(ret);
390 }
391
392 __strong_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
393
394 int
395 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
396 {
397         int ret = 0;
398
399         if ((attr == NULL) || (*attr == NULL))
400                 ret = EINVAL;
401         else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
402                 ret = ENOTSUP;
403         } else
404                 (*attr)->sched_policy = policy;
405
406         return(ret);
407 }
408
409 __strong_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
410
411 int
412 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
413 {
414         int ret = 0;
415
416         if ((attr == NULL) || (*attr == NULL)) {
417                 /* Return an invalid argument: */
418                 ret = EINVAL;
419         } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
420             (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
421                 ret = EINVAL;
422         } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
423                 (*attr)->flags |= contentionscope;
424         } else {
425                 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
426         }
427         return (ret);
428 }
429
430 __strong_reference(_pthread_attr_setscope, pthread_attr_setscope);
431
432 int
433 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
434                        size_t stacksize)
435 {
436         int     ret;
437
438         /* Check for invalid arguments: */
439         if (attr == NULL || *attr == NULL || stackaddr == NULL
440             || stacksize < PTHREAD_STACK_MIN)
441                 ret = EINVAL;
442         else {
443                 /* Save the stack address and stack size */
444                 (*attr)->stackaddr_attr = stackaddr;
445                 (*attr)->stacksize_attr = stacksize;
446                 ret = 0;
447         }
448         return(ret);
449 }
450
451 __strong_reference(_pthread_attr_setstack, pthread_attr_setstack);
452
453 int
454 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
455 {
456         int     ret;
457
458         /* Check for invalid arguments: */
459         if (attr == NULL || *attr == NULL || stackaddr == NULL)
460                 ret = EINVAL;
461         else {
462                 /* Save the stack address: */
463                 (*attr)->stackaddr_attr = stackaddr;
464                 ret = 0;
465         }
466         return(ret);
467 }
468
469 __strong_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
470
471 int
472 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
473 {
474         int     ret;
475
476         /* Check for invalid arguments: */
477         if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
478                 ret = EINVAL;
479         else {
480                 /* Save the stack size: */
481                 (*attr)->stacksize_attr = stacksize;
482                 ret = 0;
483         }
484         return(ret);
485 }
486
487 __strong_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);