Update.
[glibc.git] / linuxthreads / attr.c
blob76c137660e39e49ace4792723faaed1ca8f86c86
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 <unistd.h>
18 #include <sys/param.h>
19 #include "pthread.h"
20 #include "internals.h"
23 int __pthread_attr_init_2_1(pthread_attr_t *attr)
25 size_t ps = __getpagesize ();
27 attr->detachstate = PTHREAD_CREATE_JOINABLE;
28 attr->schedpolicy = SCHED_OTHER;
29 attr->schedparam.sched_priority = 0;
30 attr->inheritsched = PTHREAD_EXPLICIT_SCHED;
31 attr->scope = PTHREAD_SCOPE_SYSTEM;
32 attr->guardsize = ps;
33 attr->stackaddr = NULL;
34 attr->stackaddr_set = 0;
35 attr->stacksize = STACK_SIZE - ps;
36 return 0;
38 #if defined HAVE_ELF && defined PIC && defined DO_VERSIONING
39 default_symbol_version (__pthread_attr_init_2_1, pthread_attr_init, GLIBC_2.1);
41 int __pthread_attr_init_2_0(pthread_attr_t *attr)
43 attr->detachstate = PTHREAD_CREATE_JOINABLE;
44 attr->schedpolicy = SCHED_OTHER;
45 attr->schedparam.sched_priority = 0;
46 attr->inheritsched = PTHREAD_EXPLICIT_SCHED;
47 attr->scope = PTHREAD_SCOPE_SYSTEM;
48 return 0;
50 symbol_version (__pthread_attr_init_2_0, pthread_attr_init, GLIBC_2.0);
51 #else
52 strong_alias (__pthread_attr_init_2_1, pthread_attr_init)
53 #endif
55 int pthread_attr_destroy(pthread_attr_t *attr)
57 return 0;
60 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
62 if (detachstate < PTHREAD_CREATE_JOINABLE ||
63 detachstate > PTHREAD_CREATE_DETACHED)
64 return EINVAL;
65 attr->detachstate = detachstate;
66 return 0;
69 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
71 *detachstate = attr->detachstate;
72 return 0;
75 int pthread_attr_setschedparam(pthread_attr_t *attr,
76 const struct sched_param *param)
78 int max_prio = __sched_get_priority_max(attr->schedpolicy);
79 int min_prio = __sched_get_priority_min(attr->schedpolicy);
81 if (param->sched_priority < min_prio || param->sched_priority > max_prio)
82 return EINVAL;
83 attr->schedparam = *param;
84 return 0;
87 int pthread_attr_getschedparam(const pthread_attr_t *attr,
88 struct sched_param *param)
90 *param = attr->schedparam;
91 return 0;
94 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
96 if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR)
97 return EINVAL;
98 if (policy != SCHED_OTHER && geteuid() != 0)
99 return ENOTSUP;
100 attr->schedpolicy = policy;
101 return 0;
104 int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
106 *policy = attr->schedpolicy;
107 return 0;
110 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
112 if (inherit != PTHREAD_INHERIT_SCHED && inherit != PTHREAD_EXPLICIT_SCHED)
113 return EINVAL;
114 attr->inheritsched = inherit;
115 return 0;
118 int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
120 *inherit = attr->inheritsched;
121 return 0;
124 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
126 switch (scope) {
127 case PTHREAD_SCOPE_SYSTEM:
128 attr->scope = scope;
129 return 0;
130 case PTHREAD_SCOPE_PROCESS:
131 return ENOTSUP;
132 default:
133 return EINVAL;
137 int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
139 *scope = attr->scope;
140 return 0;
143 int __pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
145 size_t ps = __getpagesize ();
147 /* First round up the guard size. */
148 guardsize = roundup (guardsize, ps);
150 /* The current implementation of LinuxThreads allocates 2MB stack space
151 for each thread. So the maximum guardsize is 2MB - pagesize. */
152 if (guardsize >= STACK_SIZE - ps)
153 return EINVAL;
155 attr->guardsize = guardsize;
157 return 0;
159 weak_alias (__pthread_attr_setguardsize, pthread_attr_setguardsize)
161 int __pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
163 *guardsize = attr->guardsize;
164 return 0;
166 weak_alias (__pthread_attr_getguardsize, pthread_attr_getguardsize)
168 int __pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
170 attr->stackaddr = stackaddr;
171 attr->stackaddr_set = 1;
172 return 0;
174 weak_alias (__pthread_attr_setstackaddr, pthread_attr_setstackaddr)
176 int __pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
178 /* XXX This function has a stupid definition. The standard specifies
179 no error value but what is if no stack address was set? We simply
180 return the value we have in the member. */
181 *stackaddr = attr->stackaddr;
182 return 0;
184 weak_alias (__pthread_attr_getstackaddr, pthread_attr_etstackaddr)
186 int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
188 size_t ps = __getpagesize ();
190 /* We don't accept value smaller than PTHREAD_STACK_MIN or bigger than
191 2MB - pagesize. */
192 if (stacksize < PTHREAD_STACK_MIN || stacksize > STACK_SIZE - ps)
193 return EINVAL;
195 attr->stacksize = stacksize;
196 return 0;
198 weak_alias (__pthread_attr_setstacksize, pthread_attr_setstacksize)
200 int __pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
202 *stacksize = attr->stacksize;
203 return 0;
205 weak_alias (__pthread_attr_getstacksize, pthread_attr_getstacksize)