Update.
[glibc.git] / linuxthreads / attr.c
blob966cea12037cf7d7f897f12aa2890ce9aeb65b3d
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"
23 #include <shlib-compat.h>
25 int __pthread_attr_init_2_1(pthread_attr_t *attr)
27 size_t ps = __getpagesize ();
29 attr->__detachstate = PTHREAD_CREATE_JOINABLE;
30 attr->__schedpolicy = SCHED_OTHER;
31 attr->__schedparam.sched_priority = 0;
32 attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
33 attr->__scope = PTHREAD_SCOPE_SYSTEM;
34 attr->__guardsize = ps;
35 attr->__stackaddr = NULL;
36 attr->__stackaddr_set = 0;
37 attr->__stacksize = STACK_SIZE - ps;
38 return 0;
41 versioned_symbol (libpthread, __pthread_attr_init_2_1, pthread_attr_init,
42 GLIBC_2_1);
44 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
45 int __pthread_attr_init_2_0(pthread_attr_t *attr)
47 attr->__detachstate = PTHREAD_CREATE_JOINABLE;
48 attr->__schedpolicy = SCHED_OTHER;
49 attr->__schedparam.sched_priority = 0;
50 attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
51 attr->__scope = PTHREAD_SCOPE_SYSTEM;
52 return 0;
54 compat_symbol (libpthread, __pthread_attr_init_2_0, pthread_attr_init,
55 GLIBC_2_0);
56 #endif
58 int pthread_attr_destroy(pthread_attr_t *attr)
60 return 0;
63 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
65 if (detachstate < PTHREAD_CREATE_JOINABLE ||
66 detachstate > PTHREAD_CREATE_DETACHED)
67 return EINVAL;
68 attr->__detachstate = detachstate;
69 return 0;
72 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
74 *detachstate = attr->__detachstate;
75 return 0;
78 int pthread_attr_setschedparam(pthread_attr_t *attr,
79 const struct sched_param *param)
81 int max_prio = __sched_get_priority_max(attr->__schedpolicy);
82 int min_prio = __sched_get_priority_min(attr->__schedpolicy);
84 if (param->sched_priority < min_prio || param->sched_priority > max_prio)
85 return EINVAL;
86 memcpy (&attr->__schedparam, param, sizeof (struct sched_param));
87 return 0;
90 int pthread_attr_getschedparam(const pthread_attr_t *attr,
91 struct sched_param *param)
93 memcpy (param, &attr->__schedparam, sizeof (struct sched_param));
94 return 0;
97 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
99 if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR)
100 return EINVAL;
101 attr->__schedpolicy = policy;
102 return 0;
105 int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
107 *policy = attr->__schedpolicy;
108 return 0;
111 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
113 if (inherit != PTHREAD_INHERIT_SCHED && inherit != PTHREAD_EXPLICIT_SCHED)
114 return EINVAL;
115 attr->__inheritsched = inherit;
116 return 0;
119 int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
121 *inherit = attr->__inheritsched;
122 return 0;
125 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
127 switch (scope) {
128 case PTHREAD_SCOPE_SYSTEM:
129 attr->__scope = scope;
130 return 0;
131 case PTHREAD_SCOPE_PROCESS:
132 return ENOTSUP;
133 default:
134 return EINVAL;
138 int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
140 *scope = attr->__scope;
141 return 0;
144 int __pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
146 size_t ps = __getpagesize ();
148 /* First round up the guard size. */
149 guardsize = roundup (guardsize, ps);
151 /* The guard size must not be larger than the stack itself */
152 if (guardsize >= attr->__stacksize) return EINVAL;
154 attr->__guardsize = guardsize;
156 return 0;
158 weak_alias (__pthread_attr_setguardsize, pthread_attr_setguardsize)
160 int __pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
162 *guardsize = attr->__guardsize;
163 return 0;
165 weak_alias (__pthread_attr_getguardsize, pthread_attr_getguardsize)
167 int __pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
169 attr->__stackaddr = stackaddr;
170 attr->__stackaddr_set = 1;
171 return 0;
173 weak_alias (__pthread_attr_setstackaddr, pthread_attr_setstackaddr)
175 int __pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
177 /* XXX This function has a stupid definition. The standard specifies
178 no error value but what is if no stack address was set? We simply
179 return the value we have in the member. */
180 *stackaddr = attr->__stackaddr;
181 return 0;
183 weak_alias (__pthread_attr_getstackaddr, pthread_attr_getstackaddr)
185 int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
187 /* We don't accept value smaller than PTHREAD_STACK_MIN. */
188 if (stacksize < PTHREAD_STACK_MIN)
189 return EINVAL;
191 attr->__stacksize = stacksize;
192 return 0;
194 weak_alias (__pthread_attr_setstacksize, pthread_attr_setstacksize)
196 int __pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
198 *stacksize = attr->__stacksize;
199 return 0;
201 weak_alias (__pthread_attr_getstacksize, pthread_attr_getstacksize)