]> git.sur5r.net Git - openldap/blob - include/ldap_int_thread.h
ITS#6669
[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-2010 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_NT_THREADS)
149 /*************************************
150  *                                   *
151  * thread definitions for NT threads *
152  *                                   *
153  *************************************/
154
155 #include <process.h>
156 #include <windows.h>
157
158 LDAP_BEGIN_DECL
159
160 typedef unsigned long   ldap_int_thread_t;
161 typedef HANDLE  ldap_int_thread_mutex_t;
162 typedef HANDLE  ldap_int_thread_cond_t;
163 typedef DWORD   ldap_int_thread_key_t;
164
165 LDAP_END_DECL
166
167 #else
168 /***********************************
169  *                                 *
170  * thread definitions for no       *
171  * underlying library support      *
172  *                                 *
173  ***********************************/
174
175 #ifndef NO_THREADS
176 #define NO_THREADS 1
177 #endif
178
179 LDAP_BEGIN_DECL
180
181 typedef int                     ldap_int_thread_t;
182 typedef int                     ldap_int_thread_mutex_t;
183 typedef int                     ldap_int_thread_cond_t;
184 typedef int                     ldap_int_thread_key_t;
185
186 #define LDAP_THREAD_HAVE_TPOOL 1
187 typedef int                     ldap_int_thread_pool_t;
188
189 LDAP_END_DECL
190
191 #endif /* no threads support */
192
193
194 LDAP_BEGIN_DECL
195
196 #ifndef ldap_int_thread_equal
197 #define ldap_int_thread_equal(a, b)     ((a) == (b))
198 #endif
199
200 #ifndef LDAP_THREAD_HAVE_RDWR
201 typedef struct ldap_int_thread_rdwr_s * ldap_int_thread_rdwr_t;
202 #endif
203
204 LDAP_F(int) ldap_int_thread_pool_startup ( void );
205 LDAP_F(int) ldap_int_thread_pool_shutdown ( void );
206
207 #ifndef LDAP_THREAD_HAVE_TPOOL
208 typedef struct ldap_int_thread_pool_s * ldap_int_thread_pool_t;
209 #endif
210
211 typedef struct ldap_int_thread_rmutex_s * ldap_int_thread_rmutex_t;
212 LDAP_END_DECL
213
214
215 #if defined(LDAP_THREAD_DEBUG) && !((LDAP_THREAD_DEBUG +0) & 2U)
216 #define LDAP_THREAD_DEBUG_WRAP 1
217 #endif
218
219 #ifdef LDAP_THREAD_DEBUG_WRAP
220 /**************************************
221  *                                    *
222  * definitions for type-wrapped debug *
223  *                                    *
224  **************************************/
225
226 LDAP_BEGIN_DECL
227
228 #ifndef LDAP_UINTPTR_T  /* May be configured in CPPFLAGS */
229 #define LDAP_UINTPTR_T  unsigned long
230 #endif
231
232 typedef enum {
233         ldap_debug_magic =      -(int) (((unsigned)-1)/19)
234 } ldap_debug_magic_t;
235
236 typedef enum {
237         /* Could fill in "locked" etc here later */
238         ldap_debug_state_inited = (int) (((unsigned)-1)/11),
239         ldap_debug_state_destroyed
240 } ldap_debug_state_t;
241
242 typedef struct {
243         /* Enclosed in magic numbers in the hope of catching overwrites */
244         ldap_debug_magic_t      magic;  /* bit pattern to recognize usages  */
245         LDAP_UINTPTR_T          self;   /* ~(LDAP_UINTPTR_T)&(this struct) */
246         union ldap_debug_mem_u {        /* Dummy memory reference */
247                 unsigned char   *ptr;
248                 LDAP_UINTPTR_T  num;
249         } mem;
250         ldap_debug_state_t      state;  /* doubles as another magic number */
251 } ldap_debug_usage_info_t;
252
253 typedef struct {
254         ldap_int_thread_mutex_t wrapped;
255         ldap_debug_usage_info_t usage;
256         ldap_int_thread_t       owner;
257 } ldap_debug_thread_mutex_t;
258
259 typedef struct {
260         ldap_int_thread_cond_t  wrapped;
261         ldap_debug_usage_info_t usage;
262 } ldap_debug_thread_cond_t;
263
264 typedef struct {
265         ldap_int_thread_rdwr_t  wrapped;
266         ldap_debug_usage_info_t usage;
267 } ldap_debug_thread_rdwr_t;
268
269 #ifndef NDEBUG
270 #define LDAP_INT_THREAD_ASSERT_MUTEX_OWNER(mutex) \
271         ldap_debug_thread_assert_mutex_owner( \
272                 __FILE__, __LINE__, "owns(" #mutex ")", mutex )
273 LDAP_F(void) ldap_debug_thread_assert_mutex_owner LDAP_P((
274         LDAP_CONST char *file,
275         int line,
276         LDAP_CONST char *msg,
277         ldap_debug_thread_mutex_t *mutex ));
278 #endif /* NDEBUG */
279
280 LDAP_END_DECL
281
282 #endif /* LDAP_THREAD_DEBUG_WRAP */
283
284 #endif /* _LDAP_INT_THREAD_H */