1 /* $NetBSD: pthread_attr.c,v 1.11 2008/10/10 09:13:20 ad Exp $ */
4 * Copyright (c) 2001, 2002, 2003, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: pthread_attr.c,v 1.11 2008/10/10 09:13:20 ad Exp $");
42 #include "pthread_int.h"
44 static struct pthread_attr_private
*pthread__attr_init_private(
47 static struct pthread_attr_private
*
48 pthread__attr_init_private(pthread_attr_t
*attr
)
50 struct pthread_attr_private
*p
;
52 if ((p
= attr
->pta_private
) != NULL
)
55 p
= malloc(sizeof(*p
));
57 memset(p
, 0, sizeof(*p
));
58 attr
->pta_private
= p
;
59 p
->ptap_policy
= SCHED_OTHER
;
66 pthread_attr_init(pthread_attr_t
*attr
)
69 attr
->pta_magic
= PT_ATTR_MAGIC
;
71 attr
->pta_private
= NULL
;
78 pthread_attr_destroy(pthread_attr_t
*attr
)
80 struct pthread_attr_private
*p
;
82 if ((p
= attr
->pta_private
) != NULL
)
90 pthread_attr_get_np(pthread_t thread
, pthread_attr_t
*attr
)
92 struct pthread_attr_private
*p
;
94 p
= pthread__attr_init_private(attr
);
98 attr
->pta_flags
= thread
->pt_flags
&
99 (PT_FLAG_DETACHED
| PT_FLAG_SCOPE_SYSTEM
| PT_FLAG_EXPLICIT_SCHED
);
101 p
->ptap_namearg
= thread
->pt_name
;
102 p
->ptap_stackaddr
= thread
->pt_stack
.ss_sp
;
103 p
->ptap_stacksize
= thread
->pt_stack
.ss_size
;
104 p
->ptap_guardsize
= (size_t)sysconf(_SC_PAGESIZE
);
105 return pthread_getschedparam(thread
, &p
->ptap_policy
, &p
->ptap_sp
);
110 pthread_attr_getdetachstate(const pthread_attr_t
*attr
, int *detachstate
)
113 if (attr
->pta_flags
& PT_FLAG_DETACHED
)
114 *detachstate
= PTHREAD_CREATE_DETACHED
;
116 *detachstate
= PTHREAD_CREATE_JOINABLE
;
123 pthread_attr_setdetachstate(pthread_attr_t
*attr
, int detachstate
)
126 switch (detachstate
) {
127 case PTHREAD_CREATE_JOINABLE
:
128 attr
->pta_flags
&= ~PT_FLAG_DETACHED
;
130 case PTHREAD_CREATE_DETACHED
:
131 attr
->pta_flags
|= PT_FLAG_DETACHED
;
142 pthread_attr_getguardsize(const pthread_attr_t
*attr
, size_t *guard
)
144 struct pthread_attr_private
*p
;
146 if ((p
= attr
->pta_private
) == NULL
)
147 *guard
= (size_t)sysconf(_SC_PAGESIZE
);
149 *guard
= p
->ptap_guardsize
;
156 pthread_attr_setguardsize(pthread_attr_t
*attr
, size_t guard
)
158 struct pthread_attr_private
*p
;
160 p
= pthread__attr_init_private(attr
);
164 p
->ptap_guardsize
= guard
;
171 pthread_attr_getinheritsched(const pthread_attr_t
*attr
, int *inherit
)
174 if (attr
->pta_flags
& PT_FLAG_EXPLICIT_SCHED
)
175 *inherit
= PTHREAD_EXPLICIT_SCHED
;
177 *inherit
= PTHREAD_INHERIT_SCHED
;
184 pthread_attr_setinheritsched(pthread_attr_t
*attr
, int inherit
)
188 case PTHREAD_INHERIT_SCHED
:
189 attr
->pta_flags
&= ~PT_FLAG_EXPLICIT_SCHED
;
191 case PTHREAD_EXPLICIT_SCHED
:
192 attr
->pta_flags
|= PT_FLAG_EXPLICIT_SCHED
;
203 pthread_attr_getscope(const pthread_attr_t
*attr
, int *scope
)
206 if (attr
->pta_flags
& PT_FLAG_SCOPE_SYSTEM
)
207 *scope
= PTHREAD_SCOPE_SYSTEM
;
209 *scope
= PTHREAD_SCOPE_PROCESS
;
216 pthread_attr_setscope(pthread_attr_t
*attr
, int scope
)
220 case PTHREAD_SCOPE_PROCESS
:
221 attr
->pta_flags
&= ~PT_FLAG_SCOPE_SYSTEM
;
223 case PTHREAD_SCOPE_SYSTEM
:
224 attr
->pta_flags
|= PT_FLAG_SCOPE_SYSTEM
;
235 pthread_attr_setschedparam(pthread_attr_t
*attr
,
236 const struct sched_param
*param
)
238 struct pthread_attr_private
*p
;
243 p
= pthread__attr_init_private(attr
);
246 error
= pthread__checkpri(param
->sched_priority
);
254 pthread_attr_getschedparam(const pthread_attr_t
*attr
,
255 struct sched_param
*param
)
257 struct pthread_attr_private
*p
;
261 p
= attr
->pta_private
;
263 memset(param
, 0, sizeof(*param
));
271 pthread_attr_setschedpolicy(pthread_attr_t
*attr
, int policy
)
273 struct pthread_attr_private
*p
;
280 p
= pthread__attr_init_private(attr
);
283 p
->ptap_policy
= policy
;
292 pthread_attr_getschedpolicy(const pthread_attr_t
*attr
, int *policy
)
294 struct pthread_attr_private
*p
;
296 p
= attr
->pta_private
;
298 *policy
= SCHED_OTHER
;
301 *policy
= p
->ptap_policy
;
307 pthread_attr_getstack(const pthread_attr_t
*attr
, void **addr
, size_t *size
)
309 struct pthread_attr_private
*p
;
311 if ((p
= attr
->pta_private
) == NULL
) {
313 *size
= pthread__stacksize
;
315 *addr
= p
->ptap_stackaddr
;
316 *size
= p
->ptap_stacksize
;
324 pthread_attr_setstack(pthread_attr_t
*attr
, void *addr
, size_t size
)
326 struct pthread_attr_private
*p
;
328 p
= pthread__attr_init_private(attr
);
332 p
->ptap_stackaddr
= addr
;
333 p
->ptap_stacksize
= size
;
340 pthread_attr_getstacksize(const pthread_attr_t
*attr
, size_t *size
)
342 struct pthread_attr_private
*p
;
344 if ((p
= attr
->pta_private
) == NULL
)
345 *size
= pthread__stacksize
;
347 *size
= p
->ptap_stacksize
;
354 pthread_attr_setstacksize(pthread_attr_t
*attr
, size_t size
)
356 struct pthread_attr_private
*p
;
358 if (size
< (size_t)sysconf(_SC_THREAD_STACK_MIN
))
361 p
= pthread__attr_init_private(attr
);
365 p
->ptap_stacksize
= size
;
372 pthread_attr_getstackaddr(const pthread_attr_t
*attr
, void **addr
)
374 struct pthread_attr_private
*p
;
376 if ((p
= attr
->pta_private
) == NULL
)
379 *addr
= p
->ptap_stackaddr
;
386 pthread_attr_setstackaddr(pthread_attr_t
*attr
, void *addr
)
388 struct pthread_attr_private
*p
;
390 p
= pthread__attr_init_private(attr
);
394 p
->ptap_stackaddr
= addr
;
401 pthread_attr_getname_np(const pthread_attr_t
*attr
, char *name
, size_t len
,
404 struct pthread_attr_private
*p
;
406 if ((p
= attr
->pta_private
) == NULL
) {
411 strlcpy(name
, p
->ptap_name
, len
);
413 *argp
= p
->ptap_namearg
;
421 pthread_attr_setname_np(pthread_attr_t
*attr
, const char *name
, void *arg
)
423 struct pthread_attr_private
*p
;
426 p
= pthread__attr_init_private(attr
);
430 namelen
= snprintf(p
->ptap_name
, PTHREAD_MAX_NAMELEN_NP
, name
, arg
);
431 if (namelen
>= PTHREAD_MAX_NAMELEN_NP
) {
432 p
->ptap_name
[0] = '\0';
435 p
->ptap_namearg
= arg
;
441 pthread_attr_setcreatesuspend_np(pthread_attr_t
*attr
)
443 attr
->pta_flags
|= PT_FLAG_SUSPENDED
;