Home | History | Annotate | Line # | Download | only in include
      1 /*	$NetBSD: reentrant.h,v 1.23 2025/02/03 22:30:15 andvar Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by J.T. Conklin, by Nathan J. Williams, and 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 /*
     33  * Requirements:
     34  *
     35  * 1. The thread safe mechanism should be lightweight so the library can
     36  *    be used by non-threaded applications without unreasonable overhead.
     37  *
     38  * 2. There should be no dependency on a thread engine for non-threaded
     39  *    applications.
     40  *
     41  * 3. There should be no dependency on any particular thread engine.
     42  *
     43  * 4. The library should be able to be compiled without support for thread
     44  *    safety.
     45  *
     46  *
     47  * Rationale:
     48  *
     49  * One approach for thread safety is to provide discrete versions of the
     50  * library: one thread safe, the other not.  The disadvantage of this is
     51  * that libc is rather large, and two copies of a library which are 99%+
     52  * identical is not an efficient use of resources.
     53  *
     54  * Another approach is to provide a single thread safe library.  However,
     55  * it should not add significant run time or code size overhead to non-
     56  * threaded applications.
     57  *
     58  * Since the NetBSD C library is used in other projects, it should be
     59  * easy to replace the mutual exclusion primitives with ones provided by
     60  * another system.  Similarly, it should also be easy to remove all
     61  * support for thread safety completely if the target environment does
     62  * not support threads.
     63  *
     64  *
     65  * Implementation Details:
     66  *
     67  * The thread primitives used by the library (mutex_t, mutex_lock, etc.)
     68  * are macros which expand to the corresponding primitives provided by
     69  * the thread engine or to nothing.  The latter is used so that code is
     70  * not unreasonably cluttered with #ifdefs when all thread safe support
     71  * is removed.
     72  *
     73  * The thread macros can be directly mapped to the mutex primitives from
     74  * pthreads, however it should be reasonably easy to wrap another mutex
     75  * implementation so it presents a similar interface.
     76  *
     77  * The thread functions operate by dispatching to symbols which are, by
     78  * default, weak-aliased to no-op functions in thread-stub/thread-stub.c
     79  * (some uses of thread operations are conditional on __isthreaded, but
     80  * not all of them are).
     81  *
     82  * When the thread library is linked in, it provides strong-alias versions
     83  * of those symbols which dispatch to its own real thread operations.
     84  *
     85  */
     86 
     87 /*
     88  * Abstract thread interface for thread-safe libraries.  These routines
     89  * will use stubs in libc if the application is not linked against the
     90  * pthread library, and the real function in the pthread library if it
     91  * is.
     92  */
     93 
     94 #ifndef	_LIBC_REENTRANT_H_
     95 #define	_LIBC_REENTRANT_H_
     96 
     97 #include <pthread.h>
     98 #include <signal.h>
     99 
    100 #define	mutex_t			pthread_mutex_t
    101 #define	MUTEX_INITIALIZER	PTHREAD_MUTEX_INITIALIZER
    102 
    103 #define	mutexattr_t		pthread_mutexattr_t
    104 
    105 #define	MUTEX_TYPE_NORMAL	PTHREAD_MUTEX_NORMAL
    106 #define	MUTEX_TYPE_ERRORCHECK	PTHREAD_MUTEX_ERRORCHECK
    107 #define	MUTEX_TYPE_RECURSIVE	PTHREAD_MUTEX_RECURSIVE
    108 
    109 #define	cond_t			pthread_cond_t
    110 #define	COND_INITIALIZER	PTHREAD_COND_INITIALIZER
    111 
    112 #define	condattr_t		pthread_condattr_t
    113 
    114 #define	rwlock_t		pthread_rwlock_t
    115 #define	RWLOCK_INITIALIZER	PTHREAD_RWLOCK_INITIALIZER
    116 
    117 #define	rwlockattr_t		pthread_rwlockattr_t
    118 
    119 #define	thread_key_t		pthread_key_t
    120 
    121 #define	thr_t			pthread_t
    122 
    123 #define	thrattr_t		pthread_attr_t
    124 
    125 #define	once_t			pthread_once_t
    126 #define	ONCE_INITIALIZER	PTHREAD_ONCE_INIT
    127 
    128 #ifdef _REENTRANT
    129 
    130 #ifndef __LIBC_THREAD_STUBS
    131 
    132 __BEGIN_DECLS
    133 int	__libc_mutex_init(mutex_t *, const mutexattr_t *);
    134 int	__libc_mutex_lock(mutex_t *);
    135 int	__libc_mutex_trylock(mutex_t *);
    136 int	__libc_mutex_unlock(mutex_t *);
    137 int	__libc_mutex_destroy(mutex_t *);
    138 
    139 int	__libc_mutexattr_init(mutexattr_t *);
    140 int	__libc_mutexattr_settype(mutexattr_t *, int);
    141 int	__libc_mutexattr_destroy(mutexattr_t *);
    142 __END_DECLS
    143 
    144 #define	mutex_init(m, a)	__libc_mutex_init((m), (a))
    145 #define	mutex_lock(m)		__libc_mutex_lock((m))
    146 #define	mutex_trylock(m)	__libc_mutex_trylock((m))
    147 #define	mutex_unlock(m)		__libc_mutex_unlock((m))
    148 #define	mutex_destroy(m)	__libc_mutex_destroy((m))
    149 
    150 #define	mutexattr_init(ma)	__libc_mutexattr_init((ma))
    151 #define	mutexattr_settype(ma, t) __libc_mutexattr_settype((ma), (t))
    152 #define	mutexattr_destroy(ma)	__libc_mutexattr_destroy((ma))
    153 
    154 __BEGIN_DECLS
    155 int	__libc_cond_init(cond_t *, const condattr_t *);
    156 int	__libc_cond_signal(cond_t *);
    157 int	__libc_cond_broadcast(cond_t *);
    158 int	__libc_cond_wait(cond_t *, mutex_t *);
    159 #ifndef __LIBC12_SOURCE__
    160 int	__libc_cond_timedwait(cond_t *, mutex_t *, const struct timespec *);
    161 #endif
    162 int	__libc_cond_destroy(cond_t *);
    163 __END_DECLS
    164 
    165 #define	cond_init(c, t, a)     	__libc_cond_init((c), (a))
    166 #define	cond_signal(c)		__libc_cond_signal((c))
    167 #define	cond_broadcast(c)	__libc_cond_broadcast((c))
    168 #define	cond_wait(c, m)		__libc_cond_wait((c), (m))
    169 #define	cond_timedwait(c, m, t)	__libc_cond_timedwait((c), (m), (t))
    170 #define	cond_destroy(c)		__libc_cond_destroy((c))
    171 
    172 __BEGIN_DECLS
    173 int	__libc_rwlock_init(rwlock_t *, const rwlockattr_t *);
    174 int	__libc_rwlock_rdlock(rwlock_t *);
    175 int	__libc_rwlock_wrlock(rwlock_t *);
    176 int	__libc_rwlock_tryrdlock(rwlock_t *);
    177 int	__libc_rwlock_trywrlock(rwlock_t *);
    178 int	__libc_rwlock_unlock(rwlock_t *);
    179 int	__libc_rwlock_destroy(rwlock_t *);
    180 __END_DECLS
    181 
    182 #define	rwlock_init(l, a)	__libc_rwlock_init((l), (a))
    183 #define	rwlock_rdlock(l)	__libc_rwlock_rdlock((l))
    184 #define	rwlock_wrlock(l)	__libc_rwlock_wrlock((l))
    185 #define	rwlock_tryrdlock(l)	__libc_rwlock_tryrdlock((l))
    186 #define	rwlock_trywrlock(l)	__libc_rwlock_trywrlock((l))
    187 #define	rwlock_unlock(l)	__libc_rwlock_unlock((l))
    188 #define	rwlock_destroy(l)	__libc_rwlock_destroy((l))
    189 
    190 __BEGIN_DECLS
    191 int	__libc_thr_keycreate(thread_key_t *, void (*)(void *));
    192 int	__libc_thr_setspecific(thread_key_t, const void *);
    193 void	*__libc_thr_getspecific(thread_key_t);
    194 int	__libc_thr_keydelete(thread_key_t);
    195 __END_DECLS
    196 
    197 #define	thr_keycreate(k, d)	__libc_thr_keycreate((k), (d))
    198 #define	thr_setspecific(k, p)	__libc_thr_setspecific((k), (p))
    199 #define	thr_getspecific(k)	__libc_thr_getspecific((k))
    200 #define	thr_keydelete(k)	__libc_thr_keydelete((k))
    201 
    202 __BEGIN_DECLS
    203 int	__libc_thr_once(once_t *, void (*)(void));
    204 int	__libc_thr_sigsetmask(int, const sigset_t *, sigset_t *);
    205 thr_t	__libc_thr_self(void);
    206 int	__libc_thr_yield(void);
    207 void	__libc_thr_create(thr_t *, const thrattr_t *,
    208 	    void *(*)(void *), void *);
    209 void	__libc_thr_exit(void *) __attribute__((__noreturn__));
    210 int	*__libc_thr_errno(void);
    211 int	__libc_thr_setcancelstate(int, int *);
    212 unsigned int	__libc_thr_curcpu(void);
    213 
    214 extern int __isthreaded;
    215 __END_DECLS
    216 
    217 #define	thr_once(o, f)		__libc_thr_once((o), (f))
    218 #define	thr_sigsetmask(f, n, o)	__libc_thr_sigsetmask((f), (n), (o))
    219 #define	thr_self()		__libc_thr_self()
    220 #define	thr_yield()		__libc_thr_yield()
    221 #define	thr_create(tp, ta, f, a) __libc_thr_create((tp), (ta), (f), (a))
    222 #define	thr_exit(v)		__libc_thr_exit((v))
    223 #define	thr_errno()		__libc_thr_errno()
    224 #define	thr_enabled()		(__isthreaded)
    225 #define thr_setcancelstate(n, o) __libc_thr_setcancelstate((n),(o))
    226 #define thr_curcpu()		__libc_thr_curcpu()
    227 
    228 #else /* __LIBC_THREAD_STUBS */
    229 
    230 __BEGIN_DECLS
    231 void	__libc_thr_init_stub(void);
    232 
    233 int	__libc_mutex_init_stub(mutex_t *, const mutexattr_t *);
    234 int	__libc_mutex_lock_stub(mutex_t *);
    235 int	__libc_mutex_trylock_stub(mutex_t *);
    236 int	__libc_mutex_unlock_stub(mutex_t *);
    237 int	__libc_mutex_destroy_stub(mutex_t *);
    238 
    239 int	__libc_mutexattr_init_stub(mutexattr_t *);
    240 int	__libc_mutexattr_destroy_stub(mutexattr_t *);
    241 int	__libc_mutexattr_settype_stub(mutexattr_t *, int);
    242 
    243 int	__libc_cond_init_stub(cond_t *, const condattr_t *);
    244 int	__libc_cond_signal_stub(cond_t *);
    245 int	__libc_cond_broadcast_stub(cond_t *);
    246 int	__libc_cond_wait_stub(cond_t *, mutex_t *);
    247 int	__libc_cond_timedwait_stub(cond_t *, mutex_t *,
    248 				   const struct timespec *);
    249 int	__libc_cond_destroy_stub(cond_t *);
    250 
    251 int	__libc_rwlock_init_stub(rwlock_t *, const rwlockattr_t *);
    252 int	__libc_rwlock_rdlock_stub(rwlock_t *);
    253 int	__libc_rwlock_wrlock_stub(rwlock_t *);
    254 int	__libc_rwlock_tryrdlock_stub(rwlock_t *);
    255 int	__libc_rwlock_trywrlock_stub(rwlock_t *);
    256 int	__libc_rwlock_unlock_stub(rwlock_t *);
    257 int	__libc_rwlock_destroy_stub(rwlock_t *);
    258 
    259 int	__libc_thr_keycreate_stub(thread_key_t *, void (*)(void *));
    260 int	__libc_thr_setspecific_stub(thread_key_t, const void *);
    261 void	*__libc_thr_getspecific_stub(thread_key_t);
    262 int	__libc_thr_keydelete_stub(thread_key_t);
    263 
    264 int	__libc_thr_once_stub(once_t *, void (*)(void));
    265 int	__libc_thr_sigsetmask_stub(int, const sigset_t *, sigset_t *);
    266 thr_t	__libc_thr_self_stub(void);
    267 int	__libc_thr_yield_stub(void);
    268 int	__libc_thr_create_stub(thr_t *, const thrattr_t *,
    269 	    void *(*)(void *), void *);
    270 void	__libc_thr_exit_stub(void *) __dead;
    271 int	*__libc_thr_errno_stub(void);
    272 int	__libc_thr_setcancelstate_stub(int, int *);
    273 int	__libc_thr_equal_stub(pthread_t, pthread_t);
    274 unsigned int	__libc_thr_curcpu_stub(void);
    275 __END_DECLS
    276 
    277 #endif /* __LIBC_THREAD_STUBS */
    278 
    279 #define	FLOCKFILE(fp)		__flockfile_internal(fp, 1)
    280 #define	FUNLOCKFILE(fp)		__funlockfile_internal(fp, 1)
    281 
    282 #else /* _REENTRANT */
    283 
    284 #define	mutex_init(m, a) __nothing
    285 #define	mutex_lock(m) __nothing
    286 #define	mutex_trylock(m) __nothing
    287 #define	mutex_unlock(m)	__nothing
    288 #define	mutex_destroy(m) __nothing
    289 
    290 #define	cond_init(c, t, a) __nothing
    291 #define	cond_signal(c) __nothing
    292 #define	cond_broadcast(c) __nothing
    293 #define	cond_wait(c, m) __nothing
    294 #define	cond_timedwait(c, m, t) __nothing
    295 #define	cond_destroy(c) __nothing
    296 
    297 #define	rwlock_init(l, a) __nothing
    298 #define	rwlock_rdlock(l) __nothing
    299 #define	rwlock_wrlock(l) __nothing
    300 #define	rwlock_tryrdlock(l) __nothing
    301 #define	rwlock_trywrlock(l) __nothing
    302 #define	rwlock_unlock(l) __nothing
    303 #define	rwlock_destroy(l) __nothing
    304 
    305 #define	thr_keycreate(k, d) /*LINTED*/0
    306 #define	thr_setspecific(k, p) __nothing
    307 #define	thr_getspecific(k) /*LINTED*/0
    308 #define	thr_keydelete(k) __nothing
    309 
    310 #define	mutexattr_init(ma) __nothing
    311 #define	mutexattr_settype(ma, t) __nothing
    312 #define	mutexattr_destroy(ma) __nothing
    313 
    314 static inline int
    315 thr_once(once_t *once_control, void (*routine)(void))
    316 {
    317 	if (__predict_false(once_control->pto_done == 0)) {
    318 		(*routine)();
    319 		once_control->pto_done = 1;
    320 	}
    321 	return 0;
    322 }
    323 #define	thr_sigsetmask(f, n, o)	__nothing
    324 #define	thr_self() __nothing
    325 #define	thr_errno() __nothing
    326 #define	thr_curcpu()		((unsigned int)0)
    327 
    328 #define	FLOCKFILE(fp) __nothing
    329 #define	FUNLOCKFILE(fp) __nothing
    330 
    331 #endif /* _REENTRANT */
    332 
    333 #endif	/* _LIBC_REENTRANT_H_ */
    334