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 <sys/types.h>
36 #include <sys/sysctl.h>
37 #include <machine/tls.h>
42 #include <pthread_np.h>
43 #include "un-namespace.h"
45 #include "thr_private.h"
48 _pthread_attr_destroy(pthread_attr_t *attr)
52 /* Check for invalid arguments: */
53 if (attr == NULL || *attr == NULL)
54 /* Invalid argument: */
57 /* Free the memory allocated to the attribute object: */
61 * Leave the attribute pointer NULL now that the memory
70 __strong_reference(_pthread_attr_destroy, pthread_attr_destroy);
73 _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst)
75 struct pthread *curthread;
76 struct pthread_attr attr;
79 if (pid == NULL || dst == NULL || *dst == NULL)
82 curthread = tls_get_curthread();
83 if ((ret = _thr_ref_add(curthread, pid, /*include dead*/0)) != 0)
86 if (pid->tlflags & TLFLAGS_DETACHED)
87 attr.flags |= PTHREAD_DETACHED;
88 _thr_ref_delete(curthread, pid);
89 memcpy(*dst, &attr, sizeof(struct pthread_attr));
94 __strong_reference(_pthread_attr_get_np, pthread_attr_get_np);
97 _pthread_attr_getaffinity_np(const pthread_attr_t *attr, size_t cpusetsize,
100 const cpu_set_t *ret;
103 if (attr == NULL || *attr == NULL || mask == NULL)
106 if (((*attr)->flags & THR_CPUMASK) == 0) {
110 if (sysctlbyname("machdep.smp_active", &mask1, &len,
115 ret = &(*attr)->cpumask;
118 if (cpusetsize > sizeof(*ret)) {
119 memset(mask, 0, cpusetsize);
120 memcpy(mask, ret, sizeof(*ret));
122 memcpy(mask, ret, cpusetsize);
127 __strong_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np);
130 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
134 /* Check for invalid arguments: */
135 if (attr == NULL || *attr == NULL || detachstate == NULL)
138 /* Check if the detached flag is set: */
139 if ((*attr)->flags & PTHREAD_DETACHED)
140 /* Return detached: */
141 *detachstate = PTHREAD_CREATE_DETACHED;
143 /* Return joinable: */
144 *detachstate = PTHREAD_CREATE_JOINABLE;
150 __strong_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
153 _pthread_attr_getguardsize(const pthread_attr_t * __restrict attr,
154 size_t * __restrict guardsize)
158 /* Check for invalid arguments: */
159 if (attr == NULL || *attr == NULL || guardsize == NULL)
162 /* Return the guard size: */
163 *guardsize = (*attr)->guardsize_attr;
169 __strong_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
172 _pthread_attr_getinheritsched(const pthread_attr_t * __restrict attr,
173 int * __restrict sched_inherit)
177 if ((attr == NULL) || (*attr == NULL))
180 *sched_inherit = (*attr)->sched_inherit;
185 __strong_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
188 _pthread_attr_getschedparam(const pthread_attr_t * __restrict attr,
189 struct sched_param * __restrict param)
193 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
196 param->sched_priority = (*attr)->prio;
201 __strong_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
204 _pthread_attr_getschedpolicy(const pthread_attr_t * __restrict attr,
205 int * __restrict policy)
209 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
212 *policy = (*attr)->sched_policy;
217 __strong_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
220 _pthread_attr_getscope(const pthread_attr_t * __restrict attr,
221 int * __restrict contentionscope)
225 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
226 /* Return an invalid argument: */
230 *contentionscope = ((*attr)->flags & PTHREAD_SCOPE_SYSTEM) ?
231 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
236 __strong_reference(_pthread_attr_getscope, pthread_attr_getscope);
239 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
240 void ** __restrict stackaddr,
241 size_t * __restrict stacksize)
245 /* Check for invalid arguments: */
246 if (attr == NULL || *attr == NULL || stackaddr == NULL
247 || stacksize == NULL )
250 /* Return the stack address and size */
251 *stackaddr = (*attr)->stackaddr_attr;
252 *stacksize = (*attr)->stacksize_attr;
258 __strong_reference(_pthread_attr_getstack, pthread_attr_getstack);
261 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
265 /* Check for invalid arguments: */
266 if (attr == NULL || *attr == NULL || stackaddr == NULL)
269 /* Return the stack address: */
270 *stackaddr = (*attr)->stackaddr_attr;
276 __strong_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
279 _pthread_attr_getstacksize(const pthread_attr_t * __restrict attr,
280 size_t * __restrict stacksize)
284 /* Check for invalid arguments: */
285 if (attr == NULL || *attr == NULL || stacksize == NULL)
288 /* Return the stack size: */
289 *stacksize = (*attr)->stacksize_attr;
295 __strong_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
298 _pthread_attr_init(pthread_attr_t *attr)
301 pthread_attr_t pattr;
303 /* Allocate memory for the attribute object: */
304 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
305 /* Insufficient memory: */
308 /* Initialise the attribute object with the defaults: */
309 memcpy(pattr, &_pthread_attr_default,
310 sizeof(struct pthread_attr));
312 /* Return a pointer to the attribute object: */
319 __strong_reference(_pthread_attr_init, pthread_attr_init);
322 _pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize,
323 const cpu_set_t *mask)
325 cpu_set_t active, mask1;
326 size_t len, cplen = cpusetsize;
328 if (attr == NULL || *attr == NULL || mask == NULL)
331 if (cplen > sizeof(mask1))
332 cplen = sizeof(mask1);
334 memcpy(&mask1, mask, cplen);
336 len = sizeof(active);
337 if (sysctlbyname("machdep.smp_active", &active, &len, NULL, 0) < 0)
340 CPUMASK_ANDMASK(mask1, active);
341 if (CPUMASK_TESTZERO(mask1))
344 (*attr)->cpumask = mask1;
345 (*attr)->flags |= THR_CPUMASK;
349 __strong_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np);
352 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
356 if (attr == NULL || *attr == NULL) {
359 (*attr)->suspend = THR_CREATE_SUSPENDED;
365 __strong_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
368 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
372 /* Check for invalid arguments: */
373 if (attr == NULL || *attr == NULL ||
374 (detachstate != PTHREAD_CREATE_DETACHED &&
375 detachstate != PTHREAD_CREATE_JOINABLE))
378 /* Check if detached state: */
379 if (detachstate == PTHREAD_CREATE_DETACHED)
380 /* Set the detached flag: */
381 (*attr)->flags |= PTHREAD_DETACHED;
383 /* Reset the detached flag: */
384 (*attr)->flags &= ~PTHREAD_DETACHED;
390 __strong_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
393 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
397 /* Check for invalid arguments. */
398 if (attr == NULL || *attr == NULL)
401 /* Save the stack size. */
402 (*attr)->guardsize_attr = guardsize;
408 __strong_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
411 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
415 if ((attr == NULL) || (*attr == NULL))
417 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
418 sched_inherit != PTHREAD_EXPLICIT_SCHED)
421 (*attr)->sched_inherit = sched_inherit;
426 __strong_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
429 _pthread_attr_setschedparam(pthread_attr_t * __restrict attr,
430 const struct sched_param * __restrict param)
434 if ((attr == NULL) || (*attr == NULL))
440 policy = (*attr)->sched_policy;
443 int minv = sched_get_priority_min(policy);
444 int maxv = sched_get_priority_max(policy);
445 if (minv == -1 || maxv == -1 ||
446 param->sched_priority < minv ||
447 param->sched_priority > maxv) {
452 (*attr)->prio = param->sched_priority;
457 __strong_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
460 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int pol)
464 if ((attr == NULL) || (*attr == NULL))
466 else if (pol != SCHED_FIFO && pol != SCHED_OTHER && pol != SCHED_RR)
469 (*attr)->sched_policy = pol;
474 __strong_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
477 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
481 if ((attr == NULL) || (*attr == NULL)) {
482 /* Return an invalid argument: */
484 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
485 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
487 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
488 (*attr)->flags |= contentionscope;
490 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
495 __strong_reference(_pthread_attr_setscope, pthread_attr_setscope);
498 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
503 /* Check for invalid arguments: */
504 if (attr == NULL || *attr == NULL || stackaddr == NULL
505 || stacksize < PTHREAD_STACK_MIN)
508 /* Save the stack address and stack size */
509 (*attr)->stackaddr_attr = stackaddr;
510 (*attr)->stacksize_attr = stacksize;
516 __strong_reference(_pthread_attr_setstack, pthread_attr_setstack);
519 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
523 /* Check for invalid arguments: */
524 if (attr == NULL || *attr == NULL || stackaddr == NULL)
527 /* Save the stack address: */
528 (*attr)->stackaddr_attr = stackaddr;
534 __strong_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
537 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
541 /* Check for invalid arguments: */
542 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
545 /* Save the stack size: */
546 (*attr)->stacksize_attr = stacksize;
552 __strong_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);