1 /* $NetBSD: thread-stub.c,v 1.27 2013/05/28 17:29:41 christos 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.27 2013/05/28 17:29:41 christos 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
51 #include <errno.h>
52 #include <signal.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55
56 extern int __isthreaded;
57
58 #define DIE() (void)raise(SIGABRT)
59
60 #define CHECK_NOT_THREADED_ALWAYS() \
61 do { \
62 if (__isthreaded) \
63 DIE(); \
64 } while (/*CONSTCOND*/0)
65
66 #if 1
67 #define CHECK_NOT_THREADED() CHECK_NOT_THREADED_ALWAYS()
68 #else
69 #define CHECK_NOT_THREADED() /* nothing */
70 #endif
71
__weak_alias(pthread_join,__libc_pthread_join)72 __weak_alias(pthread_join, __libc_pthread_join)
73 __weak_alias(pthread_detach, __libc_pthread_detach)
74
75 int
76 pthread_join(pthread_t thread, void **valptr)
77 {
78
79 if (thread == pthread_self())
80 return EDEADLK;
81 return ESRCH;
82 }
83
84 int
pthread_detach(pthread_t thread)85 pthread_detach(pthread_t thread)
86 {
87
88 if (thread == pthread_self())
89 return 0;
90 return ESRCH;
91 }
92
93 /* mutexes */
94
95 int __libc_mutex_catchall_stub(mutex_t *);
96
97 __weak_alias(__libc_mutex_init,__libc_mutex_init_stub)
98 __weak_alias(__libc_mutex_lock,__libc_mutex_catchall_stub)
99 __weak_alias(__libc_mutex_trylock,__libc_mutex_catchall_stub)
100 __weak_alias(__libc_mutex_unlock,__libc_mutex_catchall_stub)
101 __weak_alias(__libc_mutex_destroy,__libc_mutex_catchall_stub)
102
103 __strong_alias(__libc_mutex_lock_stub,__libc_mutex_catchall_stub)
104 __strong_alias(__libc_mutex_trylock_stub,__libc_mutex_catchall_stub)
105 __strong_alias(__libc_mutex_unlock_stub,__libc_mutex_catchall_stub)
106 __strong_alias(__libc_mutex_destroy_stub,__libc_mutex_catchall_stub)
107
108 int __libc_mutexattr_catchall_stub(mutexattr_t *);
109
__weak_alias(__libc_mutexattr_init,__libc_mutexattr_catchall_stub)110 __weak_alias(__libc_mutexattr_init,__libc_mutexattr_catchall_stub)
111 __weak_alias(__libc_mutexattr_destroy,__libc_mutexattr_catchall_stub)
112 __weak_alias(__libc_mutexattr_settype,__libc_mutexattr_settype_stub)
113
114 __strong_alias(__libc_mutexattr_init_stub,__libc_mutexattr_catchall_stub)
115 __strong_alias(__libc_mutexattr_destroy_stub,__libc_mutexattr_catchall_stub)
116
117 int
118 __libc_mutex_init_stub(mutex_t *m, const mutexattr_t *a)
119 {
120 /* LINTED deliberate lack of effect */
121 (void)m;
122 /* LINTED deliberate lack of effect */
123 (void)a;
124
125 CHECK_NOT_THREADED();
126
127 return (0);
128 }
129
130 int
__libc_mutex_catchall_stub(mutex_t * m)131 __libc_mutex_catchall_stub(mutex_t *m)
132 {
133 /* LINTED deliberate lack of effect */
134 (void)m;
135
136 CHECK_NOT_THREADED();
137
138 return (0);
139 }
140
141 int
__libc_mutexattr_settype_stub(mutexattr_t * ma,int type)142 __libc_mutexattr_settype_stub(mutexattr_t *ma, int type)
143 {
144 /* LINTED deliberate lack of effect */
145 (void)ma;
146 /* LINTED deliberate lack of effect */
147 (void)type;
148
149 return (0);
150 }
151
152 int
__libc_mutexattr_catchall_stub(mutexattr_t * ma)153 __libc_mutexattr_catchall_stub(mutexattr_t *ma)
154 {
155 /* LINTED deliberate lack of effect */
156 (void)ma;
157
158 CHECK_NOT_THREADED();
159
160 return (0);
161 }
162
163 /* condition variables */
164
165 int __libc_cond_catchall_stub(cond_t *);
166
__weak_alias(__libc_cond_init,__libc_cond_init_stub)167 __weak_alias(__libc_cond_init,__libc_cond_init_stub)
168 __weak_alias(__libc_cond_signal,__libc_cond_catchall_stub)
169 __weak_alias(__libc_cond_broadcast,__libc_cond_catchall_stub)
170 __weak_alias(__libc_cond_wait,__libc_cond_catchall_stub)
171 __weak_alias(__libc_cond_timedwait,__libc_cond_timedwait_stub)
172 __weak_alias(__libc_cond_destroy,__libc_cond_catchall_stub)
173
174 __strong_alias(__libc_cond_signal_stub,__libc_cond_catchall_stub)
175 __strong_alias(__libc_cond_broadcast_stub,__libc_cond_catchall_stub)
176 __strong_alias(__libc_cond_destroy_stub,__libc_cond_catchall_stub)
177
178 int
179 __libc_cond_init_stub(cond_t *c, const condattr_t *a)
180 {
181 /* LINTED deliberate lack of effect */
182 (void)c;
183 /* LINTED deliberate lack of effect */
184 (void)a;
185
186 CHECK_NOT_THREADED();
187
188 return (0);
189 }
190
191 int
__libc_cond_wait_stub(cond_t * c,mutex_t * m)192 __libc_cond_wait_stub(cond_t *c, mutex_t *m)
193 {
194 /* LINTED deliberate lack of effect */
195 (void)c;
196 /* LINTED deliberate lack of effect */
197 (void)m;
198
199 CHECK_NOT_THREADED();
200
201 return (0);
202 }
203
204 int
__libc_cond_timedwait_stub(cond_t * c,mutex_t * m,const struct timespec * t)205 __libc_cond_timedwait_stub(cond_t *c, mutex_t *m, const struct timespec *t)
206 {
207 /* LINTED deliberate lack of effect */
208 (void)c;
209 /* LINTED deliberate lack of effect */
210 (void)m;
211 /* LINTED deliberate lack of effect */
212 (void)t;
213
214 CHECK_NOT_THREADED();
215
216 return (0);
217 }
218
219 int
__libc_cond_catchall_stub(cond_t * c)220 __libc_cond_catchall_stub(cond_t *c)
221 {
222 /* LINTED deliberate lack of effect */
223 (void)c;
224
225 CHECK_NOT_THREADED();
226
227 return (0);
228 }
229
230
231 /* read-write locks */
232
233 int __libc_rwlock_catchall_stub(rwlock_t *);
234
__weak_alias(__libc_rwlock_init,__libc_rwlock_init_stub)235 __weak_alias(__libc_rwlock_init,__libc_rwlock_init_stub)
236 __weak_alias(__libc_rwlock_rdlock,__libc_rwlock_catchall_stub)
237 __weak_alias(__libc_rwlock_wrlock,__libc_rwlock_catchall_stub)
238 __weak_alias(__libc_rwlock_tryrdlock,__libc_rwlock_catchall_stub)
239 __weak_alias(__libc_rwlock_trywrlock,__libc_rwlock_catchall_stub)
240 __weak_alias(__libc_rwlock_unlock,__libc_rwlock_catchall_stub)
241 __weak_alias(__libc_rwlock_destroy,__libc_rwlock_catchall_stub)
242
243 __strong_alias(__libc_rwlock_rdlock_stub,__libc_rwlock_catchall_stub)
244 __strong_alias(__libc_rwlock_wrlock_stub,__libc_rwlock_catchall_stub)
245 __strong_alias(__libc_rwlock_tryrdlock_stub,__libc_rwlock_catchall_stub)
246 __strong_alias(__libc_rwlock_trywrlock_stub,__libc_rwlock_catchall_stub)
247 __strong_alias(__libc_rwlock_unlock_stub,__libc_rwlock_catchall_stub)
248 __strong_alias(__libc_rwlock_destroy_stub,__libc_rwlock_catchall_stub)
249
250
251 int
252 __libc_rwlock_init_stub(rwlock_t *l, const rwlockattr_t *a)
253 {
254 /* LINTED deliberate lack of effect */
255 (void)l;
256 /* LINTED deliberate lack of effect */
257 (void)a;
258
259 CHECK_NOT_THREADED();
260
261 return (0);
262 }
263
264 int
__libc_rwlock_catchall_stub(rwlock_t * l)265 __libc_rwlock_catchall_stub(rwlock_t *l)
266 {
267 /* LINTED deliberate lack of effect */
268 (void)l;
269
270 CHECK_NOT_THREADED();
271
272 return (0);
273 }
274
275
276 /*
277 * thread-specific data; we need to actually provide a simple TSD
278 * implementation, since some thread-safe libraries want to use it.
279 */
280
281 #define TSD_KEYS_MAX 64
282
283 static struct {
284 void *tsd_val;
285 void (*tsd_dtor)(void *);
286 int tsd_inuse;
287 } __libc_tsd[TSD_KEYS_MAX];
288 static int __libc_tsd_nextkey;
289
__weak_alias(__libc_thr_keycreate,__libc_thr_keycreate_stub)290 __weak_alias(__libc_thr_keycreate,__libc_thr_keycreate_stub)
291 __weak_alias(__libc_thr_setspecific,__libc_thr_setspecific_stub)
292 __weak_alias(__libc_thr_getspecific,__libc_thr_getspecific_stub)
293 __weak_alias(__libc_thr_keydelete,__libc_thr_keydelete_stub)
294
295 int
296 __libc_thr_keycreate_stub(thread_key_t *k, void (*d)(void *))
297 {
298 int i;
299
300 for (i = __libc_tsd_nextkey; i < TSD_KEYS_MAX; i++) {
301 if (__libc_tsd[i].tsd_inuse == 0)
302 goto out;
303 }
304
305 for (i = 0; i < __libc_tsd_nextkey; i++) {
306 if (__libc_tsd[i].tsd_inuse == 0)
307 goto out;
308 }
309
310 return (EAGAIN);
311
312 out:
313 /*
314 * XXX We don't actually do anything with the destructor. We
315 * XXX probably should.
316 */
317 __libc_tsd[i].tsd_inuse = 1;
318 __libc_tsd_nextkey = (i + i) % TSD_KEYS_MAX;
319 __libc_tsd[i].tsd_dtor = d;
320 *k = i;
321
322 return (0);
323 }
324
325 int
__libc_thr_setspecific_stub(thread_key_t k,const void * v)326 __libc_thr_setspecific_stub(thread_key_t k, const void *v)
327 {
328
329 __libc_tsd[k].tsd_val = __UNCONST(v);
330
331 return (0);
332 }
333
334 void *
__libc_thr_getspecific_stub(thread_key_t k)335 __libc_thr_getspecific_stub(thread_key_t k)
336 {
337
338 return (__libc_tsd[k].tsd_val);
339 }
340
341 int
__libc_thr_keydelete_stub(thread_key_t k)342 __libc_thr_keydelete_stub(thread_key_t k)
343 {
344
345 /*
346 * XXX Do not recycle key; see big comment in libpthread.
347 */
348
349 __libc_tsd[k].tsd_dtor = NULL;
350
351 return (0);
352 }
353
354
355 /* misc. */
356
__weak_alias(__libc_thr_once,__libc_thr_once_stub)357 __weak_alias(__libc_thr_once,__libc_thr_once_stub)
358 __weak_alias(__libc_thr_sigsetmask,__libc_thr_sigsetmask_stub)
359 __weak_alias(__libc_thr_self,__libc_thr_self_stub)
360 __weak_alias(__libc_thr_yield,__libc_thr_yield_stub)
361 __weak_alias(__libc_thr_create,__libc_thr_create_stub)
362 __weak_alias(__libc_thr_exit,__libc_thr_exit_stub)
363 __weak_alias(__libc_thr_setcancelstate,__libc_thr_setcancelstate_stub)
364 __weak_alias(__libc_thr_equal,__libc_thr_equal_stub)
365 __weak_alias(__libc_thr_curcpu,__libc_thr_curcpu_stub)
366
367
368 int
369 __libc_thr_once_stub(once_t *o, void (*r)(void))
370 {
371
372 /* XXX Knowledge of libpthread types. */
373
374 if (o->pto_done == 0) {
375 (*r)();
376 o->pto_done = 1;
377 }
378
379 return (0);
380 }
381
382 int
__libc_thr_sigsetmask_stub(int h,const sigset_t * s,sigset_t * o)383 __libc_thr_sigsetmask_stub(int h, const sigset_t *s, sigset_t *o)
384 {
385
386 CHECK_NOT_THREADED();
387
388 return sigprocmask(h, s, o);
389 }
390
391 thr_t
__libc_thr_self_stub(void)392 __libc_thr_self_stub(void)
393 {
394
395 return ((thr_t) -1);
396 }
397
398 int
__libc_thr_yield_stub(void)399 __libc_thr_yield_stub(void)
400 {
401
402 /* Nothing to do. */
403 return (0);
404 }
405
406 int
__libc_thr_create_stub(thr_t * tp,const thrattr_t * ta,void * (* f)(void *),void * a)407 __libc_thr_create_stub(thr_t *tp, const thrattr_t *ta,
408 void *(*f)(void *), void *a)
409 {
410 /* LINTED deliberate lack of effect */
411 (void)tp;
412 /* LINTED deliberate lack of effect */
413 (void)ta;
414 /* LINTED deliberate lack of effect */
415 (void)f;
416 /* LINTED deliberate lack of effect */
417 (void)a;
418
419 DIE();
420
421 return (EOPNOTSUPP);
422 }
423
424 __dead void
__libc_thr_exit_stub(void * v)425 __libc_thr_exit_stub(void *v)
426 {
427 /* LINTED deliberate lack of effect */
428 (void)v;
429 exit(0);
430 }
431
432 int
__libc_thr_setcancelstate_stub(int new,int * old)433 __libc_thr_setcancelstate_stub(int new, int *old)
434 {
435 /* LINTED deliberate lack of effect */
436 (void)new;
437
438 /* LINTED deliberate lack of effect */
439 (void)old;
440
441 CHECK_NOT_THREADED();
442
443 return (0);
444 }
445
446 int
__libc_thr_equal_stub(pthread_t t1,pthread_t t2)447 __libc_thr_equal_stub(pthread_t t1, pthread_t t2)
448 {
449
450 /* assert that t1=t2=pthread_self() */
451 return (t1 == t2);
452 }
453
454 unsigned int
__libc_thr_curcpu_stub(void)455 __libc_thr_curcpu_stub(void)
456 {
457
458 return (0);
459 }
460
461 #endif /* _REENTRANT */
462