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 *attr, size_t *guardsize)
204 /* Check for invalid arguments: */
205 if (attr == NULL || *attr == NULL || guardsize == NULL)
208 /* Return the guard size: */
209 *guardsize = (*attr)->guardsize_attr;
215 __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
218 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
222 if ((attr == NULL) || (*attr == NULL))
225 *sched_inherit = (*attr)->sched_inherit;
230 __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
233 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
237 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
240 param->sched_priority = (*attr)->prio;
245 __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
248 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
252 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
255 *policy = (*attr)->sched_policy;
260 __weak_reference(_pthread_attr_getscope, pthread_attr_getscope);
263 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
267 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
268 /* Return an invalid argument: */
272 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
273 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
278 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
281 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
282 void ** __restrict stackaddr,
283 size_t * __restrict stacksize)
287 /* Check for invalid arguments: */
288 if (attr == NULL || *attr == NULL || stackaddr == NULL
289 || stacksize == NULL )
292 /* Return the stack address and size */
293 *stackaddr = (*attr)->stackaddr_attr;
294 *stacksize = (*attr)->stacksize_attr;
300 __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
303 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
307 /* Check for invalid arguments: */
308 if (attr == NULL || *attr == NULL || stackaddr == NULL)
311 /* Return the stack address: */
312 *stackaddr = (*attr)->stackaddr_attr;
318 __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
321 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
325 /* Check for invalid arguments: */
326 if (attr == NULL || *attr == NULL || stacksize == NULL)
329 /* Return the stack size: */
330 *stacksize = (*attr)->stacksize_attr;
336 __weak_reference(_pthread_attr_init, pthread_attr_init);
339 _pthread_attr_init(pthread_attr_t *attr)
342 pthread_attr_t pattr;
346 /* Allocate memory for the attribute object: */
347 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
348 /* Insufficient memory: */
351 /* Initialise the attribute object with the defaults: */
352 memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr));
354 /* Return a pointer to the attribute object: */
361 __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
364 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
368 if (attr == NULL || *attr == NULL) {
371 (*attr)->suspend = THR_CREATE_SUSPENDED;
377 __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
380 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
384 /* Check for invalid arguments: */
385 if (attr == NULL || *attr == NULL ||
386 (detachstate != PTHREAD_CREATE_DETACHED &&
387 detachstate != PTHREAD_CREATE_JOINABLE))
390 /* Check if detached state: */
391 if (detachstate == PTHREAD_CREATE_DETACHED)
392 /* Set the detached flag: */
393 (*attr)->flags |= PTHREAD_DETACHED;
395 /* Reset the detached flag: */
396 (*attr)->flags &= ~PTHREAD_DETACHED;
402 __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
405 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
409 /* Check for invalid arguments. */
410 if (attr == NULL || *attr == NULL)
413 /* Save the stack size. */
414 (*attr)->guardsize_attr = guardsize;
420 __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
423 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
427 if ((attr == NULL) || (*attr == NULL))
429 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
430 sched_inherit != PTHREAD_EXPLICIT_SCHED)
433 (*attr)->sched_inherit = sched_inherit;
438 __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
441 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
445 if ((attr == NULL) || (*attr == NULL))
451 policy = (*attr)->sched_policy;
453 if (policy == SCHED_FIFO || policy == SCHED_RR) {
454 if (param->sched_priority < _thr_priorities[policy-1].pri_min ||
455 param->sched_priority > _thr_priorities[policy-1].pri_max)
459 * Ignore it for SCHED_OTHER now, patches for glib ports
460 * are wrongly using M:N thread library's internal macro
461 * THR_MIN_PRIORITY and THR_MAX_PRIORITY.
465 (*attr)->prio = param->sched_priority;
470 __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
473 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
477 if ((attr == NULL) || (*attr == NULL))
479 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
482 (*attr)->sched_policy = policy;
483 (*attr)->prio = _thr_priorities[policy-1].pri_default;
488 __weak_reference(_pthread_attr_setscope, pthread_attr_setscope);
491 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
495 if ((attr == NULL) || (*attr == NULL)) {
496 /* Return an invalid argument: */
498 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
499 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
501 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
502 (*attr)->flags |= contentionscope;
504 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
509 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
512 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
517 /* Check for invalid arguments: */
518 if (attr == NULL || *attr == NULL || stackaddr == NULL
519 || stacksize < PTHREAD_STACK_MIN)
522 /* Save the stack address and stack size */
523 (*attr)->stackaddr_attr = stackaddr;
524 (*attr)->stacksize_attr = stacksize;
530 __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
533 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
537 /* Check for invalid arguments: */
538 if (attr == NULL || *attr == NULL || stackaddr == NULL)
541 /* Save the stack address: */
542 (*attr)->stackaddr_attr = stackaddr;
548 __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);
551 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
555 /* Check for invalid arguments: */
556 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
559 /* Save the stack size: */
560 (*attr)->stacksize_attr = stacksize;
567 _get_kern_cpuset_size(void)
569 static int kern_cpuset_size = 0;
571 if (kern_cpuset_size == 0) {
574 len = sizeof(kern_cpuset_size);
575 if (sysctlbyname("kern.sched.cpusetsize", &kern_cpuset_size,
577 PANIC("failed to get sysctl kern.sched.cpusetsize");
580 return (kern_cpuset_size);
583 __weak_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np);
585 _pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize,
586 const cpuset_t *cpusetp)
591 if (pattr == NULL || (attr = (*pattr)) == NULL)
594 if (cpusetsize == 0 || cpusetp == NULL) {
595 if (attr->cpuset != NULL) {
598 attr->cpusetsize = 0;
602 size_t kern_size = _get_kern_cpuset_size();
603 /* Kernel rejects small set, we check it here too. */
604 if (cpusetsize < kern_size)
606 if (cpusetsize > kern_size) {
607 /* Kernel checks invalid bits, we check it here too. */
609 for (i = kern_size; i < cpusetsize; ++i) {
610 if (((const char *)cpusetp)[i])
614 if (attr->cpuset == NULL) {
615 attr->cpuset = calloc(1, kern_size);
616 if (attr->cpuset == NULL)
618 attr->cpusetsize = kern_size;
620 memcpy(attr->cpuset, cpusetp, kern_size);
626 __weak_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np);
628 _pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize,
634 if (pattr == NULL || (attr = (*pattr)) == NULL)
637 /* Kernel rejects small set, we check it here too. */
638 size_t kern_size = _get_kern_cpuset_size();
639 if (cpusetsize < kern_size)
641 if (attr->cpuset != NULL)
642 memcpy(cpusetp, attr->cpuset, MIN(cpusetsize,
645 memset(cpusetp, -1, kern_size);
646 if (cpusetsize > kern_size)
647 memset(((char *)cpusetp) + kern_size, 0,
648 cpusetsize - kern_size);