xref: /netbsd-src/external/bsd/openldap/dist/include/ldap_int_thread.h (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1 /*	$NetBSD: ldap_int_thread.h,v 1.1.1.7 2019/08/08 13:31:03 christos Exp $	*/
2 
3 /* ldap_int_thread.h - ldap internal thread wrappers header file */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2019 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 
19 
20 LDAP_BEGIN_DECL
21 
22 /* Can be done twice in libldap_r.  See libldap_r/ldap_thr_debug.h. */
23 LDAP_F(int) ldap_int_thread_initialize LDAP_P(( void ));
24 LDAP_F(int) ldap_int_thread_destroy    LDAP_P(( void ));
25 
26 LDAP_END_DECL
27 
28 
29 #ifndef _LDAP_INT_THREAD_H
30 #define _LDAP_INT_THREAD_H
31 
32 #if defined( HAVE_PTHREADS )
33 /**********************************
34  *                                *
35  * definitions for POSIX Threads  *
36  *                                *
37  **********************************/
38 
39 #include <pthread.h>
40 #ifdef HAVE_SCHED_H
41 #include <sched.h>
42 #endif
43 
44 LDAP_BEGIN_DECL
45 
46 typedef pthread_t		ldap_int_thread_t;
47 typedef pthread_mutex_t		ldap_int_thread_mutex_t;
48 typedef pthread_cond_t		ldap_int_thread_cond_t;
49 typedef pthread_key_t		ldap_int_thread_key_t;
50 
51 #define ldap_int_thread_equal(a, b)	pthread_equal((a), (b))
52 
53 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
54 	defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
55 	defined( _POSIX_THREADSAFE_FUNCTIONS )
56 #define HAVE_REENTRANT_FUNCTIONS 1
57 #endif
58 
59 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
60 	defined( HAVE_THR_GETCONCURRENCY )
61 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
62 #endif
63 
64 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
65 	defined( HAVE_THR_SETCONCURRENCY )
66 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
67 #endif
68 
69 #if defined( HAVE_PTHREAD_RWLOCK_DESTROY )
70 #define LDAP_THREAD_HAVE_RDWR 1
71 typedef pthread_rwlock_t ldap_int_thread_rdwr_t;
72 #endif
73 
74 #ifndef LDAP_INT_MUTEX_NULL
75 #define LDAP_INT_MUTEX_NULL	PTHREAD_MUTEX_INITIALIZER
76 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
77 #endif
78 
79 LDAP_END_DECL
80 
81 #elif defined ( HAVE_MACH_CTHREADS )
82 /**********************************
83  *                                *
84  * definitions for Mach CThreads  *
85  *                                *
86  **********************************/
87 
88 #if defined( HAVE_MACH_CTHREADS_H )
89 #	include <mach/cthreads.h>
90 #elif defined( HAVE_CTHREADS_H )
91 #	include <cthreads.h>
92 #endif
93 
94 LDAP_BEGIN_DECL
95 
96 typedef cthread_t		ldap_int_thread_t;
97 typedef struct mutex		ldap_int_thread_mutex_t;
98 typedef struct condition	ldap_int_thread_cond_t;
99 typedef cthread_key_t		ldap_int_thread_key_t;
100 
101 #ifndef LDAP_INT_MUTEX_NULL
102 #define LDAP_INT_MUTEX_NULL	MUTEX_INITIALIZER
103 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
104 #endif
105 
106 LDAP_END_DECL
107 
108 #elif defined( HAVE_GNU_PTH )
109 /***********************************
110  *                                 *
111  * thread definitions for GNU Pth  *
112  *                                 *
113  ***********************************/
114 
115 #define PTH_SYSCALL_SOFT 1
116 #include <pth.h>
117 
118 LDAP_BEGIN_DECL
119 
120 typedef pth_t		ldap_int_thread_t;
121 typedef pth_mutex_t	ldap_int_thread_mutex_t;
122 typedef pth_cond_t	ldap_int_thread_cond_t;
123 typedef pth_key_t	ldap_int_thread_key_t;
124 
125 #if 0
126 #define LDAP_THREAD_HAVE_RDWR 1
127 typedef pth_rwlock_t ldap_int_thread_rdwr_t;
128 #endif
129 
130 #ifndef LDAP_INT_MUTEX_NULL
131 #define LDAP_INT_MUTEX_NULL	PTH_MUTEX_INIT
132 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
133 #endif
134 
135 LDAP_END_DECL
136 
137 #elif defined( HAVE_THR )
138 /********************************************
139  *                                          *
140  * thread definitions for Solaris LWP (THR) *
141  *                                          *
142  ********************************************/
143 
144 #include <thread.h>
145 #include <synch.h>
146 
147 LDAP_BEGIN_DECL
148 
149 typedef thread_t		ldap_int_thread_t;
150 typedef mutex_t			ldap_int_thread_mutex_t;
151 typedef cond_t			ldap_int_thread_cond_t;
152 typedef thread_key_t	ldap_int_thread_key_t;
153 
154 #define HAVE_REENTRANT_FUNCTIONS 1
155 
156 #ifdef HAVE_THR_GETCONCURRENCY
157 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
158 #endif
159 #ifdef HAVE_THR_SETCONCURRENCY
160 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
161 #endif
162 
163 #ifndef LDAP_INT_MUTEX_NULL
164 #define LDAP_INT_MUTEX_NULL	DEFAULTMUTEX
165 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
166 #endif
167 
168 #elif defined(HAVE_NT_THREADS)
169 /*************************************
170  *                                   *
171  * thread definitions for NT threads *
172  *                                   *
173  *************************************/
174 
175 #include <process.h>
176 #include <windows.h>
177 
178 LDAP_BEGIN_DECL
179 
180 typedef unsigned long	ldap_int_thread_t;
181 typedef HANDLE	ldap_int_thread_mutex_t;
182 typedef HANDLE	ldap_int_thread_cond_t;
183 typedef DWORD	ldap_int_thread_key_t;
184 
185 #ifndef LDAP_INT_MUTEX_NULL
186 #define LDAP_INT_MUTEX_NULL		((HANDLE)0)
187 #define LDAP_INT_MUTEX_FIRSTCREATE(m) \
188 		((void) ((m) || ldap_pvt_thread_mutex_init(&(m))))
189 #endif
190 
191 LDAP_END_DECL
192 
193 #else
194 /***********************************
195  *                                 *
196  * thread definitions for no       *
197  * underlying library support      *
198  *                                 *
199  ***********************************/
200 
201 #ifndef NO_THREADS
202 #define NO_THREADS 1
203 #endif
204 
205 LDAP_BEGIN_DECL
206 
207 typedef int			ldap_int_thread_t;
208 typedef int			ldap_int_thread_mutex_t;
209 typedef int			ldap_int_thread_cond_t;
210 typedef int			ldap_int_thread_key_t;
211 
212 #define LDAP_THREAD_HAVE_TPOOL 1
213 typedef int			ldap_int_thread_pool_t;
214 
215 #ifndef LDAP_INT_MUTEX_NULL
216 #define LDAP_INT_MUTEX_NULL				0
217 #define LDAP_INT_MUTEX_FIRSTCREATE(m)	((void) 0)
218 #endif
219 
220 LDAP_END_DECL
221 
222 #endif /* no threads support */
223 
224 
225 LDAP_BEGIN_DECL
226 
227 #ifndef ldap_int_thread_equal
228 #define ldap_int_thread_equal(a, b)	((a) == (b))
229 #endif
230 
231 #ifndef LDAP_THREAD_HAVE_RDWR
232 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
233 #endif
234 
235 LDAP_F(int) ldap_int_thread_pool_startup ( void );
236 LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
237 
238 #ifndef LDAP_THREAD_HAVE_TPOOL
239 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
240 #endif
241 
242 typedef struct ldap_int_thread_rmutex_s * ldap_int_thread_rmutex_t;
243 LDAP_END_DECL
244 
245 
246 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
247 #define LDAP_THREAD_DEBUG_WRAP 1
248 #endif
249 
250 #ifdef LDAP_THREAD_DEBUG_WRAP
251 /**************************************
252  *                                    *
253  * definitions for type-wrapped debug *
254  *                                    *
255  **************************************/
256 
257 LDAP_BEGIN_DECL
258 
259 #ifndef LDAP_UINTPTR_T	/* May be configured in CPPFLAGS */
260 #define LDAP_UINTPTR_T	unsigned long
261 #endif
262 
263 typedef enum {
264 	ldap_debug_magic =	-(int) (((unsigned)-1)/19)
265 } ldap_debug_magic_t;
266 
267 typedef enum {
268 	/* Could fill in "locked" etc here later */
269 	ldap_debug_state_inited = (int) (((unsigned)-1)/11),
270 	ldap_debug_state_destroyed
271 } ldap_debug_state_t;
272 
273 typedef struct {
274 	/* Enclosed in magic numbers in the hope of catching overwrites */
275 	ldap_debug_magic_t	magic;	/* bit pattern to recognize usages  */
276 	LDAP_UINTPTR_T		self;	/* ~(LDAP_UINTPTR_T)&(this struct) */
277 	union ldap_debug_mem_u {	/* Dummy memory reference */
278 		unsigned char	*ptr;
279 		LDAP_UINTPTR_T	num;
280 	} mem;
281 	ldap_debug_state_t	state;	/* doubles as another magic number */
282 } ldap_debug_usage_info_t;
283 
284 typedef struct {
285 	ldap_int_thread_mutex_t	wrapped;
286 	ldap_debug_usage_info_t	usage;
287 	ldap_int_thread_t	owner;
288 } ldap_debug_thread_mutex_t;
289 
290 #define	LDAP_DEBUG_MUTEX_NULL	{LDAP_INT_MUTEX_NULL, {0,0,{0},0} /*,owner*/}
291 #define	LDAP_DEBUG_MUTEX_FIRSTCREATE(m) \
292 	((void) ((m).usage.state || ldap_pvt_thread_mutex_init(&(m))))
293 
294 typedef struct {
295 	ldap_int_thread_cond_t	wrapped;
296 	ldap_debug_usage_info_t	usage;
297 } ldap_debug_thread_cond_t;
298 
299 typedef struct {
300 	ldap_int_thread_rdwr_t	wrapped;
301 	ldap_debug_usage_info_t	usage;
302 } ldap_debug_thread_rdwr_t;
303 
304 #ifndef NDEBUG
305 #define	LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \
306 	ldap_debug_thread_assert_mutex_owner( \
307 		__FILE__, __LINE__, "owns(" #mutex ")", mutex )
308 LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P((
309 	LDAP_CONST char *file,
310 	int line,
311 	LDAP_CONST char *msg,
312 	ldap_debug_thread_mutex_t *mutex ));
313 #endif /* NDEBUG */
314 
315 LDAP_END_DECL
316 
317 #endif /* LDAP_THREAD_DEBUG_WRAP */
318 
319 #endif /* _LDAP_INT_THREAD_H */
320