xref: /dragonfly/lib/libc/include/reentrant.h (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino  * Copyright (c) 1997,98 The NetBSD Foundation, Inc.
3*86d7f5d3SJohn Marino  * All rights reserved.
4*86d7f5d3SJohn Marino  *
5*86d7f5d3SJohn Marino  * This code is derived from software contributed to The NetBSD Foundation
6*86d7f5d3SJohn Marino  * by J.T. Conklin.
7*86d7f5d3SJohn Marino  *
8*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
9*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
10*86d7f5d3SJohn Marino  * are met:
11*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
12*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
13*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
14*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
15*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
16*86d7f5d3SJohn Marino  * 3. All advertising materials mentioning features or use of this software
17*86d7f5d3SJohn Marino  *    must display the following acknowledgement:
18*86d7f5d3SJohn Marino  *        This product includes software developed by the NetBSD
19*86d7f5d3SJohn Marino  *        Foundation, Inc. and its contributors.
20*86d7f5d3SJohn Marino  * 4. Neither the name of The NetBSD Foundation nor the names of its
21*86d7f5d3SJohn Marino  *    contributors may be used to endorse or promote products derived
22*86d7f5d3SJohn Marino  *    from this software without specific prior written permission.
23*86d7f5d3SJohn Marino  *
24*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25*86d7f5d3SJohn Marino  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26*86d7f5d3SJohn Marino  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27*86d7f5d3SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28*86d7f5d3SJohn Marino  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29*86d7f5d3SJohn Marino  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30*86d7f5d3SJohn Marino  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31*86d7f5d3SJohn Marino  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32*86d7f5d3SJohn Marino  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33*86d7f5d3SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34*86d7f5d3SJohn Marino  * POSSIBILITY OF SUCH DAMAGE.
35*86d7f5d3SJohn Marino  *
36*86d7f5d3SJohn Marino  * $FreeBSD: src/lib/libc/include/reentrant.h,v 1.3 2004/02/25 21:03:45 green Exp $
37*86d7f5d3SJohn Marino  */
38*86d7f5d3SJohn Marino 
39*86d7f5d3SJohn Marino /*
40*86d7f5d3SJohn Marino  * Requirements:
41*86d7f5d3SJohn Marino  *
42*86d7f5d3SJohn Marino  * 1. The thread safe mechanism should be lightweight so the library can
43*86d7f5d3SJohn Marino  *    be used by non-threaded applications without unreasonable overhead.
44*86d7f5d3SJohn Marino  *
45*86d7f5d3SJohn Marino  * 2. There should be no dependency on a thread engine for non-threaded
46*86d7f5d3SJohn Marino  *    applications.
47*86d7f5d3SJohn Marino  *
48*86d7f5d3SJohn Marino  * 3. There should be no dependency on any particular thread engine.
49*86d7f5d3SJohn Marino  *
50*86d7f5d3SJohn Marino  * 4. The library should be able to be compiled without support for thread
51*86d7f5d3SJohn Marino  *    safety.
52*86d7f5d3SJohn Marino  *
53*86d7f5d3SJohn Marino  *
54*86d7f5d3SJohn Marino  * Rationale:
55*86d7f5d3SJohn Marino  *
56*86d7f5d3SJohn Marino  * One approach for thread safety is to provide discrete versions of the
57*86d7f5d3SJohn Marino  * library: one thread safe, the other not.  The disadvantage of this is
58*86d7f5d3SJohn Marino  * that libc is rather large, and two copies of a library which are 99%+
59*86d7f5d3SJohn Marino  * identical is not an efficent use of resources.
60*86d7f5d3SJohn Marino  *
61*86d7f5d3SJohn Marino  * Another approach is to provide a single thread safe library.  However,
62*86d7f5d3SJohn Marino  * it should not add significant run time or code size overhead to non-
63*86d7f5d3SJohn Marino  * threaded applications.
64*86d7f5d3SJohn Marino  *
65*86d7f5d3SJohn Marino  * Since the NetBSD C library is used in other projects, it should be
66*86d7f5d3SJohn Marino  * easy to replace the mutual exclusion primitives with ones provided by
67*86d7f5d3SJohn Marino  * another system.  Similarly, it should also be easy to remove all
68*86d7f5d3SJohn Marino  * support for thread safety completely if the target environment does
69*86d7f5d3SJohn Marino  * not support threads.
70*86d7f5d3SJohn Marino  *
71*86d7f5d3SJohn Marino  *
72*86d7f5d3SJohn Marino  * Implementation Details:
73*86d7f5d3SJohn Marino  *
74*86d7f5d3SJohn Marino  * The mutex primitives used by the library (mutex_t, mutex_lock, etc.)
75*86d7f5d3SJohn Marino  * are macros which expand to the cooresponding primitives provided by
76*86d7f5d3SJohn Marino  * the thread engine or to nothing.  The latter is used so that code is
77*86d7f5d3SJohn Marino  * not unreasonably cluttered with #ifdefs when all thread safe support
78*86d7f5d3SJohn Marino  * is removed.
79*86d7f5d3SJohn Marino  *
80*86d7f5d3SJohn Marino  * The mutex macros can be directly mapped to the mutex primitives from
81*86d7f5d3SJohn Marino  * pthreads, however it should be reasonably easy to wrap another mutex
82*86d7f5d3SJohn Marino  * implementation so it presents a similar interface.
83*86d7f5d3SJohn Marino  *
84*86d7f5d3SJohn Marino  * Stub implementations of the mutex functions are provided with *weak*
85*86d7f5d3SJohn Marino  * linkage.  These functions simply return success.  When linked with a
86*86d7f5d3SJohn Marino  * thread library (i.e. -lpthread), the functions will override the
87*86d7f5d3SJohn Marino  * stubs.
88*86d7f5d3SJohn Marino  */
89*86d7f5d3SJohn Marino 
90*86d7f5d3SJohn Marino #include <pthread.h>
91*86d7f5d3SJohn Marino #include <pthread_np.h>
92*86d7f5d3SJohn Marino #include "libc_private.h"
93*86d7f5d3SJohn Marino 
94*86d7f5d3SJohn Marino #define mutex_t			pthread_mutex_t
95*86d7f5d3SJohn Marino #define cond_t			pthread_cond_t
96*86d7f5d3SJohn Marino #define rwlock_t		pthread_rwlock_t
97*86d7f5d3SJohn Marino #define once_t			pthread_once_t
98*86d7f5d3SJohn Marino 
99*86d7f5d3SJohn Marino #define thread_key_t		pthread_key_t
100*86d7f5d3SJohn Marino #define MUTEX_INITIALIZER	PTHREAD_MUTEX_INITIALIZER
101*86d7f5d3SJohn Marino #define RWLOCK_INITIALIZER	PTHREAD_RWLOCK_INITIALIZER
102*86d7f5d3SJohn Marino #define ONCE_INITIALIZER	PTHREAD_ONCE_INIT
103*86d7f5d3SJohn Marino 
104*86d7f5d3SJohn Marino #define mutex_init(m, a)	_pthread_mutex_init(m, a)
105*86d7f5d3SJohn Marino #define mutex_lock(m)		if (__isthreaded) \
106*86d7f5d3SJohn Marino 				_pthread_mutex_lock(m)
107*86d7f5d3SJohn Marino #define mutex_unlock(m)		if (__isthreaded) \
108*86d7f5d3SJohn Marino 				_pthread_mutex_unlock(m)
109*86d7f5d3SJohn Marino #define mutex_trylock(m)	(__isthreaded ? 0 : _pthread_mutex_trylock(m))
110*86d7f5d3SJohn Marino 
111*86d7f5d3SJohn Marino #define cond_init(c, a, p)	_pthread_cond_init(c, a)
112*86d7f5d3SJohn Marino #define cond_signal(m)		if (__isthreaded) \
113*86d7f5d3SJohn Marino 				_pthread_cond_signal(m)
114*86d7f5d3SJohn Marino #define cond_broadcast(m)	if (__isthreaded) \
115*86d7f5d3SJohn Marino 				_pthread_cond_broadcast(m)
116*86d7f5d3SJohn Marino #define cond_wait(c, m)		if (__isthreaded) \
117*86d7f5d3SJohn Marino 				_pthread_cond_wait(c, m)
118*86d7f5d3SJohn Marino 
119*86d7f5d3SJohn Marino #define rwlock_init(l, a)	_pthread_rwlock_init(l, a)
120*86d7f5d3SJohn Marino #define rwlock_rdlock(l)	if (__isthreaded) \
121*86d7f5d3SJohn Marino 				_pthread_rwlock_rdlock(l)
122*86d7f5d3SJohn Marino #define rwlock_wrlock(l)	if (__isthreaded) \
123*86d7f5d3SJohn Marino 				_pthread_rwlock_wrlock(l)
124*86d7f5d3SJohn Marino #define rwlock_unlock(l)	if (__isthreaded) \
125*86d7f5d3SJohn Marino 				_pthread_rwlock_unlock(l)
126*86d7f5d3SJohn Marino 
127*86d7f5d3SJohn Marino #define thr_keycreate(k, d)	_pthread_key_create(k, d)
128*86d7f5d3SJohn Marino #define thr_setspecific(k, p)	_pthread_setspecific(k, p)
129*86d7f5d3SJohn Marino #define thr_getspecific(k)	_pthread_getspecific(k)
130*86d7f5d3SJohn Marino #define thr_sigsetmask(f, n, o)	_pthread_sigmask(f, n, o)
131*86d7f5d3SJohn Marino 
132*86d7f5d3SJohn Marino #define thr_once(o, i)		_pthread_once(o, i)
133*86d7f5d3SJohn Marino #define thr_self()		_pthread_self()
134*86d7f5d3SJohn Marino #define thr_exit(x)		_pthread_exit(x)
135*86d7f5d3SJohn Marino #define thr_main()		_pthread_main_np()
136