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.
34 #include "namespace.h"
35 #include <machine/tls.h>
40 #include <pthread_np.h>
41 #include "un-namespace.h"
43 #include "thr_private.h"
45 /* Default thread attributes. */
46 struct pthread_attr _pthread_attr_default = {
47 .sched_policy = SCHED_OTHER,
49 .prio = THR_DEFAULT_PRIORITY,
50 .suspend = THR_CREATE_RUNNING,
52 .stackaddr_attr = NULL,
53 .stacksize_attr = THR_STACK_DEFAULT,
58 _pthread_attr_destroy(pthread_attr_t *attr)
62 /* Check for invalid arguments: */
63 if (attr == NULL || *attr == NULL)
64 /* Invalid argument: */
67 /* Free the memory allocated to the attribute object: */
71 * Leave the attribute pointer NULL now that the memory
80 __strong_reference(_pthread_attr_destroy, pthread_attr_destroy);
83 _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst)
85 struct pthread *curthread;
86 struct pthread_attr attr;
89 if (pid == NULL || dst == NULL || *dst == NULL)
92 curthread = tls_get_curthread();
93 if ((ret = _thr_ref_add(curthread, pid, /*include dead*/0)) != 0)
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));
104 __strong_reference(_pthread_attr_get_np, pthread_attr_get_np);
107 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
111 /* Check for invalid arguments: */
112 if (attr == NULL || *attr == NULL || detachstate == NULL)
115 /* Check if the detached flag is set: */
116 if ((*attr)->flags & PTHREAD_DETACHED)
117 /* Return detached: */
118 *detachstate = PTHREAD_CREATE_DETACHED;
120 /* Return joinable: */
121 *detachstate = PTHREAD_CREATE_JOINABLE;
127 __strong_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
130 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
134 /* Check for invalid arguments: */
135 if (attr == NULL || *attr == NULL || guardsize == NULL)
138 /* Return the guard size: */
139 *guardsize = (*attr)->guardsize_attr;
145 __strong_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
148 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
152 if ((attr == NULL) || (*attr == NULL))
155 *sched_inherit = (*attr)->sched_inherit;
160 __strong_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
163 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
167 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
170 param->sched_priority = (*attr)->prio;
175 __strong_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
178 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
182 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
185 *policy = (*attr)->sched_policy;
190 __strong_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
193 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
197 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
198 /* Return an invalid argument: */
202 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
203 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
208 __strong_reference(_pthread_attr_getscope, pthread_attr_getscope);
211 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
212 void ** __restrict stackaddr,
213 size_t * __restrict stacksize)
217 /* Check for invalid arguments: */
218 if (attr == NULL || *attr == NULL || stackaddr == NULL
219 || stacksize == NULL )
222 /* Return the stack address and size */
223 *stackaddr = (*attr)->stackaddr_attr;
224 *stacksize = (*attr)->stacksize_attr;
230 __strong_reference(_pthread_attr_getstack, pthread_attr_getstack);
233 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
237 /* Check for invalid arguments: */
238 if (attr == NULL || *attr == NULL || stackaddr == NULL)
241 /* Return the stack address: */
242 *stackaddr = (*attr)->stackaddr_attr;
248 __strong_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
251 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
255 /* Check for invalid arguments: */
256 if (attr == NULL || *attr == NULL || stacksize == NULL)
259 /* Return the stack size: */
260 *stacksize = (*attr)->stacksize_attr;
266 __strong_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
269 _pthread_attr_init(pthread_attr_t *attr)
272 pthread_attr_t pattr;
274 /* Allocate memory for the attribute object: */
275 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
276 /* Insufficient memory: */
279 /* Initialise the attribute object with the defaults: */
280 memcpy(pattr, &_pthread_attr_default,
281 sizeof(struct pthread_attr));
283 /* Return a pointer to the attribute object: */
290 __strong_reference(_pthread_attr_init, pthread_attr_init);
293 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
297 if (attr == NULL || *attr == NULL) {
300 (*attr)->suspend = THR_CREATE_SUSPENDED;
306 __strong_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
309 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
313 /* Check for invalid arguments: */
314 if (attr == NULL || *attr == NULL ||
315 (detachstate != PTHREAD_CREATE_DETACHED &&
316 detachstate != PTHREAD_CREATE_JOINABLE))
319 /* Check if detached state: */
320 if (detachstate == PTHREAD_CREATE_DETACHED)
321 /* Set the detached flag: */
322 (*attr)->flags |= PTHREAD_DETACHED;
324 /* Reset the detached flag: */
325 (*attr)->flags &= ~PTHREAD_DETACHED;
331 __strong_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
334 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
338 /* Check for invalid arguments. */
339 if (attr == NULL || *attr == NULL)
342 /* Save the stack size. */
343 (*attr)->guardsize_attr = guardsize;
349 __strong_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
352 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
356 if ((attr == NULL) || (*attr == NULL))
358 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
359 sched_inherit != PTHREAD_EXPLICIT_SCHED)
362 (*attr)->sched_inherit = sched_inherit;
367 __strong_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
370 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
374 if ((attr == NULL) || (*attr == NULL))
376 else if (param == NULL) {
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) {
386 (*attr)->prio = param->sched_priority;
392 __strong_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
395 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
399 if ((attr == NULL) || (*attr == NULL))
401 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
404 (*attr)->sched_policy = policy;
409 __strong_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
412 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
416 if ((attr == NULL) || (*attr == NULL)) {
417 /* Return an invalid argument: */
419 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
420 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
422 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
423 (*attr)->flags |= contentionscope;
425 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
430 __strong_reference(_pthread_attr_setscope, pthread_attr_setscope);
433 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
438 /* Check for invalid arguments: */
439 if (attr == NULL || *attr == NULL || stackaddr == NULL
440 || stacksize < PTHREAD_STACK_MIN)
443 /* Save the stack address and stack size */
444 (*attr)->stackaddr_attr = stackaddr;
445 (*attr)->stacksize_attr = stacksize;
451 __strong_reference(_pthread_attr_setstack, pthread_attr_setstack);
454 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
458 /* Check for invalid arguments: */
459 if (attr == NULL || *attr == NULL || stackaddr == NULL)
462 /* Save the stack address: */
463 (*attr)->stackaddr_attr = stackaddr;
469 __strong_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
472 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
476 /* Check for invalid arguments: */
477 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
480 /* Save the stack size: */
481 (*attr)->stacksize_attr = stacksize;
487 __strong_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);