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