Home | History | Annotate | Line # | Download | only in include
reentrant.h revision 1.6
      1 /*	$NetBSD: reentrant.h,v 1.6 2000/06/02 23:11:06 fvdl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997,98 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.
      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 mutex 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 mutex 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  * Stub implementations of the mutex functions are provided with *weak*
     85  * linkage.  These functions simply return success.  When linked with a
     86  * thread library (i.e. -lpthread), the functions will override the
     87  * stubs.
     88  */
     89 
     90 /* FIXME: Using _REENT during integration testing.  It should be changed
     91    to _REENTRANT once pthread engine is available */
     92 
     93 #ifdef _REENT
     94 
     95 #include <pthread.h>
     96 
     97 #define mutex_t			pthread_mutex_t
     98 #define MUTEX_INITIALIZER	PTHREAD_MUTEX_INITIALIZER
     99 
    100 #define mutex_init(m, a)	pthread_mutex_init(m, a)
    101 #define mutex_lock(m)		pthread_mutex_lock(m)
    102 #define mutex_trylock(m)	pthread_mutex_trylock(m)
    103 #define mutex_unlock(m)		pthread_mutex_unlock(m)
    104 
    105 #define cond_t			pthread_cond_t
    106 #define cond_signal(m)		pthread_cond_signal(m)
    107 #define cond_wait(c, m)		pthread_cond_wait(c, m)
    108 #define cond_init(c, a, p)	pthread_cond_init(c, a)
    109 
    110 #define rwlock_t		pthread_rwlock_t
    111 #define RWLOCK_INITIALIZER	PTHREAD_RWLOCK_INITIALIZER
    112 
    113 #define rwlock_init(l, a)	pthread_rwlock_init(l, a)
    114 #define rwlock_rdlock(l)	pthread_rwlock_rdlock(l)
    115 #define rwlock_wrlock(l)	pthread_rwlock_wrlock(l)
    116 #define rwlock_unlock(l)	pthread_rwlock_unlock(l)
    117 
    118 #define thread_key_t		pthread_key_t
    119 #define thr_keycreate(k, d)	pthread_key_create(k, d)
    120 #define thr_setspecific(k, p)	pthread_setspecific(k, p)
    121 #define thr_getspecific(k)	pthread_getspecific(k)
    122 #define thr_sigsetmask(f, n, o)	pthread_sigmask(f, n, o)
    123 
    124 #define thr_self()		pthread_self()
    125 #define thr_exit(x)		pthread_exit(x)
    126 
    127 #define FLOCKFILE(fp)		flockfile(fp)
    128 #define FUNLOCKFILE(fp)		funlockfile(fp)
    129 
    130 #else
    131 
    132 #define mutex_init(m, a)
    133 #define mutex_lock(m)
    134 #define mutex_trylock(m)
    135 #define mutex_unlock(m)
    136 
    137 #define cond_signal(m)
    138 #define cond_wait(c, m)
    139 #define cond_init(c, a, p)
    140 
    141 #define rwlock_init(l, a)
    142 #define rwlock_rdlock(l)
    143 #define rwlock_wrlock(l)
    144 #define rwlock_unlock(l)
    145 
    146 #define thr_keycreate(k, d)
    147 #define thr_setspecific(k, p)
    148 #define thr_getspecific(k)
    149 #define thr_sigsetmask(f, n, o)
    150 
    151 #define thr_self()
    152 #define thr_exit()
    153 
    154 #define FLOCKFILE(fp)
    155 #define FUNLOCKFILE(fp)
    156 
    157 #endif
    158