]> git.ipfire.org Git - thirdparty/glibc.git/blob - htl/forward.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / htl / forward.c
1 /* Libc stubs for pthread functions. Hurd pthread version.
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <dlfcn.h>
21 #include <stdlib.h>
22 #include <shlib-compat.h>
23 #include <pthread-functions.h>
24 #include <libc-lock.h>
25 #include <fork.h>
26
27 /* Pointers to the libc functions. */
28 struct pthread_functions __libc_pthread_functions attribute_hidden;
29 int __libc_pthread_functions_init attribute_hidden;
30
31
32 #define FORWARD2(name, rettype, decl, params, defaction) \
33 rettype \
34 name decl \
35 { \
36 if (!__libc_pthread_functions_init) \
37 defaction; \
38 \
39 return PTHFCT_CALL (ptr_##name, params); \
40 }
41
42 /* Same as FORWARD2, only without return. */
43 #define FORWARD_NORETURN(name, rettype, decl, params, defaction) \
44 rettype \
45 name decl \
46 { \
47 if (!__libc_pthread_functions_init) \
48 defaction; \
49 \
50 PTHFCT_CALL (ptr_##name, params); \
51 }
52
53 #define FORWARD(name, decl, params, defretval) \
54 FORWARD2 (name, int, decl, params, return defretval)
55
56 FORWARD (pthread_attr_destroy, (pthread_attr_t *attr), (attr), 0)
57
58 FORWARD (pthread_attr_init, (pthread_attr_t *attr), (attr), 0)
59
60 FORWARD (pthread_attr_getdetachstate,
61 (const pthread_attr_t *attr, int *detachstate), (attr, detachstate),
62 0)
63 FORWARD (pthread_attr_setdetachstate, (pthread_attr_t *attr, int detachstate),
64 (attr, detachstate), 0)
65
66 FORWARD (pthread_attr_getinheritsched,
67 (const pthread_attr_t *attr, int *inherit), (attr, inherit), 0)
68 FORWARD (pthread_attr_setinheritsched, (pthread_attr_t *attr, int inherit),
69 (attr, inherit), 0)
70
71 FORWARD (pthread_attr_getschedparam,
72 (const pthread_attr_t *attr, struct sched_param *param),
73 (attr, param), 0)
74 FORWARD (pthread_attr_setschedparam,
75 (pthread_attr_t *attr, const struct sched_param *param),
76 (attr, param), 0)
77
78 FORWARD (pthread_attr_getschedpolicy,
79 (const pthread_attr_t *attr, int *policy), (attr, policy), 0)
80 FORWARD (pthread_attr_setschedpolicy, (pthread_attr_t *attr, int policy),
81 (attr, policy), 0)
82
83 FORWARD (pthread_attr_getscope,
84 (const pthread_attr_t *attr, int *scope), (attr, scope), 0)
85 FORWARD (pthread_attr_setscope, (pthread_attr_t *attr, int scope),
86 (attr, scope), 0)
87
88
89 FORWARD (pthread_condattr_destroy, (pthread_condattr_t *attr), (attr), 0)
90 FORWARD (pthread_condattr_init, (pthread_condattr_t *attr), (attr), 0)
91
92
93 FORWARD (pthread_cond_broadcast, (pthread_cond_t *cond), (cond), 0)
94 FORWARD (pthread_cond_destroy, (pthread_cond_t *cond), (cond), 0)
95 FORWARD (pthread_cond_init,
96 (pthread_cond_t *cond, const pthread_condattr_t *cond_attr),
97 (cond, cond_attr), 0)
98 FORWARD (pthread_cond_signal, (pthread_cond_t *cond), (cond), 0)
99 FORWARD (pthread_cond_wait, (pthread_cond_t *cond, pthread_mutex_t *mutex),
100 (cond, mutex), 0)
101 FORWARD (pthread_cond_timedwait,
102 (pthread_cond_t *cond, pthread_mutex_t *mutex,
103 const struct timespec *abstime), (cond, mutex, abstime), 0)
104
105 FORWARD (pthread_equal, (pthread_t thread1, pthread_t thread2),
106 (thread1, thread2), 1)
107
108
109 /* Use an alias to avoid warning, as pthread_exit is declared noreturn. */
110 FORWARD_NORETURN (__pthread_exit, void, (void *retval), (retval),
111 exit (EXIT_SUCCESS))
112 strong_alias (__pthread_exit, pthread_exit);
113
114
115 FORWARD (pthread_getschedparam,
116 (pthread_t target_thread, int *policy, struct sched_param *param),
117 (target_thread, policy, param), 0)
118 FORWARD (pthread_setschedparam,
119 (pthread_t target_thread, int policy,
120 const struct sched_param *param), (target_thread, policy, param), 0)
121
122
123 FORWARD (pthread_mutex_destroy, (pthread_mutex_t *mutex), (mutex), 0)
124
125 FORWARD (pthread_mutex_init,
126 (pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr),
127 (mutex, mutexattr), 0)
128
129 FORWARD (pthread_mutex_lock, (pthread_mutex_t *mutex), (mutex), 0)
130
131 FORWARD (pthread_mutex_unlock, (pthread_mutex_t *mutex), (mutex), 0)
132
133
134 FORWARD2 (pthread_self, pthread_t, (void), (), return 0)
135
136
137 FORWARD (__pthread_setcancelstate, (int state, int *oldstate),
138 (state, oldstate), 0)
139 strong_alias (__pthread_setcancelstate, pthread_setcancelstate);
140
141 FORWARD (pthread_setcanceltype, (int type, int *oldtype), (type, oldtype), 0)
142
143 struct __pthread_cancelation_handler *dummy_list;
144 FORWARD2 (__pthread_get_cleanup_stack, struct __pthread_cancelation_handler **,
145 (void), (), return &dummy_list);
146
147 \f
148 /* Fork interaction */
149
150 struct atfork
151 {
152 void (*prepare) (void);
153 void (*parent) (void);
154 void (*child) (void);
155 void *dso_handle;
156 struct atfork *prev;
157 struct atfork *next;
158 };
159
160 /* TODO: better locking */
161 __libc_lock_define_initialized (static, atfork_lock);
162 static struct atfork *fork_handlers, *fork_last_handler;
163
164 static void
165 atfork_pthread_prepare (void)
166 {
167 struct atfork *handlers, *last_handler;
168
169 __libc_lock_lock (atfork_lock);
170 handlers = fork_handlers;
171 last_handler = fork_last_handler;
172 __libc_lock_unlock (atfork_lock);
173
174 if (last_handler == NULL)
175 return;
176
177 while (1)
178 {
179 if (last_handler->prepare != NULL)
180 last_handler->prepare ();
181 if (last_handler == handlers)
182 break;
183 last_handler = last_handler->prev;
184 }
185 }
186 text_set_element (_hurd_atfork_prepare_hook, atfork_pthread_prepare);
187
188 static void
189 atfork_pthread_parent (void)
190 {
191 struct atfork *handlers;
192
193 __libc_lock_lock (atfork_lock);
194 handlers = fork_handlers;
195 __libc_lock_unlock (atfork_lock);
196
197 while (handlers != NULL)
198 {
199 if (handlers->parent != NULL)
200 handlers->parent ();
201 handlers = handlers->next;
202 }
203 }
204 text_set_element (_hurd_atfork_parent_hook, atfork_pthread_parent);
205
206 static void
207 atfork_pthread_child (void)
208 {
209 struct atfork *handlers;
210
211 __libc_lock_lock (atfork_lock);
212 handlers = fork_handlers;
213 __libc_lock_unlock (atfork_lock);
214
215 while (handlers != NULL)
216 {
217 if (handlers->child != NULL)
218 handlers->child ();
219 handlers = handlers->next;
220 }
221 }
222 text_set_element (_hurd_atfork_child_hook, atfork_pthread_child);
223
224 int
225 __register_atfork (void (*prepare) (void),
226 void (*parent) (void),
227 void (*child) (void),
228 void *dso_handle)
229 {
230 struct atfork *new = malloc (sizeof (*new));
231 if (new == NULL)
232 return errno;
233
234 new->prepare = prepare;
235 new->parent = parent;
236 new->child = child;
237 new->dso_handle = dso_handle;
238 new->prev = NULL;
239
240 __libc_lock_lock (atfork_lock);
241 new->next = fork_handlers;
242 if (fork_handlers != NULL)
243 fork_handlers->prev = new;
244 fork_handlers = new;
245 if (fork_last_handler == NULL)
246 fork_last_handler = new;
247 __libc_lock_unlock (atfork_lock);
248
249 return 0;
250 }
251 libc_hidden_def (__register_atfork)
252
253 void
254 __unregister_atfork (void *dso_handle)
255 {
256 struct atfork **handlers, *prev = NULL, *next;
257 __libc_lock_lock (atfork_lock);
258 handlers = &fork_handlers;
259 while (*handlers != NULL)
260 {
261 if ((*handlers)->dso_handle == dso_handle)
262 {
263 /* Drop this handler from the list. */
264 if (*handlers == fork_last_handler)
265 {
266 /* Was last, new last is prev, if any. */
267 fork_last_handler = prev;
268 }
269
270 next = (*handlers)->next;
271 if (next != NULL)
272 next->prev = prev;
273 *handlers = next;
274 }
275 else
276 {
277 /* Just proceed to next handler. */
278 prev = *handlers;
279 handlers = &prev->next;
280 }
281 }
282 __libc_lock_unlock (atfork_lock);
283 }