1 /* ldap_int_thread.h - ldap internal thread wrappers header file */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2017 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 /* Can be done twice in libldap_r. See libldap_r/ldap_thr_debug.h. */
21 LDAP_F(int) ldap_int_thread_initialize LDAP_P(( void ));
22 LDAP_F(int) ldap_int_thread_destroy LDAP_P(( void ));
27 typedef struct ldap_int_thread_rmutex_s * ldap_int_thread_rmutex_t;
30 #ifndef _LDAP_INT_THREAD_H
31 #define _LDAP_INT_THREAD_H
33 #if defined( HAVE_PTHREADS )
34 /**********************************
36 * definitions for POSIX Threads *
38 **********************************/
47 #define LDAP_THREAD_HAVE_NATIVE_RECURSIVE_MUTEX
48 typedef pthread_t ldap_int_thread_t;
49 typedef pthread_mutex_t ldap_int_thread_mutex_recursive_t;
50 typedef pthread_mutex_t ldap_int_thread_mutex_t;
51 typedef pthread_cond_t ldap_int_thread_cond_t;
52 typedef pthread_key_t ldap_int_thread_key_t;
54 #define ldap_int_thread_equal(a, b) pthread_equal((a), (b))
56 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
57 defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
58 defined( _POSIX_THREADSAFE_FUNCTIONS )
59 #define HAVE_REENTRANT_FUNCTIONS 1
62 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
63 defined( HAVE_THR_GETCONCURRENCY )
64 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
67 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
68 defined( HAVE_THR_SETCONCURRENCY )
69 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
72 #if defined( HAVE_PTHREAD_RWLOCK_DESTROY )
73 #define LDAP_THREAD_HAVE_RDWR 1
74 typedef pthread_rwlock_t ldap_int_thread_rdwr_t;
77 #ifndef LDAP_INT_MUTEX_NULL
78 #define LDAP_INT_MUTEX_NULL PTHREAD_MUTEX_INITIALIZER
79 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
84 #elif defined ( HAVE_MACH_CTHREADS )
85 /**********************************
87 * definitions for Mach CThreads *
89 **********************************/
91 #if defined( HAVE_MACH_CTHREADS_H )
92 # include <mach/cthreads.h>
93 #elif defined( HAVE_CTHREADS_H )
94 # include <cthreads.h>
99 typedef cthread_t ldap_int_thread_t;
100 typedef ldap_int_thread_rmutex_t ldap_int_thread_mutex_recursive_t;
101 typedef struct mutex ldap_int_thread_mutex_t;
102 typedef struct condition ldap_int_thread_cond_t;
103 typedef cthread_key_t ldap_int_thread_key_t;
105 #ifndef LDAP_INT_MUTEX_NULL
106 #define LDAP_INT_MUTEX_NULL MUTEX_INITIALIZER
107 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
112 #elif defined( HAVE_GNU_PTH )
113 /***********************************
115 * thread definitions for GNU Pth *
117 ***********************************/
119 #define PTH_SYSCALL_SOFT 1
124 #define LDAP_THREAD_HAVE_NATIVE_RECURSIVE_MUTEX
125 typedef pth_t ldap_int_thread_t;
126 typedef pth_mutex_t ldap_int_thread_mutex_recursive_t;
127 typedef pth_mutex_t ldap_int_thread_mutex_t;
128 typedef pth_cond_t ldap_int_thread_cond_t;
129 typedef pth_key_t ldap_int_thread_key_t;
132 #define LDAP_THREAD_HAVE_RDWR 1
133 typedef pth_rwlock_t ldap_int_thread_rdwr_t;
136 #ifndef LDAP_INT_MUTEX_NULL
137 #define LDAP_INT_MUTEX_NULL PTH_MUTEX_INIT
138 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
143 #elif defined( HAVE_THR )
144 /********************************************
146 * thread definitions for Solaris LWP (THR) *
148 ********************************************/
155 #define LDAP_THREAD_HAVE_NATIVE_RECURSIVE_MUTEX
156 typedef thread_t ldap_int_thread_t;
157 typedef mutex_t ldap_int_thread_mutex_recursive_t;
158 typedef mutex_t ldap_int_thread_mutex_t;
159 typedef cond_t ldap_int_thread_cond_t;
160 typedef thread_key_t ldap_int_thread_key_t;
162 #define HAVE_REENTRANT_FUNCTIONS 1
164 #ifdef HAVE_THR_GETCONCURRENCY
165 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
167 #ifdef HAVE_THR_SETCONCURRENCY
168 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
171 #ifndef LDAP_INT_MUTEX_NULL
172 #define LDAP_INT_MUTEX_NULL DEFAULTMUTEX
173 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
176 #elif defined(HAVE_NT_THREADS)
177 /*************************************
179 * thread definitions for NT threads *
181 *************************************/
188 #define LDAP_THREAD_HAVE_NATIVE_RECURSIVE_MUTEX
189 typedef unsigned long ldap_int_thread_t;
190 typedef HANDLE ldap_int_thread_mutex_recursive_t;
191 typedef HANDLE ldap_int_thread_mutex_t;
192 typedef HANDLE ldap_int_thread_cond_t;
193 typedef DWORD ldap_int_thread_key_t;
195 #ifndef LDAP_INT_MUTEX_NULL
196 #define LDAP_INT_MUTEX_NULL ((HANDLE)0)
197 #define LDAP_INT_MUTEX_FIRSTCREATE(m) \
198 ((void) ((m) || ldap_pvt_thread_mutex_init(&(m))))
204 /***********************************
206 * thread definitions for no *
207 * underlying library support *
209 ***********************************/
217 #define LDAP_THREAD_HAVE_NATIVE_RECURSIVE_MUTEX
218 typedef int ldap_int_thread_t;
219 typedef int ldap_int_thread_mutex_recursive_t;
220 typedef int ldap_int_thread_mutex_t;
221 typedef int ldap_int_thread_cond_t;
222 typedef int ldap_int_thread_key_t;
224 #define LDAP_THREAD_HAVE_TPOOL 1
225 typedef int ldap_int_thread_pool_t;
227 #ifndef LDAP_INT_MUTEX_NULL
228 #define LDAP_INT_MUTEX_NULL 0
229 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
234 #endif /* no threads support */
239 #ifndef ldap_int_thread_equal
240 #define ldap_int_thread_equal(a, b) ((a) == (b))
243 #ifndef LDAP_THREAD_HAVE_RDWR
244 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
247 LDAP_F(int) ldap_int_thread_pool_startup ( void );
248 LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
250 #ifndef LDAP_THREAD_HAVE_TPOOL
251 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
256 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
257 #define LDAP_THREAD_DEBUG_WRAP 1
260 #ifdef LDAP_THREAD_DEBUG_WRAP
261 /**************************************
263 * definitions for type-wrapped debug *
265 **************************************/
269 #ifndef LDAP_UINTPTR_T /* May be configured in CPPFLAGS */
270 #define LDAP_UINTPTR_T unsigned long
274 ldap_debug_magic = -(int) (((unsigned)-1)/19)
275 } ldap_debug_magic_t;
278 /* Could fill in "locked" etc here later */
279 ldap_debug_state_inited = (int) (((unsigned)-1)/11),
280 ldap_debug_state_destroyed
281 } ldap_debug_state_t;
284 /* Enclosed in magic numbers in the hope of catching overwrites */
285 ldap_debug_magic_t magic; /* bit pattern to recognize usages */
286 LDAP_UINTPTR_T self; /* ~(LDAP_UINTPTR_T)&(this struct) */
287 union ldap_debug_mem_u { /* Dummy memory reference */
291 ldap_debug_state_t state; /* doubles as another magic number */
292 } ldap_debug_usage_info_t;
295 ldap_int_thread_mutex_t wrapped;
296 ldap_debug_usage_info_t usage;
297 ldap_int_thread_t owner;
298 } ldap_debug_thread_mutex_t;
300 #define LDAP_DEBUG_MUTEX_NULL {LDAP_INT_MUTEX_NULL, {0,0,{0},0} /*,owner*/}
301 #define LDAP_DEBUG_MUTEX_FIRSTCREATE(m) \
302 ((void) ((m).usage.state || ldap_pvt_thread_mutex_init(&(m))))
305 ldap_int_thread_cond_t wrapped;
306 ldap_debug_usage_info_t usage;
307 } ldap_debug_thread_cond_t;
310 ldap_int_thread_rdwr_t wrapped;
311 ldap_debug_usage_info_t usage;
312 } ldap_debug_thread_rdwr_t;
315 #define LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \
316 ldap_debug_thread_assert_mutex_owner( \
317 __FILE__, __LINE__, "owns(" #mutex ")", mutex )
318 LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P((
319 LDAP_CONST char *file,
321 LDAP_CONST char *msg,
322 ldap_debug_thread_mutex_t *mutex ));
327 #endif /* LDAP_THREAD_DEBUG_WRAP */
329 #endif /* _LDAP_INT_THREAD_H */