pthread_attr.c revision 1.4 1 1.4 nathanw /* $NetBSD: pthread_attr.c,v 1.4 2004/12/29 00:59:57 nathanw Exp $ */
2 1.1 nathanw
3 1.1 nathanw /*-
4 1.1 nathanw * Copyright (c) 2001,2002,2003 The NetBSD Foundation, Inc.
5 1.1 nathanw * All rights reserved.
6 1.1 nathanw *
7 1.1 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.1 nathanw * by Nathan J. Williams.
9 1.1 nathanw *
10 1.1 nathanw * Redistribution and use in source and binary forms, with or without
11 1.1 nathanw * modification, are permitted provided that the following conditions
12 1.1 nathanw * are met:
13 1.1 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.1 nathanw * notice, this list of conditions and the following disclaimer.
15 1.1 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.1 nathanw * documentation and/or other materials provided with the distribution.
18 1.1 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.1 nathanw * must display the following acknowledgement:
20 1.1 nathanw * This product includes software developed by the NetBSD
21 1.1 nathanw * Foundation, Inc. and its contributors.
22 1.1 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 nathanw * contributors may be used to endorse or promote products derived
24 1.1 nathanw * from this software without specific prior written permission.
25 1.1 nathanw *
26 1.1 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.1 nathanw */
38 1.1 nathanw
39 1.1 nathanw #include <sys/cdefs.h>
40 1.4 nathanw __RCSID("$NetBSD: pthread_attr.c,v 1.4 2004/12/29 00:59:57 nathanw Exp $");
41 1.1 nathanw
42 1.1 nathanw #include <errno.h>
43 1.1 nathanw #include <stdio.h>
44 1.1 nathanw #include <stdlib.h>
45 1.1 nathanw #include <string.h>
46 1.1 nathanw #include <unistd.h>
47 1.1 nathanw
48 1.1 nathanw #include "pthread.h"
49 1.1 nathanw #include "pthread_int.h"
50 1.1 nathanw
51 1.1 nathanw
52 1.1 nathanw static struct pthread_attr_private *
53 1.1 nathanw pthread__attr_init_private(pthread_attr_t *attr)
54 1.1 nathanw {
55 1.1 nathanw struct pthread_attr_private *p;
56 1.1 nathanw
57 1.1 nathanw if ((p = attr->pta_private) != NULL)
58 1.1 nathanw return p;
59 1.1 nathanw
60 1.1 nathanw p = malloc(sizeof(*p));
61 1.1 nathanw if (p != NULL) {
62 1.1 nathanw memset(p, 0, sizeof(*p));
63 1.1 nathanw attr->pta_private = p;
64 1.1 nathanw }
65 1.1 nathanw return p;
66 1.1 nathanw }
67 1.1 nathanw
68 1.1 nathanw
69 1.1 nathanw int
70 1.1 nathanw pthread_attr_init(pthread_attr_t *attr)
71 1.1 nathanw {
72 1.1 nathanw
73 1.1 nathanw attr->pta_magic = PT_ATTR_MAGIC;
74 1.1 nathanw attr->pta_flags = 0;
75 1.1 nathanw attr->pta_private = NULL;
76 1.1 nathanw
77 1.1 nathanw return 0;
78 1.1 nathanw }
79 1.1 nathanw
80 1.1 nathanw
81 1.1 nathanw int
82 1.1 nathanw pthread_attr_destroy(pthread_attr_t *attr)
83 1.1 nathanw {
84 1.1 nathanw struct pthread_attr_private *p;
85 1.1 nathanw
86 1.1 nathanw if ((p = attr->pta_private) != NULL)
87 1.1 nathanw free(p);
88 1.1 nathanw
89 1.1 nathanw return 0;
90 1.1 nathanw }
91 1.1 nathanw
92 1.1 nathanw
93 1.1 nathanw int
94 1.1 nathanw pthread_attr_get_np(pthread_t thread, pthread_attr_t *attr)
95 1.1 nathanw {
96 1.1 nathanw struct pthread_attr_private *p;
97 1.1 nathanw
98 1.1 nathanw p = pthread__attr_init_private(attr);
99 1.1 nathanw if (p == NULL)
100 1.1 nathanw return ENOMEM;
101 1.1 nathanw
102 1.1 nathanw attr->pta_flags = thread->pt_flags &
103 1.1 nathanw (PT_FLAG_DETACHED | PT_FLAG_SCOPE_SYSTEM | PT_FLAG_EXPLICIT_SCHED);
104 1.1 nathanw
105 1.1 nathanw p->ptap_namearg = thread->pt_name;
106 1.1 nathanw p->ptap_stackaddr = thread->pt_stack.ss_sp;
107 1.1 nathanw p->ptap_stacksize = thread->pt_stack.ss_size;
108 1.1 nathanw p->ptap_guardsize = (size_t)sysconf(_SC_PAGESIZE);
109 1.1 nathanw
110 1.1 nathanw return 0;
111 1.1 nathanw }
112 1.1 nathanw
113 1.1 nathanw
114 1.1 nathanw int
115 1.1 nathanw pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
116 1.1 nathanw {
117 1.1 nathanw
118 1.1 nathanw if (attr->pta_flags & PT_FLAG_DETACHED)
119 1.1 nathanw *detachstate = PTHREAD_CREATE_DETACHED;
120 1.1 nathanw else
121 1.1 nathanw *detachstate = PTHREAD_CREATE_JOINABLE;
122 1.1 nathanw
123 1.1 nathanw return 0;
124 1.1 nathanw }
125 1.1 nathanw
126 1.1 nathanw
127 1.1 nathanw int
128 1.1 nathanw pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
129 1.1 nathanw {
130 1.1 nathanw
131 1.1 nathanw switch (detachstate) {
132 1.1 nathanw case PTHREAD_CREATE_JOINABLE:
133 1.1 nathanw attr->pta_flags &= ~PT_FLAG_DETACHED;
134 1.1 nathanw break;
135 1.1 nathanw case PTHREAD_CREATE_DETACHED:
136 1.1 nathanw attr->pta_flags |= PT_FLAG_DETACHED;
137 1.1 nathanw break;
138 1.1 nathanw default:
139 1.1 nathanw return EINVAL;
140 1.1 nathanw }
141 1.1 nathanw
142 1.1 nathanw return 0;
143 1.1 nathanw }
144 1.1 nathanw
145 1.1 nathanw
146 1.1 nathanw int
147 1.1 nathanw pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guard)
148 1.1 nathanw {
149 1.1 nathanw struct pthread_attr_private *p;
150 1.1 nathanw
151 1.1 nathanw if ((p = attr->pta_private) == NULL)
152 1.1 nathanw *guard = (size_t)sysconf(_SC_PAGESIZE);
153 1.1 nathanw else
154 1.1 nathanw *guard = p->ptap_guardsize;
155 1.1 nathanw
156 1.1 nathanw return 0;
157 1.1 nathanw }
158 1.1 nathanw
159 1.1 nathanw
160 1.1 nathanw int
161 1.1 nathanw pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard)
162 1.1 nathanw {
163 1.1 nathanw struct pthread_attr_private *p;
164 1.1 nathanw
165 1.1 nathanw p = pthread__attr_init_private(attr);
166 1.1 nathanw if (p == NULL)
167 1.1 nathanw return ENOMEM;
168 1.1 nathanw
169 1.1 nathanw p->ptap_guardsize = guard;
170 1.1 nathanw
171 1.1 nathanw return 0;
172 1.1 nathanw }
173 1.1 nathanw
174 1.1 nathanw
175 1.1 nathanw int
176 1.1 nathanw pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
177 1.1 nathanw {
178 1.1 nathanw
179 1.1 nathanw if (attr->pta_flags & PT_FLAG_EXPLICIT_SCHED)
180 1.1 nathanw *inherit = PTHREAD_EXPLICIT_SCHED;
181 1.1 nathanw else
182 1.1 nathanw *inherit = PTHREAD_INHERIT_SCHED;
183 1.1 nathanw
184 1.1 nathanw return 0;
185 1.1 nathanw }
186 1.1 nathanw
187 1.1 nathanw
188 1.1 nathanw int
189 1.1 nathanw pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
190 1.1 nathanw {
191 1.1 nathanw
192 1.1 nathanw switch (inherit) {
193 1.1 nathanw case PTHREAD_INHERIT_SCHED:
194 1.1 nathanw attr->pta_flags &= ~PT_FLAG_EXPLICIT_SCHED;
195 1.1 nathanw break;
196 1.1 nathanw case PTHREAD_EXPLICIT_SCHED:
197 1.1 nathanw attr->pta_flags |= PT_FLAG_EXPLICIT_SCHED;
198 1.1 nathanw break;
199 1.1 nathanw default:
200 1.1 nathanw return EINVAL;
201 1.1 nathanw }
202 1.1 nathanw
203 1.1 nathanw return 0;
204 1.1 nathanw }
205 1.1 nathanw
206 1.1 nathanw
207 1.1 nathanw int
208 1.1 nathanw pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
209 1.1 nathanw {
210 1.1 nathanw
211 1.1 nathanw if (attr->pta_flags & PT_FLAG_SCOPE_SYSTEM)
212 1.1 nathanw *scope = PTHREAD_SCOPE_SYSTEM;
213 1.1 nathanw else
214 1.1 nathanw *scope = PTHREAD_SCOPE_PROCESS;
215 1.1 nathanw
216 1.1 nathanw return 0;
217 1.1 nathanw }
218 1.1 nathanw
219 1.1 nathanw
220 1.1 nathanw int
221 1.1 nathanw pthread_attr_setscope(pthread_attr_t *attr, int scope)
222 1.1 nathanw {
223 1.1 nathanw
224 1.1 nathanw switch (scope) {
225 1.1 nathanw case PTHREAD_SCOPE_PROCESS:
226 1.1 nathanw attr->pta_flags &= ~PT_FLAG_SCOPE_SYSTEM;
227 1.1 nathanw break;
228 1.1 nathanw case PTHREAD_SCOPE_SYSTEM:
229 1.1 nathanw attr->pta_flags |= PT_FLAG_SCOPE_SYSTEM;
230 1.1 nathanw break;
231 1.1 nathanw default:
232 1.1 nathanw return EINVAL;
233 1.1 nathanw }
234 1.1 nathanw
235 1.1 nathanw return 0;
236 1.1 nathanw }
237 1.1 nathanw
238 1.1 nathanw
239 1.1 nathanw int
240 1.2 christos /*ARGSUSED*/
241 1.1 nathanw pthread_attr_setschedparam(pthread_attr_t *attr,
242 1.1 nathanw const struct sched_param *param)
243 1.1 nathanw {
244 1.1 nathanw
245 1.1 nathanw if (param == NULL)
246 1.1 nathanw return EINVAL;
247 1.1 nathanw
248 1.1 nathanw if (param->sched_priority != 0)
249 1.1 nathanw return EINVAL;
250 1.1 nathanw
251 1.1 nathanw return 0;
252 1.1 nathanw }
253 1.1 nathanw
254 1.1 nathanw
255 1.1 nathanw int
256 1.2 christos /*ARGSUSED*/
257 1.1 nathanw pthread_attr_getschedparam(const pthread_attr_t *attr,
258 1.1 nathanw struct sched_param *param)
259 1.1 nathanw {
260 1.1 nathanw
261 1.1 nathanw if (param == NULL)
262 1.1 nathanw return EINVAL;
263 1.1 nathanw
264 1.1 nathanw param->sched_priority = 0;
265 1.1 nathanw
266 1.1 nathanw return 0;
267 1.1 nathanw }
268 1.1 nathanw
269 1.1 nathanw
270 1.1 nathanw int
271 1.4 nathanw /*ARGSUSED*/
272 1.4 nathanw pthread_attr_setschedpolicy(pthread_attr_t *attr,
273 1.4 nathanw int policy)
274 1.4 nathanw {
275 1.4 nathanw
276 1.4 nathanw if (policy != SCHED_OTHER)
277 1.4 nathanw return ENOTSUP;
278 1.4 nathanw
279 1.4 nathanw return 0;
280 1.4 nathanw }
281 1.4 nathanw
282 1.4 nathanw
283 1.4 nathanw int
284 1.4 nathanw /*ARGSUSED*/
285 1.4 nathanw pthread_attr_getschedpolicy(const pthread_attr_t *attr,
286 1.4 nathanw int *policy)
287 1.4 nathanw {
288 1.4 nathanw
289 1.4 nathanw policy = SCHED_OTHER;
290 1.4 nathanw
291 1.4 nathanw return 0;
292 1.4 nathanw }
293 1.4 nathanw
294 1.4 nathanw
295 1.4 nathanw int
296 1.1 nathanw pthread_attr_getstack(const pthread_attr_t *attr, void **addr, size_t *size)
297 1.1 nathanw {
298 1.1 nathanw struct pthread_attr_private *p;
299 1.1 nathanw
300 1.1 nathanw if ((p = attr->pta_private) == NULL) {
301 1.1 nathanw *addr = NULL;
302 1.1 nathanw *size = PT_STACKSIZE;
303 1.1 nathanw } else {
304 1.1 nathanw *addr = p->ptap_stackaddr;
305 1.1 nathanw *size = p->ptap_stacksize;
306 1.1 nathanw }
307 1.1 nathanw
308 1.1 nathanw return 0;
309 1.1 nathanw }
310 1.1 nathanw
311 1.1 nathanw
312 1.1 nathanw int
313 1.1 nathanw pthread_attr_setstack(pthread_attr_t *attr, void *addr, size_t size)
314 1.1 nathanw {
315 1.1 nathanw struct pthread_attr_private *p;
316 1.1 nathanw
317 1.1 nathanw p = pthread__attr_init_private(attr);
318 1.1 nathanw if (p == NULL)
319 1.1 nathanw return ENOMEM;
320 1.1 nathanw
321 1.1 nathanw p->ptap_stackaddr = addr;
322 1.1 nathanw p->ptap_stacksize = size;
323 1.1 nathanw
324 1.1 nathanw return 0;
325 1.1 nathanw }
326 1.1 nathanw
327 1.1 nathanw
328 1.1 nathanw int
329 1.1 nathanw pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *size)
330 1.1 nathanw {
331 1.1 nathanw struct pthread_attr_private *p;
332 1.1 nathanw
333 1.1 nathanw if ((p = attr->pta_private) == NULL)
334 1.1 nathanw *size = PT_STACKSIZE;
335 1.1 nathanw else
336 1.1 nathanw *size = p->ptap_stacksize;
337 1.1 nathanw
338 1.1 nathanw return 0;
339 1.1 nathanw }
340 1.1 nathanw
341 1.1 nathanw
342 1.1 nathanw int
343 1.1 nathanw pthread_attr_setstacksize(pthread_attr_t *attr, size_t size)
344 1.1 nathanw {
345 1.1 nathanw struct pthread_attr_private *p;
346 1.1 nathanw
347 1.1 nathanw p = pthread__attr_init_private(attr);
348 1.1 nathanw if (p == NULL)
349 1.1 nathanw return ENOMEM;
350 1.1 nathanw
351 1.1 nathanw p->ptap_stacksize = size;
352 1.1 nathanw
353 1.1 nathanw return 0;
354 1.1 nathanw }
355 1.1 nathanw
356 1.1 nathanw
357 1.1 nathanw int
358 1.1 nathanw pthread_attr_getstackaddr(const pthread_attr_t *attr, void **addr)
359 1.1 nathanw {
360 1.1 nathanw struct pthread_attr_private *p;
361 1.1 nathanw
362 1.1 nathanw if ((p = attr->pta_private) == NULL)
363 1.1 nathanw *addr = NULL;
364 1.1 nathanw else
365 1.1 nathanw *addr = p->ptap_stackaddr;
366 1.1 nathanw
367 1.1 nathanw return 0;
368 1.1 nathanw }
369 1.1 nathanw
370 1.1 nathanw
371 1.1 nathanw int
372 1.1 nathanw pthread_attr_setstackaddr(pthread_attr_t *attr, void *addr)
373 1.1 nathanw {
374 1.1 nathanw struct pthread_attr_private *p;
375 1.1 nathanw
376 1.1 nathanw p = pthread__attr_init_private(attr);
377 1.1 nathanw if (p == NULL)
378 1.1 nathanw return ENOMEM;
379 1.1 nathanw
380 1.1 nathanw p->ptap_stackaddr = addr;
381 1.1 nathanw
382 1.1 nathanw return 0;
383 1.1 nathanw }
384 1.1 nathanw
385 1.1 nathanw
386 1.1 nathanw int
387 1.1 nathanw pthread_attr_getname_np(const pthread_attr_t *attr, char *name, size_t len,
388 1.1 nathanw void **argp)
389 1.1 nathanw {
390 1.1 nathanw struct pthread_attr_private *p;
391 1.1 nathanw
392 1.1 nathanw if ((p = attr->pta_private) == NULL) {
393 1.1 nathanw name[0] = '\0';
394 1.1 nathanw if (argp != NULL)
395 1.1 nathanw *argp = NULL;
396 1.1 nathanw } else {
397 1.1 nathanw strlcpy(name, p->ptap_name, len);
398 1.1 nathanw if (argp != NULL)
399 1.1 nathanw *argp = p->ptap_namearg;
400 1.1 nathanw }
401 1.1 nathanw
402 1.1 nathanw return 0;
403 1.1 nathanw }
404 1.1 nathanw
405 1.1 nathanw
406 1.1 nathanw int
407 1.1 nathanw pthread_attr_setname_np(pthread_attr_t *attr, const char *name, void *arg)
408 1.1 nathanw {
409 1.1 nathanw struct pthread_attr_private *p;
410 1.1 nathanw int namelen;
411 1.1 nathanw
412 1.1 nathanw p = pthread__attr_init_private(attr);
413 1.1 nathanw if (p == NULL)
414 1.1 nathanw return ENOMEM;
415 1.1 nathanw
416 1.1 nathanw namelen = snprintf(p->ptap_name, PTHREAD_MAX_NAMELEN_NP, name, arg);
417 1.1 nathanw if (namelen >= PTHREAD_MAX_NAMELEN_NP) {
418 1.1 nathanw p->ptap_name[0] = '\0';
419 1.1 nathanw return EINVAL;
420 1.1 nathanw }
421 1.1 nathanw p->ptap_namearg = arg;
422 1.1 nathanw
423 1.3 christos return 0;
424 1.3 christos }
425 1.3 christos
426 1.3 christos int
427 1.3 christos pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
428 1.3 christos {
429 1.3 christos attr->pta_flags |= PT_FLAG_SUSPENDED;
430 1.1 nathanw return 0;
431 1.1 nathanw }
432