reentrant.h revision 1.22 1 1.22 riastrad /* $NetBSD: reentrant.h,v 1.22 2024/08/27 13:43:02 riastradh Exp $ */
2 1.5 kleink
3 1.1 jtc /*-
4 1.7 thorpej * Copyright (c) 1997, 1998, 2003 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.7 thorpej * by J.T. Conklin, by Nathan J. Williams, and by Jason R. Thorpe.
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 *
19 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jtc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jtc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jtc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jtc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jtc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jtc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jtc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jtc * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jtc */
31 1.1 jtc
32 1.1 jtc /*
33 1.1 jtc * Requirements:
34 1.1 jtc *
35 1.1 jtc * 1. The thread safe mechanism should be lightweight so the library can
36 1.1 jtc * be used by non-threaded applications without unreasonable overhead.
37 1.1 jtc *
38 1.1 jtc * 2. There should be no dependency on a thread engine for non-threaded
39 1.1 jtc * applications.
40 1.1 jtc *
41 1.1 jtc * 3. There should be no dependency on any particular thread engine.
42 1.1 jtc *
43 1.1 jtc * 4. The library should be able to be compiled without support for thread
44 1.1 jtc * safety.
45 1.1 jtc *
46 1.1 jtc *
47 1.1 jtc * Rationale:
48 1.1 jtc *
49 1.1 jtc * One approach for thread safety is to provide discrete versions of the
50 1.1 jtc * library: one thread safe, the other not. The disadvantage of this is
51 1.1 jtc * that libc is rather large, and two copies of a library which are 99%+
52 1.21 andvar * identical is not an efficient use of resources.
53 1.1 jtc *
54 1.1 jtc * Another approach is to provide a single thread safe library. However,
55 1.1 jtc * it should not add significant run time or code size overhead to non-
56 1.1 jtc * threaded applications.
57 1.1 jtc *
58 1.1 jtc * Since the NetBSD C library is used in other projects, it should be
59 1.1 jtc * easy to replace the mutual exclusion primitives with ones provided by
60 1.1 jtc * another system. Similarly, it should also be easy to remove all
61 1.1 jtc * support for thread safety completely if the target environment does
62 1.1 jtc * not support threads.
63 1.1 jtc *
64 1.1 jtc *
65 1.1 jtc * Implementation Details:
66 1.1 jtc *
67 1.7 thorpej * The thread primitives used by the library (mutex_t, mutex_lock, etc.)
68 1.1 jtc * are macros which expand to the cooresponding primitives provided by
69 1.1 jtc * the thread engine or to nothing. The latter is used so that code is
70 1.1 jtc * not unreasonably cluttered with #ifdefs when all thread safe support
71 1.1 jtc * is removed.
72 1.1 jtc *
73 1.7 thorpej * The thread macros can be directly mapped to the mutex primitives from
74 1.1 jtc * pthreads, however it should be reasonably easy to wrap another mutex
75 1.1 jtc * implementation so it presents a similar interface.
76 1.1 jtc *
77 1.7 thorpej * The thread functions operate by dispatching to symbols which are, by
78 1.7 thorpej * default, weak-aliased to no-op functions in thread-stub/thread-stub.c
79 1.7 thorpej * (some uses of thread operations are conditional on __isthreaded, but
80 1.7 thorpej * not all of them are).
81 1.7 thorpej *
82 1.7 thorpej * When the thread library is linked in, it provides strong-alias versions
83 1.7 thorpej * of those symbols which dispatch to its own real thread operations.
84 1.8 thorpej *
85 1.1 jtc */
86 1.1 jtc
87 1.10 nathanw /*
88 1.12 rtr * Abstract thread interface for thread-safe libraries. These routines
89 1.10 nathanw * will use stubs in libc if the application is not linked against the
90 1.10 nathanw * pthread library, and the real function in the pthread library if it
91 1.10 nathanw * is.
92 1.10 nathanw */
93 1.10 nathanw
94 1.22 riastrad #ifndef _LIBC_REENTRANT_H_
95 1.22 riastrad #define _LIBC_REENTRANT_H_
96 1.22 riastrad
97 1.10 nathanw #include <pthread.h>
98 1.10 nathanw #include <signal.h>
99 1.10 nathanw
100 1.10 nathanw #define mutex_t pthread_mutex_t
101 1.10 nathanw #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
102 1.10 nathanw
103 1.10 nathanw #define mutexattr_t pthread_mutexattr_t
104 1.10 nathanw
105 1.10 nathanw #define MUTEX_TYPE_NORMAL PTHREAD_MUTEX_NORMAL
106 1.10 nathanw #define MUTEX_TYPE_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK
107 1.10 nathanw #define MUTEX_TYPE_RECURSIVE PTHREAD_MUTEX_RECURSIVE
108 1.10 nathanw
109 1.10 nathanw #define cond_t pthread_cond_t
110 1.10 nathanw #define COND_INITIALIZER PTHREAD_COND_INITIALIZER
111 1.10 nathanw
112 1.10 nathanw #define condattr_t pthread_condattr_t
113 1.10 nathanw
114 1.10 nathanw #define rwlock_t pthread_rwlock_t
115 1.10 nathanw #define RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
116 1.10 nathanw
117 1.10 nathanw #define rwlockattr_t pthread_rwlockattr_t
118 1.10 nathanw
119 1.10 nathanw #define thread_key_t pthread_key_t
120 1.10 nathanw
121 1.10 nathanw #define thr_t pthread_t
122 1.10 nathanw
123 1.10 nathanw #define thrattr_t pthread_attr_t
124 1.10 nathanw
125 1.10 nathanw #define once_t pthread_once_t
126 1.10 nathanw #define ONCE_INITIALIZER PTHREAD_ONCE_INIT
127 1.10 nathanw
128 1.15 joerg #ifdef _REENTRANT
129 1.15 joerg
130 1.10 nathanw #ifndef __LIBC_THREAD_STUBS
131 1.10 nathanw
132 1.10 nathanw __BEGIN_DECLS
133 1.10 nathanw int __libc_mutex_init(mutex_t *, const mutexattr_t *);
134 1.10 nathanw int __libc_mutex_lock(mutex_t *);
135 1.10 nathanw int __libc_mutex_trylock(mutex_t *);
136 1.10 nathanw int __libc_mutex_unlock(mutex_t *);
137 1.10 nathanw int __libc_mutex_destroy(mutex_t *);
138 1.10 nathanw
139 1.10 nathanw int __libc_mutexattr_init(mutexattr_t *);
140 1.10 nathanw int __libc_mutexattr_settype(mutexattr_t *, int);
141 1.10 nathanw int __libc_mutexattr_destroy(mutexattr_t *);
142 1.10 nathanw __END_DECLS
143 1.10 nathanw
144 1.10 nathanw #define mutex_init(m, a) __libc_mutex_init((m), (a))
145 1.10 nathanw #define mutex_lock(m) __libc_mutex_lock((m))
146 1.10 nathanw #define mutex_trylock(m) __libc_mutex_trylock((m))
147 1.10 nathanw #define mutex_unlock(m) __libc_mutex_unlock((m))
148 1.10 nathanw #define mutex_destroy(m) __libc_mutex_destroy((m))
149 1.10 nathanw
150 1.10 nathanw #define mutexattr_init(ma) __libc_mutexattr_init((ma))
151 1.10 nathanw #define mutexattr_settype(ma, t) __libc_mutexattr_settype((ma), (t))
152 1.10 nathanw #define mutexattr_destroy(ma) __libc_mutexattr_destroy((ma))
153 1.10 nathanw
154 1.10 nathanw __BEGIN_DECLS
155 1.10 nathanw int __libc_cond_init(cond_t *, const condattr_t *);
156 1.10 nathanw int __libc_cond_signal(cond_t *);
157 1.10 nathanw int __libc_cond_broadcast(cond_t *);
158 1.10 nathanw int __libc_cond_wait(cond_t *, mutex_t *);
159 1.14 christos #ifndef __LIBC12_SOURCE__
160 1.10 nathanw int __libc_cond_timedwait(cond_t *, mutex_t *, const struct timespec *);
161 1.14 christos #endif
162 1.10 nathanw int __libc_cond_destroy(cond_t *);
163 1.10 nathanw __END_DECLS
164 1.10 nathanw
165 1.10 nathanw #define cond_init(c, t, a) __libc_cond_init((c), (a))
166 1.10 nathanw #define cond_signal(c) __libc_cond_signal((c))
167 1.10 nathanw #define cond_broadcast(c) __libc_cond_broadcast((c))
168 1.10 nathanw #define cond_wait(c, m) __libc_cond_wait((c), (m))
169 1.10 nathanw #define cond_timedwait(c, m, t) __libc_cond_timedwait((c), (m), (t))
170 1.10 nathanw #define cond_destroy(c) __libc_cond_destroy((c))
171 1.10 nathanw
172 1.10 nathanw __BEGIN_DECLS
173 1.10 nathanw int __libc_rwlock_init(rwlock_t *, const rwlockattr_t *);
174 1.10 nathanw int __libc_rwlock_rdlock(rwlock_t *);
175 1.10 nathanw int __libc_rwlock_wrlock(rwlock_t *);
176 1.10 nathanw int __libc_rwlock_tryrdlock(rwlock_t *);
177 1.10 nathanw int __libc_rwlock_trywrlock(rwlock_t *);
178 1.10 nathanw int __libc_rwlock_unlock(rwlock_t *);
179 1.10 nathanw int __libc_rwlock_destroy(rwlock_t *);
180 1.10 nathanw __END_DECLS
181 1.10 nathanw
182 1.10 nathanw #define rwlock_init(l, a) __libc_rwlock_init((l), (a))
183 1.10 nathanw #define rwlock_rdlock(l) __libc_rwlock_rdlock((l))
184 1.10 nathanw #define rwlock_wrlock(l) __libc_rwlock_wrlock((l))
185 1.10 nathanw #define rwlock_tryrdlock(l) __libc_rwlock_tryrdlock((l))
186 1.10 nathanw #define rwlock_trywrlock(l) __libc_rwlock_trywrlock((l))
187 1.10 nathanw #define rwlock_unlock(l) __libc_rwlock_unlock((l))
188 1.10 nathanw #define rwlock_destroy(l) __libc_rwlock_destroy((l))
189 1.10 nathanw
190 1.10 nathanw __BEGIN_DECLS
191 1.10 nathanw int __libc_thr_keycreate(thread_key_t *, void (*)(void *));
192 1.10 nathanw int __libc_thr_setspecific(thread_key_t, const void *);
193 1.10 nathanw void *__libc_thr_getspecific(thread_key_t);
194 1.10 nathanw int __libc_thr_keydelete(thread_key_t);
195 1.10 nathanw __END_DECLS
196 1.10 nathanw
197 1.10 nathanw #define thr_keycreate(k, d) __libc_thr_keycreate((k), (d))
198 1.10 nathanw #define thr_setspecific(k, p) __libc_thr_setspecific((k), (p))
199 1.10 nathanw #define thr_getspecific(k) __libc_thr_getspecific((k))
200 1.10 nathanw #define thr_keydelete(k) __libc_thr_keydelete((k))
201 1.10 nathanw
202 1.10 nathanw __BEGIN_DECLS
203 1.10 nathanw int __libc_thr_once(once_t *, void (*)(void));
204 1.10 nathanw int __libc_thr_sigsetmask(int, const sigset_t *, sigset_t *);
205 1.10 nathanw thr_t __libc_thr_self(void);
206 1.10 nathanw int __libc_thr_yield(void);
207 1.10 nathanw void __libc_thr_create(thr_t *, const thrattr_t *,
208 1.10 nathanw void *(*)(void *), void *);
209 1.10 nathanw void __libc_thr_exit(void *) __attribute__((__noreturn__));
210 1.10 nathanw int *__libc_thr_errno(void);
211 1.10 nathanw int __libc_thr_setcancelstate(int, int *);
212 1.11 ad unsigned int __libc_thr_curcpu(void);
213 1.10 nathanw
214 1.10 nathanw extern int __isthreaded;
215 1.10 nathanw __END_DECLS
216 1.10 nathanw
217 1.10 nathanw #define thr_once(o, f) __libc_thr_once((o), (f))
218 1.10 nathanw #define thr_sigsetmask(f, n, o) __libc_thr_sigsetmask((f), (n), (o))
219 1.10 nathanw #define thr_self() __libc_thr_self()
220 1.10 nathanw #define thr_yield() __libc_thr_yield()
221 1.10 nathanw #define thr_create(tp, ta, f, a) __libc_thr_create((tp), (ta), (f), (a))
222 1.10 nathanw #define thr_exit(v) __libc_thr_exit((v))
223 1.10 nathanw #define thr_errno() __libc_thr_errno()
224 1.10 nathanw #define thr_enabled() (__isthreaded)
225 1.10 nathanw #define thr_setcancelstate(n, o) __libc_thr_setcancelstate((n),(o))
226 1.11 ad #define thr_curcpu() __libc_thr_curcpu()
227 1.16 christos
228 1.16 christos #else /* __LIBC_THREAD_STUBS */
229 1.16 christos
230 1.16 christos __BEGIN_DECLS
231 1.16 christos void __libc_thr_init_stub(void);
232 1.16 christos
233 1.16 christos int __libc_mutex_init_stub(mutex_t *, const mutexattr_t *);
234 1.16 christos int __libc_mutex_lock_stub(mutex_t *);
235 1.16 christos int __libc_mutex_trylock_stub(mutex_t *);
236 1.16 christos int __libc_mutex_unlock_stub(mutex_t *);
237 1.16 christos int __libc_mutex_destroy_stub(mutex_t *);
238 1.16 christos
239 1.16 christos int __libc_mutexattr_init_stub(mutexattr_t *);
240 1.16 christos int __libc_mutexattr_destroy_stub(mutexattr_t *);
241 1.16 christos int __libc_mutexattr_settype_stub(mutexattr_t *, int);
242 1.16 christos
243 1.16 christos int __libc_cond_init_stub(cond_t *, const condattr_t *);
244 1.16 christos int __libc_cond_signal_stub(cond_t *);
245 1.16 christos int __libc_cond_broadcast_stub(cond_t *);
246 1.16 christos int __libc_cond_wait_stub(cond_t *, mutex_t *);
247 1.16 christos int __libc_cond_timedwait_stub(cond_t *, mutex_t *,
248 1.16 christos const struct timespec *);
249 1.16 christos int __libc_cond_destroy_stub(cond_t *);
250 1.16 christos
251 1.16 christos int __libc_rwlock_init_stub(rwlock_t *, const rwlockattr_t *);
252 1.16 christos int __libc_rwlock_rdlock_stub(rwlock_t *);
253 1.16 christos int __libc_rwlock_wrlock_stub(rwlock_t *);
254 1.16 christos int __libc_rwlock_tryrdlock_stub(rwlock_t *);
255 1.16 christos int __libc_rwlock_trywrlock_stub(rwlock_t *);
256 1.16 christos int __libc_rwlock_unlock_stub(rwlock_t *);
257 1.16 christos int __libc_rwlock_destroy_stub(rwlock_t *);
258 1.16 christos
259 1.16 christos int __libc_thr_keycreate_stub(thread_key_t *, void (*)(void *));
260 1.16 christos int __libc_thr_setspecific_stub(thread_key_t, const void *);
261 1.16 christos void *__libc_thr_getspecific_stub(thread_key_t);
262 1.16 christos int __libc_thr_keydelete_stub(thread_key_t);
263 1.16 christos
264 1.16 christos int __libc_thr_once_stub(once_t *, void (*)(void));
265 1.16 christos int __libc_thr_sigsetmask_stub(int, const sigset_t *, sigset_t *);
266 1.16 christos thr_t __libc_thr_self_stub(void);
267 1.16 christos int __libc_thr_yield_stub(void);
268 1.16 christos int __libc_thr_create_stub(thr_t *, const thrattr_t *,
269 1.16 christos void *(*)(void *), void *);
270 1.17 joerg void __libc_thr_exit_stub(void *) __dead;
271 1.16 christos int *__libc_thr_errno_stub(void);
272 1.16 christos int __libc_thr_setcancelstate_stub(int, int *);
273 1.16 christos int __libc_thr_equal_stub(pthread_t, pthread_t);
274 1.16 christos unsigned int __libc_thr_curcpu_stub(void);
275 1.16 christos __END_DECLS
276 1.16 christos
277 1.10 nathanw #endif /* __LIBC_THREAD_STUBS */
278 1.7 thorpej
279 1.9 nathanw #define FLOCKFILE(fp) __flockfile_internal(fp, 1)
280 1.9 nathanw #define FUNLOCKFILE(fp) __funlockfile_internal(fp, 1)
281 1.7 thorpej
282 1.7 thorpej #else /* _REENTRANT */
283 1.7 thorpej
284 1.20 christos #define mutex_init(m, a) __nothing
285 1.20 christos #define mutex_lock(m) __nothing
286 1.20 christos #define mutex_trylock(m) __nothing
287 1.20 christos #define mutex_unlock(m) __nothing
288 1.20 christos #define mutex_destroy(m) __nothing
289 1.20 christos
290 1.20 christos #define cond_init(c, t, a) __nothing
291 1.20 christos #define cond_signal(c) __nothing
292 1.20 christos #define cond_broadcast(c) __nothing
293 1.20 christos #define cond_wait(c, m) __nothing
294 1.20 christos #define cond_timedwait(c, m, t) __nothing
295 1.20 christos #define cond_destroy(c) __nothing
296 1.20 christos
297 1.20 christos #define rwlock_init(l, a) __nothing
298 1.20 christos #define rwlock_rdlock(l) __nothing
299 1.20 christos #define rwlock_wrlock(l) __nothing
300 1.20 christos #define rwlock_tryrdlock(l) __nothing
301 1.20 christos #define rwlock_trywrlock(l) __nothing
302 1.20 christos #define rwlock_unlock(l) __nothing
303 1.20 christos #define rwlock_destroy(l) __nothing
304 1.18 christos
305 1.18 christos #define thr_keycreate(k, d) /*LINTED*/0
306 1.20 christos #define thr_setspecific(k, p) __nothing
307 1.18 christos #define thr_getspecific(k) /*LINTED*/0
308 1.20 christos #define thr_keydelete(k) __nothing
309 1.18 christos
310 1.20 christos #define mutexattr_init(ma) __nothing
311 1.20 christos #define mutexattr_settype(ma, t) __nothing
312 1.20 christos #define mutexattr_destroy(ma) __nothing
313 1.7 thorpej
314 1.15 joerg static inline int
315 1.15 joerg thr_once(once_t *once_control, void (*routine)(void))
316 1.15 joerg {
317 1.15 joerg if (__predict_false(once_control->pto_done == 0)) {
318 1.15 joerg (*routine)();
319 1.15 joerg once_control->pto_done = 1;
320 1.15 joerg }
321 1.15 joerg return 0;
322 1.15 joerg }
323 1.20 christos #define thr_sigsetmask(f, n, o) __nothing
324 1.20 christos #define thr_self() __nothing
325 1.20 christos #define thr_errno() __nothing
326 1.11 ad #define thr_curcpu() ((unsigned int)0)
327 1.2 jtc
328 1.20 christos #define FLOCKFILE(fp) __nothing
329 1.20 christos #define FUNLOCKFILE(fp) __nothing
330 1.1 jtc
331 1.7 thorpej #endif /* _REENTRANT */
332 1.22 riastrad
333 1.22 riastrad #endif /* _LIBC_REENTRANT_H_ */
334