Home | History | Annotate | Line # | Download | only in include
reentrant.h revision 1.7
      1  1.7  thorpej /*	$NetBSD: reentrant.h,v 1.7 2003/01/18 11:23:54 thorpej Exp $	*/
      2  1.5   kleink 
      3  1.1      jtc /*-
      4  1.7  thorpej  * Copyright (c) 1997, 1998, 2003 The NetBSD Foundation, Inc.
      5  1.1      jtc  * All rights reserved.
      6  1.1      jtc  *
      7  1.1      jtc  * This code is derived from software contributed to The NetBSD Foundation
      8  1.7  thorpej  * by J.T. Conklin, by Nathan J. Williams, and by Jason R. Thorpe.
      9  1.1      jtc  *
     10  1.1      jtc  * Redistribution and use in source and binary forms, with or without
     11  1.1      jtc  * modification, are permitted provided that the following conditions
     12  1.1      jtc  * are met:
     13  1.1      jtc  * 1. Redistributions of source code must retain the above copyright
     14  1.1      jtc  *    notice, this list of conditions and the following disclaimer.
     15  1.1      jtc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      jtc  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      jtc  *    documentation and/or other materials provided with the distribution.
     18  1.1      jtc  * 3. All advertising materials mentioning features or use of this software
     19  1.1      jtc  *    must display the following acknowledgement:
     20  1.1      jtc  *        This product includes software developed by the NetBSD
     21  1.1      jtc  *        Foundation, Inc. and its contributors.
     22  1.1      jtc  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1      jtc  *    contributors may be used to endorse or promote products derived
     24  1.1      jtc  *    from this software without specific prior written permission.
     25  1.1      jtc  *
     26  1.1      jtc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1      jtc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1      jtc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1      jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1      jtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1      jtc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1      jtc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1      jtc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1      jtc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1      jtc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1      jtc  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1      jtc  */
     38  1.1      jtc 
     39  1.1      jtc /*
     40  1.1      jtc  * Requirements:
     41  1.1      jtc  *
     42  1.1      jtc  * 1. The thread safe mechanism should be lightweight so the library can
     43  1.1      jtc  *    be used by non-threaded applications without unreasonable overhead.
     44  1.1      jtc  *
     45  1.1      jtc  * 2. There should be no dependency on a thread engine for non-threaded
     46  1.1      jtc  *    applications.
     47  1.1      jtc  *
     48  1.1      jtc  * 3. There should be no dependency on any particular thread engine.
     49  1.1      jtc  *
     50  1.1      jtc  * 4. The library should be able to be compiled without support for thread
     51  1.1      jtc  *    safety.
     52  1.1      jtc  *
     53  1.1      jtc  *
     54  1.1      jtc  * Rationale:
     55  1.1      jtc  *
     56  1.1      jtc  * One approach for thread safety is to provide discrete versions of the
     57  1.1      jtc  * library: one thread safe, the other not.  The disadvantage of this is
     58  1.1      jtc  * that libc is rather large, and two copies of a library which are 99%+
     59  1.1      jtc  * identical is not an efficent use of resources.
     60  1.1      jtc  *
     61  1.1      jtc  * Another approach is to provide a single thread safe library.  However,
     62  1.1      jtc  * it should not add significant run time or code size overhead to non-
     63  1.1      jtc  * threaded applications.
     64  1.1      jtc  *
     65  1.1      jtc  * Since the NetBSD C library is used in other projects, it should be
     66  1.1      jtc  * easy to replace the mutual exclusion primitives with ones provided by
     67  1.1      jtc  * another system.  Similarly, it should also be easy to remove all
     68  1.1      jtc  * support for thread safety completely if the target environment does
     69  1.1      jtc  * not support threads.
     70  1.1      jtc  *
     71  1.1      jtc  *
     72  1.1      jtc  * Implementation Details:
     73  1.1      jtc  *
     74  1.7  thorpej  * The thread primitives used by the library (mutex_t, mutex_lock, etc.)
     75  1.1      jtc  * are macros which expand to the cooresponding primitives provided by
     76  1.1      jtc  * the thread engine or to nothing.  The latter is used so that code is
     77  1.1      jtc  * not unreasonably cluttered with #ifdefs when all thread safe support
     78  1.1      jtc  * is removed.
     79  1.1      jtc  *
     80  1.7  thorpej  * The thread macros can be directly mapped to the mutex primitives from
     81  1.1      jtc  * pthreads, however it should be reasonably easy to wrap another mutex
     82  1.1      jtc  * implementation so it presents a similar interface.
     83  1.1      jtc  *
     84  1.7  thorpej  * The thread functions operate by dispatching to symbols which are, by
     85  1.7  thorpej  * default, weak-aliased to no-op functions in thread-stub/thread-stub.c
     86  1.7  thorpej  * (some uses of thread operations are conditional on __isthreaded, but
     87  1.7  thorpej  * not all of them are).
     88  1.7  thorpej  *
     89  1.7  thorpej  * When the thread library is linked in, it provides strong-alias versions
     90  1.7  thorpej  * of those symbols which dispatch to its own real thread operations.
     91  1.1      jtc  */
     92  1.1      jtc 
     93  1.7  thorpej #ifdef _REENTRANT
     94  1.1      jtc 
     95  1.1      jtc #include <pthread.h>
     96  1.7  thorpej #include <signal.h>
     97  1.1      jtc 
     98  1.7  thorpej #define	mutex_t			pthread_mutex_t
     99  1.7  thorpej #define	MUTEX_INITIALIZER	PTHREAD_MUTEX_INITIALIZER
    100  1.1      jtc 
    101  1.7  thorpej #define	mutexattr_t		pthread_mutexattr_t
    102  1.6     fvdl 
    103  1.7  thorpej #define	cond_t			pthread_cond_t
    104  1.7  thorpej #define	COND_INITIALIZER	PTHREAD_COND_INITIALIZER
    105  1.7  thorpej 
    106  1.7  thorpej #define	condattr_t		pthread_condattr_t
    107  1.7  thorpej 
    108  1.7  thorpej #define	rwlock_t		pthread_rwlock_t
    109  1.7  thorpej #define	RWLOCK_INITIALIZER	PTHREAD_RWLOCK_INITIALIZER
    110  1.7  thorpej 
    111  1.7  thorpej #define	rwlockattr_t		pthread_rwlockattr_t
    112  1.7  thorpej 
    113  1.7  thorpej #define	thread_key_t		pthread_key_t
    114  1.7  thorpej 
    115  1.7  thorpej #define	thr_t			pthread_t
    116  1.7  thorpej 
    117  1.7  thorpej #define	once_t			pthread_once_t
    118  1.7  thorpej #define	ONCE_INITIALIZER	PTHREAD_ONCE_INIT
    119  1.7  thorpej 
    120  1.7  thorpej #ifndef __LIBC_THREAD_STUBS
    121  1.7  thorpej 
    122  1.7  thorpej int	__libc_mutex_init(mutex_t *, const mutexattr_t *);
    123  1.7  thorpej int	__libc_mutex_lock(mutex_t *);
    124  1.7  thorpej int	__libc_mutex_trylock(mutex_t *);
    125  1.7  thorpej int	__libc_mutex_unlock(mutex_t *);
    126  1.7  thorpej int	__libc_mutex_destroy(mutex_t *);
    127  1.7  thorpej 
    128  1.7  thorpej #define	mutex_init(m, a)	__libc_mutex_init((m), (a))
    129  1.7  thorpej #define	mutex_lock(m)		__libc_mutex_lock((m))
    130  1.7  thorpej #define	mutex_trylock(m)	__libc_mutex_trylock((m))
    131  1.7  thorpej #define	mutex_unlock(m)		__libc_mutex_unlock((m))
    132  1.7  thorpej #define	mutex_destroy(m)	__libc_mutex_destroy((m))
    133  1.7  thorpej 
    134  1.7  thorpej int	__libc_cond_init(cond_t *, const condattr_t *);
    135  1.7  thorpej int	__libc_cond_signal(cond_t *);
    136  1.7  thorpej int	__libc_cond_broadcast(cond_t *);
    137  1.7  thorpej int	__libc_cond_wait(cond_t *, mutex_t *);
    138  1.7  thorpej int	__libc_cond_timedwait(cond_t *, mutex_t *, const struct timespec *);
    139  1.7  thorpej int	__libc_cond_destroy(cond_t *);
    140  1.7  thorpej 
    141  1.7  thorpej #define	cond_init(c, t, a)     	__libc_cond_init((c), (a))
    142  1.7  thorpej #define	cond_signal(c)		__libc_cond_signal((c))
    143  1.7  thorpej #define	cond_broadcast(c)	__libc_cond_broadcast((c))
    144  1.7  thorpej #define	cond_wait(c, m)		__libc_cond_wait((c), (m))
    145  1.7  thorpej #define	cond_timedwait(c, m, t)	__libc_cond_timedwait((c), (m), (t))
    146  1.7  thorpej #define	cond_destroy(c)		__libc_cond_destroy((c))
    147  1.7  thorpej 
    148  1.7  thorpej int	__libc_rwlock_init(rwlock_t *, const rwlockattr_t *);
    149  1.7  thorpej int	__libc_rwlock_rdlock(rwlock_t *);
    150  1.7  thorpej int	__libc_rwlock_wrlock(rwlock_t *);
    151  1.7  thorpej int	__libc_rwlock_tryrdlock(rwlock_t *);
    152  1.7  thorpej int	__libc_rwlock_trywrlock(rwlock_t *);
    153  1.7  thorpej int	__libc_rwlock_unlock(rwlock_t *);
    154  1.7  thorpej int	__libc_rwlock_destroy(rwlock_t *);
    155  1.7  thorpej 
    156  1.7  thorpej #define	rwlock_init(l, a)	__libc_rwlock_init((l), (a))
    157  1.7  thorpej #define	rwlock_rdlock(l)	__libc_rwlock_rdlock((l))
    158  1.7  thorpej #define	rwlock_wrlock(l)	__libc_rwlock_wrlock((l))
    159  1.7  thorpej #define	rwlock_tryrdlock(l)	__libc_rwlock_tryrdlock((l))
    160  1.7  thorpej #define	rwlock_trywrlock(l)	__libc_rwlock_trywrlock((l))
    161  1.7  thorpej #define	rwlock_unlock(l)	__libc_rwlock_unlock((l))
    162  1.7  thorpej #define	rwlock_destroy(l)	__libc_rwlock_destroy((l))
    163  1.7  thorpej 
    164  1.7  thorpej int	__libc_thr_keycreate(thread_key_t *, void (*)(void *));
    165  1.7  thorpej int	__libc_thr_setspecific(thread_key_t, const void *);
    166  1.7  thorpej void	*__libc_thr_getspecific(thread_key_t);
    167  1.7  thorpej int	__libc_thr_keydelete(thread_key_t);
    168  1.7  thorpej 
    169  1.7  thorpej #define	thr_keycreate(k, d)	__libc_thr_keycreate((k), (d))
    170  1.7  thorpej #define	thr_setspecific(k, p)	__libc_thr_setspecific((k), (p))
    171  1.7  thorpej #define	thr_getspecific(k)	__libc_thr_getspecific((k))
    172  1.7  thorpej #define	thr_keydelete(k)	__libc_thr_keydelete((k))
    173  1.7  thorpej 
    174  1.7  thorpej int	__libc_thr_once(once_t *, void (*)(void));
    175  1.7  thorpej int	__libc_thr_sigsetmask(int, const sigset_t *, sigset_t *);
    176  1.7  thorpej thr_t	__libc_thr_self(void);
    177  1.7  thorpej int	*__libc_thr_errno(void);
    178  1.7  thorpej 
    179  1.7  thorpej #define	thr_once(o, f)		__libc_thr_once((o), (f))
    180  1.7  thorpej #define	thr_sigsetmask(f, n, o)	__libc_thr_sigsetmask((f), (n), (o))
    181  1.7  thorpej #define	thr_self()		__libc_thr_self()
    182  1.7  thorpej #define	thr_errno()		__libc_thr_errno()
    183  1.7  thorpej 
    184  1.7  thorpej #define	FLOCKFILE(fp)		flockfile(fp)
    185  1.7  thorpej #define	FUNLOCKFILE(fp)		funlockfile(fp)
    186  1.7  thorpej 
    187  1.7  thorpej #endif /* __LIBC_THREAD_STUBS */
    188  1.7  thorpej 
    189  1.7  thorpej #else /* _REENTRANT */
    190  1.7  thorpej 
    191  1.7  thorpej #define	mutex_init(m, a)
    192  1.7  thorpej #define	mutex_lock(m)
    193  1.7  thorpej #define	mutex_trylock(m)
    194  1.7  thorpej #define	mutex_unlock(m)
    195  1.7  thorpej #define	mutex_destroy(m)
    196  1.7  thorpej 
    197  1.7  thorpej #define	cond_init(c, t, a)
    198  1.7  thorpej #define	cond_signal(c)
    199  1.7  thorpej #define	cond_broadcast(c)
    200  1.7  thorpej #define	cond_wait(c, m)
    201  1.7  thorpej #define	cond_timedwait(c, m, t)
    202  1.7  thorpej #define	cond_destroy(c)
    203  1.7  thorpej 
    204  1.7  thorpej #define	rwlock_init(l, a)
    205  1.7  thorpej #define	rwlock_rdlock(l)
    206  1.7  thorpej #define	rwlock_wrlock(l)
    207  1.7  thorpej #define	rwlock_tryrdlock(l)
    208  1.7  thorpej #define	rwlock_trywrlock(l)
    209  1.7  thorpej #define	rwlock_unlock(l)
    210  1.7  thorpej #define	rwlock_destroy(l)
    211  1.7  thorpej 
    212  1.7  thorpej #define	thr_keycreate(k, d)
    213  1.7  thorpej #define	thr_setspecific(k, p)
    214  1.7  thorpej #define	thr_getspecific(k)
    215  1.7  thorpej #define	thr_keydelete(k)
    216  1.7  thorpej 
    217  1.7  thorpej #define	thr_once(o, f)
    218  1.7  thorpej #define	thr_sigsetmask(f, n, o)
    219  1.7  thorpej #define	thr_self()
    220  1.7  thorpej #define	thr_errno()
    221  1.2      jtc 
    222  1.7  thorpej #define	FLOCKFILE(fp)
    223  1.7  thorpej #define	FUNLOCKFILE(fp)
    224  1.1      jtc 
    225  1.7  thorpej #endif /* _REENTRANT */
    226