]> git.sur5r.net Git - openldap/blob - include/ldap_int_thread.h
Do not require ac/string.h for lber_pvt.h
[openldap] / include / ldap_int_thread.h
1 /* ldap_int_thread.h - ldap internal thread wrappers header file */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  * 
5  * Copyright 1998-2018 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17
18 LDAP_BEGIN_DECL
19
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 ));
23
24 LDAP_END_DECL
25
26 LDAP_BEGIN_DECL
27 typedef struct ldap_int_thread_rmutex_s * ldap_int_thread_rmutex_t;
28 LDAP_END_DECL
29
30 #ifndef _LDAP_INT_THREAD_H
31 #define _LDAP_INT_THREAD_H
32
33 #if defined( HAVE_PTHREADS )
34 /**********************************
35  *                                *
36  * definitions for POSIX Threads  *
37  *                                *
38  **********************************/
39
40 #include <pthread.h>
41 #ifdef HAVE_SCHED_H
42 #include <sched.h>
43 #endif
44
45 LDAP_BEGIN_DECL
46
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;
53
54 #define ldap_int_thread_equal(a, b)     pthread_equal((a), (b))
55
56 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
57         defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
58         defined( _POSIX_THREADSAFE_FUNCTIONS )
59 #define HAVE_REENTRANT_FUNCTIONS 1
60 #endif
61
62 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
63         defined( HAVE_THR_GETCONCURRENCY )
64 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
65 #endif
66
67 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
68         defined( HAVE_THR_SETCONCURRENCY )
69 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
70 #endif
71
72 #if defined( HAVE_PTHREAD_RWLOCK_DESTROY )
73 #define LDAP_THREAD_HAVE_RDWR 1
74 typedef pthread_rwlock_t ldap_int_thread_rdwr_t;
75 #endif
76
77 #ifndef LDAP_INT_MUTEX_NULL
78 #define LDAP_INT_MUTEX_NULL     PTHREAD_MUTEX_INITIALIZER
79 #define LDAP_INT_MUTEX_FIRSTCREATE(m)   ((void) 0)
80 #endif
81
82 LDAP_END_DECL
83
84 #elif defined ( HAVE_MACH_CTHREADS )
85 /**********************************
86  *                                *
87  * definitions for Mach CThreads  *
88  *                                *
89  **********************************/
90
91 #if defined( HAVE_MACH_CTHREADS_H )
92 #       include <mach/cthreads.h>
93 #elif defined( HAVE_CTHREADS_H )
94 #       include <cthreads.h>
95 #endif
96
97 LDAP_BEGIN_DECL
98
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;
104
105 #ifndef LDAP_INT_MUTEX_NULL
106 #define LDAP_INT_MUTEX_NULL     MUTEX_INITIALIZER
107 #define LDAP_INT_MUTEX_FIRSTCREATE(m)   ((void) 0)
108 #endif
109
110 LDAP_END_DECL
111
112 #elif defined( HAVE_GNU_PTH )
113 /***********************************
114  *                                 *
115  * thread definitions for GNU Pth  *
116  *                                 *
117  ***********************************/
118
119 #define PTH_SYSCALL_SOFT 1
120 #include <pth.h>
121
122 LDAP_BEGIN_DECL
123
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;
130
131 #if 0
132 #define LDAP_THREAD_HAVE_RDWR 1
133 typedef pth_rwlock_t ldap_int_thread_rdwr_t;
134 #endif
135
136 #ifndef LDAP_INT_MUTEX_NULL
137 #define LDAP_INT_MUTEX_NULL     PTH_MUTEX_INIT
138 #define LDAP_INT_MUTEX_FIRSTCREATE(m)   ((void) 0)
139 #endif
140
141 LDAP_END_DECL
142
143 #elif defined( HAVE_THR )
144 /********************************************
145  *                                          *
146  * thread definitions for Solaris LWP (THR) *
147  *                                          *
148  ********************************************/
149
150 #include <thread.h>
151 #include <synch.h>
152
153 LDAP_BEGIN_DECL
154
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;
161
162 #define HAVE_REENTRANT_FUNCTIONS 1
163
164 #ifdef HAVE_THR_GETCONCURRENCY
165 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
166 #endif
167 #ifdef HAVE_THR_SETCONCURRENCY
168 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
169 #endif
170
171 #ifndef LDAP_INT_MUTEX_NULL
172 #define LDAP_INT_MUTEX_NULL     DEFAULTMUTEX
173 #define LDAP_INT_MUTEX_FIRSTCREATE(m)   ((void) 0)
174 #endif
175
176 #elif defined(HAVE_NT_THREADS)
177 /*************************************
178  *                                   *
179  * thread definitions for NT threads *
180  *                                   *
181  *************************************/
182
183 #include <process.h>
184 #include <windows.h>
185
186 LDAP_BEGIN_DECL
187
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;
194
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))))
199 #endif
200
201 LDAP_END_DECL
202
203 #else
204 /***********************************
205  *                                 *
206  * thread definitions for no       *
207  * underlying library support      *
208  *                                 *
209  ***********************************/
210
211 #ifndef NO_THREADS
212 #define NO_THREADS 1
213 #endif
214
215 LDAP_BEGIN_DECL
216
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;
223
224 #define LDAP_THREAD_HAVE_TPOOL 1
225 typedef int                     ldap_int_thread_pool_t;
226
227 #ifndef LDAP_INT_MUTEX_NULL
228 #define LDAP_INT_MUTEX_NULL                             0
229 #define LDAP_INT_MUTEX_FIRSTCREATE(m)   ((void) 0)
230 #endif
231
232 LDAP_END_DECL
233
234 #endif /* no threads support */
235
236
237 LDAP_BEGIN_DECL
238
239 #ifndef ldap_int_thread_equal
240 #define ldap_int_thread_equal(a, b)     ((a) == (b))
241 #endif
242
243 #ifndef LDAP_THREAD_HAVE_RDWR
244 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
245 #endif
246
247 LDAP_F(int) ldap_int_thread_pool_startup ( void );
248 LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
249
250 #ifndef LDAP_THREAD_HAVE_TPOOL
251 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
252 #endif
253 LDAP_END_DECL
254
255
256 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
257 #define LDAP_THREAD_DEBUG_WRAP 1
258 #endif
259
260 #ifdef LDAP_THREAD_DEBUG_WRAP
261 /**************************************
262  *                                    *
263  * definitions for type-wrapped debug *
264  *                                    *
265  **************************************/
266
267 LDAP_BEGIN_DECL
268
269 #ifndef LDAP_UINTPTR_T  /* May be configured in CPPFLAGS */
270 #define LDAP_UINTPTR_T  unsigned long
271 #endif
272
273 typedef enum {
274         ldap_debug_magic =      -(int) (((unsigned)-1)/19)
275 } ldap_debug_magic_t;
276
277 typedef enum {
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;
282
283 typedef struct {
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 */
288                 unsigned char   *ptr;
289                 LDAP_UINTPTR_T  num;
290         } mem;
291         ldap_debug_state_t      state;  /* doubles as another magic number */
292 } ldap_debug_usage_info_t;
293
294 typedef struct {
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;
299
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))))
303
304 typedef struct {
305         ldap_int_thread_cond_t  wrapped;
306         ldap_debug_usage_info_t usage;
307 } ldap_debug_thread_cond_t;
308
309 typedef struct {
310         ldap_int_thread_rdwr_t  wrapped;
311         ldap_debug_usage_info_t usage;
312 } ldap_debug_thread_rdwr_t;
313
314 #ifndef NDEBUG
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,
320         int line,
321         LDAP_CONST char *msg,
322         ldap_debug_thread_mutex_t *mutex ));
323 #endif /* NDEBUG */
324
325 LDAP_END_DECL
326
327 #endif /* LDAP_THREAD_DEBUG_WRAP */
328
329 #endif /* _LDAP_INT_THREAD_H */