]> git.sur5r.net Git - openldap/blob - include/ldap_int_thread.h
ITS#5810: exclude instead of include backends, explain exclusion
[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-2009 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
27 #ifndef _LDAP_INT_THREAD_H
28 #define _LDAP_INT_THREAD_H
29
30 #if defined( HAVE_PTHREADS )
31 /**********************************
32  *                                *
33  * definitions for POSIX Threads  *
34  *                                *
35  **********************************/
36
37 #include <pthread.h>
38 #ifdef HAVE_SCHED_H
39 #include <sched.h>
40 #endif
41
42 LDAP_BEGIN_DECL
43
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;
48
49 #define ldap_int_thread_equal(a, b)     pthread_equal((a), (b))
50
51 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
52         defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
53         defined( _POSIX_THREADSAFE_FUNCTIONS )
54 #define HAVE_REENTRANT_FUNCTIONS 1
55 #endif
56
57 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
58         defined( HAVE_THR_GETCONCURRENCY )
59 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
60 #endif
61
62 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
63         defined( HAVE_THR_SETCONCURRENCY )
64 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
65 #endif
66
67 #if defined( HAVE_PTHREAD_RWLOCK_DESTROY )
68 #define LDAP_THREAD_HAVE_RDWR 1
69 typedef pthread_rwlock_t ldap_int_thread_rdwr_t;
70 #endif
71
72 LDAP_END_DECL
73
74 #elif defined ( HAVE_MACH_CTHREADS )
75 /**********************************
76  *                                *
77  * definitions for Mach CThreads  *
78  *                                *
79  **********************************/
80
81 #if defined( HAVE_MACH_CTHREADS_H )
82 #       include <mach/cthreads.h>
83 #elif defined( HAVE_CTHREADS_H )
84 #       include <cthreads.h>
85 #endif
86
87 LDAP_BEGIN_DECL
88
89 typedef cthread_t               ldap_int_thread_t;
90 typedef struct mutex            ldap_int_thread_mutex_t;
91 typedef struct condition        ldap_int_thread_cond_t;
92 typedef cthread_key_t           ldap_int_thread_key_t;
93
94 LDAP_END_DECL
95
96 #elif defined( HAVE_GNU_PTH )
97 /***********************************
98  *                                 *
99  * thread definitions for GNU Pth  *
100  *                                 *
101  ***********************************/
102
103 #define PTH_SYSCALL_SOFT 1
104 #include <pth.h>
105
106 LDAP_BEGIN_DECL
107
108 typedef pth_t           ldap_int_thread_t;
109 typedef pth_mutex_t     ldap_int_thread_mutex_t;
110 typedef pth_cond_t      ldap_int_thread_cond_t;
111 typedef pth_key_t       ldap_int_thread_key_t;
112
113 #if 0
114 #define LDAP_THREAD_HAVE_RDWR 1
115 typedef pth_rwlock_t ldap_int_thread_rdwr_t;
116 #endif
117
118 LDAP_END_DECL
119
120 #elif defined( HAVE_THR )
121 /********************************************
122  *                                          *
123  * thread definitions for Solaris LWP (THR) *
124  *                                          *
125  ********************************************/
126
127 #include <thread.h>
128 #include <synch.h>
129
130 LDAP_BEGIN_DECL
131
132 typedef thread_t                ldap_int_thread_t;
133 typedef mutex_t                 ldap_int_thread_mutex_t;
134 typedef cond_t                  ldap_int_thread_cond_t;
135 typedef thread_key_t    ldap_int_thread_key_t;
136
137 #define HAVE_REENTRANT_FUNCTIONS 1
138
139 #ifdef HAVE_THR_GETCONCURRENCY
140 #define LDAP_THREAD_HAVE_GETCONCURRENCY 1
141 #endif
142 #ifdef HAVE_THR_SETCONCURRENCY
143 #define LDAP_THREAD_HAVE_SETCONCURRENCY 1
144 #endif
145
146 LDAP_END_DECL
147
148 #elif defined( HAVE_LWP )
149 /*************************************
150  *                                   *
151  * thread definitions for SunOS LWP  *
152  *                                   *
153  *************************************/
154
155 #include <lwp/lwp.h>
156 #include <lwp/stackdep.h>
157 #define LDAP_THREAD_HAVE_SLEEP 1
158
159 LDAP_BEGIN_DECL
160
161 typedef thread_t                ldap_int_thread_t;
162 typedef mon_t                   ldap_int_thread_mutex_t;
163 struct ldap_int_thread_lwp_cv {
164         int             lcv_created;
165         cv_t            lcv_cv;
166 };
167 typedef struct ldap_int_thread_lwp_cv ldap_int_thread_cond_t;
168
169 #define HAVE_REENTRANT_FUNCTIONS 1
170
171 LDAP_END_DECL
172
173 #elif defined(HAVE_NT_THREADS)
174 /*************************************
175  *                                   *
176  * thread definitions for NT threads *
177  *                                   *
178  *************************************/
179
180 #include <process.h>
181 #include <windows.h>
182
183 LDAP_BEGIN_DECL
184
185 typedef unsigned long   ldap_int_thread_t;
186 typedef HANDLE  ldap_int_thread_mutex_t;
187 typedef HANDLE  ldap_int_thread_cond_t;
188 typedef DWORD   ldap_int_thread_key_t;
189
190 LDAP_END_DECL
191
192 #else
193 /***********************************
194  *                                 *
195  * thread definitions for no       *
196  * underlying library support      *
197  *                                 *
198  ***********************************/
199
200 #ifndef NO_THREADS
201 #define NO_THREADS 1
202 #endif
203
204 LDAP_BEGIN_DECL
205
206 typedef int                     ldap_int_thread_t;
207 typedef int                     ldap_int_thread_mutex_t;
208 typedef int                     ldap_int_thread_cond_t;
209 typedef int                     ldap_int_thread_key_t;
210
211 #define LDAP_THREAD_HAVE_TPOOL 1
212 typedef int                     ldap_int_thread_pool_t;
213
214 LDAP_END_DECL
215
216 #endif /* no threads support */
217
218
219 LDAP_BEGIN_DECL
220
221 #ifndef ldap_int_thread_equal
222 #define ldap_int_thread_equal(a, b)     ((a) == (b))
223 #endif
224
225 #ifndef LDAP_THREAD_HAVE_RDWR
226 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
227 #endif
228
229 LDAP_F(int) ldap_int_thread_pool_startup ( void );
230 LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
231
232 #ifndef LDAP_THREAD_HAVE_TPOOL
233 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
234 #endif
235
236 typedef struct ldap_int_thread_rmutex_s * ldap_int_thread_rmutex_t;
237 LDAP_END_DECL
238
239
240 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
241 #define LDAP_THREAD_DEBUG_WRAP 1
242 #endif
243
244 #ifdef LDAP_THREAD_DEBUG_WRAP
245 /**************************************
246  *                                    *
247  * definitions for type-wrapped debug *
248  *                                    *
249  **************************************/
250
251 LDAP_BEGIN_DECL
252
253 #ifndef LDAP_UINTPTR_T  /* May be configured in CPPFLAGS */
254 #define LDAP_UINTPTR_T  unsigned long
255 #endif
256
257 typedef enum {
258         ldap_debug_magic =      -(int) (((unsigned)-1)/19)
259 } ldap_debug_magic_t;
260
261 typedef enum {
262         /* Could fill in "locked" etc here later */
263         ldap_debug_state_inited = (int) (((unsigned)-1)/11),
264         ldap_debug_state_destroyed
265 } ldap_debug_state_t;
266
267 typedef struct {
268         /* Enclosed in magic numbers in the hope of catching overwrites */
269         ldap_debug_magic_t      magic;  /* bit pattern to recognize usages  */
270         LDAP_UINTPTR_T          self;   /* ~(LDAP_UINTPTR_T)&(this struct) */
271         union ldap_debug_mem_u {        /* Dummy memory reference */
272                 unsigned char   *ptr;
273                 LDAP_UINTPTR_T  num;
274         } mem;
275         ldap_debug_state_t      state;  /* doubles as another magic number */
276 } ldap_debug_usage_info_t;
277
278 typedef struct {
279         ldap_int_thread_mutex_t wrapped;
280         ldap_debug_usage_info_t usage;
281         ldap_int_thread_t       owner;
282 } ldap_debug_thread_mutex_t;
283
284 typedef struct {
285         ldap_int_thread_cond_t  wrapped;
286         ldap_debug_usage_info_t usage;
287 } ldap_debug_thread_cond_t;
288
289 typedef struct {
290         ldap_int_thread_rdwr_t  wrapped;
291         ldap_debug_usage_info_t usage;
292 } ldap_debug_thread_rdwr_t;
293
294 #ifndef NDEBUG
295 #define LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \
296         ldap_debug_thread_assert_mutex_owner( \
297                 __FILE__, __LINE__, "owns(" #mutex ")", mutex )
298 LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P((
299         LDAP_CONST char *file,
300         int line,
301         LDAP_CONST char *msg,
302         ldap_debug_thread_mutex_t *mutex ));
303 #endif /* NDEBUG */
304
305 LDAP_END_DECL
306
307 #endif /* LDAP_THREAD_DEBUG_WRAP */
308
309 #endif /* _LDAP_INT_THREAD_H */