Update.
[glibc.git] / linuxthreads / attr.c
blob2a70ebe67420e2ccf89b9882d03cca336966b3c1
1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
4 /* */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
9 /* */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
15 /* Handling of thread attributes */
17 #include <errno.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/param.h>
21 #include "pthread.h"
22 #include "internals.h"
24 int __pthread_attr_init_2_1(pthread_attr_t *attr)
26 size_t ps = __getpagesize ();
28 attr->__detachstate = PTHREAD_CREATE_JOINABLE;
29 attr->__schedpolicy = SCHED_OTHER;
30 attr->__schedparam.sched_priority = 0;
31 attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
32 attr->__scope = PTHREAD_SCOPE_SYSTEM;
33 attr->__guardsize = ps;
34 attr->__stackaddr = NULL;
35 attr->__stackaddr_set = 0;
36 attr->__stacksize = STACK_SIZE - ps;
37 return 0;
39 #if defined HAVE_ELF && defined PIC && defined DO_VERSIONING
40 default_symbol_version (__pthread_attr_init_2_1, pthread_attr_init, GLIBC_2.1);
42 int __pthread_attr_init_2_0(pthread_attr_t *attr)
44 attr->__detachstate = PTHREAD_CREATE_JOINABLE;
45 attr->__schedpolicy = SCHED_OTHER;
46 attr->__schedparam.sched_priority = 0;
47 attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
48 attr->__scope = PTHREAD_SCOPE_SYSTEM;
49 return 0;
51 symbol_version (__pthread_attr_init_2_0, pthread_attr_init, GLIBC_2.0);
52 #else
53 strong_alias (__pthread_attr_init_2_1, pthread_attr_init)
54 #endif
56 int pthread_attr_destroy(pthread_attr_t *attr)
58 return 0;
61 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
63 if (detachstate < PTHREAD_CREATE_JOINABLE ||
64 detachstate > PTHREAD_CREATE_DETACHED)
65 return EINVAL;
66 attr->__detachstate = detachstate;
67 return 0;
70 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
72 *detachstate = attr->__detachstate;
73 return 0;
76 int pthread_attr_setschedparam(pthread_attr_t *attr,
77 const struct sched_param *param)
79 int max_prio = __sched_get_priority_max(attr->__schedpolicy);
80 int min_prio = __sched_get_priority_min(attr->__schedpolicy);
82 if (param->sched_priority < min_prio || param->sched_priority > max_prio)
83 return EINVAL;
84 memcpy (&attr->__schedparam, param, sizeof (struct sched_param));
85 return 0;
88 int pthread_attr_getschedparam(const pthread_attr_t *attr,
89 struct sched_param *param)
91 memcpy (param, &attr->__schedparam, sizeof (struct sched_param));
92 return 0;
95 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
97 if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR)
98 return EINVAL;
99 attr->__schedpolicy = policy;
100 return 0;
103 int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
105 *policy = attr->__schedpolicy;
106 return 0;
109 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
111 if (inherit != PTHREAD_INHERIT_SCHED && inherit != PTHREAD_EXPLICIT_SCHED)
112 return EINVAL;
113 attr->__inheritsched = inherit;
114 return 0;
117 int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
119 *inherit = attr->__inheritsched;
120 return 0;
123 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
125 switch (scope) {
126 case PTHREAD_SCOPE_SYSTEM:
127 attr->__scope = scope;
128 return 0;
129 case PTHREAD_SCOPE_PROCESS:
130 return ENOTSUP;
131 default:
132 return EINVAL;
136 int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
138 *scope = attr->__scope;
139 return 0;
142 int __pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
144 size_t ps = __getpagesize ();
146 /* First round up the guard size. */
147 guardsize = roundup (guardsize, ps);
149 /* The guard size must not be larger than the stack itself */
150 if (guardsize >= attr->__stacksize) return EINVAL;
152 attr->__guardsize = guardsize;
154 return 0;
156 weak_alias (__pthread_attr_setguardsize, pthread_attr_setguardsize)
158 int __pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
160 *guardsize = attr->__guardsize;
161 return 0;
163 weak_alias (__pthread_attr_getguardsize, pthread_attr_getguardsize)
165 int __pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
167 attr->__stackaddr = stackaddr;
168 attr->__stackaddr_set = 1;
169 return 0;
171 weak_alias (__pthread_attr_setstackaddr, pthread_attr_setstackaddr)
173 int __pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
175 /* XXX This function has a stupid definition. The standard specifies
176 no error value but what is if no stack address was set? We simply
177 return the value we have in the member. */
178 *stackaddr = attr->__stackaddr;
179 return 0;
181 weak_alias (__pthread_attr_getstackaddr, pthread_attr_getstackaddr)
183 int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
185 /* We don't accept value smaller than PTHREAD_STACK_MIN. */
186 if (stacksize < PTHREAD_STACK_MIN)
187 return EINVAL;
189 attr->__stacksize = stacksize;
190 return 0;
192 weak_alias (__pthread_attr_setstacksize, pthread_attr_setstacksize)
194 int __pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
196 *stacksize = attr->__stacksize;
197 return 0;
199 weak_alias (__pthread_attr_getstacksize, pthread_attr_getstacksize)