2 * Copyright (c) 1995-1997 John Birrell <jb@cimlogic.com.au>.
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 John Birrell.
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 JOHN BIRRELL 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
36 * Copyright (c) 2003 Craig Rodrigues <rodrigc@attbi.com>.
37 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by Craig Rodrigues.
50 * 4. Neither the name of the author nor the names of any co-contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY CRAIG RODRIGUES AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
70 * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.
71 * Copyright (c) 2002,2003 Alexey Zelkin <phantom@FreeBSD.org>
72 * Copyright (c) 2003 Jeff Roberson <jeff@FreeBSD.org>
73 * All rights reserved.
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
78 * 1. Redistributions of source code must retain the above copyright
79 * notice(s), this list of conditions and the following disclaimer
80 * unmodified other than the allowable addition of one or more
82 * 2. Redistributions in binary form must reproduce the above copyright
83 * notice(s), this list of conditions and the following disclaimer in
84 * the documentation and/or other materials provided with the
87 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
88 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
91 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
92 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
93 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
94 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
95 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
96 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
97 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100 /* XXXTHR I rewrote the entire file, can we lose some of the copyrights? */
102 #include <sys/param.h>
106 #include <pthread_np.h>
110 #include "thr_private.h"
112 __weak_reference(_pthread_attr_destroy, pthread_attr_destroy);
113 __weak_reference(_pthread_attr_init, pthread_attr_init);
114 __weak_reference(_pthread_attr_setcreatesuspend_np,
115 pthread_attr_setcreatesuspend_np);
116 __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
117 __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
118 __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
119 __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
120 __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
121 __weak_reference(_pthread_attr_setscope, pthread_attr_setscope);
122 __weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
123 __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
124 __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);
125 __weak_reference(_pthread_attr_get_np, pthread_attr_get_np);
126 __weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
127 __weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
128 __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
129 __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
130 __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
131 __weak_reference(_pthread_attr_getscope, pthread_attr_getscope);
132 __weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
133 __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
134 __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
137 _pthread_attr_init(pthread_attr_t *attr)
139 pthread_attr_t pattr;
141 if ((pattr = (pthread_attr_t)
142 malloc(sizeof(struct pthread_attr))) == NULL)
145 memcpy(pattr, &pthread_attr_default, sizeof(struct pthread_attr));
152 _pthread_attr_destroy(pthread_attr_t *attr)
154 if (attr == NULL || *attr == NULL)
164 _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
166 if (attr == NULL || *attr == NULL) {
170 (*attr)->suspend = PTHREAD_CREATE_SUSPENDED;
176 _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
178 if (attr == NULL || *attr == NULL ||
179 (detachstate != PTHREAD_CREATE_DETACHED &&
180 detachstate != PTHREAD_CREATE_JOINABLE))
183 if (detachstate == PTHREAD_CREATE_DETACHED)
184 (*attr)->flags |= PTHREAD_DETACHED;
186 (*attr)->flags &= ~PTHREAD_DETACHED;
192 _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
195 if (attr == NULL || *attr == NULL)
198 (*attr)->guardsize_attr = roundup(guardsize, _pthread_page_size);
204 _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
206 if (attr == NULL || *attr == NULL)
209 (*attr)->sched_inherit = sched_inherit;
215 _pthread_attr_setschedparam(pthread_attr_t *attr,
216 const struct sched_param *param)
218 if (attr == NULL || *attr == NULL)
224 if (param->sched_priority < PTHREAD_MIN_PRIORITY ||
225 param->sched_priority > PTHREAD_MAX_PRIORITY)
228 (*attr)->prio = param->sched_priority;
234 _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
236 if (attr == NULL || *attr == NULL)
239 if (policy < SCHED_FIFO || policy > SCHED_RR)
242 (*attr)->sched_policy = policy;
248 _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
250 if (attr == NULL || *attr == NULL)
253 if (contentionscope != PTHREAD_SCOPE_PROCESS ||
254 contentionscope == PTHREAD_SCOPE_SYSTEM)
255 /* We don't support PTHREAD_SCOPE_SYSTEM. */
258 (*attr)->flags |= contentionscope;
264 _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
267 if (attr == NULL || *attr == NULL || stackaddr == NULL
268 || stacksize < PTHREAD_STACK_MIN)
271 (*attr)->stackaddr_attr = stackaddr;
272 (*attr)->stacksize_attr = stacksize;
278 _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
280 if (attr == NULL || *attr == NULL || stackaddr == NULL)
283 (*attr)->stackaddr_attr = stackaddr;
289 _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
291 if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
294 (*attr)->stacksize_attr = stacksize;
300 _pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst)
304 if (pid == NULL || dst == NULL || *dst == NULL)
307 if ((ret = _find_thread(pid)) != 0)
310 memcpy(*dst, &pid->attr, sizeof(struct pthread_attr));
313 * Special case, if stack address was not provided by caller
314 * of pthread_create(), then return address allocated internally
316 if ((*dst)->stackaddr_attr == NULL)
317 (*dst)->stackaddr_attr = pid->stack;
323 _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
326 if (attr == NULL || *attr == NULL || detachstate == NULL)
329 /* Check if the detached flag is set: */
330 if ((*attr)->flags & PTHREAD_DETACHED)
331 *detachstate = PTHREAD_CREATE_DETACHED;
333 *detachstate = PTHREAD_CREATE_JOINABLE;
339 _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
341 if (attr == NULL || *attr == NULL || guardsize == NULL)
344 *guardsize = (*attr)->guardsize_attr;
350 _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
352 if (attr == NULL || *attr == NULL)
355 *sched_inherit = (*attr)->sched_inherit;
361 _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
363 if (attr == NULL || *attr == NULL || param == NULL)
366 param->sched_priority = (*attr)->prio;
372 _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
374 if (attr == NULL || *attr == NULL || policy == NULL)
377 *policy = (*attr)->sched_policy;
383 _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
385 if (attr == NULL || *attr == NULL || contentionscope == NULL)
388 *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
389 PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
395 _pthread_attr_getstack(const pthread_attr_t * __restrict attr,
396 void ** __restrict stackaddr,
397 size_t * __restrict stacksize)
399 if (attr == NULL || *attr == NULL || stackaddr == NULL
400 || stacksize == NULL)
403 *stackaddr = (*attr)->stackaddr_attr;
404 *stacksize = (*attr)->stacksize_attr;
410 _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
412 if (attr == NULL || *attr == NULL || stackaddr == NULL)
415 *stackaddr = (*attr)->stackaddr_attr;
421 _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
423 if (attr == NULL || *attr == NULL || stacksize == NULL)
426 *stacksize = (*attr)->stacksize_attr;