Home | History | Annotate | Download | only in net
      1 /*	$NetBSD: reentrant.h,v 1.10 2004/12/14 00:23:19 nathanw 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Requirements:
     41  *
     42  * 1. The thread safe mechanism should be lightweight so the library can
     43  *    be used by non-threaded applications without unreasonable overhead.
     44  *
     45  * 2. There should be no dependency on a thread engine for non-threaded
     46  *    applications.
     47  *
     48  * 3. There should be no dependency on any particular thread engine.
     49  *
     50  * 4. The library should be able to be compiled without support for thread
     51  *    safety.
     52  *
     53  *
     54  * Rationale:
     55  *
     56  * One approach for thread safety is to provide discrete versions of the
     57  * library: one thread safe, the other not.  The disadvantage of this is
     58  * that libc is rather large, and two copies of a library which are 99%+
     59  * identical is not an efficent use of resources.
     60  *
     61  * Another approach is to provide a single thread safe library.  However,
     62  * it should not add significant run time or code size overhead to non-
     63  * threaded applications.
     64  *
     65  * Since the NetBSD C library is used in other projects, it should be
     66  * easy to replace the mutual exclusion primitives with ones provided by
     67  * another system.  Similarly, it should also be easy to remove all
     68  * support for thread safety completely if the target environment does
     69  * not support threads.
     70  *
     71  *
     72  * Implementation Details:
     73  *
     74  * The thread primitives used by the library (mutex_t, mutex_lock, etc.)
     75  * are macros which expand to the cooresponding primitives provided by
     76  * the thread engine or to nothing.  The latter is used so that code is
     77  * not unreasonably cluttered with #ifdefs when all thread safe support
     78  * is removed.
     79  *
     80  * The thread macros can be directly mapped to the mutex primitives from
     81  * pthreads, however it should be reasonably easy to wrap another mutex
     82  * implementation so it presents a similar interface.
     83  *
     84  * The thread functions operate by dispatching to symbols which are, by
     85  * default, weak-aliased to no-op functions in thread-stub/thread-stub.c
     86  * (some uses of thread operations are conditional on __isthreaded, but
     87  * not all of them are).
     88  *
     89  * When the thread library is linked in, it provides strong-alias versions
     90  * of those symbols which dispatch to its own real thread operations.
     91  *
     92  */
     93 
     94 #ifdef _REENTRANT
     95 
     96 /*
     97  * Abtract thread interface for thread-safe libraries.  These routines
     98  * will use stubs in libc if the application is not linked against the
     99  * pthread library, and the real function in the pthread library if it
    100  * is.
    101  */
    102 
    103 #include <pthread.h>
    104 #include <signal.h>
    105 #include <sys/cdefs.h>
    106 
    107 #define	mutex_t			pthread_mutex_t
    108 #define	MUTEX_INITIALIZER	PTHREAD_MUTEX_INITIALIZER
    109 
    110 #define	mutexattr_t		pthread_mutexattr_t
    111 
    112 #define	MUTEX_TYPE_NORMAL	PTHREAD_MUTEX_NORMAL
    113 #define	MUTEX_TYPE_ERRORCHECK	PTHREAD_MUTEX_ERRORCHECK
    114 #define	MUTEX_TYPE_RECURSIVE	PTHREAD_MUTEX_RECURSIVE
    115 
    116 #define	cond_t			pthread_cond_t
    117 #define	COND_INITIALIZER	PTHREAD_COND_INITIALIZER
    118 
    119 #define	condattr_t		pthread_condattr_t
    120 
    121 #define	rwlock_t		pthread_rwlock_t
    122 #define	RWLOCK_INITIALIZER	PTHREAD_RWLOCK_INITIALIZER
    123 
    124 #define	rwlockattr_t		pthread_rwlockattr_t
    125 
    126 #define	thread_key_t		pthread_key_t
    127 
    128 #define	thr_t			pthread_t
    129 
    130 #define	thrattr_t		pthread_attr_t
    131 
    132 #define	once_t			pthread_once_t
    133 #define	ONCE_INITIALIZER	PTHREAD_ONCE_INIT
    134 
    135 #ifndef __LIBC_THREAD_STUBS
    136 
    137 __BEGIN_DECLS
    138 int	__libc_mutex_init(mutex_t *, const mutexattr_t *);
    139 int	__libc_mutex_lock(mutex_t *);
    140 int	__libc_mutex_trylock(mutex_t *);
    141 int	__libc_mutex_unlock(mutex_t *);
    142 int	__libc_mutex_destroy(mutex_t *);
    143 
    144 int	__libc_mutexattr_init(mutexattr_t *);
    145 int	__libc_mutexattr_settype(mutexattr_t *, int);
    146 int	__libc_mutexattr_destroy(mutexattr_t *);
    147 __END_DECLS
    148 
    149 #define	mutex_init(m, a)	__libc_mutex_init((m), (a))
    150 #define	mutex_lock(m)		__libc_mutex_lock((m))
    151 #define	mutex_trylock(m)	__libc_mutex_trylock((m))
    152 #define	mutex_unlock(m)		__libc_mutex_unlock((m))
    153 #define	mutex_destroy(m)	__libc_mutex_destroy((m))
    154 
    155 #define	mutexattr_init(ma)	__libc_mutexattr_init((ma))
    156 #define	mutexattr_settype(ma, t) __libc_mutexattr_settype((ma), (t))
    157 #define	mutexattr_destroy(ma)	__libc_mutexattr_destroy((ma))
    158 
    159 __BEGIN_DECLS
    160 int	__libc_cond_init(cond_t *, const condattr_t *);
    161 int	__libc_cond_signal(cond_t *);
    162 int	__libc_cond_broadcast(cond_t *);
    163 int	__libc_cond_wait(cond_t *, mutex_t *);
    164 int	__libc_cond_timedwait(cond_t *, mutex_t *, const struct timespec *);
    165 int	__libc_cond_destroy(cond_t *);
    166 __END_DECLS
    167 
    168 #define	cond_init(c, t, a)     	__libc_cond_init((c), (a))
    169 #define	cond_signal(c)		__libc_cond_signal((c))
    170 #define	cond_broadcast(c)	__libc_cond_broadcast((c))
    171 #define	cond_wait(c, m)		__libc_cond_wait((c), (m))
    172 #define	cond_timedwait(c, m, t)	__libc_cond_timedwait((c), (m), (t))
    173 #define	cond_destroy(c)		__libc_cond_destroy((c))
    174 
    175 __BEGIN_DECLS
    176 int	__libc_rwlock_init(rwlock_t *, const rwlockattr_t *);
    177 int	__libc_rwlock_rdlock(rwlock_t *);
    178 int	__libc_rwlock_wrlock(rwlock_t *);
    179 int	__libc_rwlock_tryrdlock(rwlock_t *);
    180 int	__libc_rwlock_trywrlock(rwlock_t *);
    181 int	__libc_rwlock_unlock(rwlock_t *);
    182 int	__libc_rwlock_destroy(rwlock_t *);
    183 __END_DECLS
    184 
    185 #define	rwlock_init(l, a)	__libc_rwlock_init((l), (a))
    186 #define	rwlock_rdlock(l)	__libc_rwlock_rdlock((l))
    187 #define	rwlock_wrlock(l)	__libc_rwlock_wrlock((l))
    188 #define	rwlock_tryrdlock(l)	__libc_rwlock_tryrdlock((l))
    189 #define	rwlock_trywrlock(l)	__libc_rwlock_trywrlock((l))
    190 #define	rwlock_unlock(l)	__libc_rwlock_unlock((l))
    191 #define	rwlock_destroy(l)	__libc_rwlock_destroy((l))
    192 
    193 __BEGIN_DECLS
    194 int	__libc_thr_keycreate(thread_key_t *, void (*)(void *));
    195 int	__libc_thr_setspecific(thread_key_t, const void *);
    196 void	*__libc_thr_getspecific(thread_key_t);
    197 int	__libc_thr_keydelete(thread_key_t);
    198 __END_DECLS
    199 
    200 #define	thr_keycreate(k, d)	__libc_thr_keycreate((k), (d))
    201 #define	thr_setspecific(k, p)	__libc_thr_setspecific((k), (p))
    202 #define	thr_getspecific(k)	__libc_thr_getspecific((k))
    203 #define	thr_keydelete(k)	__libc_thr_keydelete((k))
    204 
    205 __BEGIN_DECLS
    206 int	__libc_thr_once(once_t *, void (*)(void));
    207 int	__libc_thr_sigsetmask(int, const sigset_t *, sigset_t *);
    208 thr_t	__libc_thr_self(void);
    209 int	__libc_thr_yield(void);
    210 void	__libc_thr_create(thr_t *, const thrattr_t *,
    211 	    void *(*)(void *), void *);
    212 void	__libc_thr_exit(void *) __attribute__((__noreturn__));
    213 int	*__libc_thr_errno(void);
    214 int	__libc_thr_setcancelstate(int, int *);
    215 
    216 extern int __isthreaded;
    217 __END_DECLS
    218 
    219 #define	thr_once(o, f)		__libc_thr_once((o), (f))
    220 #define	thr_sigsetmask(f, n, o)	__libc_thr_sigsetmask((f), (n), (o))
    221 #define	thr_self()		__libc_thr_self()
    222 #define	thr_yield()		__libc_thr_yield()
    223 #define	thr_create(tp, ta, f, a) __libc_thr_create((tp), (ta), (f), (a))
    224 #define	thr_exit(v)		__libc_thr_exit((v))
    225 #define	thr_errno()		__libc_thr_errno()
    226 #define	thr_enabled()		(__isthreaded)
    227 #define thr_setcancelstate(n, o) __libc_thr_setcancelstate((n),(o))
    228 #endif /* __LIBC_THREAD_STUBS */
    229 
    230 #define	FLOCKFILE(fp)		__flockfile_internal(fp, 1)
    231 #define	FUNLOCKFILE(fp)		__funlockfile_internal(fp, 1)
    232 
    233 #else /* _REENTRANT */
    234 
    235 #define	mutex_init(m, a)
    236 #define	mutex_lock(m)
    237 #define	mutex_trylock(m)
    238 #define	mutex_unlock(m)
    239 #define	mutex_destroy(m)
    240 
    241 #define	cond_init(c, t, a)
    242 #define	cond_signal(c)
    243 #define	cond_broadcast(c)
    244 #define	cond_wait(c, m)
    245 #define	cond_timedwait(c, m, t)
    246 #define	cond_destroy(c)
    247 
    248 #define	rwlock_init(l, a)
    249 #define	rwlock_rdlock(l)
    250 #define	rwlock_wrlock(l)
    251 #define	rwlock_tryrdlock(l)
    252 #define	rwlock_trywrlock(l)
    253 #define	rwlock_unlock(l)
    254 #define	rwlock_destroy(l)
    255 
    256 #define	thr_keycreate(k, d)
    257 #define	thr_setspecific(k, p)
    258 #define	thr_getspecific(k)
    259 #define	thr_keydelete(k)
    260 
    261 #define	thr_once(o, f)
    262 #define	thr_sigsetmask(f, n, o)
    263 #define	thr_self()
    264 #define	thr_errno()
    265 
    266 #define	FLOCKFILE(fp)
    267 #define	FUNLOCKFILE(fp)
    268 
    269 #endif /* _REENTRANT */
    270