2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2003 Craig Rodrigues <rodrigc@attbi.com>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Craig Rodrigues.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY CRAIG RODRIGUES AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
38 * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.
39 * Copyright (c) 2002,2003 Alexey Zelkin <phantom@FreeBSD.org>
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice(s), this list of conditions and the following disclaimer
47 * unmodified other than the allowable addition of one or more
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice(s), this list of conditions and the following disclaimer in
51 * the documentation and/or other materials provided with the
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
55 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
58 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
61 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
62 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
63 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
64 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 * Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
69 * All rights reserved.
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions
74 * 1. Redistributions of source code must retain the above copyright
75 * notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright
77 * notice, this list of conditions and the following disclaimer in the
78 * documentation and/or other materials provided with the distribution.
79 * 3. Neither the name of the author nor the names of any co-contributors
80 * may be used to endorse or promote products derived from this software
81 * without specific prior written permission.
83 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
84 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
89 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
91 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
92 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
96 #include <sys/cdefs.h>
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(_thr_attr_destroy, _pthread_attr_destroy);
111 __weak_reference(_thr_attr_destroy, pthread_attr_destroy);
114 _thr_attr_destroy(pthread_attr_t *attr)
118 /* Check for invalid arguments: */
119 if (attr == NULL || *attr == NULL)
120 /* Invalid argument: */
123 if ((*attr)->cpuset != NULL)
124 free((*attr)->cpuset);
125 /* Free the memory allocated to the attribute object: */
129 * Leave the attribute pointer NULL now that the memory
138 __weak_reference(_thr_attr_get_np, pthread_attr_get_np);
139 __weak_reference(_thr_attr_get_np, _pthread_attr_get_np);
142 _thr_attr_get_np(pthread_t pthread, pthread_attr_t *dstattr)
144 struct pthread *curthread;
145 struct pthread_attr attr, *dst;
149 if (pthread == NULL || dstattr == NULL || (dst = *dstattr) == NULL)
151 kern_size = _get_kern_cpuset_size();
152 if (dst->cpuset == NULL) {
153 dst->cpuset = calloc(1, kern_size);
154 dst->cpusetsize = kern_size;
156 curthread = _get_curthread();
157 if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)) != 0)
159 attr = pthread->attr;
160 if (pthread->flags & THR_FLAGS_DETACHED)
161 attr.flags |= PTHREAD_DETACHED;
162 ret = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, TID(pthread),
163 dst->cpusetsize, dst->cpuset);
166 THR_THREAD_UNLOCK(curthread, pthread);
168 memcpy(&dst->pthread_attr_start_copy,
169 &attr.pthread_attr_start_copy,
170 offsetof(struct pthread_attr, pthread_attr_end_copy) -
171 offsetof(struct pthread_attr, pthread_attr_start_copy));
176 __weak_reference(_thr_attr_getdetachstate, pthread_attr_getdetachstate);
177 __weak_reference(_thr_attr_getdetachstate, _pthread_attr_getdetachstate);
180 _thr_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
184 /* Check for invalid arguments: */
185 if (attr == NULL || *attr == NULL || detachstate == NULL)
188 /* Check if the detached flag is set: */
189 if ((*attr)->flags & PTHREAD_DETACHED)
190 /* Return detached: */
191 *detachstate = PTHREAD_CREATE_DETACHED;
193 /* Return joinable: */
194 *detachstate = PTHREAD_CREATE_JOINABLE;
200 __weak_reference(_thr_attr_getguardsize, pthread_attr_getguardsize);
201 __weak_reference(_thr_attr_getguardsize, _pthread_attr_getguardsize);
204 _thr_attr_getguardsize(const pthread_attr_t * __restrict attr,
205 size_t * __restrict guardsize)
209 /* Check for invalid arguments: */
210 if (attr == NULL || *attr == NULL || guardsize == NULL)
213 /* Return the guard size: */
214 *guardsize = (*attr)->guardsize_attr;
220 __weak_reference(_thr_attr_getinheritsched, pthread_attr_getinheritsched);
221 __weak_reference(_thr_attr_getinheritsched, _pthread_attr_getinheritsched);
224 _thr_attr_getinheritsched(const pthread_attr_t * __restrict attr,
225 int * __restrict sched_inherit)
229 if ((attr == NULL) || (*attr == NULL))
232 *sched_inherit = (*attr)->sched_inherit;
237 __weak_reference(_thr_attr_getschedparam, pthread_attr_getschedparam);
238 __weak_reference(_thr_attr_getschedparam, _pthread_attr_getschedparam);
241 _thr_attr_getschedparam(const pthread_attr_t * __restrict attr,
242 struct sched_param * __restrict param)
246 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
249 param->sched_priority = (*attr)->prio;
254 __weak_reference(_thr_attr_getschedpolicy, pthread_attr_getschedpolicy);
255 __weak_reference(_thr_attr_getschedpolicy, _pthread_attr_getschedpolicy);
258 _thr_attr_getschedpolicy(const pthread_attr_t * __restrict attr,
259 int * __restrict policy)
263 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
266 *policy = (*attr)->sched_policy;
271 __weak_reference(_thr_attr_getscope, pthread_attr_getscope);
272 __weak_reference(_thr_attr_getscope, _pthread_attr_getscope);
275 _thr_attr_getscope(const pthread_attr_t * __restrict attr,
276 int * __restrict contentionscope)
280 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
281 /* Return an invalid argument: */
285 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
286 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
291 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
294 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
295 void ** __restrict stackaddr, size_t * __restrict stacksize)
299 /* Check for invalid arguments: */
300 if (attr == NULL || *attr == NULL || stackaddr == NULL
301 || stacksize == NULL )
304 /* Return the stack address and size */
305 *stackaddr = (*attr)->stackaddr_attr;
306 *stacksize = (*attr)->stacksize_attr;
312 __weak_reference(_thr_attr_getstackaddr, pthread_attr_getstackaddr);
313 __weak_reference(_thr_attr_getstackaddr, _pthread_attr_getstackaddr);
316 _thr_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
320 /* Check for invalid arguments: */
321 if (attr == NULL || *attr == NULL || stackaddr == NULL)
324 /* Return the stack address: */
325 *stackaddr = (*attr)->stackaddr_attr;
331 __weak_reference(_thr_attr_getstacksize, pthread_attr_getstacksize);
332 __weak_reference(_thr_attr_getstacksize, _pthread_attr_getstacksize);
335 _thr_attr_getstacksize(const pthread_attr_t * __restrict attr,
336 size_t * __restrict stacksize)
340 /* Check for invalid arguments: */
341 if (attr == NULL || *attr == NULL || stacksize == NULL)
344 /* Return the stack size: */
345 *stacksize = (*attr)->stacksize_attr;
351 __weak_reference(_thr_attr_init, pthread_attr_init);
352 __weak_reference(_thr_attr_init, _pthread_attr_init);
355 _thr_attr_init(pthread_attr_t *attr)
358 pthread_attr_t pattr;
362 /* Allocate memory for the attribute object: */
363 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
364 /* Insufficient memory: */
367 /* Initialise the attribute object with the defaults: */
368 memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr));
370 /* Return a pointer to the attribute object: */
377 __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
380 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
384 if (attr == NULL || *attr == NULL) {
387 (*attr)->suspend = THR_CREATE_SUSPENDED;
393 __weak_reference(_thr_attr_setdetachstate, pthread_attr_setdetachstate);
394 __weak_reference(_thr_attr_setdetachstate, _pthread_attr_setdetachstate);
397 _thr_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
401 /* Check for invalid arguments: */
402 if (attr == NULL || *attr == NULL ||
403 (detachstate != PTHREAD_CREATE_DETACHED &&
404 detachstate != PTHREAD_CREATE_JOINABLE))
407 /* Check if detached state: */
408 if (detachstate == PTHREAD_CREATE_DETACHED)
409 /* Set the detached flag: */
410 (*attr)->flags |= PTHREAD_DETACHED;
412 /* Reset the detached flag: */
413 (*attr)->flags &= ~PTHREAD_DETACHED;
419 __weak_reference(_thr_attr_setguardsize, pthread_attr_setguardsize);
420 __weak_reference(_thr_attr_setguardsize, _pthread_attr_setguardsize);
423 _thr_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
427 /* Check for invalid arguments. */
428 if (attr == NULL || *attr == NULL)
431 /* Save the stack size. */
432 (*attr)->guardsize_attr = guardsize;
438 __weak_reference(_thr_attr_setinheritsched, pthread_attr_setinheritsched);
439 __weak_reference(_thr_attr_setinheritsched, _pthread_attr_setinheritsched);
442 _thr_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
446 if ((attr == NULL) || (*attr == NULL))
448 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
449 sched_inherit != PTHREAD_EXPLICIT_SCHED)
452 (*attr)->sched_inherit = sched_inherit;
457 __weak_reference(_thr_attr_setschedparam, pthread_attr_setschedparam);
458 __weak_reference(_thr_attr_setschedparam, _pthread_attr_setschedparam);
461 _thr_attr_setschedparam(pthread_attr_t * __restrict attr,
462 const struct sched_param * __restrict param)
466 if ((attr == NULL) || (*attr == NULL))
472 policy = (*attr)->sched_policy;
474 if (policy == SCHED_FIFO || policy == SCHED_RR) {
475 if (param->sched_priority < _thr_priorities[policy-1].pri_min ||
476 param->sched_priority > _thr_priorities[policy-1].pri_max)
480 * Ignore it for SCHED_OTHER now, patches for glib ports
481 * are wrongly using M:N thread library's internal macro
482 * THR_MIN_PRIORITY and THR_MAX_PRIORITY.
486 (*attr)->prio = param->sched_priority;
491 __weak_reference(_thr_attr_setschedpolicy, pthread_attr_setschedpolicy);
492 __weak_reference(_thr_attr_setschedpolicy, _pthread_attr_setschedpolicy);
495 _thr_attr_setschedpolicy(pthread_attr_t *attr, int policy)
499 if ((attr == NULL) || (*attr == NULL))
501 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
504 (*attr)->sched_policy = policy;
505 (*attr)->prio = _thr_priorities[policy-1].pri_default;
510 __weak_reference(_thr_attr_setscope, pthread_attr_setscope);
511 __weak_reference(_thr_attr_setscope, _pthread_attr_setscope);
514 _thr_attr_setscope(pthread_attr_t *attr, int contentionscope)
518 if ((attr == NULL) || (*attr == NULL)) {
519 /* Return an invalid argument: */
521 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
522 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
524 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
525 (*attr)->flags |= contentionscope;
527 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
532 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
535 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
540 /* Check for invalid arguments: */
541 if (attr == NULL || *attr == NULL || stackaddr == NULL
542 || stacksize < PTHREAD_STACK_MIN)
545 /* Save the stack address and stack size */
546 (*attr)->stackaddr_attr = stackaddr;
547 (*attr)->stacksize_attr = stacksize;
553 __weak_reference(_thr_attr_setstackaddr, pthread_attr_setstackaddr);
554 __weak_reference(_thr_attr_setstackaddr, _pthread_attr_setstackaddr);
557 _thr_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
561 /* Check for invalid arguments: */
562 if (attr == NULL || *attr == NULL || stackaddr == NULL)
565 /* Save the stack address: */
566 (*attr)->stackaddr_attr = stackaddr;
572 __weak_reference(_thr_attr_setstacksize, pthread_attr_setstacksize);
573 __weak_reference(_thr_attr_setstacksize, _pthread_attr_setstacksize);
576 _thr_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
580 /* Check for invalid arguments: */
581 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
584 /* Save the stack size: */
585 (*attr)->stacksize_attr = stacksize;
592 _get_kern_cpuset_size(void)
594 static int kern_cpuset_size = 0;
596 if (kern_cpuset_size == 0) {
599 len = sizeof(kern_cpuset_size);
600 if (sysctlbyname("kern.sched.cpusetsizemin", &kern_cpuset_size,
601 &len, NULL, 0) != 0 &&
602 sysctlbyname("kern.sched.cpusetsize", &kern_cpuset_size,
604 PANIC("failed to get sysctl kern.sched.cpusetsize");
607 return (kern_cpuset_size);
610 __weak_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np);
612 _pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize,
613 const cpuset_t *cpusetp)
618 if (pattr == NULL || (attr = (*pattr)) == NULL)
621 if (cpusetsize == 0 || cpusetp == NULL) {
622 if (attr->cpuset != NULL) {
625 attr->cpusetsize = 0;
629 size_t kern_size = _get_kern_cpuset_size();
630 /* Kernel rejects small set, we check it here too. */
631 if (cpusetsize < kern_size)
633 if (cpusetsize > kern_size) {
634 /* Kernel checks invalid bits, we check it here too. */
636 for (i = kern_size; i < cpusetsize; ++i) {
637 if (((const char *)cpusetp)[i])
641 if (attr->cpuset == NULL) {
642 attr->cpuset = calloc(1, kern_size);
643 if (attr->cpuset == NULL)
645 attr->cpusetsize = kern_size;
647 memcpy(attr->cpuset, cpusetp, kern_size);
653 __weak_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np);
655 _pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize,
661 if (pattr == NULL || (attr = (*pattr)) == NULL)
664 /* Kernel rejects small set, we check it here too. */
665 size_t kern_size = _get_kern_cpuset_size();
666 if (cpusetsize < kern_size)
668 if (attr->cpuset != NULL)
669 memcpy(cpusetp, attr->cpuset, MIN(cpusetsize,
672 memset(cpusetp, -1, kern_size);
673 if (cpusetsize > kern_size)
674 memset(((char *)cpusetp) + kern_size, 0,
675 cpusetsize - kern_size);