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