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
94 #include <sys/cdefs.h>
95 __FBSDID("$FreeBSD$");
97 #include "namespace.h"
102 #include <pthread_np.h>
103 #include <sys/sysctl.h>
104 #include "un-namespace.h"
106 #include "thr_private.h"
108 static size_t _get_kern_cpuset_size(void);
110 __weak_reference(_pthread_attr_destroy, pthread_attr_destroy);
113 _pthread_attr_destroy(pthread_attr_t *attr)
117 /* Check for invalid arguments: */
118 if (attr == NULL || *attr == NULL)
119 /* Invalid argument: */
122 if ((*attr)->cpuset != NULL)
123 free((*attr)->cpuset);
124 /* Free the memory allocated to the attribute object: */
128 * Leave the attribute pointer NULL now that the memory
137 __weak_reference(_pthread_attr_get_np, pthread_attr_get_np);
140 _pthread_attr_get_np(pthread_t pthread, pthread_attr_t *dstattr)
142 struct pthread *curthread;
143 struct pthread_attr attr, *dst;
147 if (pthread == NULL || dstattr == NULL || (dst = *dstattr) == NULL)
149 kern_size = _get_kern_cpuset_size();
150 if (dst->cpuset == NULL) {
151 dst->cpuset = calloc(1, kern_size);
152 dst->cpusetsize = kern_size;
154 curthread = _get_curthread();
155 if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)) != 0)
157 attr = pthread->attr;
158 if (pthread->flags & THR_FLAGS_DETACHED)
159 attr.flags |= PTHREAD_DETACHED;
160 ret = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, TID(pthread),
161 dst->cpusetsize, dst->cpuset);
164 THR_THREAD_UNLOCK(curthread, pthread);
166 memcpy(&dst->pthread_attr_start_copy,
167 &attr.pthread_attr_start_copy,
168 offsetof(struct pthread_attr, pthread_attr_end_copy) -
169 offsetof(struct pthread_attr, pthread_attr_start_copy));
174 __weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
177 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
181 /* Check for invalid arguments: */
182 if (attr == NULL || *attr == NULL || detachstate == NULL)
185 /* Check if the detached flag is set: */
186 if ((*attr)->flags & PTHREAD_DETACHED)
187 /* Return detached: */
188 *detachstate = PTHREAD_CREATE_DETACHED;
190 /* Return joinable: */
191 *detachstate = PTHREAD_CREATE_JOINABLE;
197 __weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
200 _pthread_attr_getguardsize(const pthread_attr_t * __restrict attr,
201 size_t * __restrict guardsize)
205 /* Check for invalid arguments: */
206 if (attr == NULL || *attr == NULL || guardsize == NULL)
209 /* Return the guard size: */
210 *guardsize = (*attr)->guardsize_attr;
216 __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
219 _pthread_attr_getinheritsched(const pthread_attr_t * __restrict attr,
220 int * __restrict sched_inherit)
224 if ((attr == NULL) || (*attr == NULL))
227 *sched_inherit = (*attr)->sched_inherit;
232 __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
235 _pthread_attr_getschedparam(const pthread_attr_t * __restrict attr,
236 struct sched_param * __restrict param)
240 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
243 param->sched_priority = (*attr)->prio;
248 __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
251 _pthread_attr_getschedpolicy(const pthread_attr_t * __restrict attr,
252 int * __restrict policy)
256 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
259 *policy = (*attr)->sched_policy;
264 __weak_reference(_pthread_attr_getscope, pthread_attr_getscope);
267 _pthread_attr_getscope(const pthread_attr_t * __restrict attr,
268 int * __restrict contentionscope)
272 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
273 /* Return an invalid argument: */
277 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
278 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
283 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
286 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
287 void ** __restrict stackaddr,
288 size_t * __restrict stacksize)
292 /* Check for invalid arguments: */
293 if (attr == NULL || *attr == NULL || stackaddr == NULL
294 || stacksize == NULL )
297 /* Return the stack address and size */
298 *stackaddr = (*attr)->stackaddr_attr;
299 *stacksize = (*attr)->stacksize_attr;
305 __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
308 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
312 /* Check for invalid arguments: */
313 if (attr == NULL || *attr == NULL || stackaddr == NULL)
316 /* Return the stack address: */
317 *stackaddr = (*attr)->stackaddr_attr;
323 __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
326 _pthread_attr_getstacksize(const pthread_attr_t * __restrict attr,
327 size_t * __restrict stacksize)
331 /* Check for invalid arguments: */
332 if (attr == NULL || *attr == NULL || stacksize == NULL)
335 /* Return the stack size: */
336 *stacksize = (*attr)->stacksize_attr;
342 __weak_reference(_pthread_attr_init, pthread_attr_init);
345 _pthread_attr_init(pthread_attr_t *attr)
348 pthread_attr_t pattr;
352 /* Allocate memory for the attribute object: */
353 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
354 /* Insufficient memory: */
357 /* Initialise the attribute object with the defaults: */
358 memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr));
360 /* Return a pointer to the attribute object: */
367 __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
370 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
374 if (attr == NULL || *attr == NULL) {
377 (*attr)->suspend = THR_CREATE_SUSPENDED;
383 __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
386 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
390 /* Check for invalid arguments: */
391 if (attr == NULL || *attr == NULL ||
392 (detachstate != PTHREAD_CREATE_DETACHED &&
393 detachstate != PTHREAD_CREATE_JOINABLE))
396 /* Check if detached state: */
397 if (detachstate == PTHREAD_CREATE_DETACHED)
398 /* Set the detached flag: */
399 (*attr)->flags |= PTHREAD_DETACHED;
401 /* Reset the detached flag: */
402 (*attr)->flags &= ~PTHREAD_DETACHED;
408 __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
411 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
415 /* Check for invalid arguments. */
416 if (attr == NULL || *attr == NULL)
419 /* Save the stack size. */
420 (*attr)->guardsize_attr = guardsize;
426 __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
429 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
433 if ((attr == NULL) || (*attr == NULL))
435 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
436 sched_inherit != PTHREAD_EXPLICIT_SCHED)
439 (*attr)->sched_inherit = sched_inherit;
444 __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
447 _pthread_attr_setschedparam(pthread_attr_t * __restrict attr,
448 const struct sched_param * __restrict param)
452 if ((attr == NULL) || (*attr == NULL))
458 policy = (*attr)->sched_policy;
460 if (policy == SCHED_FIFO || policy == SCHED_RR) {
461 if (param->sched_priority < _thr_priorities[policy-1].pri_min ||
462 param->sched_priority > _thr_priorities[policy-1].pri_max)
466 * Ignore it for SCHED_OTHER now, patches for glib ports
467 * are wrongly using M:N thread library's internal macro
468 * THR_MIN_PRIORITY and THR_MAX_PRIORITY.
472 (*attr)->prio = param->sched_priority;
477 __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
480 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
484 if ((attr == NULL) || (*attr == NULL))
486 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
489 (*attr)->sched_policy = policy;
490 (*attr)->prio = _thr_priorities[policy-1].pri_default;
495 __weak_reference(_pthread_attr_setscope, pthread_attr_setscope);
498 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
502 if ((attr == NULL) || (*attr == NULL)) {
503 /* Return an invalid argument: */
505 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
506 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
508 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
509 (*attr)->flags |= contentionscope;
511 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
516 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
519 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
524 /* Check for invalid arguments: */
525 if (attr == NULL || *attr == NULL || stackaddr == NULL
526 || stacksize < PTHREAD_STACK_MIN)
529 /* Save the stack address and stack size */
530 (*attr)->stackaddr_attr = stackaddr;
531 (*attr)->stacksize_attr = stacksize;
537 __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
540 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
544 /* Check for invalid arguments: */
545 if (attr == NULL || *attr == NULL || stackaddr == NULL)
548 /* Save the stack address: */
549 (*attr)->stackaddr_attr = stackaddr;
555 __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);
558 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
562 /* Check for invalid arguments: */
563 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
566 /* Save the stack size: */
567 (*attr)->stacksize_attr = stacksize;
574 _get_kern_cpuset_size(void)
576 static int kern_cpuset_size = 0;
578 if (kern_cpuset_size == 0) {
581 len = sizeof(kern_cpuset_size);
582 if (sysctlbyname("kern.sched.cpusetsize", &kern_cpuset_size,
584 PANIC("failed to get sysctl kern.sched.cpusetsize");
587 return (kern_cpuset_size);
590 __weak_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np);
592 _pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize,
593 const cpuset_t *cpusetp)
598 if (pattr == NULL || (attr = (*pattr)) == NULL)
601 if (cpusetsize == 0 || cpusetp == NULL) {
602 if (attr->cpuset != NULL) {
605 attr->cpusetsize = 0;
609 size_t kern_size = _get_kern_cpuset_size();
610 /* Kernel rejects small set, we check it here too. */
611 if (cpusetsize < kern_size)
613 if (cpusetsize > kern_size) {
614 /* Kernel checks invalid bits, we check it here too. */
616 for (i = kern_size; i < cpusetsize; ++i) {
617 if (((const char *)cpusetp)[i])
621 if (attr->cpuset == NULL) {
622 attr->cpuset = calloc(1, kern_size);
623 if (attr->cpuset == NULL)
625 attr->cpusetsize = kern_size;
627 memcpy(attr->cpuset, cpusetp, kern_size);
633 __weak_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np);
635 _pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize,
641 if (pattr == NULL || (attr = (*pattr)) == NULL)
644 /* Kernel rejects small set, we check it here too. */
645 size_t kern_size = _get_kern_cpuset_size();
646 if (cpusetsize < kern_size)
648 if (attr->cpuset != NULL)
649 memcpy(cpusetp, attr->cpuset, MIN(cpusetsize,
652 memset(cpusetp, -1, kern_size);
653 if (cpusetsize > kern_size)
654 memset(((char *)cpusetp) + kern_size, 0,
655 cpusetsize - kern_size);