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