]> git.sur5r.net Git - openldap/blob - include/ldap_pvt_thread.h
415c6d5d0e3ff080b2c24795eb2b8496ae2101e3
[openldap] / include / ldap_pvt_thread.h
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11 /* ldap_pvt_thread.h - ldap threads header file NG */
12
13 #ifndef _LDAP_PVT_THREAD_H
14 #define _LDAP_PVT_THREAD_H
15
16 #include "ldap_cdefs.h"
17
18 #if defined( HAVE_PTHREADS )
19 /**********************************
20  *                                *
21  * definitions for POSIX Threads  *
22  *                                *
23  **********************************/
24
25 #include <pthread.h>
26 #ifdef HAVE_SCHED_H
27 #include <sched.h>
28 #endif
29
30 LDAP_BEGIN_DECL
31
32 typedef pthread_t               ldap_pvt_thread_t;
33 typedef pthread_mutex_t         ldap_pvt_thread_mutex_t;
34 typedef pthread_cond_t          ldap_pvt_thread_cond_t;
35
36 #if defined( _POSIX_REENTRANT_FUNCTIONS ) || \
37         defined( _POSIX_THREAD_SAFE_FUNCTIONS ) || \
38         defined( _POSIX_THREADSAFE_FUNCTIONS )
39 #define HAVE_REENTRANT_FUNCTIONS 1
40 #endif
41
42 #if defined( HAVE_PTHREAD_GETCONCURRENCY ) || \
43         defined( HAVE_THR_GETCONCURRENCY )
44 #define HAVE_GETCONCURRENCY 1
45 #endif
46
47 #if defined( HAVE_PTHREAD_SETCONCURRENCY ) || \
48         defined( HAVE_THR_SETCONCURRENCY )
49 #define HAVE_SETCONCURRENCY 1
50 #endif
51
52 LDAP_END_DECL
53
54 #elif defined ( HAVE_MACH_CTHREADS )
55 /**********************************
56  *                                *
57  * definitions for Mach CThreads  *
58  *                                *
59  **********************************/
60
61 #include <mach/cthreads.h>
62
63 LDAP_BEGIN_DECL
64
65 typedef cthread_t               ldap_pvt_thread_t;
66 typedef struct mutex            ldap_pvt_thread_mutex_t;
67 typedef struct condition        ldap_pvt_thread_cond_t;
68
69 LDAP_END_DECL
70
71 #elif defined( HAVE_GNU_PTH )
72 /***********************************
73  *                                 *
74  * thread definitions for GNU Pth  *
75  *                                 *
76  ***********************************/
77
78 #include <pth.h>
79
80 LDAP_BEGIN_DECL
81
82 typedef pth_t           ldap_pvt_thread_t;
83 typedef pth_mutex_t     ldap_pvt_thread_mutex_t;
84 typedef pth_cond_t      ldap_pvt_thread_cond_t;
85
86 LDAP_END_DECL
87
88
89 #elif defined( HAVE_THR )
90 /********************************************
91  *                                          *
92  * thread definitions for Solaris LWP (THR) *
93  *                                          *
94  ********************************************/
95
96 #include <thread.h>
97 #include <synch.h>
98
99 LDAP_BEGIN_DECL
100
101 typedef thread_t                ldap_pvt_thread_t;
102 typedef mutex_t                 ldap_pvt_thread_mutex_t;
103 typedef cond_t                  ldap_pvt_thread_cond_t;
104
105 #define HAVE_REENTRANT_FUNCTIONS 1
106
107 #ifdef HAVE_THR_GETCONCURRENCY
108 #define HAVE_GETCONCURRENCY 1
109 #endif
110 #ifdef HAVE_THR_SETCONCURRENCY
111 #define HAVE_SETCONCURRENCY 1
112 #endif
113
114 LDAP_END_DECL
115
116 #elif defined( HAVE_LWP )
117 /*************************************
118  *                                   *
119  * thread definitions for SunOS LWP  *
120  *                                   *
121  *************************************/
122
123 #include <lwp/lwp.h>
124 #include <lwp/stackdep.h>
125
126 LDAP_BEGIN_DECL
127
128 typedef thread_t                ldap_pvt_thread_t;
129 typedef mon_t                   ldap_pvt_thread_mutex_t;
130 struct ldap_pvt_thread_lwp_cv {
131         int             lcv_created;
132         cv_t            lcv_cv;
133 };
134 typedef struct ldap_pvt_thread_lwp_cv ldap_pvt_thread_cond_t;
135
136 #define HAVE_REENTRANT_FUNCTIONS 1
137
138 LDAP_END_DECL
139
140 /* If we're in the NT env at all, we want these defs, threaded or not (which should be fixed) */
141 #elif defined(WINNT) || defined(_WINNT) || defined(_WIN32) || defined(HAVE_NT_THREADS)
142
143 LDAP_BEGIN_DECL
144
145 #include <process.h>
146 #include <windows.h>
147
148 typedef unsigned long   ldap_pvt_thread_t;
149 typedef HANDLE  ldap_pvt_thread_mutex_t;
150 typedef HANDLE  ldap_pvt_thread_cond_t;
151
152 LDAP_END_DECL
153
154 #else
155
156 /***********************************
157  *                                 *
158  * thread definitions for no       *
159  * underlying library support      *
160  *                                 *
161  ***********************************/
162
163 LDAP_BEGIN_DECL
164
165 #ifndef NO_THREADS
166 #define NO_THREADS 1
167 #endif
168
169 typedef int                     ldap_pvt_thread_t;
170 typedef int                     ldap_pvt_thread_mutex_t;
171 typedef int                     ldap_pvt_thread_cond_t;
172
173 LDAP_END_DECL
174
175 #endif /* no threads support */
176
177 LDAP_BEGIN_DECL
178
179 #ifdef __MINGW32__
180 #   undef LDAP_F_PRE
181 #   ifdef LIBLDAP_DECL
182 #       define LDAP_F_PRE       extern __declspec(LIBLDAP_DECL)
183 #   else
184 #       define LDAP_F_PRE       extern
185 #   endif
186 #endif
187
188 LDAP_F( int )
189 ldap_pvt_thread_initialize LDAP_P(( void ));
190
191 LDAP_F( int )
192 ldap_pvt_thread_destroy LDAP_P(( void ));
193
194 LDAP_F( unsigned int )
195 ldap_pvt_thread_sleep LDAP_P(( unsigned int s ));
196
197 #ifdef HAVE_GETCONCURRENCY
198 LDAP_F( int )
199 ldap_pvt_thread_get_concurrency LDAP_P(( void ));
200 #endif
201
202 #ifdef HAVE_SETCONCURRENCY
203 #       ifndef LDAP_THREAD_CONCURRENCY
204         /* three concurrent threads should be enough */
205 #       define LDAP_THREAD_CONCURRENCY  3
206 #       endif
207 LDAP_F( int )
208 ldap_pvt_thread_set_concurrency LDAP_P(( int ));
209 #endif
210
211 #define LDAP_PVT_THREAD_CREATE_JOINABLE 0
212 #define LDAP_PVT_THREAD_CREATE_DETACHED 1
213
214 LDAP_F( int ) 
215 ldap_pvt_thread_create LDAP_P((
216         ldap_pvt_thread_t * thread, 
217         int     detach,
218         void *(*start_routine)( void * ), 
219         void *arg));
220
221 LDAP_F( void ) 
222 ldap_pvt_thread_exit LDAP_P(( void *retval ));
223
224 LDAP_F( int )
225 ldap_pvt_thread_join LDAP_P(( ldap_pvt_thread_t thread, void **status ));
226
227 LDAP_F( int )
228 ldap_pvt_thread_kill LDAP_P(( ldap_pvt_thread_t thread, int signo ));
229
230 LDAP_F( int )
231 ldap_pvt_thread_yield LDAP_P(( void ));
232
233 LDAP_F( int )
234 ldap_pvt_thread_cond_init LDAP_P(( ldap_pvt_thread_cond_t *cond ));
235
236 LDAP_F( int )
237 ldap_pvt_thread_cond_destroy LDAP_P(( ldap_pvt_thread_cond_t *cond ));
238
239 LDAP_F( int )
240 ldap_pvt_thread_cond_signal LDAP_P(( ldap_pvt_thread_cond_t *cond ));
241
242 LDAP_F( int )
243 ldap_pvt_thread_cond_broadcast LDAP_P(( ldap_pvt_thread_cond_t *cond ));
244
245 LDAP_F( int )
246 ldap_pvt_thread_cond_wait LDAP_P((
247         ldap_pvt_thread_cond_t *cond, 
248         ldap_pvt_thread_mutex_t *mutex ));
249
250 LDAP_F( int )
251 ldap_pvt_thread_mutex_init LDAP_P(( ldap_pvt_thread_mutex_t *mutex ));
252
253 LDAP_F( int )
254 ldap_pvt_thread_mutex_destroy LDAP_P(( ldap_pvt_thread_mutex_t *mutex ));
255
256 LDAP_F( int )
257 ldap_pvt_thread_mutex_lock LDAP_P(( ldap_pvt_thread_mutex_t *mutex ));
258
259 LDAP_F( int )
260 ldap_pvt_thread_mutex_trylock LDAP_P(( ldap_pvt_thread_mutex_t *mutex ));
261
262 LDAP_F( int )
263 ldap_pvt_thread_mutex_unlock LDAP_P(( ldap_pvt_thread_mutex_t *mutex ));
264
265 typedef struct ldap_pvt_thread_rdwr_var {
266         ldap_pvt_thread_mutex_t ltrw_mutex;     
267         ldap_pvt_thread_cond_t ltrw_read;       /* wait for read */
268         ldap_pvt_thread_cond_t ltrw_write;      /* wait for write */
269         int ltrw_valid;
270 #define LDAP_PVT_THREAD_RDWR_VALID 0x0bad
271         int ltrw_r_active;
272         int ltrw_w_active;
273         int ltrw_r_wait;
274         int ltrw_w_wait;
275 } ldap_pvt_thread_rdwr_t;
276
277 LDAP_F( int )
278 ldap_pvt_thread_rdwr_init LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
279
280 LDAP_F( int )
281 ldap_pvt_thread_rdwr_destroy LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
282
283 LDAP_F( int )
284 ldap_pvt_thread_rdwr_rlock LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
285
286 LDAP_F( int )
287 ldap_pvt_thread_rdwr_rtrylock LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
288
289 LDAP_F( int )
290 ldap_pvt_thread_rdwr_runlock LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
291
292 LDAP_F( int )
293 ldap_pvt_thread_rdwr_wlock LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
294
295 LDAP_F( int )
296 ldap_pvt_thread_rdwr_wtrylock LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
297
298 LDAP_F( int )
299 ldap_pvt_thread_rdwr_wunlock LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
300
301 #ifdef LDAP_DEBUG
302 LDAP_F( int )
303 ldap_pvt_thread_rdwr_readers LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
304
305 LDAP_F( int )
306 ldap_pvt_thread_rdwr_writers LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
307
308 LDAP_F( int )
309 ldap_pvt_thread_rdwr_active LDAP_P((ldap_pvt_thread_rdwr_t *rdwrp));
310 #endif /* LDAP_DEBUG */
311
312 #define LDAP_PVT_THREAD_EINVAL EINVAL
313 #define LDAP_PVT_THREAD_EBUSY EINVAL
314
315 LDAP_END_DECL
316
317 #endif /* _LDAP_THREAD_H */