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 __weak_reference(_pthread_attr_destroy, pthread_attr_destroy);
110 _pthread_attr_destroy(pthread_attr_t *attr)
114 /* Check for invalid arguments: */
115 if (attr == NULL || *attr == NULL)
116 /* Invalid argument: */
119 /* Free the memory allocated to the attribute object: */
123 * Leave the attribute pointer NULL now that the memory
132 __weak_reference(_pthread_attr_get_np, pthread_attr_get_np);
135 _pthread_attr_get_np(pthread_t pthread, pthread_attr_t *dst)
137 struct pthread *curthread;
138 struct pthread_attr attr;
141 if (pthread == NULL || dst == NULL || *dst == NULL)
144 curthread = _get_curthread();
145 if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)) != 0)
147 attr = pthread->attr;
148 if (pthread->flags & THR_FLAGS_DETACHED)
149 attr.flags |= PTHREAD_DETACHED;
150 THR_THREAD_UNLOCK(curthread, pthread);
152 memcpy(*dst, &attr, sizeof(struct pthread_attr));
154 (*dst)->cpuset = NULL;
155 (*dst)->cpusetsize = 0;
159 __weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
162 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
166 /* Check for invalid arguments: */
167 if (attr == NULL || *attr == NULL || detachstate == NULL)
170 /* Check if the detached flag is set: */
171 if ((*attr)->flags & PTHREAD_DETACHED)
172 /* Return detached: */
173 *detachstate = PTHREAD_CREATE_DETACHED;
175 /* Return joinable: */
176 *detachstate = PTHREAD_CREATE_JOINABLE;
182 __weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
185 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
189 /* Check for invalid arguments: */
190 if (attr == NULL || *attr == NULL || guardsize == NULL)
193 /* Return the guard size: */
194 *guardsize = (*attr)->guardsize_attr;
200 __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
203 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
207 if ((attr == NULL) || (*attr == NULL))
210 *sched_inherit = (*attr)->sched_inherit;
215 __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
218 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
222 if ((attr == NULL) || (*attr == NULL) || (param == NULL))
225 param->sched_priority = (*attr)->prio;
230 __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
233 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
237 if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
240 *policy = (*attr)->sched_policy;
245 __weak_reference(_pthread_attr_getscope, pthread_attr_getscope);
248 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
252 if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
253 /* Return an invalid argument: */
257 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
258 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
263 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
266 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
267 void ** __restrict stackaddr,
268 size_t * __restrict stacksize)
272 /* Check for invalid arguments: */
273 if (attr == NULL || *attr == NULL || stackaddr == NULL
274 || stacksize == NULL )
277 /* Return the stack address and size */
278 *stackaddr = (*attr)->stackaddr_attr;
279 *stacksize = (*attr)->stacksize_attr;
285 __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
288 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
292 /* Check for invalid arguments: */
293 if (attr == NULL || *attr == NULL || stackaddr == NULL)
296 /* Return the stack address: */
297 *stackaddr = (*attr)->stackaddr_attr;
303 __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
306 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
310 /* Check for invalid arguments: */
311 if (attr == NULL || *attr == NULL || stacksize == NULL)
314 /* Return the stack size: */
315 *stacksize = (*attr)->stacksize_attr;
321 __weak_reference(_pthread_attr_init, pthread_attr_init);
324 _pthread_attr_init(pthread_attr_t *attr)
327 pthread_attr_t pattr;
331 /* Allocate memory for the attribute object: */
332 if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
333 /* Insufficient memory: */
336 /* Initialise the attribute object with the defaults: */
337 memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr));
339 /* Return a pointer to the attribute object: */
346 __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
349 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
353 if (attr == NULL || *attr == NULL) {
356 (*attr)->suspend = THR_CREATE_SUSPENDED;
362 __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
365 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
369 /* Check for invalid arguments: */
370 if (attr == NULL || *attr == NULL ||
371 (detachstate != PTHREAD_CREATE_DETACHED &&
372 detachstate != PTHREAD_CREATE_JOINABLE))
375 /* Check if detached state: */
376 if (detachstate == PTHREAD_CREATE_DETACHED)
377 /* Set the detached flag: */
378 (*attr)->flags |= PTHREAD_DETACHED;
380 /* Reset the detached flag: */
381 (*attr)->flags &= ~PTHREAD_DETACHED;
387 __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
390 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
394 /* Check for invalid arguments. */
395 if (attr == NULL || *attr == NULL)
398 /* Save the stack size. */
399 (*attr)->guardsize_attr = guardsize;
405 __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
408 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
412 if ((attr == NULL) || (*attr == NULL))
414 else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
415 sched_inherit != PTHREAD_EXPLICIT_SCHED)
418 (*attr)->sched_inherit = sched_inherit;
423 __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
426 _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
430 if ((attr == NULL) || (*attr == NULL))
436 policy = (*attr)->sched_policy;
438 if (policy == SCHED_FIFO || policy == SCHED_RR) {
439 if (param->sched_priority < _thr_priorities[policy-1].pri_min ||
440 param->sched_priority > _thr_priorities[policy-1].pri_max)
444 * Ignore it for SCHED_OTHER now, patches for glib ports
445 * are wrongly using M:N thread library's internal macro
446 * THR_MIN_PRIORITY and THR_MAX_PRIORITY.
450 (*attr)->prio = param->sched_priority;
455 __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
458 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
462 if ((attr == NULL) || (*attr == NULL))
464 else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
467 (*attr)->sched_policy = policy;
468 (*attr)->prio = _thr_priorities[policy-1].pri_default;
473 __weak_reference(_pthread_attr_setscope, pthread_attr_setscope);
476 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
480 if ((attr == NULL) || (*attr == NULL)) {
481 /* Return an invalid argument: */
483 } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
484 (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
486 } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
487 (*attr)->flags |= contentionscope;
489 (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
494 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
497 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
502 /* Check for invalid arguments: */
503 if (attr == NULL || *attr == NULL || stackaddr == NULL
504 || stacksize < PTHREAD_STACK_MIN)
507 /* Save the stack address and stack size */
508 (*attr)->stackaddr_attr = stackaddr;
509 (*attr)->stacksize_attr = stacksize;
515 __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
518 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
522 /* Check for invalid arguments: */
523 if (attr == NULL || *attr == NULL || stackaddr == NULL)
526 /* Save the stack address: */
527 (*attr)->stackaddr_attr = stackaddr;
533 __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);
536 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
540 /* Check for invalid arguments: */
541 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
544 /* Save the stack size: */
545 (*attr)->stacksize_attr = stacksize;
552 _get_kern_cpuset_size(void)
554 static int kern_cpuset_size = 0;
556 if (kern_cpuset_size == 0) {
559 len = sizeof(kern_cpuset_size);
560 if (sysctlbyname("kern.smp.maxcpus", &kern_cpuset_size,
562 PANIC("failed to get sysctl kern.smp.maxcpus");
564 kern_cpuset_size = (kern_cpuset_size + 7) / 8;
567 return (kern_cpuset_size);
570 __weak_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np);
572 _pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize,
573 const cpuset_t *cpusetp)
578 if (pattr == NULL || (attr = (*pattr)) == NULL)
581 if (cpusetsize == 0 || cpusetp == NULL) {
582 if (attr->cpuset != NULL) {
585 attr->cpusetsize = 0;
590 if (cpusetsize > attr->cpusetsize) {
591 size_t kern_size = _get_kern_cpuset_size();
592 if (cpusetsize > kern_size) {
594 for (i = kern_size; i < cpusetsize; ++i) {
595 if (((char *)cpusetp)[i])
599 void *newset = realloc(attr->cpuset, cpusetsize);
602 attr->cpuset = newset;
603 attr->cpusetsize = cpusetsize;
605 memset(((char *)attr->cpuset) + cpusetsize, 0,
606 attr->cpusetsize - cpusetsize);
607 attr->cpusetsize = cpusetsize;
609 memcpy(attr->cpuset, cpusetp, cpusetsize);
615 __weak_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np);
617 _pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize,
623 if (pattr == NULL || (attr = (*pattr)) == NULL)
625 else if (attr->cpuset != NULL) {
626 memcpy(cpusetp, attr->cpuset, MIN(cpusetsize, attr->cpusetsize));
627 if (cpusetsize > attr->cpusetsize)
628 memset(((char *)cpusetp) + attr->cpusetsize, 0,
629 cpusetsize - attr->cpusetsize);
631 size_t kern_size = _get_kern_cpuset_size();
632 memset(cpusetp, -1, MIN(cpusetsize, kern_size));
633 if (cpusetsize > kern_size)
634 memset(((char *)cpusetp) + kern_size, 0,
635 cpusetsize - kern_size);