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