1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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 */
18 #include <sys/param.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
;
33 attr
->stackaddr
= NULL
;
34 attr
->stackaddr_set
= 0;
35 attr
->stacksize
= STACK_SIZE
- ps
;
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
;
50 symbol_version (__pthread_attr_init_2_0
, pthread_attr_init
, GLIBC_2
.0
);
52 strong_alias (__pthread_attr_init_2_1
, pthread_attr_init
)
55 int pthread_attr_destroy(pthread_attr_t
*attr
)
60 int pthread_attr_setdetachstate(pthread_attr_t
*attr
, int detachstate
)
62 if (detachstate
< PTHREAD_CREATE_JOINABLE
||
63 detachstate
> PTHREAD_CREATE_DETACHED
)
65 attr
->detachstate
= detachstate
;
69 int pthread_attr_getdetachstate(const pthread_attr_t
*attr
, int *detachstate
)
71 *detachstate
= attr
->detachstate
;
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
)
83 attr
->schedparam
= *param
;
87 int pthread_attr_getschedparam(const pthread_attr_t
*attr
,
88 struct sched_param
*param
)
90 *param
= attr
->schedparam
;
94 int pthread_attr_setschedpolicy(pthread_attr_t
*attr
, int policy
)
96 if (policy
!= SCHED_OTHER
&& policy
!= SCHED_FIFO
&& policy
!= SCHED_RR
)
98 if (policy
!= SCHED_OTHER
&& geteuid() != 0)
100 attr
->schedpolicy
= policy
;
104 int pthread_attr_getschedpolicy(const pthread_attr_t
*attr
, int *policy
)
106 *policy
= attr
->schedpolicy
;
110 int pthread_attr_setinheritsched(pthread_attr_t
*attr
, int inherit
)
112 if (inherit
!= PTHREAD_INHERIT_SCHED
&& inherit
!= PTHREAD_EXPLICIT_SCHED
)
114 attr
->inheritsched
= inherit
;
118 int pthread_attr_getinheritsched(const pthread_attr_t
*attr
, int *inherit
)
120 *inherit
= attr
->inheritsched
;
124 int pthread_attr_setscope(pthread_attr_t
*attr
, int scope
)
127 case PTHREAD_SCOPE_SYSTEM
:
130 case PTHREAD_SCOPE_PROCESS
:
137 int pthread_attr_getscope(const pthread_attr_t
*attr
, int *scope
)
139 *scope
= attr
->scope
;
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
)
155 attr
->guardsize
= guardsize
;
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
;
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;
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
;
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
192 if (stacksize
< PTHREAD_STACK_MIN
|| stacksize
> STACK_SIZE
- ps
)
195 attr
->stacksize
= stacksize
;
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
;
205 weak_alias (__pthread_attr_getstacksize
, pthread_attr_getstacksize
)