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>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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
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>
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,
52 .prio = THR_DEFAULT_PRIORITY,
53 .suspend = THR_CREATE_RUNNING,
55 .stackaddr_attr = NULL,
56 .stacksize_attr = THR_STACK_DEFAULT,
61 _pthread_attr_destroy(pthread_attr_t *attr)
65 /* Check for invalid arguments: */
66 if (attr == NULL || *attr == NULL)
67 /* Invalid argument: */
70 /* Free the memory allocated to the attribute object: */
74 * Leave the attribute pointer NULL now that the memory
83 __strong_reference(_pthread_attr_destroy, pthread_attr_destroy);
86 _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst)
88 struct pthread *curthread;
89 struct pthread_attr attr;
92 if (pid == NULL || dst == NULL || *dst == NULL)
95 curthread = tls_get_curthread();
96 if ((ret = _thr_ref_add(curthread, pid, /*include dead*/0)) != 0)
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));
107 __strong_reference(_pthread_attr_get_np, pthread_attr_get_np);
110 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
114 /* Check for invalid arguments: */
115 if (attr == NULL || *attr == NULL || detachstate == NULL)
118 /* Check if the detached flag is set: */
119 if ((*attr)->flags & PTHREAD_DETACHED)
120 /* Return detached: */
121 *detachstate = PTHREAD_CREATE_DETACHED;
123 /* Return joinable: */
124 *detachstate = PTHREAD_CREATE_JOINABLE;
130 __strong_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
133 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
137 /* Check for invalid arguments: */
138 if (attr == NULL || *attr == NULL || guardsize == NULL)
141 /* Return the guard size: */
142 *guardsize = (*attr)->guardsize_attr;
148 __strong_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
151 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
155 if ((attr == NULL) || (*attr == NULL))
158 *sched_inherit = (*attr)->sched_inherit;
163 __strong_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
166 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
170 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
173 param->sched_priority = (*attr)->prio;
178 __strong_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
181 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
185 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
188 *policy = (*attr)->sched_policy;
193 __strong_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
196 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
200 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
201 /* Return an invalid argument: */
205 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
206 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
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)
220 /* Check for invalid arguments: */
221 if (attr == NULL || *attr == NULL || stackaddr == NULL
222 || stacksize == NULL )
225 /* Return the stack address and size */
226 *stackaddr = (*attr)->stackaddr_attr;
227 *stacksize = (*attr)->stacksize_attr;
233 __strong_reference(_pthread_attr_getstack, pthread_attr_getstack);
236 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
240 /* Check for invalid arguments: */
241 if (attr == NULL || *attr == NULL || stackaddr == NULL)
244 /* Return the stack address: */
245 *stackaddr = (*attr)->stackaddr_attr;
251 __strong_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
254 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
258 /* Check for invalid arguments: */
259 if (attr == NULL || *attr == NULL || stacksize == NULL)
262 /* Return the stack size: */
263 *stacksize = (*attr)->stacksize_attr;
269 __strong_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
272 _pthread_attr_init(pthread_attr_t *attr)
275 pthread_attr_t pattr;
279 /* Allocate memory for the attribute object: */
280 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
281 /* Insufficient memory: */
284 /* Initialise the attribute object with the defaults: */
285 memcpy(pattr, &_pthread_attr_default,
286 sizeof(struct pthread_attr));
288 /* Return a pointer to the attribute object: */
295 __strong_reference(_pthread_attr_init, pthread_attr_init);
298 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
302 if (attr == NULL || *attr == NULL) {
305 (*attr)->suspend = THR_CREATE_SUSPENDED;
311 __strong_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
314 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
318 /* Check for invalid arguments: */
319 if (attr == NULL || *attr == NULL ||
320 (detachstate != PTHREAD_CREATE_DETACHED &&
321 detachstate != PTHREAD_CREATE_JOINABLE))
324 /* Check if detached state: */
325 if (detachstate == PTHREAD_CREATE_DETACHED)
326 /* Set the detached flag: */
327 (*attr)->flags |= PTHREAD_DETACHED;
329 /* Reset the detached flag: */
330 (*attr)->flags &= ~PTHREAD_DETACHED;
336 __strong_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
339 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
343 /* Check for invalid arguments. */
344 if (attr == NULL || *attr == NULL)
347 /* Save the stack size. */
348 (*attr)->guardsize_attr = guardsize;
354 __strong_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
357 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
361 if ((attr == NULL) || (*attr == NULL))
363 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
364 sched_inherit != PTHREAD_EXPLICIT_SCHED)
367 (*attr)->sched_inherit = sched_inherit;
372 __strong_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
375 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
379 if ((attr == NULL) || (*attr == NULL))
381 else if (param == NULL) {
384 int minv = sched_get_priority_min((*attr)->sched_policy);
385 int maxv = sched_get_priority_max((*attr)->sched_policy);
386 if (minv == -1 || maxv == -1 ||
387 param->sched_priority < minv ||
388 param->sched_priority > maxv) {
391 (*attr)->prio = param->sched_priority;
397 __strong_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
400 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
404 if ((attr == NULL) || (*attr == NULL))
406 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
409 (*attr)->sched_policy = policy;
414 __strong_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
417 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
421 if ((attr == NULL) || (*attr == NULL)) {
422 /* Return an invalid argument: */
424 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
425 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
427 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
428 (*attr)->flags |= contentionscope;
430 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
435 __strong_reference(_pthread_attr_setscope, pthread_attr_setscope);
438 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
443 /* Check for invalid arguments: */
444 if (attr == NULL || *attr == NULL || stackaddr == NULL
445 || stacksize < PTHREAD_STACK_MIN)
448 /* Save the stack address and stack size */
449 (*attr)->stackaddr_attr = stackaddr;
450 (*attr)->stacksize_attr = stacksize;
456 __strong_reference(_pthread_attr_setstack, pthread_attr_setstack);
459 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
463 /* Check for invalid arguments: */
464 if (attr == NULL || *attr == NULL || stackaddr == NULL)
467 /* Save the stack address: */
468 (*attr)->stackaddr_attr = stackaddr;
474 __strong_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
477 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
481 /* Check for invalid arguments: */
482 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
485 /* Save the stack size: */
486 (*attr)->stacksize_attr = stacksize;
492 __strong_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);