libthread_xu: Remove unnecessary calls to _thr_check_init()
[dragonfly.git] / lib / libthread_xu / thread / thr_attr.c
blobf5c3dc3387963bd60a177cd0474812d08dd28315
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.
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.
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.
33 * $DragonFly: src/lib/libthread_xu/thread/thr_attr.c,v 1.9 2008/07/15 01:18:53 dillon Exp $
36 #include "namespace.h"
37 #include <machine/tls.h>
39 #include <errno.h>
40 #include <pthread.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <pthread_np.h>
44 #include "un-namespace.h"
46 #include "thr_private.h"
48 /* Default thread attributes. */
49 struct pthread_attr _pthread_attr_default = {
50 .sched_policy = SCHED_OTHER,
51 .sched_inherit = 0,
52 .prio = THR_DEFAULT_PRIORITY,
53 .suspend = THR_CREATE_RUNNING,
54 .flags = 0,
55 .stackaddr_attr = NULL,
56 .stacksize_attr = THR_STACK_DEFAULT,
57 .guardsize_attr = 0
60 int
61 _pthread_attr_destroy(pthread_attr_t *attr)
63 int ret;
65 /* Check for invalid arguments: */
66 if (attr == NULL || *attr == NULL)
67 /* Invalid argument: */
68 ret = EINVAL;
69 else {
70 /* Free the memory allocated to the attribute object: */
71 free(*attr);
74 * Leave the attribute pointer NULL now that the memory
75 * has been freed:
77 *attr = NULL;
78 ret = 0;
80 return(ret);
83 __strong_reference(_pthread_attr_destroy, pthread_attr_destroy);
85 int
86 _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst)
88 struct pthread *curthread;
89 struct pthread_attr attr;
90 int ret;
92 if (pid == NULL || dst == NULL || *dst == NULL)
93 return (EINVAL);
95 curthread = tls_get_curthread();
96 if ((ret = _thr_ref_add(curthread, pid, /*include dead*/0)) != 0)
97 return (ret);
98 attr = pid->attr;
99 if (pid->tlflags & TLFLAGS_DETACHED)
100 attr.flags |= PTHREAD_DETACHED;
101 _thr_ref_delete(curthread, pid);
102 memcpy(*dst, &attr, sizeof(struct pthread_attr));
104 return (0);
107 __strong_reference(_pthread_attr_get_np, pthread_attr_get_np);
110 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
112 int ret;
114 /* Check for invalid arguments: */
115 if (attr == NULL || *attr == NULL || detachstate == NULL)
116 ret = EINVAL;
117 else {
118 /* Check if the detached flag is set: */
119 if ((*attr)->flags & PTHREAD_DETACHED)
120 /* Return detached: */
121 *detachstate = PTHREAD_CREATE_DETACHED;
122 else
123 /* Return joinable: */
124 *detachstate = PTHREAD_CREATE_JOINABLE;
125 ret = 0;
127 return(ret);
130 __strong_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
133 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
135 int ret;
137 /* Check for invalid arguments: */
138 if (attr == NULL || *attr == NULL || guardsize == NULL)
139 ret = EINVAL;
140 else {
141 /* Return the guard size: */
142 *guardsize = (*attr)->guardsize_attr;
143 ret = 0;
145 return(ret);
148 __strong_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
151 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
153 int ret = 0;
155 if ((attr == NULL) || (*attr == NULL))
156 ret = EINVAL;
157 else
158 *sched_inherit = (*attr)->sched_inherit;
160 return(ret);
163 __strong_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
166 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
168 int ret = 0;
170 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
171 ret = EINVAL;
172 else
173 param->sched_priority = (*attr)->prio;
175 return(ret);
178 __strong_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
181 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
183 int ret = 0;
185 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
186 ret = EINVAL;
187 else
188 *policy = (*attr)->sched_policy;
190 return(ret);
193 __strong_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
196 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
198 int ret = 0;
200 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
201 /* Return an invalid argument: */
202 ret = EINVAL;
204 else
205 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
206 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
208 return(ret);
211 __strong_reference(_pthread_attr_getscope, pthread_attr_getscope);
214 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
215 void ** __restrict stackaddr,
216 size_t * __restrict stacksize)
218 int ret;
220 /* Check for invalid arguments: */
221 if (attr == NULL || *attr == NULL || stackaddr == NULL
222 || stacksize == NULL )
223 ret = EINVAL;
224 else {
225 /* Return the stack address and size */
226 *stackaddr = (*attr)->stackaddr_attr;
227 *stacksize = (*attr)->stacksize_attr;
228 ret = 0;
230 return(ret);
233 __strong_reference(_pthread_attr_getstack, pthread_attr_getstack);
236 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
238 int ret;
240 /* Check for invalid arguments: */
241 if (attr == NULL || *attr == NULL || stackaddr == NULL)
242 ret = EINVAL;
243 else {
244 /* Return the stack address: */
245 *stackaddr = (*attr)->stackaddr_attr;
246 ret = 0;
248 return(ret);
251 __strong_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
254 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
256 int ret;
258 /* Check for invalid arguments: */
259 if (attr == NULL || *attr == NULL || stacksize == NULL)
260 ret = EINVAL;
261 else {
262 /* Return the stack size: */
263 *stacksize = (*attr)->stacksize_attr;
264 ret = 0;
266 return(ret);
269 __strong_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
272 _pthread_attr_init(pthread_attr_t *attr)
274 int ret;
275 pthread_attr_t pattr;
277 /* Allocate memory for the attribute object: */
278 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
279 /* Insufficient memory: */
280 ret = ENOMEM;
281 else {
282 /* Initialise the attribute object with the defaults: */
283 memcpy(pattr, &_pthread_attr_default,
284 sizeof(struct pthread_attr));
286 /* Return a pointer to the attribute object: */
287 *attr = pattr;
288 ret = 0;
290 return(ret);
293 __strong_reference(_pthread_attr_init, pthread_attr_init);
296 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
298 int ret;
300 if (attr == NULL || *attr == NULL) {
301 ret = EINVAL;
302 } else {
303 (*attr)->suspend = THR_CREATE_SUSPENDED;
304 ret = 0;
306 return(ret);
309 __strong_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
312 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
314 int ret;
316 /* Check for invalid arguments: */
317 if (attr == NULL || *attr == NULL ||
318 (detachstate != PTHREAD_CREATE_DETACHED &&
319 detachstate != PTHREAD_CREATE_JOINABLE))
320 ret = EINVAL;
321 else {
322 /* Check if detached state: */
323 if (detachstate == PTHREAD_CREATE_DETACHED)
324 /* Set the detached flag: */
325 (*attr)->flags |= PTHREAD_DETACHED;
326 else
327 /* Reset the detached flag: */
328 (*attr)->flags &= ~PTHREAD_DETACHED;
329 ret = 0;
331 return(ret);
334 __strong_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
337 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
339 int ret;
341 /* Check for invalid arguments. */
342 if (attr == NULL || *attr == NULL)
343 ret = EINVAL;
344 else {
345 /* Save the stack size. */
346 (*attr)->guardsize_attr = guardsize;
347 ret = 0;
349 return(ret);
352 __strong_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
355 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
357 int ret = 0;
359 if ((attr == NULL) || (*attr == NULL))
360 ret = EINVAL;
361 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
362 sched_inherit != PTHREAD_EXPLICIT_SCHED)
363 ret = ENOTSUP;
364 else
365 (*attr)->sched_inherit = sched_inherit;
367 return(ret);
370 __strong_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
373 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
375 int ret = 0;
377 if ((attr == NULL) || (*attr == NULL))
378 ret = EINVAL;
379 else if (param == NULL) {
380 ret = ENOTSUP;
381 } else {
382 int minv = sched_get_priority_min((*attr)->sched_policy);
383 int maxv = sched_get_priority_max((*attr)->sched_policy);
384 if (minv == -1 || maxv == -1 ||
385 param->sched_priority < minv ||
386 param->sched_priority > maxv) {
387 ret = ENOTSUP;
388 } else {
389 (*attr)->prio = param->sched_priority;
392 return(ret);
395 __strong_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
398 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
400 int ret = 0;
402 if ((attr == NULL) || (*attr == NULL))
403 ret = EINVAL;
404 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
405 ret = ENOTSUP;
406 } else
407 (*attr)->sched_policy = policy;
409 return(ret);
412 __strong_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
415 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
417 int ret = 0;
419 if ((attr == NULL) || (*attr == NULL)) {
420 /* Return an invalid argument: */
421 ret = EINVAL;
422 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
423 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
424 ret = EINVAL;
425 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
426 (*attr)->flags |= contentionscope;
427 } else {
428 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
430 return (ret);
433 __strong_reference(_pthread_attr_setscope, pthread_attr_setscope);
436 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
437 size_t stacksize)
439 int ret;
441 /* Check for invalid arguments: */
442 if (attr == NULL || *attr == NULL || stackaddr == NULL
443 || stacksize < PTHREAD_STACK_MIN)
444 ret = EINVAL;
445 else {
446 /* Save the stack address and stack size */
447 (*attr)->stackaddr_attr = stackaddr;
448 (*attr)->stacksize_attr = stacksize;
449 ret = 0;
451 return(ret);
454 __strong_reference(_pthread_attr_setstack, pthread_attr_setstack);
457 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
459 int ret;
461 /* Check for invalid arguments: */
462 if (attr == NULL || *attr == NULL || stackaddr == NULL)
463 ret = EINVAL;
464 else {
465 /* Save the stack address: */
466 (*attr)->stackaddr_attr = stackaddr;
467 ret = 0;
469 return(ret);
472 __strong_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
475 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
477 int ret;
479 /* Check for invalid arguments: */
480 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
481 ret = EINVAL;
482 else {
483 /* Save the stack size: */
484 (*attr)->stacksize_attr = stacksize;
485 ret = 0;
487 return(ret);
490 __strong_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);