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 #ifndef _LDAP_INT_THREAD_H
28 #define _LDAP_INT_THREAD_H
30 #if defined( HAVE_PTHREADS )
31 /**********************************
33 * definitions for POSIX Threads *
35 **********************************/
44 typedef pthread_t ldap_int_thread_t;
45 typedef pthread_mutex_t ldap_int_thread_mutex_t;
46 typedef pthread_cond_t ldap_int_thread_cond_t;
47 typedef pthread_key_t ldap_int_thread_key_t;
49 #define ldap_int_thread_equal(a, b) pthread_equal((a), (b))
51 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
52 defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
53 defined( _POSIX_THREADSAFE_FUNCTIONS )
54 #define HAVE_REENTRANT_FUNCTIONS 1
57 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
58 defined( HAVE_THR_GETCONCURRENCY )
59 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
62 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
63 defined( HAVE_THR_SETCONCURRENCY )
64 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
67 #if defined( HAVE_PTHREAD_RWLOCK_DESTROY )
68 #define LDAP_THREAD_HAVE_RDWR 1
69 typedef pthread_rwlock_t ldap_int_thread_rdwr_t;
72 #ifndef LDAP_INT_MUTEX_NULL
73 #define LDAP_INT_MUTEX_NULL PTHREAD_MUTEX_INITIALIZER
74 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
79 #elif defined ( HAVE_MACH_CTHREADS )
80 /**********************************
82 * definitions for Mach CThreads *
84 **********************************/
86 #if defined( HAVE_MACH_CTHREADS_H )
87 # include <mach/cthreads.h>
88 #elif defined( HAVE_CTHREADS_H )
89 # include <cthreads.h>
94 typedef cthread_t ldap_int_thread_t;
95 typedef struct mutex ldap_int_thread_mutex_t;
96 typedef struct condition ldap_int_thread_cond_t;
97 typedef cthread_key_t ldap_int_thread_key_t;
99 #ifndef LDAP_INT_MUTEX_NULL
100 #define LDAP_INT_MUTEX_NULL MUTEX_INITIALIZER
101 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
106 #elif defined( HAVE_GNU_PTH )
107 /***********************************
109 * thread definitions for GNU Pth *
111 ***********************************/
113 #define PTH_SYSCALL_SOFT 1
118 typedef pth_t ldap_int_thread_t;
119 typedef pth_mutex_t ldap_int_thread_mutex_t;
120 typedef pth_cond_t ldap_int_thread_cond_t;
121 typedef pth_key_t ldap_int_thread_key_t;
124 #define LDAP_THREAD_HAVE_RDWR 1
125 typedef pth_rwlock_t ldap_int_thread_rdwr_t;
128 #ifndef LDAP_INT_MUTEX_NULL
129 #define LDAP_INT_MUTEX_NULL PTH_MUTEX_INIT
130 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
135 #elif defined( HAVE_THR )
136 /********************************************
138 * thread definitions for Solaris LWP (THR) *
140 ********************************************/
147 typedef thread_t ldap_int_thread_t;
148 typedef mutex_t ldap_int_thread_mutex_t;
149 typedef cond_t ldap_int_thread_cond_t;
150 typedef thread_key_t ldap_int_thread_key_t;
152 #define HAVE_REENTRANT_FUNCTIONS 1
154 #ifdef HAVE_THR_GETCONCURRENCY
155 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
157 #ifdef HAVE_THR_SETCONCURRENCY
158 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
161 #ifndef LDAP_INT_MUTEX_NULL
162 #define LDAP_INT_MUTEX_NULL DEFAULTMUTEX
163 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
166 #elif defined(HAVE_NT_THREADS)
167 /*************************************
169 * thread definitions for NT threads *
171 *************************************/
178 typedef unsigned long ldap_int_thread_t;
179 typedef HANDLE ldap_int_thread_mutex_t;
180 typedef HANDLE ldap_int_thread_cond_t;
181 typedef DWORD ldap_int_thread_key_t;
183 #ifndef LDAP_INT_MUTEX_NULL
184 #define LDAP_INT_MUTEX_NULL ((HANDLE)0)
185 #define LDAP_INT_MUTEX_FIRSTCREATE(m) \
186 ((void) ((m) || ldap_pvt_thread_mutex_init(&(m))))
192 /***********************************
194 * thread definitions for no *
195 * underlying library support *
197 ***********************************/
205 typedef int ldap_int_thread_t;
206 typedef int ldap_int_thread_mutex_t;
207 typedef int ldap_int_thread_cond_t;
208 typedef int ldap_int_thread_key_t;
210 #define LDAP_THREAD_HAVE_TPOOL 1
211 typedef int ldap_int_thread_pool_t;
213 #ifndef LDAP_INT_MUTEX_NULL
214 #define LDAP_INT_MUTEX_NULL 0
215 #define LDAP_INT_MUTEX_FIRSTCREATE(m) ((void) 0)
220 #endif /* no threads support */
225 #ifndef ldap_int_thread_equal
226 #define ldap_int_thread_equal(a, b) ((a) == (b))
229 #ifndef LDAP_THREAD_HAVE_RDWR
230 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
233 LDAP_F(int) ldap_int_thread_pool_startup ( void );
234 LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
236 #ifndef LDAP_THREAD_HAVE_TPOOL
237 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
240 typedef struct ldap_int_thread_rmutex_s * ldap_int_thread_rmutex_t;
244 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
245 #define LDAP_THREAD_DEBUG_WRAP 1
248 #ifdef LDAP_THREAD_DEBUG_WRAP
249 /**************************************
251 * definitions for type-wrapped debug *
253 **************************************/
257 #ifndef LDAP_UINTPTR_T /* May be configured in CPPFLAGS */
258 #define LDAP_UINTPTR_T unsigned long
262 ldap_debug_magic = -(int) (((unsigned)-1)/19)
263 } ldap_debug_magic_t;
266 /* Could fill in "locked" etc here later */
267 ldap_debug_state_inited = (int) (((unsigned)-1)/11),
268 ldap_debug_state_destroyed
269 } ldap_debug_state_t;
272 /* Enclosed in magic numbers in the hope of catching overwrites */
273 ldap_debug_magic_t magic; /* bit pattern to recognize usages */
274 LDAP_UINTPTR_T self; /* ~(LDAP_UINTPTR_T)&(this struct) */
275 union ldap_debug_mem_u { /* Dummy memory reference */
279 ldap_debug_state_t state; /* doubles as another magic number */
280 } ldap_debug_usage_info_t;
283 ldap_int_thread_mutex_t wrapped;
284 ldap_debug_usage_info_t usage;
285 ldap_int_thread_t owner;
286 } ldap_debug_thread_mutex_t;
288 #define LDAP_DEBUG_MUTEX_NULL {LDAP_INT_MUTEX_NULL, {0,0,{0},0} /*,owner*/}
289 #define LDAP_DEBUG_MUTEX_FIRSTCREATE(m) \
290 ((void) ((m).usage.state || ldap_pvt_thread_mutex_init(&(m))))
293 ldap_int_thread_cond_t wrapped;
294 ldap_debug_usage_info_t usage;
295 } ldap_debug_thread_cond_t;
298 ldap_int_thread_rdwr_t wrapped;
299 ldap_debug_usage_info_t usage;
300 } ldap_debug_thread_rdwr_t;
303 #define LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \
304 ldap_debug_thread_assert_mutex_owner( \
305 __FILE__, __LINE__, "owns(" #mutex ")", mutex )
306 LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P((
307 LDAP_CONST char *file,
309 LDAP_CONST char *msg,
310 ldap_debug_thread_mutex_t *mutex ));
315 #endif /* LDAP_THREAD_DEBUG_WRAP */
317 #endif /* _LDAP_INT_THREAD_H */