xref: /netbsd-src/lib/libpthread/pthread_attr.c (revision 568eb77ef2a2b1fbe2457d69c7aa6e1dd3d8de8b)
1*568eb77eSriastradh /*	$NetBSD: pthread_attr.c,v 1.21 2022/04/10 10:38:33 riastradh Exp $	*/
2ec2c1698Snathanw 
3ec2c1698Snathanw /*-
4cbd43ffaSad  * Copyright (c) 2001, 2002, 2003, 2008 The NetBSD Foundation, Inc.
5ec2c1698Snathanw  * All rights reserved.
6ec2c1698Snathanw  *
7ec2c1698Snathanw  * This code is derived from software contributed to The NetBSD Foundation
8ec2c1698Snathanw  * by Nathan J. Williams.
9ec2c1698Snathanw  *
10ec2c1698Snathanw  * Redistribution and use in source and binary forms, with or without
11ec2c1698Snathanw  * modification, are permitted provided that the following conditions
12ec2c1698Snathanw  * are met:
13ec2c1698Snathanw  * 1. Redistributions of source code must retain the above copyright
14ec2c1698Snathanw  *    notice, this list of conditions and the following disclaimer.
15ec2c1698Snathanw  * 2. Redistributions in binary form must reproduce the above copyright
16ec2c1698Snathanw  *    notice, this list of conditions and the following disclaimer in the
17ec2c1698Snathanw  *    documentation and/or other materials provided with the distribution.
18ec2c1698Snathanw  *
19ec2c1698Snathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20ec2c1698Snathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21ec2c1698Snathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22ec2c1698Snathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23ec2c1698Snathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ec2c1698Snathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ec2c1698Snathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ec2c1698Snathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ec2c1698Snathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ec2c1698Snathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ec2c1698Snathanw  * POSSIBILITY OF SUCH DAMAGE.
30ec2c1698Snathanw  */
31ec2c1698Snathanw 
32ec2c1698Snathanw #include <sys/cdefs.h>
33*568eb77eSriastradh __RCSID("$NetBSD: pthread_attr.c,v 1.21 2022/04/10 10:38:33 riastradh Exp $");
347adb4107Sriastradh 
357adb4107Sriastradh /* Need to use libc-private names for atomic operations. */
367adb4107Sriastradh #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
37ec2c1698Snathanw 
38ec2c1698Snathanw #include <errno.h>
39ec2c1698Snathanw #include <stdio.h>
40ec2c1698Snathanw #include <stdlib.h>
41ec2c1698Snathanw #include <string.h>
42ec2c1698Snathanw #include <unistd.h>
43ec2c1698Snathanw 
44e8deba04Schristos #ifndef __lint__
45e8deba04Schristos #define pthread_attr_get_np _pthread_attr_get_np
46e8deba04Schristos #endif
47e8deba04Schristos 
485861d73fSchristos #include "pthread.h"
495861d73fSchristos #include "pthread_int.h"
505861d73fSchristos 
51e8deba04Schristos __weak_alias(pthread_attr_get_np, _pthread_attr_get_np)
52e8deba04Schristos 
53c6409540Schristos static struct pthread_attr_private *pthread__attr_init_private(
54c6409540Schristos     pthread_attr_t *);
55ec2c1698Snathanw 
56ec2c1698Snathanw static struct pthread_attr_private *
pthread__attr_init_private(pthread_attr_t * attr)57ec2c1698Snathanw pthread__attr_init_private(pthread_attr_t *attr)
58ec2c1698Snathanw {
59ec2c1698Snathanw 	struct pthread_attr_private *p;
60ec2c1698Snathanw 
61ec2c1698Snathanw 	if ((p = attr->pta_private) != NULL)
62ec2c1698Snathanw 		return p;
63ec2c1698Snathanw 
647f878ea3Smartin 	p = calloc(1, sizeof(*p));
65ec2c1698Snathanw 	if (p != NULL) {
66ec2c1698Snathanw 		attr->pta_private = p;
67cbd43ffaSad 		p->ptap_policy = SCHED_OTHER;
687f878ea3Smartin 		p->ptap_stacksize = pthread__stacksize;
697f878ea3Smartin 		p->ptap_guardsize = pthread__guardsize;
70ec2c1698Snathanw 	}
71ec2c1698Snathanw 	return p;
72ec2c1698Snathanw }
73ec2c1698Snathanw 
74ec2c1698Snathanw 
75ec2c1698Snathanw int
pthread_attr_init(pthread_attr_t * attr)76ec2c1698Snathanw pthread_attr_init(pthread_attr_t *attr)
77ec2c1698Snathanw {
78ec2c1698Snathanw 
79ec2c1698Snathanw 	attr->pta_magic = PT_ATTR_MAGIC;
80ec2c1698Snathanw 	attr->pta_flags = 0;
81ec2c1698Snathanw 	attr->pta_private = NULL;
82ec2c1698Snathanw 
83ec2c1698Snathanw 	return 0;
84ec2c1698Snathanw }
85ec2c1698Snathanw 
86ec2c1698Snathanw 
87ec2c1698Snathanw int
pthread_attr_destroy(pthread_attr_t * attr)88ec2c1698Snathanw pthread_attr_destroy(pthread_attr_t *attr)
89ec2c1698Snathanw {
90ec2c1698Snathanw 	struct pthread_attr_private *p;
91ec2c1698Snathanw 
92b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
93b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
94b3401c13Skamil 
95ec2c1698Snathanw 	if ((p = attr->pta_private) != NULL)
96ec2c1698Snathanw 		free(p);
97ec2c1698Snathanw 
98b3401c13Skamil 	attr->pta_magic = PT_ATTR_DEAD;
99b3401c13Skamil 
100ec2c1698Snathanw 	return 0;
101ec2c1698Snathanw }
102ec2c1698Snathanw 
103ec2c1698Snathanw 
104ec2c1698Snathanw int
pthread_attr_get_np(pthread_t thread,pthread_attr_t * attr)105ec2c1698Snathanw pthread_attr_get_np(pthread_t thread, pthread_attr_t *attr)
106ec2c1698Snathanw {
107ec2c1698Snathanw 	struct pthread_attr_private *p;
108ec2c1698Snathanw 
109b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
110b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
111b3401c13Skamil 
112ec2c1698Snathanw 	p = pthread__attr_init_private(attr);
113ec2c1698Snathanw 	if (p == NULL)
114ec2c1698Snathanw 		return ENOMEM;
115ec2c1698Snathanw 
116ec2c1698Snathanw 	attr->pta_flags = thread->pt_flags &
117ec2c1698Snathanw 	    (PT_FLAG_DETACHED | PT_FLAG_SCOPE_SYSTEM | PT_FLAG_EXPLICIT_SCHED);
118ec2c1698Snathanw 
119ec2c1698Snathanw 	p->ptap_namearg = thread->pt_name;
120ec2c1698Snathanw 	p->ptap_stackaddr = thread->pt_stack.ss_sp;
121ec2c1698Snathanw 	p->ptap_stacksize = thread->pt_stack.ss_size;
1225f391f4aSjoerg 	p->ptap_guardsize = thread->pt_guardsize;
123cbd43ffaSad 	return pthread_getschedparam(thread, &p->ptap_policy, &p->ptap_sp);
124ec2c1698Snathanw }
125ec2c1698Snathanw 
126ec2c1698Snathanw 
127ec2c1698Snathanw int
pthread_attr_getdetachstate(const pthread_attr_t * attr,int * detachstate)128ec2c1698Snathanw pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
129ec2c1698Snathanw {
130ec2c1698Snathanw 
131b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
132b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
133b3401c13Skamil 
134ec2c1698Snathanw 	if (attr->pta_flags & PT_FLAG_DETACHED)
135ec2c1698Snathanw 		*detachstate = PTHREAD_CREATE_DETACHED;
136ec2c1698Snathanw 	else
137ec2c1698Snathanw 		*detachstate = PTHREAD_CREATE_JOINABLE;
138ec2c1698Snathanw 
139ec2c1698Snathanw 	return 0;
140ec2c1698Snathanw }
141ec2c1698Snathanw 
142ec2c1698Snathanw 
143ec2c1698Snathanw int
pthread_attr_setdetachstate(pthread_attr_t * attr,int detachstate)144ec2c1698Snathanw pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
145ec2c1698Snathanw {
146ec2c1698Snathanw 
147b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
148b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
149b3401c13Skamil 
150ec2c1698Snathanw 	switch (detachstate) {
151ec2c1698Snathanw 	case PTHREAD_CREATE_JOINABLE:
152ec2c1698Snathanw 		attr->pta_flags &= ~PT_FLAG_DETACHED;
153ec2c1698Snathanw 		break;
154ec2c1698Snathanw 	case PTHREAD_CREATE_DETACHED:
155ec2c1698Snathanw 		attr->pta_flags |= PT_FLAG_DETACHED;
156ec2c1698Snathanw 		break;
157ec2c1698Snathanw 	default:
158ec2c1698Snathanw 		return EINVAL;
159ec2c1698Snathanw 	}
160ec2c1698Snathanw 
161ec2c1698Snathanw 	return 0;
162ec2c1698Snathanw }
163ec2c1698Snathanw 
164ec2c1698Snathanw 
165ec2c1698Snathanw int
pthread_attr_getguardsize(const pthread_attr_t * attr,size_t * guard)166ec2c1698Snathanw pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guard)
167ec2c1698Snathanw {
168ec2c1698Snathanw 	struct pthread_attr_private *p;
169ec2c1698Snathanw 
170b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
171b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
172b3401c13Skamil 
173ec2c1698Snathanw 	if ((p = attr->pta_private) == NULL)
1745f391f4aSjoerg 		*guard = pthread__guardsize;
175ec2c1698Snathanw 	else
176ec2c1698Snathanw 		*guard = p->ptap_guardsize;
177ec2c1698Snathanw 
178ec2c1698Snathanw 	return 0;
179ec2c1698Snathanw }
180ec2c1698Snathanw 
181ec2c1698Snathanw 
182ec2c1698Snathanw int
pthread_attr_setguardsize(pthread_attr_t * attr,size_t guard)183ec2c1698Snathanw pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard)
184ec2c1698Snathanw {
185ec2c1698Snathanw 	struct pthread_attr_private *p;
186ec2c1698Snathanw 
187b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
188b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
189b3401c13Skamil 
190ec2c1698Snathanw 	p = pthread__attr_init_private(attr);
191ec2c1698Snathanw 	if (p == NULL)
192ec2c1698Snathanw 		return ENOMEM;
193ec2c1698Snathanw 
194ec2c1698Snathanw 	p->ptap_guardsize = guard;
195ec2c1698Snathanw 
196ec2c1698Snathanw 	return 0;
197ec2c1698Snathanw }
198ec2c1698Snathanw 
199ec2c1698Snathanw 
200ec2c1698Snathanw int
pthread_attr_getinheritsched(const pthread_attr_t * attr,int * inherit)201ec2c1698Snathanw pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
202ec2c1698Snathanw {
203ec2c1698Snathanw 
204b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
205b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
206b3401c13Skamil 
207ec2c1698Snathanw 	if (attr->pta_flags & PT_FLAG_EXPLICIT_SCHED)
208ec2c1698Snathanw 		*inherit = PTHREAD_EXPLICIT_SCHED;
209ec2c1698Snathanw 	else
210ec2c1698Snathanw 		*inherit = PTHREAD_INHERIT_SCHED;
211ec2c1698Snathanw 
212ec2c1698Snathanw 	return 0;
213ec2c1698Snathanw }
214ec2c1698Snathanw 
215ec2c1698Snathanw 
216ec2c1698Snathanw int
pthread_attr_setinheritsched(pthread_attr_t * attr,int inherit)217ec2c1698Snathanw pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
218ec2c1698Snathanw {
219ec2c1698Snathanw 
220b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
221b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
222b3401c13Skamil 
223ec2c1698Snathanw 	switch (inherit) {
224ec2c1698Snathanw 	case PTHREAD_INHERIT_SCHED:
225ec2c1698Snathanw 		attr->pta_flags &= ~PT_FLAG_EXPLICIT_SCHED;
226ec2c1698Snathanw 		break;
227ec2c1698Snathanw 	case PTHREAD_EXPLICIT_SCHED:
228ec2c1698Snathanw 		attr->pta_flags |= PT_FLAG_EXPLICIT_SCHED;
229ec2c1698Snathanw 		break;
230ec2c1698Snathanw 	default:
231ec2c1698Snathanw 		return EINVAL;
232ec2c1698Snathanw 	}
233ec2c1698Snathanw 
234ec2c1698Snathanw 	return 0;
235ec2c1698Snathanw }
236ec2c1698Snathanw 
237ec2c1698Snathanw 
238ec2c1698Snathanw int
pthread_attr_getscope(const pthread_attr_t * attr,int * scope)239ec2c1698Snathanw pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
240ec2c1698Snathanw {
241ec2c1698Snathanw 
242b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
243b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
244b3401c13Skamil 
245ec2c1698Snathanw 	if (attr->pta_flags & PT_FLAG_SCOPE_SYSTEM)
246ec2c1698Snathanw 		*scope = PTHREAD_SCOPE_SYSTEM;
247ec2c1698Snathanw 	else
248ec2c1698Snathanw 		*scope = PTHREAD_SCOPE_PROCESS;
249ec2c1698Snathanw 
250ec2c1698Snathanw 	return 0;
251ec2c1698Snathanw }
252ec2c1698Snathanw 
253ec2c1698Snathanw 
254ec2c1698Snathanw int
pthread_attr_setscope(pthread_attr_t * attr,int scope)255ec2c1698Snathanw pthread_attr_setscope(pthread_attr_t *attr, int scope)
256ec2c1698Snathanw {
257ec2c1698Snathanw 
258b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
259b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
260b3401c13Skamil 
261ec2c1698Snathanw 	switch (scope) {
262ec2c1698Snathanw 	case PTHREAD_SCOPE_PROCESS:
263ec2c1698Snathanw 		attr->pta_flags &= ~PT_FLAG_SCOPE_SYSTEM;
264ec2c1698Snathanw 		break;
265ec2c1698Snathanw 	case PTHREAD_SCOPE_SYSTEM:
266ec2c1698Snathanw 		attr->pta_flags |= PT_FLAG_SCOPE_SYSTEM;
267ec2c1698Snathanw 		break;
268ec2c1698Snathanw 	default:
269ec2c1698Snathanw 		return EINVAL;
270ec2c1698Snathanw 	}
271ec2c1698Snathanw 
272ec2c1698Snathanw 	return 0;
273ec2c1698Snathanw }
274ec2c1698Snathanw 
275ec2c1698Snathanw 
276ec2c1698Snathanw int
pthread_attr_setschedparam(pthread_attr_t * attr,const struct sched_param * param)277ec2c1698Snathanw pthread_attr_setschedparam(pthread_attr_t *attr,
278ec2c1698Snathanw 			   const struct sched_param *param)
279ec2c1698Snathanw {
280cbd43ffaSad 	struct pthread_attr_private *p;
281cbd43ffaSad 	int error;
282ec2c1698Snathanw 
283b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
284b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
285b3401c13Skamil 
286ec2c1698Snathanw 	if (param == NULL)
287ec2c1698Snathanw 		return EINVAL;
288cbd43ffaSad 	p = pthread__attr_init_private(attr);
289cbd43ffaSad 	if (p == NULL)
290cbd43ffaSad 		return ENOMEM;
291cbd43ffaSad 	error = pthread__checkpri(param->sched_priority);
292cbd43ffaSad 	if (error == 0)
293cbd43ffaSad 		p->ptap_sp = *param;
294cbd43ffaSad 	return error;
295ec2c1698Snathanw }
296ec2c1698Snathanw 
297ec2c1698Snathanw 
298ec2c1698Snathanw int
pthread_attr_getschedparam(const pthread_attr_t * attr,struct sched_param * param)299ec2c1698Snathanw pthread_attr_getschedparam(const pthread_attr_t *attr,
300ec2c1698Snathanw 			   struct sched_param *param)
301ec2c1698Snathanw {
302cbd43ffaSad 	struct pthread_attr_private *p;
303ec2c1698Snathanw 
304b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
305b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
306b3401c13Skamil 
307ec2c1698Snathanw 	if (param == NULL)
308ec2c1698Snathanw 		return EINVAL;
309cbd43ffaSad 	p = attr->pta_private;
310cbd43ffaSad 	if (p == NULL)
3116f7d0483Sad 		memset(param, 0, sizeof(*param));
3126f7d0483Sad 	else
313cbd43ffaSad 		*param = p->ptap_sp;
314ec2c1698Snathanw 	return 0;
315ec2c1698Snathanw }
316ec2c1698Snathanw 
317ec2c1698Snathanw 
318ec2c1698Snathanw int
pthread_attr_setschedpolicy(pthread_attr_t * attr,int policy)319cbd43ffaSad pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
320fae5965cSnathanw {
321cbd43ffaSad 	struct pthread_attr_private *p;
322fae5965cSnathanw 
323b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
324b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
325cbd43ffaSad 
326cbd43ffaSad 	switch (policy) {
327cbd43ffaSad 	case SCHED_OTHER:
328cbd43ffaSad 	case SCHED_FIFO:
329cbd43ffaSad 	case SCHED_RR:
330cbd43ffaSad 		p = pthread__attr_init_private(attr);
331cbd43ffaSad 		if (p == NULL)
332cbd43ffaSad 			return ENOMEM;
333cbd43ffaSad 		p->ptap_policy = policy;
334cbd43ffaSad 		return 0;
335cbd43ffaSad 	default:
336fae5965cSnathanw 		return ENOTSUP;
337cbd43ffaSad 	}
338fae5965cSnathanw }
339fae5965cSnathanw 
340fae5965cSnathanw 
341fae5965cSnathanw int
pthread_attr_getschedpolicy(const pthread_attr_t * attr,int * policy)342cbd43ffaSad pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
343fae5965cSnathanw {
344cbd43ffaSad 	struct pthread_attr_private *p;
345fae5965cSnathanw 
346b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
347b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
348b3401c13Skamil 
349cbd43ffaSad 	p = attr->pta_private;
350cbd43ffaSad 	if (p == NULL) {
35121eb6bbaStv 		*policy = SCHED_OTHER;
352cbd43ffaSad 		return 0;
353cbd43ffaSad 	}
354cbd43ffaSad 	*policy = p->ptap_policy;
355fae5965cSnathanw 	return 0;
356fae5965cSnathanw }
357fae5965cSnathanw 
358fae5965cSnathanw 
359fae5965cSnathanw int
pthread_attr_getstack(const pthread_attr_t * attr,void ** addr,size_t * size)360ec2c1698Snathanw pthread_attr_getstack(const pthread_attr_t *attr, void **addr, size_t *size)
361ec2c1698Snathanw {
362ec2c1698Snathanw 	struct pthread_attr_private *p;
363ec2c1698Snathanw 
364b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
365b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
366b3401c13Skamil 
367ec2c1698Snathanw 	if ((p = attr->pta_private) == NULL) {
368ec2c1698Snathanw 		*addr = NULL;
3699e287199Sad 		*size = pthread__stacksize;
370ec2c1698Snathanw 	} else {
371ec2c1698Snathanw 		*addr = p->ptap_stackaddr;
372ec2c1698Snathanw 		*size = p->ptap_stacksize;
373ec2c1698Snathanw 	}
374ec2c1698Snathanw 
375ec2c1698Snathanw 	return 0;
376ec2c1698Snathanw }
377ec2c1698Snathanw 
378ec2c1698Snathanw 
379ec2c1698Snathanw int
pthread_attr_setstack(pthread_attr_t * attr,void * addr,size_t size)380ec2c1698Snathanw pthread_attr_setstack(pthread_attr_t *attr, void *addr, size_t size)
381ec2c1698Snathanw {
382ec2c1698Snathanw 	struct pthread_attr_private *p;
383ec2c1698Snathanw 
384b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
385b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
386b3401c13Skamil 
387ec2c1698Snathanw 	p = pthread__attr_init_private(attr);
388ec2c1698Snathanw 	if (p == NULL)
389ec2c1698Snathanw 		return ENOMEM;
390ec2c1698Snathanw 
391ec2c1698Snathanw 	p->ptap_stackaddr = addr;
392ec2c1698Snathanw 	p->ptap_stacksize = size;
393ec2c1698Snathanw 
394ec2c1698Snathanw 	return 0;
395ec2c1698Snathanw }
396ec2c1698Snathanw 
397ec2c1698Snathanw 
398ec2c1698Snathanw int
pthread_attr_getstacksize(const pthread_attr_t * attr,size_t * size)399ec2c1698Snathanw pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *size)
400ec2c1698Snathanw {
401ec2c1698Snathanw 	struct pthread_attr_private *p;
402ec2c1698Snathanw 
403b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
404b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
405b3401c13Skamil 
406ec2c1698Snathanw 	if ((p = attr->pta_private) == NULL)
4079e287199Sad 		*size = pthread__stacksize;
408ec2c1698Snathanw 	else
409ec2c1698Snathanw 		*size = p->ptap_stacksize;
410ec2c1698Snathanw 
411ec2c1698Snathanw 	return 0;
412ec2c1698Snathanw }
413ec2c1698Snathanw 
414ec2c1698Snathanw 
415ec2c1698Snathanw int
pthread_attr_setstacksize(pthread_attr_t * attr,size_t size)416ec2c1698Snathanw pthread_attr_setstacksize(pthread_attr_t *attr, size_t size)
417ec2c1698Snathanw {
418ec2c1698Snathanw 	struct pthread_attr_private *p;
419ec2c1698Snathanw 
420b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
421b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
422b3401c13Skamil 
423a06595c2Slukem 	if (size < (size_t)sysconf(_SC_THREAD_STACK_MIN))
424403a3991Sad 		return EINVAL;
425403a3991Sad 
426ec2c1698Snathanw 	p = pthread__attr_init_private(attr);
427ec2c1698Snathanw 	if (p == NULL)
428ec2c1698Snathanw 		return ENOMEM;
429ec2c1698Snathanw 
430ec2c1698Snathanw 	p->ptap_stacksize = size;
431ec2c1698Snathanw 
432ec2c1698Snathanw 	return 0;
433ec2c1698Snathanw }
434ec2c1698Snathanw 
435ec2c1698Snathanw 
436ec2c1698Snathanw int
pthread_attr_getstackaddr(const pthread_attr_t * attr,void ** addr)437ec2c1698Snathanw pthread_attr_getstackaddr(const pthread_attr_t *attr, void **addr)
438ec2c1698Snathanw {
439ec2c1698Snathanw 	struct pthread_attr_private *p;
440ec2c1698Snathanw 
441b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
442b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
443b3401c13Skamil 
444ec2c1698Snathanw 	if ((p = attr->pta_private) == NULL)
445ec2c1698Snathanw 		*addr = NULL;
446ec2c1698Snathanw 	else
447ec2c1698Snathanw 		*addr = p->ptap_stackaddr;
448ec2c1698Snathanw 
449ec2c1698Snathanw 	return 0;
450ec2c1698Snathanw }
451ec2c1698Snathanw 
452ec2c1698Snathanw 
453ec2c1698Snathanw int
pthread_attr_setstackaddr(pthread_attr_t * attr,void * addr)454ec2c1698Snathanw pthread_attr_setstackaddr(pthread_attr_t *attr, void *addr)
455ec2c1698Snathanw {
456ec2c1698Snathanw 	struct pthread_attr_private *p;
457ec2c1698Snathanw 
458b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
459b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
460b3401c13Skamil 
461ec2c1698Snathanw 	p = pthread__attr_init_private(attr);
462ec2c1698Snathanw 	if (p == NULL)
463ec2c1698Snathanw 		return ENOMEM;
464ec2c1698Snathanw 
465ec2c1698Snathanw 	p->ptap_stackaddr = addr;
466ec2c1698Snathanw 
467ec2c1698Snathanw 	return 0;
468ec2c1698Snathanw }
469ec2c1698Snathanw 
470ec2c1698Snathanw 
471ec2c1698Snathanw int
pthread_attr_getname_np(const pthread_attr_t * attr,char * name,size_t len,void ** argp)472ec2c1698Snathanw pthread_attr_getname_np(const pthread_attr_t *attr, char *name, size_t len,
473ec2c1698Snathanw     void **argp)
474ec2c1698Snathanw {
475ec2c1698Snathanw 	struct pthread_attr_private *p;
476ec2c1698Snathanw 
477b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
478b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
479b3401c13Skamil 
480ec2c1698Snathanw 	if ((p = attr->pta_private) == NULL) {
481ec2c1698Snathanw 		name[0] = '\0';
482ec2c1698Snathanw 		if (argp != NULL)
483ec2c1698Snathanw 			*argp = NULL;
484ec2c1698Snathanw 	} else {
485ec2c1698Snathanw 		strlcpy(name, p->ptap_name, len);
486ec2c1698Snathanw 		if (argp != NULL)
487ec2c1698Snathanw 			*argp = p->ptap_namearg;
488ec2c1698Snathanw 	}
489ec2c1698Snathanw 
490ec2c1698Snathanw 	return 0;
491ec2c1698Snathanw }
492ec2c1698Snathanw 
493ec2c1698Snathanw 
494ec2c1698Snathanw int
pthread_attr_setname_np(pthread_attr_t * attr,const char * name,void * arg)495ec2c1698Snathanw pthread_attr_setname_np(pthread_attr_t *attr, const char *name, void *arg)
496ec2c1698Snathanw {
497ec2c1698Snathanw 	struct pthread_attr_private *p;
498ec2c1698Snathanw 	int namelen;
499ec2c1698Snathanw 
500b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
501b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
502b3401c13Skamil 
503ec2c1698Snathanw 	p = pthread__attr_init_private(attr);
504ec2c1698Snathanw 	if (p == NULL)
505ec2c1698Snathanw 		return ENOMEM;
506ec2c1698Snathanw 
507ec2c1698Snathanw 	namelen = snprintf(p->ptap_name, PTHREAD_MAX_NAMELEN_NP, name, arg);
508ec2c1698Snathanw 	if (namelen >= PTHREAD_MAX_NAMELEN_NP) {
509ec2c1698Snathanw 		p->ptap_name[0] = '\0';
510ec2c1698Snathanw 		return EINVAL;
511ec2c1698Snathanw 	}
512ec2c1698Snathanw 	p->ptap_namearg = arg;
513ec2c1698Snathanw 
514ec2c1698Snathanw 	return 0;
515ec2c1698Snathanw }
51638b1c6f4Schristos 
51738b1c6f4Schristos int
pthread_attr_setcreatesuspend_np(pthread_attr_t * attr)51838b1c6f4Schristos pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
51938b1c6f4Schristos {
520b3401c13Skamil 
521b3401c13Skamil 	pthread__error(EINVAL, "Invalid attribute",
522b3401c13Skamil 	    attr->pta_magic == PT_ATTR_MAGIC);
523b3401c13Skamil 
52438b1c6f4Schristos 	attr->pta_flags |= PT_FLAG_SUSPENDED;
52538b1c6f4Schristos 	return 0;
52638b1c6f4Schristos }
52732d8a489Schristos 
52832d8a489Schristos int
pthread_getattr_np(pthread_t thread,pthread_attr_t * attr)52932d8a489Schristos pthread_getattr_np(pthread_t thread, pthread_attr_t *attr)
53032d8a489Schristos {
53132d8a489Schristos 	int error;
532b3401c13Skamil 
53332d8a489Schristos 	if ((error = pthread_attr_init(attr)) != 0)
53432d8a489Schristos 		return error;
53532d8a489Schristos 	if ((error = pthread_attr_get_np(thread, attr)) != 0) {
53632d8a489Schristos 		(void)pthread_attr_destroy(attr);
53732d8a489Schristos 		return error;
53832d8a489Schristos 	}
53932d8a489Schristos 	return 0;
54032d8a489Schristos }
541