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