Update.
[platform/upstream/glibc.git] / linuxthreads / attr.c
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.                 */
14
15 /* Handling of thread attributes */
16
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
24 int __pthread_attr_init_2_1(pthread_attr_t *attr)
25 {
26   size_t ps = __getpagesize ();
27
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;
38 }
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);
41
42 int __pthread_attr_init_2_0(pthread_attr_t *attr)
43 {
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;
50 }
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
55
56 int pthread_attr_destroy(pthread_attr_t *attr)
57 {
58   return 0;
59 }
60
61 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
62 {
63   if (detachstate < PTHREAD_CREATE_JOINABLE ||
64       detachstate > PTHREAD_CREATE_DETACHED)
65     return EINVAL;
66   attr->detachstate = detachstate;
67   return 0;
68 }
69
70 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
71 {
72   *detachstate = attr->detachstate;
73   return 0;
74 }
75
76 int pthread_attr_setschedparam(pthread_attr_t *attr,
77                                const struct sched_param *param)
78 {
79   int max_prio = __sched_get_priority_max(attr->schedpolicy);
80   int min_prio = __sched_get_priority_min(attr->schedpolicy);
81
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;
86 }
87
88 int pthread_attr_getschedparam(const pthread_attr_t *attr,
89                                struct sched_param *param)
90 {
91   memcpy (param, &attr->schedparam, sizeof (struct sched_param));
92   return 0;
93 }
94
95 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
96 {
97   if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR)
98     return EINVAL;
99   if (policy != SCHED_OTHER && geteuid() != 0)
100     return ENOTSUP;
101   attr->schedpolicy = policy;
102   return 0;
103 }
104
105 int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
106 {
107   *policy = attr->schedpolicy;
108   return 0;
109 }
110
111 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
112 {
113   if (inherit != PTHREAD_INHERIT_SCHED && inherit != PTHREAD_EXPLICIT_SCHED)
114     return EINVAL;
115   attr->inheritsched = inherit;
116   return 0;
117 }
118
119 int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
120 {
121   *inherit = attr->inheritsched;
122   return 0;
123 }
124
125 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
126 {
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;
135   }
136 }
137
138 int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
139 {
140   *scope = attr->scope;
141   return 0;
142 }
143
144 int __pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
145 {
146   size_t ps = __getpagesize ();
147
148   /* First round up the guard size.  */
149   guardsize = roundup (guardsize, ps);
150
151   /* The current implementation of LinuxThreads allocates 2MB stack space
152      for each thread.  So the maximum guardsize is 2MB - pagesize.  */
153   if (guardsize >= STACK_SIZE - ps)
154     return EINVAL;
155
156   attr->guardsize = guardsize;
157
158   return 0;
159 }
160 weak_alias (__pthread_attr_setguardsize, pthread_attr_setguardsize)
161
162 int __pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
163 {
164   *guardsize = attr->guardsize;
165   return 0;
166 }
167 weak_alias (__pthread_attr_getguardsize, pthread_attr_getguardsize)
168
169 int __pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
170 {
171   attr->stackaddr = stackaddr;
172   attr->stackaddr_set = 1;
173   return 0;
174 }
175 weak_alias (__pthread_attr_setstackaddr, pthread_attr_setstackaddr)
176
177 int __pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
178 {
179   /* XXX This function has a stupid definition.  The standard specifies
180      no error value but what is if no stack address was set?  We simply
181      return the value we have in the member.  */
182   *stackaddr = attr->stackaddr;
183   return 0;
184 }
185 weak_alias (__pthread_attr_getstackaddr, pthread_attr_getstackaddr)
186
187 int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
188 {
189   /* We don't accept value smaller than PTHREAD_STACK_MIN.  */
190   if (stacksize < PTHREAD_STACK_MIN)
191     return EINVAL;
192
193   attr->stacksize = stacksize;
194   return 0;
195 }
196 weak_alias (__pthread_attr_setstacksize, pthread_attr_setstacksize)
197
198 int __pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
199 {
200   *stacksize = attr->stacksize;
201   return 0;
202 }
203 weak_alias (__pthread_attr_getstacksize, pthread_attr_getstacksize)