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