blob: 8465c234cbe615ad14331c09253ea7aefb4af52a [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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/* changed for uClibc */
16#define __sched_get_priority_min sched_get_priority_min
17#define __sched_get_priority_max sched_get_priority_max
18
19/* Handling of thread attributes */
20
21#include <errno.h>
22#include <string.h>
23#include <unistd.h>
24#include <sys/param.h>
25#include "pthread.h"
26#include "internals.h"
27
28libpthread_hidden_proto(pthread_attr_destroy)
29libpthread_hidden_proto(pthread_attr_init)
30libpthread_hidden_proto(pthread_attr_getdetachstate)
31libpthread_hidden_proto(pthread_attr_setdetachstate)
32libpthread_hidden_proto(pthread_attr_getinheritsched)
33libpthread_hidden_proto(pthread_attr_setinheritsched)
34libpthread_hidden_proto(pthread_attr_setschedparam)
35libpthread_hidden_proto(pthread_attr_getschedparam)
36libpthread_hidden_proto(pthread_attr_getschedpolicy)
37libpthread_hidden_proto(pthread_attr_setschedpolicy)
38libpthread_hidden_proto(pthread_attr_getscope)
39libpthread_hidden_proto(pthread_attr_setscope)
40
41/* NOTE: With uClibc I don't think we need this versioning stuff.
42 * Therefore, define the function pthread_attr_init() here using
43 * a strong symbol. */
44
45/*int __pthread_attr_init_2_1(pthread_attr_t *attr)*/
46int pthread_attr_init(pthread_attr_t *attr)
47{
48 size_t ps = getpagesize ();
49
50 attr->__detachstate = PTHREAD_CREATE_JOINABLE;
51 attr->__schedpolicy = SCHED_OTHER;
52 attr->__schedparam.sched_priority = 0;
53 attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
54 attr->__scope = PTHREAD_SCOPE_SYSTEM;
55 attr->__guardsize = ps;
56 attr->__stackaddr = NULL;
57 attr->__stackaddr_set = 0;
58 attr->__stacksize = STACK_SIZE - ps;
59 return 0;
60}
61libpthread_hidden_def(pthread_attr_init)
62
63/* uClibc: leave out this for now. */
64#if defined DO_PTHREAD_VERSIONING_WITH_UCLIBC
65#if defined __PIC__ && defined DO_VERSIONING
66default_symbol_version (__pthread_attr_init_2_1, pthread_attr_init, GLIBC_2.1);
67
68int __pthread_attr_init_2_0(pthread_attr_t *attr)
69{
70 attr->__detachstate = PTHREAD_CREATE_JOINABLE;
71 attr->__schedpolicy = SCHED_OTHER;
72 attr->__schedparam.sched_priority = 0;
73 attr->__inheritsched = PTHREAD_EXPLICIT_SCHED;
74 attr->__scope = PTHREAD_SCOPE_SYSTEM;
75 return 0;
76}
77symbol_version (__pthread_attr_init_2_0, pthread_attr_init, GLIBC_2.0);
78#else
79strong_alias (__pthread_attr_init_2_1, pthread_attr_init)
80#endif
81#endif /* DO_PTHREAD_VERSIONING_WITH_UCLIBC */
82
83int pthread_attr_destroy(pthread_attr_t *attr attribute_unused)
84{
85 return 0;
86}
87libpthread_hidden_def(pthread_attr_destroy)
88
89
90int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
91{
92 if (detachstate < PTHREAD_CREATE_JOINABLE ||
93 detachstate > PTHREAD_CREATE_DETACHED)
94 return EINVAL;
95 attr->__detachstate = detachstate;
96 return 0;
97}
98libpthread_hidden_def(pthread_attr_setdetachstate)
99
100int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
101{
102 *detachstate = attr->__detachstate;
103 return 0;
104}
105libpthread_hidden_def(pthread_attr_getdetachstate)
106
107int pthread_attr_setschedparam(pthread_attr_t *attr,
108 const struct sched_param *param)
109{
110 int max_prio = __sched_get_priority_max(attr->__schedpolicy);
111 int min_prio = __sched_get_priority_min(attr->__schedpolicy);
112
113 if (param->sched_priority < min_prio || param->sched_priority > max_prio)
114 return EINVAL;
115 memcpy (&attr->__schedparam, param, sizeof (struct sched_param));
116 return 0;
117}
118libpthread_hidden_def(pthread_attr_setschedparam)
119
120int pthread_attr_getschedparam(const pthread_attr_t *attr,
121 struct sched_param *param)
122{
123 memcpy (param, &attr->__schedparam, sizeof (struct sched_param));
124 return 0;
125}
126libpthread_hidden_def(pthread_attr_getschedparam)
127
128int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
129{
130 if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR)
131 return EINVAL;
132 attr->__schedpolicy = policy;
133 return 0;
134}
135libpthread_hidden_def(pthread_attr_setschedpolicy)
136
137int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
138{
139 *policy = attr->__schedpolicy;
140 return 0;
141}
142libpthread_hidden_def(pthread_attr_getschedpolicy)
143
144int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
145{
146 if (inherit != PTHREAD_INHERIT_SCHED && inherit != PTHREAD_EXPLICIT_SCHED)
147 return EINVAL;
148 attr->__inheritsched = inherit;
149 return 0;
150}
151libpthread_hidden_def(pthread_attr_setinheritsched)
152
153int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
154{
155 *inherit = attr->__inheritsched;
156 return 0;
157}
158libpthread_hidden_def(pthread_attr_getinheritsched)
159
160int pthread_attr_setscope(pthread_attr_t *attr, int scope)
161{
162 switch (scope) {
163 case PTHREAD_SCOPE_SYSTEM:
164 attr->__scope = scope;
165 return 0;
166 case PTHREAD_SCOPE_PROCESS:
167 return ENOTSUP;
168 default:
169 return EINVAL;
170 }
171}
172libpthread_hidden_def(pthread_attr_setscope)
173
174int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
175{
176 *scope = attr->__scope;
177 return 0;
178}
179libpthread_hidden_def(pthread_attr_getscope)
180
181int __pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
182{
183 size_t ps = getpagesize ();
184
185 /* First round up the guard size. */
186 guardsize = roundup (guardsize, ps);
187
188 /* The guard size must not be larger than the stack itself */
189 if (guardsize >= attr->__stacksize) return EINVAL;
190
191 attr->__guardsize = guardsize;
192
193 return 0;
194}
195weak_alias (__pthread_attr_setguardsize, pthread_attr_setguardsize)
196
197int __pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
198{
199 *guardsize = attr->__guardsize;
200 return 0;
201}
202weak_alias (__pthread_attr_getguardsize, pthread_attr_getguardsize)
203
204#if 0 /* uClibc: deprecated stuff disabled */
205int __pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
206{
207 attr->__stackaddr = stackaddr;
208 attr->__stackaddr_set = 1;
209 return 0;
210}
211weak_alias (__pthread_attr_setstackaddr, pthread_attr_setstackaddr)
212
213int __pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
214{
215 /* XXX This function has a stupid definition. The standard specifies
216 no error value but what is if no stack address was set? We simply
217 return the value we have in the member. */
218 *stackaddr = attr->__stackaddr;
219 return 0;
220}
221weak_alias (__pthread_attr_getstackaddr, pthread_attr_getstackaddr)
222#endif
223
224int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
225{
226 /* We don't accept value smaller than PTHREAD_STACK_MIN. */
227 if (stacksize < PTHREAD_STACK_MIN)
228 return EINVAL;
229
230 attr->__stacksize = stacksize;
231 return 0;
232}
233weak_alias (__pthread_attr_setstacksize, pthread_attr_setstacksize)
234
235int __pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
236{
237 *stacksize = attr->__stacksize;
238 return 0;
239}
240weak_alias (__pthread_attr_getstacksize, pthread_attr_getstacksize)