thr_attr.c revision 154126
1112918Sjeff/*
2112918Sjeff * Copyright (c) 2003 Craig Rodrigues <rodrigc@attbi.com>.
3112918Sjeff * All rights reserved.
4112918Sjeff *
5112918Sjeff * Redistribution and use in source and binary forms, with or without
6112918Sjeff * modification, are permitted provided that the following conditions
7112918Sjeff * are met:
8112918Sjeff * 1. Redistributions of source code must retain the above copyright
9112918Sjeff *    notice, this list of conditions and the following disclaimer.
10112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11112918Sjeff *    notice, this list of conditions and the following disclaimer in the
12112918Sjeff *    documentation and/or other materials provided with the distribution.
13112918Sjeff * 3. All advertising materials mentioning features or use of this software
14112918Sjeff *    must display the following acknowledgement:
15112918Sjeff *	This product includes software developed by Craig Rodrigues.
16112918Sjeff * 4. Neither the name of the author nor the names of any co-contributors
17112918Sjeff *    may be used to endorse or promote products derived from this software
18112918Sjeff *    without specific prior written permission.
19112918Sjeff *
20112918Sjeff * THIS SOFTWARE IS PROVIDED BY CRAIG RODRIGUES AND CONTRIBUTORS ``AS IS'' AND
21112918Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23112918Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24112918Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25112918Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26112918Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27112918Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28112918Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29112918Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30112918Sjeff * SUCH DAMAGE.
31112918Sjeff *
32112918Sjeff */
33112918Sjeff
34112918Sjeff/*
35112918Sjeff * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
36112918Sjeff * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.
37112918Sjeff * Copyright (c) 2002,2003 Alexey Zelkin <phantom@FreeBSD.org>
38112918Sjeff * All rights reserved.
39112918Sjeff *
40112918Sjeff * Redistribution and use in source and binary forms, with or without
41112918Sjeff * modification, are permitted provided that the following conditions
42112918Sjeff * are met:
43112918Sjeff * 1. Redistributions of source code must retain the above copyright
44112918Sjeff *    notice(s), this list of conditions and the following disclaimer
45112918Sjeff *    unmodified other than the allowable addition of one or more
46112918Sjeff *    copyright notices.
47112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
48112918Sjeff *    notice(s), this list of conditions and the following disclaimer in
49112918Sjeff *    the documentation and/or other materials provided with the
50112918Sjeff *    distribution.
51112918Sjeff *
52112918Sjeff * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
53112918Sjeff * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55112918Sjeff * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
56112918Sjeff * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57112918Sjeff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58112918Sjeff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
59112918Sjeff * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
60112918Sjeff * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
61112918Sjeff * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
62112918Sjeff * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63112918Sjeff */
64112918Sjeff
65144518Sdavidxu/*
66144518Sdavidxu * Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
67144518Sdavidxu * All rights reserved.
68144518Sdavidxu *
69144518Sdavidxu * Redistribution and use in source and binary forms, with or without
70144518Sdavidxu * modification, are permitted provided that the following conditions
71144518Sdavidxu * are met:
72144518Sdavidxu * 1. Redistributions of source code must retain the above copyright
73144518Sdavidxu *    notice, this list of conditions and the following disclaimer.
74144518Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
75144518Sdavidxu *    notice, this list of conditions and the following disclaimer in the
76144518Sdavidxu *    documentation and/or other materials provided with the distribution.
77144518Sdavidxu * 3. All advertising materials mentioning features or use of this software
78144518Sdavidxu *    must display the following acknowledgement:
79144518Sdavidxu *	This product includes software developed by John Birrell.
80144518Sdavidxu * 4. Neither the name of the author nor the names of any co-contributors
81144518Sdavidxu *    may be used to endorse or promote products derived from this software
82144518Sdavidxu *    without specific prior written permission.
83144518Sdavidxu *
84144518Sdavidxu * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
85144518Sdavidxu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86144518Sdavidxu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87144518Sdavidxu * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
88144518Sdavidxu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89144518Sdavidxu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90144518Sdavidxu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91144518Sdavidxu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92144518Sdavidxu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93144518Sdavidxu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94144518Sdavidxu * SUCH DAMAGE.
95144518Sdavidxu *
96144518Sdavidxu * $FreeBSD: head/lib/libthr/thread/thr_attr.c 154126 2006-01-09 03:59:51Z davidxu $
97144518Sdavidxu */
98112918Sjeff
99112918Sjeff#include <errno.h>
100112918Sjeff#include <pthread.h>
101112918Sjeff#include <stdlib.h>
102112918Sjeff#include <string.h>
103144518Sdavidxu#include <pthread_np.h>
104112918Sjeff
105112918Sjeff#include "thr_private.h"
106112918Sjeff
107125963Smtm__weak_reference(_pthread_attr_destroy, pthread_attr_destroy);
108112918Sjeff
109112918Sjeffint
110144518Sdavidxu_pthread_attr_destroy(pthread_attr_t *attr)
111112918Sjeff{
112144518Sdavidxu	int	ret;
113112918Sjeff
114144518Sdavidxu	/* Check for invalid arguments: */
115144518Sdavidxu	if (attr == NULL || *attr == NULL)
116144518Sdavidxu		/* Invalid argument: */
117144518Sdavidxu		ret = EINVAL;
118144518Sdavidxu	else {
119144518Sdavidxu		/* Free the memory allocated to the attribute object: */
120144518Sdavidxu		free(*attr);
121112918Sjeff
122144518Sdavidxu		/*
123144518Sdavidxu		 * Leave the attribute pointer NULL now that the memory
124144518Sdavidxu		 * has been freed:
125144518Sdavidxu		 */
126144518Sdavidxu		*attr = NULL;
127144518Sdavidxu		ret = 0;
128144518Sdavidxu	}
129144518Sdavidxu	return(ret);
130112918Sjeff}
131112918Sjeff
132144518Sdavidxu__weak_reference(_pthread_attr_get_np, pthread_attr_get_np);
133144518Sdavidxu
134112918Sjeffint
135144518Sdavidxu_pthread_attr_get_np(pthread_t pid, pthread_attr_t *dst)
136112918Sjeff{
137144518Sdavidxu	struct pthread *curthread;
138144518Sdavidxu	struct pthread_attr attr;
139144518Sdavidxu	int	ret;
140144518Sdavidxu
141144518Sdavidxu	if (pid == NULL || dst == NULL || *dst == NULL)
142112918Sjeff		return (EINVAL);
143112918Sjeff
144144518Sdavidxu	curthread = _get_curthread();
145144518Sdavidxu	if ((ret = _thr_ref_add(curthread, pid, /*include dead*/0)) != 0)
146144518Sdavidxu		return (ret);
147144518Sdavidxu	attr = pid->attr;
148154126Sdavidxu	if (pid->tlflags & TLFLAGS_DETACHED)
149154126Sdavidxu		attr.flags |= PTHREAD_DETACHED;
150144518Sdavidxu	_thr_ref_delete(curthread, pid);
151144518Sdavidxu	memcpy(*dst, &attr, sizeof(struct pthread_attr));
152112918Sjeff
153112918Sjeff	return (0);
154112918Sjeff}
155112918Sjeff
156144518Sdavidxu__weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
157144518Sdavidxu
158112918Sjeffint
159144518Sdavidxu_pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
160112918Sjeff{
161144518Sdavidxu	int	ret;
162144518Sdavidxu
163144518Sdavidxu	/* Check for invalid arguments: */
164144518Sdavidxu	if (attr == NULL || *attr == NULL || detachstate == NULL)
165144518Sdavidxu		ret = EINVAL;
166144518Sdavidxu	else {
167144518Sdavidxu		/* Check if the detached flag is set: */
168144518Sdavidxu		if ((*attr)->flags & PTHREAD_DETACHED)
169144518Sdavidxu			/* Return detached: */
170144518Sdavidxu			*detachstate = PTHREAD_CREATE_DETACHED;
171144518Sdavidxu		else
172144518Sdavidxu			/* Return joinable: */
173144518Sdavidxu			*detachstate = PTHREAD_CREATE_JOINABLE;
174144518Sdavidxu		ret = 0;
175112918Sjeff	}
176144518Sdavidxu	return(ret);
177112918Sjeff}
178112918Sjeff
179144518Sdavidxu__weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
180144518Sdavidxu
181112918Sjeffint
182144518Sdavidxu_pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
183112918Sjeff{
184144518Sdavidxu	int	ret;
185112918Sjeff
186144518Sdavidxu	/* Check for invalid arguments: */
187144518Sdavidxu	if (attr == NULL || *attr == NULL || guardsize == NULL)
188144518Sdavidxu		ret = EINVAL;
189144518Sdavidxu	else {
190144518Sdavidxu		/* Return the guard size: */
191144518Sdavidxu		*guardsize = (*attr)->guardsize_attr;
192144518Sdavidxu		ret = 0;
193144518Sdavidxu	}
194144518Sdavidxu	return(ret);
195112918Sjeff}
196112918Sjeff
197144518Sdavidxu__weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
198144518Sdavidxu
199112918Sjeffint
200144518Sdavidxu_pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
201112918Sjeff{
202144518Sdavidxu	int ret = 0;
203112918Sjeff
204144518Sdavidxu	if ((attr == NULL) || (*attr == NULL))
205144518Sdavidxu		ret = EINVAL;
206144518Sdavidxu	else
207144518Sdavidxu		*sched_inherit = (*attr)->sched_inherit;
208112918Sjeff
209144518Sdavidxu	return(ret);
210112918Sjeff}
211112918Sjeff
212144518Sdavidxu__weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
213144518Sdavidxu
214112918Sjeffint
215144518Sdavidxu_pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
216112918Sjeff{
217144518Sdavidxu	int ret = 0;
218112918Sjeff
219144518Sdavidxu	if ((attr == NULL) || (*attr == NULL) || (param == NULL))
220144518Sdavidxu		ret = EINVAL;
221144518Sdavidxu	else
222144518Sdavidxu		param->sched_priority = (*attr)->prio;
223112918Sjeff
224144518Sdavidxu	return(ret);
225112918Sjeff}
226112918Sjeff
227144518Sdavidxu__weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
228144518Sdavidxu
229112918Sjeffint
230144518Sdavidxu_pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
231112918Sjeff{
232144518Sdavidxu	int ret = 0;
233112918Sjeff
234144518Sdavidxu	if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
235144518Sdavidxu		ret = EINVAL;
236144518Sdavidxu	else
237144518Sdavidxu		*policy = (*attr)->sched_policy;
238112918Sjeff
239144518Sdavidxu	return(ret);
240144518Sdavidxu}
241112918Sjeff
242144518Sdavidxu__weak_reference(_pthread_attr_getscope, pthread_attr_getscope);
243112918Sjeff
244112918Sjeffint
245144518Sdavidxu_pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
246112918Sjeff{
247144518Sdavidxu	int ret = 0;
248112918Sjeff
249144518Sdavidxu	if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
250144518Sdavidxu		/* Return an invalid argument: */
251144518Sdavidxu		ret = EINVAL;
252112918Sjeff
253144518Sdavidxu	else
254144518Sdavidxu		*contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
255144518Sdavidxu		    PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
256112918Sjeff
257144518Sdavidxu	return(ret);
258112918Sjeff}
259112918Sjeff
260144518Sdavidxu__weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
261144518Sdavidxu
262112918Sjeffint
263144518Sdavidxu_pthread_attr_getstack(const pthread_attr_t * __restrict attr,
264144518Sdavidxu                        void ** __restrict stackaddr,
265144518Sdavidxu                        size_t * __restrict stacksize)
266112918Sjeff{
267144518Sdavidxu	int     ret;
268112918Sjeff
269144518Sdavidxu	/* Check for invalid arguments: */
270144518Sdavidxu	if (attr == NULL || *attr == NULL || stackaddr == NULL
271144518Sdavidxu	    || stacksize == NULL )
272144518Sdavidxu		ret = EINVAL;
273144518Sdavidxu	else {
274144518Sdavidxu		/* Return the stack address and size */
275144518Sdavidxu		*stackaddr = (*attr)->stackaddr_attr;
276144518Sdavidxu		*stacksize = (*attr)->stacksize_attr;
277144518Sdavidxu		ret = 0;
278144518Sdavidxu	}
279144518Sdavidxu	return(ret);
280144518Sdavidxu}
281112918Sjeff
282144518Sdavidxu__weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
283112918Sjeff
284112918Sjeffint
285144518Sdavidxu_pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
286112918Sjeff{
287144518Sdavidxu	int	ret;
288112918Sjeff
289144518Sdavidxu	/* Check for invalid arguments: */
290144518Sdavidxu	if (attr == NULL || *attr == NULL || stackaddr == NULL)
291144518Sdavidxu		ret = EINVAL;
292144518Sdavidxu	else {
293144518Sdavidxu		/* Return the stack address: */
294144518Sdavidxu		*stackaddr = (*attr)->stackaddr_attr;
295144518Sdavidxu		ret = 0;
296144518Sdavidxu	}
297144518Sdavidxu	return(ret);
298112918Sjeff}
299112918Sjeff
300144518Sdavidxu__weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
301144518Sdavidxu
302112918Sjeffint
303144518Sdavidxu_pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
304112918Sjeff{
305144518Sdavidxu	int	ret;
306112918Sjeff
307144518Sdavidxu	/* Check for invalid arguments: */
308144518Sdavidxu	if (attr == NULL || *attr == NULL || stacksize  == NULL)
309144518Sdavidxu		ret = EINVAL;
310144518Sdavidxu	else {
311144518Sdavidxu		/* Return the stack size: */
312144518Sdavidxu		*stacksize = (*attr)->stacksize_attr;
313144518Sdavidxu		ret = 0;
314144518Sdavidxu	}
315144518Sdavidxu	return(ret);
316112918Sjeff}
317112918Sjeff
318144518Sdavidxu__weak_reference(_pthread_attr_init, pthread_attr_init);
319144518Sdavidxu
320112918Sjeffint
321144518Sdavidxu_pthread_attr_init(pthread_attr_t *attr)
322112918Sjeff{
323144518Sdavidxu	int	ret;
324144518Sdavidxu	pthread_attr_t	pattr;
325112918Sjeff
326144518Sdavidxu	_thr_check_init();
327112918Sjeff
328144518Sdavidxu	/* Allocate memory for the attribute object: */
329144518Sdavidxu	if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
330144518Sdavidxu		/* Insufficient memory: */
331144518Sdavidxu		ret = ENOMEM;
332144518Sdavidxu	else {
333144518Sdavidxu		/* Initialise the attribute object with the defaults: */
334144518Sdavidxu		memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr));
335144518Sdavidxu
336144518Sdavidxu		/* Return a pointer to the attribute object: */
337144518Sdavidxu		*attr = pattr;
338144518Sdavidxu		ret = 0;
339144518Sdavidxu	}
340144518Sdavidxu	return(ret);
341112918Sjeff}
342112918Sjeff
343144518Sdavidxu__weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
344144518Sdavidxu
345112918Sjeffint
346144518Sdavidxu_pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
347112918Sjeff{
348112918Sjeff	int	ret;
349112918Sjeff
350144518Sdavidxu	if (attr == NULL || *attr == NULL) {
351144518Sdavidxu		ret = EINVAL;
352144518Sdavidxu	} else {
353144518Sdavidxu		(*attr)->suspend = THR_CREATE_SUSPENDED;
354144518Sdavidxu		ret = 0;
355144518Sdavidxu	}
356144518Sdavidxu	return(ret);
357144518Sdavidxu}
358112918Sjeff
359144518Sdavidxu__weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
360112918Sjeff
361112918Sjeffint
362144518Sdavidxu_pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
363112918Sjeff{
364144518Sdavidxu	int	ret;
365112918Sjeff
366144518Sdavidxu	/* Check for invalid arguments: */
367144518Sdavidxu	if (attr == NULL || *attr == NULL ||
368144518Sdavidxu	    (detachstate != PTHREAD_CREATE_DETACHED &&
369144518Sdavidxu	    detachstate != PTHREAD_CREATE_JOINABLE))
370144518Sdavidxu		ret = EINVAL;
371144518Sdavidxu	else {
372144518Sdavidxu		/* Check if detached state: */
373144518Sdavidxu		if (detachstate == PTHREAD_CREATE_DETACHED)
374144518Sdavidxu			/* Set the detached flag: */
375144518Sdavidxu			(*attr)->flags |= PTHREAD_DETACHED;
376144518Sdavidxu		else
377144518Sdavidxu			/* Reset the detached flag: */
378144518Sdavidxu			(*attr)->flags &= ~PTHREAD_DETACHED;
379144518Sdavidxu		ret = 0;
380144518Sdavidxu	}
381144518Sdavidxu	return(ret);
382144518Sdavidxu}
383112918Sjeff
384144518Sdavidxu__weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
385112918Sjeff
386112918Sjeffint
387144518Sdavidxu_pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
388112918Sjeff{
389144518Sdavidxu	int	ret;
390112918Sjeff
391144518Sdavidxu	/* Check for invalid arguments. */
392144518Sdavidxu	if (attr == NULL || *attr == NULL)
393144518Sdavidxu		ret = EINVAL;
394144518Sdavidxu	else {
395144518Sdavidxu		/* Save the stack size. */
396144518Sdavidxu		(*attr)->guardsize_attr = guardsize;
397144518Sdavidxu		ret = 0;
398144518Sdavidxu	}
399144518Sdavidxu	return(ret);
400112918Sjeff}
401112918Sjeff
402144518Sdavidxu__weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
403144518Sdavidxu
404112918Sjeffint
405144518Sdavidxu_pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
406112918Sjeff{
407144518Sdavidxu	int ret = 0;
408112918Sjeff
409144518Sdavidxu	if ((attr == NULL) || (*attr == NULL))
410144518Sdavidxu		ret = EINVAL;
411144518Sdavidxu	else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
412144518Sdavidxu		 sched_inherit != PTHREAD_EXPLICIT_SCHED)
413144518Sdavidxu		ret = ENOTSUP;
414144518Sdavidxu	else
415144518Sdavidxu		(*attr)->sched_inherit = sched_inherit;
416112918Sjeff
417144518Sdavidxu	return(ret);
418112918Sjeff}
419112918Sjeff
420144518Sdavidxu__weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
421144518Sdavidxu
422112918Sjeffint
423144518Sdavidxu_pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
424112918Sjeff{
425144518Sdavidxu	int ret = 0;
426112918Sjeff
427144518Sdavidxu	if ((attr == NULL) || (*attr == NULL))
428144518Sdavidxu		ret = EINVAL;
429144518Sdavidxu	else if (param == NULL) {
430144518Sdavidxu		ret = ENOTSUP;
431144518Sdavidxu	} else if ((param->sched_priority < THR_MIN_PRIORITY) ||
432144518Sdavidxu	    (param->sched_priority > THR_MAX_PRIORITY)) {
433144518Sdavidxu		/* Return an unsupported value error. */
434144518Sdavidxu		ret = ENOTSUP;
435144518Sdavidxu	} else
436144518Sdavidxu		(*attr)->prio = param->sched_priority;
437112918Sjeff
438144518Sdavidxu	return(ret);
439112918Sjeff}
440112918Sjeff
441144518Sdavidxu__weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
442144518Sdavidxu
443112918Sjeffint
444144518Sdavidxu_pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
445112918Sjeff{
446144518Sdavidxu	int ret = 0;
447112918Sjeff
448144518Sdavidxu	if ((attr == NULL) || (*attr == NULL))
449144518Sdavidxu		ret = EINVAL;
450144518Sdavidxu	else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
451144518Sdavidxu		ret = ENOTSUP;
452144518Sdavidxu	} else
453144518Sdavidxu		(*attr)->sched_policy = policy;
454112918Sjeff
455144518Sdavidxu	return(ret);
456112918Sjeff}
457112918Sjeff
458144518Sdavidxu__weak_reference(_pthread_attr_setscope, pthread_attr_setscope);
459144518Sdavidxu
460112918Sjeffint
461144518Sdavidxu_pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
462112918Sjeff{
463144518Sdavidxu	int ret = 0;
464112918Sjeff
465144518Sdavidxu	if ((attr == NULL) || (*attr == NULL)) {
466144518Sdavidxu		/* Return an invalid argument: */
467144518Sdavidxu		ret = EINVAL;
468144518Sdavidxu	} else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
469144518Sdavidxu	    (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
470144518Sdavidxu		ret = EINVAL;
471144518Sdavidxu	} else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
472144518Sdavidxu		(*attr)->flags |= contentionscope;
473144518Sdavidxu	} else {
474144518Sdavidxu		(*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
475144518Sdavidxu	}
476144518Sdavidxu	return (ret);
477112918Sjeff}
478112918Sjeff
479144518Sdavidxu__weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
480144518Sdavidxu
481112918Sjeffint
482144518Sdavidxu_pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
483144518Sdavidxu                        size_t stacksize)
484112918Sjeff{
485144518Sdavidxu	int     ret;
486144518Sdavidxu
487144518Sdavidxu	/* Check for invalid arguments: */
488112918Sjeff	if (attr == NULL || *attr == NULL || stackaddr == NULL
489144518Sdavidxu	    || stacksize < PTHREAD_STACK_MIN)
490144518Sdavidxu		ret = EINVAL;
491144518Sdavidxu	else {
492144518Sdavidxu		/* Save the stack address and stack size */
493144518Sdavidxu		(*attr)->stackaddr_attr = stackaddr;
494144518Sdavidxu		(*attr)->stacksize_attr = stacksize;
495144518Sdavidxu		ret = 0;
496144518Sdavidxu	}
497144518Sdavidxu	return(ret);
498144518Sdavidxu}
499112918Sjeff
500144518Sdavidxu__weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
501112918Sjeff
502112918Sjeffint
503144518Sdavidxu_pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
504112918Sjeff{
505144518Sdavidxu	int	ret;
506144518Sdavidxu
507144518Sdavidxu	/* Check for invalid arguments: */
508112918Sjeff	if (attr == NULL || *attr == NULL || stackaddr == NULL)
509144518Sdavidxu		ret = EINVAL;
510144518Sdavidxu	else {
511144518Sdavidxu		/* Save the stack address: */
512144518Sdavidxu		(*attr)->stackaddr_attr = stackaddr;
513144518Sdavidxu		ret = 0;
514144518Sdavidxu	}
515144518Sdavidxu	return(ret);
516144518Sdavidxu}
517112918Sjeff
518144518Sdavidxu__weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);
519112918Sjeff
520112918Sjeffint
521144518Sdavidxu_pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
522112918Sjeff{
523144518Sdavidxu	int	ret;
524112918Sjeff
525144518Sdavidxu	/* Check for invalid arguments: */
526144518Sdavidxu	if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
527144518Sdavidxu		ret = EINVAL;
528144518Sdavidxu	else {
529144518Sdavidxu		/* Save the stack size: */
530144518Sdavidxu		(*attr)->stacksize_attr = stacksize;
531144518Sdavidxu		ret = 0;
532144518Sdavidxu	}
533144518Sdavidxu	return(ret);
534112918Sjeff}
535