thread-stub.c revision 1.28.14.2 1 /* $NetBSD: thread-stub.c,v 1.28.14.2 2020/04/08 14:07:14 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2003, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: thread-stub.c,v 1.28.14.2 2020/04/08 14:07:14 martin Exp $");
35 #endif /* LIBC_SCCS and not lint */
36
37 /*
38 * Stubs for thread operations, for use when threads are not used by
39 * the application. See "reentrant.h" for details.
40 */
41
42 #ifdef _REENTRANT
43
44 #define __LIBC_THREAD_STUBS
45
46 #define pthread_join __libc_pthread_join
47 #define pthread_detach __libc_pthread_detach
48 #include "namespace.h"
49 #include "reentrant.h"
50 #include "tsd.h"
51
52 #include <errno.h>
53 #include <signal.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56
57 extern int __isthreaded;
58
59 #define DIE() (void)raise(SIGABRT)
60
61 #define CHECK_NOT_THREADED_ALWAYS() \
62 do { \
63 if (__isthreaded) \
64 DIE(); \
65 } while (/*CONSTCOND*/0)
66
67 #if 1
68 #define CHECK_NOT_THREADED() CHECK_NOT_THREADED_ALWAYS()
69 #else
70 #define CHECK_NOT_THREADED() /* nothing */
71 #endif
72
73 __weak_alias(pthread_join, __libc_pthread_join)
74 __weak_alias(pthread_detach, __libc_pthread_detach)
75
76 int
77 pthread_join(pthread_t thread, void **valptr)
78 {
79
80 if (thread == pthread_self())
81 return EDEADLK;
82 return ESRCH;
83 }
84
85 int
86 pthread_detach(pthread_t thread)
87 {
88
89 if (thread == pthread_self())
90 return 0;
91 return ESRCH;
92 }
93
94 /* mutexes */
95
96 int __libc_mutex_catchall_stub(mutex_t *);
97
98 __weak_alias(__libc_mutex_init,__libc_mutex_init_stub)
99 __weak_alias(__libc_mutex_lock,__libc_mutex_catchall_stub)
100 __weak_alias(__libc_mutex_trylock,__libc_mutex_catchall_stub)
101 __weak_alias(__libc_mutex_unlock,__libc_mutex_catchall_stub)
102 __weak_alias(__libc_mutex_destroy,__libc_mutex_catchall_stub)
103
104 __strong_alias(__libc_mutex_lock_stub,__libc_mutex_catchall_stub)
105 __strong_alias(__libc_mutex_trylock_stub,__libc_mutex_catchall_stub)
106 __strong_alias(__libc_mutex_unlock_stub,__libc_mutex_catchall_stub)
107 __strong_alias(__libc_mutex_destroy_stub,__libc_mutex_catchall_stub)
108
109 int __libc_mutexattr_catchall_stub(mutexattr_t *);
110
111 __weak_alias(__libc_mutexattr_init,__libc_mutexattr_catchall_stub)
112 __weak_alias(__libc_mutexattr_destroy,__libc_mutexattr_catchall_stub)
113 __weak_alias(__libc_mutexattr_settype,__libc_mutexattr_settype_stub)
114
115 __strong_alias(__libc_mutexattr_init_stub,__libc_mutexattr_catchall_stub)
116 __strong_alias(__libc_mutexattr_destroy_stub,__libc_mutexattr_catchall_stub)
117
118 int
119 __libc_mutex_init_stub(mutex_t *m, const mutexattr_t *a)
120 {
121 /* LINTED deliberate lack of effect */
122 (void)m;
123 /* LINTED deliberate lack of effect */
124 (void)a;
125
126 CHECK_NOT_THREADED();
127
128 return (0);
129 }
130
131 int
132 __libc_mutex_catchall_stub(mutex_t *m)
133 {
134 /* LINTED deliberate lack of effect */
135 (void)m;
136
137 CHECK_NOT_THREADED();
138
139 return (0);
140 }
141
142 int
143 __libc_mutexattr_settype_stub(mutexattr_t *ma, int type)
144 {
145 /* LINTED deliberate lack of effect */
146 (void)ma;
147 /* LINTED deliberate lack of effect */
148 (void)type;
149
150 CHECK_NOT_THREADED();
151
152 return (0);
153 }
154
155 int
156 __libc_mutexattr_catchall_stub(mutexattr_t *ma)
157 {
158 /* LINTED deliberate lack of effect */
159 (void)ma;
160
161 CHECK_NOT_THREADED();
162
163 return (0);
164 }
165
166 /* condition variables */
167
168 int __libc_cond_catchall_stub(cond_t *);
169
170 __weak_alias(__libc_cond_init,__libc_cond_init_stub)
171 __weak_alias(__libc_cond_signal,__libc_cond_catchall_stub)
172 __weak_alias(__libc_cond_broadcast,__libc_cond_catchall_stub)
173 __weak_alias(__libc_cond_wait,__libc_cond_catchall_stub)
174 __weak_alias(__libc_cond_timedwait,__libc_cond_timedwait_stub)
175 __weak_alias(__libc_cond_destroy,__libc_cond_catchall_stub)
176
177 __strong_alias(__libc_cond_signal_stub,__libc_cond_catchall_stub)
178 __strong_alias(__libc_cond_broadcast_stub,__libc_cond_catchall_stub)
179 __strong_alias(__libc_cond_destroy_stub,__libc_cond_catchall_stub)
180
181 int
182 __libc_cond_init_stub(cond_t *c, const condattr_t *a)
183 {
184 /* LINTED deliberate lack of effect */
185 (void)c;
186 /* LINTED deliberate lack of effect */
187 (void)a;
188
189 CHECK_NOT_THREADED();
190
191 return (0);
192 }
193
194 int
195 __libc_cond_wait_stub(cond_t *c, mutex_t *m)
196 {
197 /* LINTED deliberate lack of effect */
198 (void)c;
199 /* LINTED deliberate lack of effect */
200 (void)m;
201
202 CHECK_NOT_THREADED();
203
204 return (0);
205 }
206
207 int
208 __libc_cond_timedwait_stub(cond_t *c, mutex_t *m, const struct timespec *t)
209 {
210 /* LINTED deliberate lack of effect */
211 (void)c;
212 /* LINTED deliberate lack of effect */
213 (void)m;
214 /* LINTED deliberate lack of effect */
215 (void)t;
216
217 CHECK_NOT_THREADED();
218
219 return (0);
220 }
221
222 int
223 __libc_cond_catchall_stub(cond_t *c)
224 {
225 /* LINTED deliberate lack of effect */
226 (void)c;
227
228 CHECK_NOT_THREADED();
229
230 return (0);
231 }
232
233
234 /* read-write locks */
235
236 int __libc_rwlock_catchall_stub(rwlock_t *);
237
238 __weak_alias(__libc_rwlock_init,__libc_rwlock_init_stub)
239 __weak_alias(__libc_rwlock_rdlock,__libc_rwlock_catchall_stub)
240 __weak_alias(__libc_rwlock_wrlock,__libc_rwlock_catchall_stub)
241 __weak_alias(__libc_rwlock_tryrdlock,__libc_rwlock_catchall_stub)
242 __weak_alias(__libc_rwlock_trywrlock,__libc_rwlock_catchall_stub)
243 __weak_alias(__libc_rwlock_unlock,__libc_rwlock_catchall_stub)
244 __weak_alias(__libc_rwlock_destroy,__libc_rwlock_catchall_stub)
245
246 __strong_alias(__libc_rwlock_rdlock_stub,__libc_rwlock_catchall_stub)
247 __strong_alias(__libc_rwlock_wrlock_stub,__libc_rwlock_catchall_stub)
248 __strong_alias(__libc_rwlock_tryrdlock_stub,__libc_rwlock_catchall_stub)
249 __strong_alias(__libc_rwlock_trywrlock_stub,__libc_rwlock_catchall_stub)
250 __strong_alias(__libc_rwlock_unlock_stub,__libc_rwlock_catchall_stub)
251 __strong_alias(__libc_rwlock_destroy_stub,__libc_rwlock_catchall_stub)
252
253
254 int
255 __libc_rwlock_init_stub(rwlock_t *l, const rwlockattr_t *a)
256 {
257 /* LINTED deliberate lack of effect */
258 (void)l;
259 /* LINTED deliberate lack of effect */
260 (void)a;
261
262 CHECK_NOT_THREADED();
263
264 return (0);
265 }
266
267 int
268 __libc_rwlock_catchall_stub(rwlock_t *l)
269 {
270 /* LINTED deliberate lack of effect */
271 (void)l;
272
273 CHECK_NOT_THREADED();
274
275 return (0);
276 }
277
278
279 /*
280 * thread-specific data; we need to actually provide a simple TSD
281 * implementation, since some thread-safe libraries want to use it.
282 */
283
284 struct __libc_tsd __libc_tsd[TSD_KEYS_MAX];
285 static int __libc_tsd_nextkey;
286
287 __weak_alias(__libc_thr_keycreate,__libc_thr_keycreate_stub)
288 __weak_alias(__libc_thr_setspecific,__libc_thr_setspecific_stub)
289 __weak_alias(__libc_thr_getspecific,__libc_thr_getspecific_stub)
290 __weak_alias(__libc_thr_keydelete,__libc_thr_keydelete_stub)
291
292 int
293 __libc_thr_keycreate_stub(thread_key_t *k, void (*d)(void *))
294 {
295 int i;
296
297 for (i = __libc_tsd_nextkey; i < TSD_KEYS_MAX; i++) {
298 if (__libc_tsd[i].tsd_inuse == 0)
299 goto out;
300 }
301
302 for (i = 0; i < __libc_tsd_nextkey; i++) {
303 if (__libc_tsd[i].tsd_inuse == 0)
304 goto out;
305 }
306
307 return (EAGAIN);
308
309 out:
310 /*
311 * XXX We don't actually do anything with the destructor. We
312 * XXX probably should.
313 */
314 __libc_tsd[i].tsd_inuse = 1;
315 __libc_tsd_nextkey = (i + i) % TSD_KEYS_MAX;
316 __libc_tsd[i].tsd_dtor = d;
317 *k = i;
318
319 return (0);
320 }
321
322 int
323 __libc_thr_setspecific_stub(thread_key_t k, const void *v)
324 {
325
326 __libc_tsd[k].tsd_val = __UNCONST(v);
327
328 return (0);
329 }
330
331 void *
332 __libc_thr_getspecific_stub(thread_key_t k)
333 {
334
335 return (__libc_tsd[k].tsd_val);
336 }
337
338 int
339 __libc_thr_keydelete_stub(thread_key_t k)
340 {
341
342 /*
343 * XXX Do not recycle key; see big comment in libpthread.
344 */
345
346 __libc_tsd[k].tsd_dtor = NULL;
347
348 return (0);
349 }
350
351
352 /* misc. */
353
354 __weak_alias(__libc_thr_once,__libc_thr_once_stub)
355 __weak_alias(__libc_thr_sigsetmask,__libc_thr_sigsetmask_stub)
356 __weak_alias(__libc_thr_self,__libc_thr_self_stub)
357 __weak_alias(__libc_thr_yield,__libc_thr_yield_stub)
358 __weak_alias(__libc_thr_create,__libc_thr_create_stub)
359 __weak_alias(__libc_thr_exit,__libc_thr_exit_stub)
360 __weak_alias(__libc_thr_setcancelstate,__libc_thr_setcancelstate_stub)
361 __weak_alias(__libc_thr_equal,__libc_thr_equal_stub)
362 __weak_alias(__libc_thr_curcpu,__libc_thr_curcpu_stub)
363
364
365 int
366 __libc_thr_once_stub(once_t *o, void (*r)(void))
367 {
368
369 /* XXX Knowledge of libpthread types. */
370
371 if (o->pto_done == 0) {
372 (*r)();
373 o->pto_done = 1;
374 }
375
376 return (0);
377 }
378
379 int
380 __libc_thr_sigsetmask_stub(int h, const sigset_t *s, sigset_t *o)
381 {
382
383 CHECK_NOT_THREADED();
384
385 if (sigprocmask(h, s, o))
386 return errno;
387 return 0;
388 }
389
390 thr_t
391 __libc_thr_self_stub(void)
392 {
393
394 return ((thr_t) -1);
395 }
396
397 int
398 __libc_thr_yield_stub(void)
399 {
400
401 /* Nothing to do. */
402 return (0);
403 }
404
405 int
406 __libc_thr_create_stub(thr_t *tp, const thrattr_t *ta,
407 void *(*f)(void *), void *a)
408 {
409 /* LINTED deliberate lack of effect */
410 (void)tp;
411 /* LINTED deliberate lack of effect */
412 (void)ta;
413 /* LINTED deliberate lack of effect */
414 (void)f;
415 /* LINTED deliberate lack of effect */
416 (void)a;
417
418 DIE();
419
420 return (EOPNOTSUPP);
421 }
422
423 __dead void
424 __libc_thr_exit_stub(void *v)
425 {
426 /* LINTED deliberate lack of effect */
427 (void)v;
428 exit(0);
429 }
430
431 int
432 __libc_thr_setcancelstate_stub(int new, int *old)
433 {
434 /* LINTED deliberate lack of effect */
435 (void)new;
436
437 /* LINTED deliberate lack of effect */
438 (void)old;
439
440 CHECK_NOT_THREADED();
441
442 return (0);
443 }
444
445 int
446 __libc_thr_equal_stub(pthread_t t1, pthread_t t2)
447 {
448
449 /* assert that t1=t2=pthread_self() */
450 return (t1 == t2);
451 }
452
453 unsigned int
454 __libc_thr_curcpu_stub(void)
455 {
456
457 return (0);
458 }
459
460 #endif /* _REENTRANT */
461