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