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