2 * Copyright (c) 2003 Craig Rodrigues <rodrigc@attbi.com>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Craig Rodrigues.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY CRAIG RODRIGUES AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
36 * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.
37 * Copyright (c) 2002,2003 Alexey Zelkin <phantom@FreeBSD.org>
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice(s), this list of conditions and the following disclaimer
45 * unmodified other than the allowable addition of one or more
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice(s), this list of conditions and the following disclaimer in
49 * the documentation and/or other materials provided with the
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
53 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
56 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
59 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
60 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
61 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
62 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 * Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
67 * All rights reserved.
69 * Redistribution and use in source and binary forms, with or without
70 * modification, are permitted provided that the following conditions
72 * 1. Redistributions of source code must retain the above copyright
73 * notice, this list of conditions and the following disclaimer.
74 * 2. Redistributions in binary form must reproduce the above copyright
75 * notice, this list of conditions and the following disclaimer in the
76 * documentation and/or other materials provided with the distribution.
77 * 3. Neither the name of the author nor the names of any co-contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
96 #include "namespace.h"
101 #include <pthread_np.h>
102 #include <sys/sysctl.h>
103 #include "un-namespace.h"
105 #include "thr_private.h"
107 static size_t _get_kern_cpuset_size(void);
109 __weak_reference(_pthread_attr_destroy, pthread_attr_destroy);
112 _pthread_attr_destroy(pthread_attr_t *attr)
116 /* Check for invalid arguments: */
117 if (attr == NULL || *attr == NULL)
118 /* Invalid argument: */
121 if ((*attr)->cpuset != NULL)
122 free((*attr)->cpuset);
123 /* Free the memory allocated to the attribute object: */
127 * Leave the attribute pointer NULL now that the memory
136 __weak_reference(_pthread_attr_get_np, pthread_attr_get_np);
139 _pthread_attr_get_np(pthread_t pthread, pthread_attr_t *dstattr)
141 struct pthread *curthread;
142 struct pthread_attr attr, *dst;
146 if (pthread == NULL || dstattr == NULL || (dst = *dstattr) == NULL)
148 kern_size = _get_kern_cpuset_size();
149 if (dst->cpuset == NULL) {
150 dst->cpuset = calloc(1, kern_size);
151 dst->cpusetsize = kern_size;
153 curthread = _get_curthread();
154 if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)) != 0)
156 attr = pthread->attr;
157 if (pthread->flags & THR_FLAGS_DETACHED)
158 attr.flags |= PTHREAD_DETACHED;
159 ret = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, TID(pthread),
160 dst->cpusetsize, dst->cpuset);
163 THR_THREAD_UNLOCK(curthread, pthread);
165 memcpy(&dst->pthread_attr_start_copy,
166 &attr.pthread_attr_start_copy,
167 offsetof(struct pthread_attr, pthread_attr_end_copy) -
168 offsetof(struct pthread_attr, pthread_attr_start_copy));
173 __weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
176 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
180 /* Check for invalid arguments: */
181 if (attr == NULL || *attr == NULL || detachstate == NULL)
184 /* Check if the detached flag is set: */
185 if ((*attr)->flags & PTHREAD_DETACHED)
186 /* Return detached: */
187 *detachstate = PTHREAD_CREATE_DETACHED;
189 /* Return joinable: */
190 *detachstate = PTHREAD_CREATE_JOINABLE;
196 __weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
199 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
203 /* Check for invalid arguments: */
204 if (attr == NULL || *attr == NULL || guardsize == NULL)
207 /* Return the guard size: */
208 *guardsize = (*attr)->guardsize_attr;
214 __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
217 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
221 if ((attr == NULL) || (*attr == NULL))
224 *sched_inherit = (*attr)->sched_inherit;
229 __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
232 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
236 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
239 param->sched_priority = (*attr)->prio;
244 __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
247 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
251 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
254 *policy = (*attr)->sched_policy;
259 __weak_reference(_pthread_attr_getscope, pthread_attr_getscope);
262 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
266 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
267 /* Return an invalid argument: */
271 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
272 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
277 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
280 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
281 void ** __restrict stackaddr,
282 size_t * __restrict stacksize)
286 /* Check for invalid arguments: */
287 if (attr == NULL || *attr == NULL || stackaddr == NULL
288 || stacksize == NULL )
291 /* Return the stack address and size */
292 *stackaddr = (*attr)->stackaddr_attr;
293 *stacksize = (*attr)->stacksize_attr;
299 __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
302 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
306 /* Check for invalid arguments: */
307 if (attr == NULL || *attr == NULL || stackaddr == NULL)
310 /* Return the stack address: */
311 *stackaddr = (*attr)->stackaddr_attr;
317 __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
320 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
324 /* Check for invalid arguments: */
325 if (attr == NULL || *attr == NULL || stacksize == NULL)
328 /* Return the stack size: */
329 *stacksize = (*attr)->stacksize_attr;
335 __weak_reference(_pthread_attr_init, pthread_attr_init);
338 _pthread_attr_init(pthread_attr_t *attr)
341 pthread_attr_t pattr;
345 /* Allocate memory for the attribute object: */
346 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
347 /* Insufficient memory: */
350 /* Initialise the attribute object with the defaults: */
351 memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr));
353 /* Return a pointer to the attribute object: */
360 __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
363 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
367 if (attr == NULL || *attr == NULL) {
370 (*attr)->suspend = THR_CREATE_SUSPENDED;
376 __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
379 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
383 /* Check for invalid arguments: */
384 if (attr == NULL || *attr == NULL ||
385 (detachstate != PTHREAD_CREATE_DETACHED &&
386 detachstate != PTHREAD_CREATE_JOINABLE))
389 /* Check if detached state: */
390 if (detachstate == PTHREAD_CREATE_DETACHED)
391 /* Set the detached flag: */
392 (*attr)->flags |= PTHREAD_DETACHED;
394 /* Reset the detached flag: */
395 (*attr)->flags &= ~PTHREAD_DETACHED;
401 __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
404 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
408 /* Check for invalid arguments. */
409 if (attr == NULL || *attr == NULL)
412 /* Save the stack size. */
413 (*attr)->guardsize_attr = guardsize;
419 __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
422 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
426 if ((attr == NULL) || (*attr == NULL))
428 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
429 sched_inherit != PTHREAD_EXPLICIT_SCHED)
432 (*attr)->sched_inherit = sched_inherit;
437 __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
440 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
444 if ((attr == NULL) || (*attr == NULL))
450 policy = (*attr)->sched_policy;
452 if (policy == SCHED_FIFO || policy == SCHED_RR) {
453 if (param->sched_priority < _thr_priorities[policy-1].pri_min ||
454 param->sched_priority > _thr_priorities[policy-1].pri_max)
458 * Ignore it for SCHED_OTHER now, patches for glib ports
459 * are wrongly using M:N thread library's internal macro
460 * THR_MIN_PRIORITY and THR_MAX_PRIORITY.
464 (*attr)->prio = param->sched_priority;
469 __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
472 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
476 if ((attr == NULL) || (*attr == NULL))
478 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
481 (*attr)->sched_policy = policy;
482 (*attr)->prio = _thr_priorities[policy-1].pri_default;
487 __weak_reference(_pthread_attr_setscope, pthread_attr_setscope);
490 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
494 if ((attr == NULL) || (*attr == NULL)) {
495 /* Return an invalid argument: */
497 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
498 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
500 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
501 (*attr)->flags |= contentionscope;
503 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
508 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
511 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
516 /* Check for invalid arguments: */
517 if (attr == NULL || *attr == NULL || stackaddr == NULL
518 || stacksize < PTHREAD_STACK_MIN)
521 /* Save the stack address and stack size */
522 (*attr)->stackaddr_attr = stackaddr;
523 (*attr)->stacksize_attr = stacksize;
529 __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
532 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
536 /* Check for invalid arguments: */
537 if (attr == NULL || *attr == NULL || stackaddr == NULL)
540 /* Save the stack address: */
541 (*attr)->stackaddr_attr = stackaddr;
547 __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);
550 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
554 /* Check for invalid arguments: */
555 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
558 /* Save the stack size: */
559 (*attr)->stacksize_attr = stacksize;
566 _get_kern_cpuset_size(void)
568 static int kern_cpuset_size = 0;
570 if (kern_cpuset_size == 0) {
573 len = sizeof(kern_cpuset_size);
574 if (sysctlbyname("kern.sched.cpusetsize", &kern_cpuset_size,
576 PANIC("failed to get sysctl kern.sched.cpusetsize");
579 return (kern_cpuset_size);
582 __weak_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np);
584 _pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize,
585 const cpuset_t *cpusetp)
590 if (pattr == NULL || (attr = (*pattr)) == NULL)
593 if (cpusetsize == 0 || cpusetp == NULL) {
594 if (attr->cpuset != NULL) {
597 attr->cpusetsize = 0;
601 size_t kern_size = _get_kern_cpuset_size();
602 /* Kernel rejects small set, we check it here too. */
603 if (cpusetsize < kern_size)
605 if (cpusetsize > kern_size) {
606 /* Kernel checks invalid bits, we check it here too. */
608 for (i = kern_size; i < cpusetsize; ++i) {
609 if (((char *)cpusetp)[i])
613 if (attr->cpuset == NULL) {
614 attr->cpuset = calloc(1, kern_size);
615 if (attr->cpuset == NULL)
617 attr->cpusetsize = kern_size;
619 memcpy(attr->cpuset, cpusetp, kern_size);
625 __weak_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np);
627 _pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize,
633 if (pattr == NULL || (attr = (*pattr)) == NULL)
636 /* Kernel rejects small set, we check it here too. */
637 size_t kern_size = _get_kern_cpuset_size();
638 if (cpusetsize < kern_size)
640 if (attr->cpuset != NULL)
641 memcpy(cpusetp, attr->cpuset, MIN(cpusetsize,
644 memset(cpusetp, -1, kern_size);
645 if (cpusetsize > kern_size)
646 memset(((char *)cpusetp) + kern_size, 0,
647 cpusetsize - kern_size);