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 __FBSDID("$FreeBSD$");
99 #include "namespace.h"
104 #include <pthread_np.h>
105 #include <sys/sysctl.h>
106 #include "un-namespace.h"
108 #include "thr_private.h"
110 static size_t _get_kern_cpuset_size(void);
112 __weak_reference(_pthread_attr_destroy, pthread_attr_destroy);
115 _pthread_attr_destroy(pthread_attr_t *attr)
119 /* Check for invalid arguments: */
120 if (attr == NULL || *attr == NULL)
121 /* Invalid argument: */
124 if ((*attr)->cpuset != NULL)
125 free((*attr)->cpuset);
126 /* Free the memory allocated to the attribute object: */
130 * Leave the attribute pointer NULL now that the memory
139 __weak_reference(_pthread_attr_get_np, pthread_attr_get_np);
142 _pthread_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(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
179 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
183 /* Check for invalid arguments: */
184 if (attr == NULL || *attr == NULL || detachstate == NULL)
187 /* Check if the detached flag is set: */
188 if ((*attr)->flags & PTHREAD_DETACHED)
189 /* Return detached: */
190 *detachstate = PTHREAD_CREATE_DETACHED;
192 /* Return joinable: */
193 *detachstate = PTHREAD_CREATE_JOINABLE;
199 __weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
202 _pthread_attr_getguardsize(const pthread_attr_t * __restrict attr,
203 size_t * __restrict guardsize)
207 /* Check for invalid arguments: */
208 if (attr == NULL || *attr == NULL || guardsize == NULL)
211 /* Return the guard size: */
212 *guardsize = (*attr)->guardsize_attr;
218 __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
221 _pthread_attr_getinheritsched(const pthread_attr_t * __restrict attr,
222 int * __restrict sched_inherit)
226 if ((attr == NULL) || (*attr == NULL))
229 *sched_inherit = (*attr)->sched_inherit;
234 __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
237 _pthread_attr_getschedparam(const pthread_attr_t * __restrict attr,
238 struct sched_param * __restrict param)
242 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
245 param->sched_priority = (*attr)->prio;
250 __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
253 _pthread_attr_getschedpolicy(const pthread_attr_t * __restrict attr,
254 int * __restrict policy)
258 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
261 *policy = (*attr)->sched_policy;
266 __weak_reference(_pthread_attr_getscope, pthread_attr_getscope);
269 _pthread_attr_getscope(const pthread_attr_t * __restrict attr,
270 int * __restrict contentionscope)
274 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
275 /* Return an invalid argument: */
279 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
280 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
285 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
288 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
289 void ** __restrict stackaddr,
290 size_t * __restrict stacksize)
294 /* Check for invalid arguments: */
295 if (attr == NULL || *attr == NULL || stackaddr == NULL
296 || stacksize == NULL )
299 /* Return the stack address and size */
300 *stackaddr = (*attr)->stackaddr_attr;
301 *stacksize = (*attr)->stacksize_attr;
307 __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
310 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
314 /* Check for invalid arguments: */
315 if (attr == NULL || *attr == NULL || stackaddr == NULL)
318 /* Return the stack address: */
319 *stackaddr = (*attr)->stackaddr_attr;
325 __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
328 _pthread_attr_getstacksize(const pthread_attr_t * __restrict attr,
329 size_t * __restrict stacksize)
333 /* Check for invalid arguments: */
334 if (attr == NULL || *attr == NULL || stacksize == NULL)
337 /* Return the stack size: */
338 *stacksize = (*attr)->stacksize_attr;
344 __weak_reference(_pthread_attr_init, pthread_attr_init);
347 _pthread_attr_init(pthread_attr_t *attr)
350 pthread_attr_t pattr;
354 /* Allocate memory for the attribute object: */
355 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
356 /* Insufficient memory: */
359 /* Initialise the attribute object with the defaults: */
360 memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr));
362 /* Return a pointer to the attribute object: */
369 __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
372 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
376 if (attr == NULL || *attr == NULL) {
379 (*attr)->suspend = THR_CREATE_SUSPENDED;
385 __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
388 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
392 /* Check for invalid arguments: */
393 if (attr == NULL || *attr == NULL ||
394 (detachstate != PTHREAD_CREATE_DETACHED &&
395 detachstate != PTHREAD_CREATE_JOINABLE))
398 /* Check if detached state: */
399 if (detachstate == PTHREAD_CREATE_DETACHED)
400 /* Set the detached flag: */
401 (*attr)->flags |= PTHREAD_DETACHED;
403 /* Reset the detached flag: */
404 (*attr)->flags &= ~PTHREAD_DETACHED;
410 __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
413 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
417 /* Check for invalid arguments. */
418 if (attr == NULL || *attr == NULL)
421 /* Save the stack size. */
422 (*attr)->guardsize_attr = guardsize;
428 __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
431 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
435 if ((attr == NULL) || (*attr == NULL))
437 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
438 sched_inherit != PTHREAD_EXPLICIT_SCHED)
441 (*attr)->sched_inherit = sched_inherit;
446 __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
449 _pthread_attr_setschedparam(pthread_attr_t * __restrict attr,
450 const struct sched_param * __restrict param)
454 if ((attr == NULL) || (*attr == NULL))
460 policy = (*attr)->sched_policy;
462 if (policy == SCHED_FIFO || policy == SCHED_RR) {
463 if (param->sched_priority < _thr_priorities[policy-1].pri_min ||
464 param->sched_priority > _thr_priorities[policy-1].pri_max)
468 * Ignore it for SCHED_OTHER now, patches for glib ports
469 * are wrongly using M:N thread library's internal macro
470 * THR_MIN_PRIORITY and THR_MAX_PRIORITY.
474 (*attr)->prio = param->sched_priority;
479 __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
482 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
486 if ((attr == NULL) || (*attr == NULL))
488 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
491 (*attr)->sched_policy = policy;
492 (*attr)->prio = _thr_priorities[policy-1].pri_default;
497 __weak_reference(_pthread_attr_setscope, pthread_attr_setscope);
500 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
504 if ((attr == NULL) || (*attr == NULL)) {
505 /* Return an invalid argument: */
507 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
508 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
510 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
511 (*attr)->flags |= contentionscope;
513 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
518 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
521 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
526 /* Check for invalid arguments: */
527 if (attr == NULL || *attr == NULL || stackaddr == NULL
528 || stacksize < PTHREAD_STACK_MIN)
531 /* Save the stack address and stack size */
532 (*attr)->stackaddr_attr = stackaddr;
533 (*attr)->stacksize_attr = stacksize;
539 __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
542 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
546 /* Check for invalid arguments: */
547 if (attr == NULL || *attr == NULL || stackaddr == NULL)
550 /* Save the stack address: */
551 (*attr)->stackaddr_attr = stackaddr;
557 __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);
560 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
564 /* Check for invalid arguments: */
565 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
568 /* Save the stack size: */
569 (*attr)->stacksize_attr = stacksize;
576 _get_kern_cpuset_size(void)
578 static int kern_cpuset_size = 0;
580 if (kern_cpuset_size == 0) {
583 len = sizeof(kern_cpuset_size);
584 if (sysctlbyname("kern.sched.cpusetsize", &kern_cpuset_size,
586 PANIC("failed to get sysctl kern.sched.cpusetsize");
589 return (kern_cpuset_size);
592 __weak_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np);
594 _pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize,
595 const cpuset_t *cpusetp)
600 if (pattr == NULL || (attr = (*pattr)) == NULL)
603 if (cpusetsize == 0 || cpusetp == NULL) {
604 if (attr->cpuset != NULL) {
607 attr->cpusetsize = 0;
611 size_t kern_size = _get_kern_cpuset_size();
612 /* Kernel rejects small set, we check it here too. */
613 if (cpusetsize < kern_size)
615 if (cpusetsize > kern_size) {
616 /* Kernel checks invalid bits, we check it here too. */
618 for (i = kern_size; i < cpusetsize; ++i) {
619 if (((const char *)cpusetp)[i])
623 if (attr->cpuset == NULL) {
624 attr->cpuset = calloc(1, kern_size);
625 if (attr->cpuset == NULL)
627 attr->cpusetsize = kern_size;
629 memcpy(attr->cpuset, cpusetp, kern_size);
635 __weak_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np);
637 _pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize,
643 if (pattr == NULL || (attr = (*pattr)) == NULL)
646 /* Kernel rejects small set, we check it here too. */
647 size_t kern_size = _get_kern_cpuset_size();
648 if (cpusetsize < kern_size)
650 if (attr->cpuset != NULL)
651 memcpy(cpusetp, attr->cpuset, MIN(cpusetsize,
654 memset(cpusetp, -1, kern_size);
655 if (cpusetsize > kern_size)
656 memset(((char *)cpusetp) + kern_size, 0,
657 cpusetsize - kern_size);