thr_attr.c revision 212536
1104478Ssam/*
2104630Ssam * Copyright (c) 2003 Craig Rodrigues <rodrigc@attbi.com>.
3104478Ssam * All rights reserved.
4139749Simp *
5104478Ssam * Redistribution and use in source and binary forms, with or without
6104478Ssam * modification, are permitted provided that the following conditions
7104478Ssam * are met:
8104478Ssam * 1. Redistributions of source code must retain the above copyright
9104478Ssam *    notice, this list of conditions and the following disclaimer.
10104478Ssam * 2. Redistributions in binary form must reproduce the above copyright
11104478Ssam *    notice, this list of conditions and the following disclaimer in the
12104478Ssam *    documentation and/or other materials provided with the distribution.
13104478Ssam * 3. All advertising materials mentioning features or use of this software
14104478Ssam *    must display the following acknowledgement:
15104478Ssam *	This product includes software developed by Craig Rodrigues.
16104478Ssam * 4. Neither the name of the author nor the names of any co-contributors
17104478Ssam *    may be used to endorse or promote products derived from this software
18104478Ssam *    without specific prior written permission.
19104478Ssam *
20104478Ssam * THIS SOFTWARE IS PROVIDED BY CRAIG RODRIGUES AND CONTRIBUTORS ``AS IS'' AND
21104478Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22104478Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23104478Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24104478Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25104478Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26104478Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27104478Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28104478Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29104478Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30104478Ssam * SUCH DAMAGE.
31104478Ssam *
32104478Ssam */
33104478Ssam
34104478Ssam/*
35104478Ssam * Copyright (c) 1998 Daniel Eischen <eischen@vigrid.com>.
36104478Ssam * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.
37104478Ssam * Copyright (c) 2002,2003 Alexey Zelkin <phantom@FreeBSD.org>
38104478Ssam * All rights reserved.
39104478Ssam *
40104478Ssam * Redistribution and use in source and binary forms, with or without
41104478Ssam * modification, are permitted provided that the following conditions
42104478Ssam * are met:
43104478Ssam * 1. Redistributions of source code must retain the above copyright
44104478Ssam *    notice(s), this list of conditions and the following disclaimer
45104478Ssam *    unmodified other than the allowable addition of one or more
46104478Ssam *    copyright notices.
47104478Ssam * 2. Redistributions in binary form must reproduce the above copyright
48104478Ssam *    notice(s), this list of conditions and the following disclaimer in
49114105Ssam *    the documentation and/or other materials provided with the
50104478Ssam *    distribution.
51104478Ssam *
52104478Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
53104478Ssam * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54104478Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55104478Ssam * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
56104478Ssam * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57104478Ssam * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58111646Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
59111646Ssam * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
60104478Ssam * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
61104478Ssam * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
62104478Ssam * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63104478Ssam */
64110522Ssam
65191894Sphilip/*
66104478Ssam * Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
67114105Ssam * All rights reserved.
68114105Ssam *
69114105Ssam * Redistribution and use in source and binary forms, with or without
70114105Ssam * modification, are permitted provided that the following conditions
71104478Ssam * are met:
72104478Ssam * 1. Redistributions of source code must retain the above copyright
73104478Ssam *    notice, this list of conditions and the following disclaimer.
74104478Ssam * 2. Redistributions in binary form must reproduce the above copyright
75104478Ssam *    notice, this list of conditions and the following disclaimer in the
76104478Ssam *    documentation and/or other materials provided with the distribution.
77104478Ssam * 3. Neither the name of the author nor the names of any co-contributors
78104478Ssam *    may be used to endorse or promote products derived from this software
79104478Ssam *    without specific prior written permission.
80104478Ssam *
81104478Ssam * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
82104478Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83104478Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84104478Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
85104478Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86104478Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87104478Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88104478Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89104478Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90104478Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91104478Ssam * SUCH DAMAGE.
92104478Ssam *
93104478Ssam * $FreeBSD: head/lib/libthr/thread/thr_attr.c 212536 2010-09-13 07:03:01Z davidxu $
94104478Ssam */
95104478Ssam
96104478Ssam#include "namespace.h"
97104478Ssam#include <errno.h>
98104630Ssam#include <pthread.h>
99104478Ssam#include <stdlib.h>
100104478Ssam#include <string.h>
101104478Ssam#include <pthread_np.h>
102104478Ssam#include <sys/sysctl.h>
103104630Ssam#include "un-namespace.h"
104104478Ssam
105104630Ssam#include "thr_private.h"
106104478Ssam
107104478Ssam__weak_reference(_pthread_attr_destroy, pthread_attr_destroy);
108104478Ssam
109104478Ssamint
110104478Ssam_pthread_attr_destroy(pthread_attr_t *attr)
111104478Ssam{
112104478Ssam	int	ret;
113104478Ssam
114104630Ssam	/* Check for invalid arguments: */
115104630Ssam	if (attr == NULL || *attr == NULL)
116104478Ssam		/* Invalid argument: */
117104478Ssam		ret = EINVAL;
118104478Ssam	else {
119104478Ssam		/* Free the memory allocated to the attribute object: */
120104478Ssam		free(*attr);
121104478Ssam
122104478Ssam		/*
123104478Ssam		 * Leave the attribute pointer NULL now that the memory
124104478Ssam		 * has been freed:
125104478Ssam		 */
126104478Ssam		*attr = NULL;
127104478Ssam		ret = 0;
128104478Ssam	}
129104478Ssam	return(ret);
130104478Ssam}
131104478Ssam
132104478Ssam__weak_reference(_pthread_attr_get_np, pthread_attr_get_np);
133104478Ssam
134104478Ssamint
135104478Ssam_pthread_attr_get_np(pthread_t pthread, pthread_attr_t *dst)
136104478Ssam{
137104478Ssam	struct pthread *curthread;
138104478Ssam	struct pthread_attr attr;
139104478Ssam	int	ret;
140104478Ssam
141104478Ssam	if (pthread == NULL || dst == NULL || *dst == NULL)
142104478Ssam		return (EINVAL);
143104478Ssam
144104478Ssam	curthread = _get_curthread();
145104478Ssam	if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)) != 0)
146104478Ssam		return (ret);
147104478Ssam	attr = pthread->attr;
148104478Ssam	if (pthread->flags & THR_FLAGS_DETACHED)
149104478Ssam		attr.flags |= PTHREAD_DETACHED;
150104478Ssam	THR_THREAD_UNLOCK(curthread, pthread);
151104478Ssam
152104478Ssam	memcpy(*dst, &attr, sizeof(struct pthread_attr));
153104478Ssam	/* XXX */
154104478Ssam	(*dst)->cpuset = NULL;
155104478Ssam	(*dst)->cpusetsize = 0;
156104478Ssam	return (0);
157104478Ssam}
158104478Ssam
159104478Ssam__weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate);
160104478Ssam
161104478Ssamint
162104478Ssam_pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
163104478Ssam{
164104478Ssam	int	ret;
165104478Ssam
166104478Ssam	/* Check for invalid arguments: */
167104478Ssam	if (attr == NULL || *attr == NULL || detachstate == NULL)
168104478Ssam		ret = EINVAL;
169104478Ssam	else {
170104478Ssam		/* Check if the detached flag is set: */
171104478Ssam		if ((*attr)->flags & PTHREAD_DETACHED)
172104478Ssam			/* Return detached: */
173104478Ssam			*detachstate = PTHREAD_CREATE_DETACHED;
174104478Ssam		else
175104478Ssam			/* Return joinable: */
176104478Ssam			*detachstate = PTHREAD_CREATE_JOINABLE;
177104478Ssam		ret = 0;
178104478Ssam	}
179104478Ssam	return(ret);
180104478Ssam}
181104478Ssam
182104478Ssam__weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize);
183104478Ssam
184104478Ssamint
185104478Ssam_pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
186104478Ssam{
187104478Ssam	int	ret;
188104478Ssam
189104478Ssam	/* Check for invalid arguments: */
190104478Ssam	if (attr == NULL || *attr == NULL || guardsize == NULL)
191104478Ssam		ret = EINVAL;
192104478Ssam	else {
193104478Ssam		/* Return the guard size: */
194104478Ssam		*guardsize = (*attr)->guardsize_attr;
195104478Ssam		ret = 0;
196104478Ssam	}
197104478Ssam	return(ret);
198104478Ssam}
199104478Ssam
200104478Ssam__weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched);
201104478Ssam
202104478Ssamint
203104478Ssam_pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit)
204104478Ssam{
205104478Ssam	int ret = 0;
206104478Ssam
207104478Ssam	if ((attr == NULL) || (*attr == NULL))
208104478Ssam		ret = EINVAL;
209104478Ssam	else
210104630Ssam		*sched_inherit = (*attr)->sched_inherit;
211104478Ssam
212104478Ssam	return(ret);
213104478Ssam}
214104478Ssam
215104478Ssam__weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam);
216104478Ssam
217104478Ssamint
218104478Ssam_pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
219104478Ssam{
220104478Ssam	int ret = 0;
221
222	if ((attr == NULL) || (*attr == NULL) || (param == NULL))
223		ret = EINVAL;
224	else
225		param->sched_priority = (*attr)->prio;
226
227	return(ret);
228}
229
230__weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy);
231
232int
233_pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
234{
235	int ret = 0;
236
237	if ((attr == NULL) || (*attr == NULL) || (policy == NULL))
238		ret = EINVAL;
239	else
240		*policy = (*attr)->sched_policy;
241
242	return(ret);
243}
244
245__weak_reference(_pthread_attr_getscope, pthread_attr_getscope);
246
247int
248_pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
249{
250	int ret = 0;
251
252	if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL))
253		/* Return an invalid argument: */
254		ret = EINVAL;
255
256	else
257		*contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ?
258		    PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS;
259
260	return(ret);
261}
262
263__weak_reference(_pthread_attr_getstack, pthread_attr_getstack);
264
265int
266_pthread_attr_getstack(const pthread_attr_t * __restrict attr,
267                        void ** __restrict stackaddr,
268                        size_t * __restrict stacksize)
269{
270	int     ret;
271
272	/* Check for invalid arguments: */
273	if (attr == NULL || *attr == NULL || stackaddr == NULL
274	    || stacksize == NULL )
275		ret = EINVAL;
276	else {
277		/* Return the stack address and size */
278		*stackaddr = (*attr)->stackaddr_attr;
279		*stacksize = (*attr)->stacksize_attr;
280		ret = 0;
281	}
282	return(ret);
283}
284
285__weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr);
286
287int
288_pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
289{
290	int	ret;
291
292	/* Check for invalid arguments: */
293	if (attr == NULL || *attr == NULL || stackaddr == NULL)
294		ret = EINVAL;
295	else {
296		/* Return the stack address: */
297		*stackaddr = (*attr)->stackaddr_attr;
298		ret = 0;
299	}
300	return(ret);
301}
302
303__weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize);
304
305int
306_pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
307{
308	int	ret;
309
310	/* Check for invalid arguments: */
311	if (attr == NULL || *attr == NULL || stacksize  == NULL)
312		ret = EINVAL;
313	else {
314		/* Return the stack size: */
315		*stacksize = (*attr)->stacksize_attr;
316		ret = 0;
317	}
318	return(ret);
319}
320
321__weak_reference(_pthread_attr_init, pthread_attr_init);
322
323int
324_pthread_attr_init(pthread_attr_t *attr)
325{
326	int	ret;
327	pthread_attr_t	pattr;
328
329	_thr_check_init();
330
331	/* Allocate memory for the attribute object: */
332	if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL)
333		/* Insufficient memory: */
334		ret = ENOMEM;
335	else {
336		/* Initialise the attribute object with the defaults: */
337		memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr));
338
339		/* Return a pointer to the attribute object: */
340		*attr = pattr;
341		ret = 0;
342	}
343	return(ret);
344}
345
346__weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np);
347
348int
349_pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
350{
351	int	ret;
352
353	if (attr == NULL || *attr == NULL) {
354		ret = EINVAL;
355	} else {
356		(*attr)->suspend = THR_CREATE_SUSPENDED;
357		ret = 0;
358	}
359	return(ret);
360}
361
362__weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate);
363
364int
365_pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
366{
367	int	ret;
368
369	/* Check for invalid arguments: */
370	if (attr == NULL || *attr == NULL ||
371	    (detachstate != PTHREAD_CREATE_DETACHED &&
372	    detachstate != PTHREAD_CREATE_JOINABLE))
373		ret = EINVAL;
374	else {
375		/* Check if detached state: */
376		if (detachstate == PTHREAD_CREATE_DETACHED)
377			/* Set the detached flag: */
378			(*attr)->flags |= PTHREAD_DETACHED;
379		else
380			/* Reset the detached flag: */
381			(*attr)->flags &= ~PTHREAD_DETACHED;
382		ret = 0;
383	}
384	return(ret);
385}
386
387__weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize);
388
389int
390_pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
391{
392	int	ret;
393
394	/* Check for invalid arguments. */
395	if (attr == NULL || *attr == NULL)
396		ret = EINVAL;
397	else {
398		/* Save the stack size. */
399		(*attr)->guardsize_attr = guardsize;
400		ret = 0;
401	}
402	return(ret);
403}
404
405__weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched);
406
407int
408_pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit)
409{
410	int ret = 0;
411
412	if ((attr == NULL) || (*attr == NULL))
413		ret = EINVAL;
414	else if (sched_inherit != PTHREAD_INHERIT_SCHED &&
415		 sched_inherit != PTHREAD_EXPLICIT_SCHED)
416		ret = ENOTSUP;
417	else
418		(*attr)->sched_inherit = sched_inherit;
419
420	return(ret);
421}
422
423__weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam);
424
425int
426_pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
427{
428	int policy;
429
430	if ((attr == NULL) || (*attr == NULL))
431		return (EINVAL);
432
433	if (param == NULL)
434		return (ENOTSUP);
435
436	policy = (*attr)->sched_policy;
437
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)
441		return (ENOTSUP);
442	} else {
443		/*
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.
447		 */
448	}
449
450	(*attr)->prio = param->sched_priority;
451
452	return (0);
453}
454
455__weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy);
456
457int
458_pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
459{
460	int ret = 0;
461
462	if ((attr == NULL) || (*attr == NULL))
463		ret = EINVAL;
464	else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) {
465		ret = ENOTSUP;
466	} else {
467		(*attr)->sched_policy = policy;
468		(*attr)->prio = _thr_priorities[policy-1].pri_default;
469	}
470	return(ret);
471}
472
473__weak_reference(_pthread_attr_setscope, pthread_attr_setscope);
474
475int
476_pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
477{
478	int ret = 0;
479
480	if ((attr == NULL) || (*attr == NULL)) {
481		/* Return an invalid argument: */
482		ret = EINVAL;
483	} else if ((contentionscope != PTHREAD_SCOPE_PROCESS) &&
484	    (contentionscope != PTHREAD_SCOPE_SYSTEM)) {
485		ret = EINVAL;
486	} else if (contentionscope == PTHREAD_SCOPE_SYSTEM) {
487		(*attr)->flags |= contentionscope;
488	} else {
489		(*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM;
490	}
491	return (ret);
492}
493
494__weak_reference(_pthread_attr_setstack, pthread_attr_setstack);
495
496int
497_pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
498                        size_t stacksize)
499{
500	int     ret;
501
502	/* Check for invalid arguments: */
503	if (attr == NULL || *attr == NULL || stackaddr == NULL
504	    || stacksize < PTHREAD_STACK_MIN)
505		ret = EINVAL;
506	else {
507		/* Save the stack address and stack size */
508		(*attr)->stackaddr_attr = stackaddr;
509		(*attr)->stacksize_attr = stacksize;
510		ret = 0;
511	}
512	return(ret);
513}
514
515__weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr);
516
517int
518_pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
519{
520	int	ret;
521
522	/* Check for invalid arguments: */
523	if (attr == NULL || *attr == NULL || stackaddr == NULL)
524		ret = EINVAL;
525	else {
526		/* Save the stack address: */
527		(*attr)->stackaddr_attr = stackaddr;
528		ret = 0;
529	}
530	return(ret);
531}
532
533__weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize);
534
535int
536_pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
537{
538	int	ret;
539
540	/* Check for invalid arguments: */
541	if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN)
542		ret = EINVAL;
543	else {
544		/* Save the stack size: */
545		(*attr)->stacksize_attr = stacksize;
546		ret = 0;
547	}
548	return(ret);
549}
550
551static size_t
552_get_kern_cpuset_size(void)
553{
554	static int kern_cpuset_size = 0;
555
556	if (kern_cpuset_size == 0) {
557		size_t len;
558
559		len = sizeof(kern_cpuset_size);
560		if (sysctlbyname("kern.smp.maxcpus", &kern_cpuset_size,
561		    &len, NULL, 0))
562			PANIC("failed to get sysctl kern.smp.maxcpus");
563
564		kern_cpuset_size = (kern_cpuset_size + 7) / 8;
565	}
566
567	return (kern_cpuset_size);
568}
569
570__weak_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np);
571int
572_pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize,
573	const cpuset_t *cpusetp)
574{
575	pthread_attr_t attr;
576	int ret;
577
578	if (pattr == NULL || (attr = (*pattr)) == NULL)
579		ret = EINVAL;
580	else {
581		if (cpusetsize == 0 || cpusetp == NULL) {
582			if (attr->cpuset != NULL) {
583				free(attr->cpuset);
584				attr->cpuset = NULL;
585				attr->cpusetsize = 0;
586			}
587			return (0);
588		}
589
590		if (cpusetsize > attr->cpusetsize) {
591			size_t kern_size = _get_kern_cpuset_size();
592			if (cpusetsize > kern_size) {
593				size_t i;
594				for (i = kern_size; i < cpusetsize; ++i) {
595					if (((char *)cpusetp)[i])
596						return (EINVAL);
597				}
598			}
599			void *newset = realloc(attr->cpuset, cpusetsize);
600       			if (newset == NULL)
601		            return (ENOMEM);
602			attr->cpuset = newset;
603			attr->cpusetsize = cpusetsize;
604		} else {
605			memset(((char *)attr->cpuset) + cpusetsize, 0,
606				attr->cpusetsize - cpusetsize);
607			attr->cpusetsize = cpusetsize;
608		}
609		memcpy(attr->cpuset, cpusetp, cpusetsize);
610		ret = 0;
611	}
612	return (ret);
613}
614
615__weak_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np);
616int
617_pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize,
618	cpuset_t *cpusetp)
619{
620	pthread_attr_t attr;
621	int ret = 0;
622
623	if (pattr == NULL || (attr = (*pattr)) == NULL)
624		ret = EINVAL;
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);
630	} else {
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);
636	}
637	return (ret);
638}
639