]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_posix.c
Remove broken MSVC build from REL_ENG branch.
[openldap] / libraries / libldap_r / thr_posix.c
1 /* thr_posix.c - wrapper around posix and posixish thread implementations.  */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 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 #include "portable.h"
18
19 #if defined( HAVE_PTHREADS )
20
21 #include <ac/errno.h>
22
23 #include "ldap_pvt_thread.h"
24
25
26 #if HAVE_PTHREADS < 6
27 #  define LDAP_INT_THREAD_ATTR_DEFAULT          pthread_attr_default
28 #  define LDAP_INT_THREAD_CONDATTR_DEFAULT      pthread_condattr_default
29 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT     pthread_mutexattr_default
30 #else
31 #  define LDAP_INT_THREAD_ATTR_DEFAULT          NULL
32 #  define LDAP_INT_THREAD_CONDATTR_DEFAULT      NULL
33 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT     NULL
34 #endif
35
36
37 int
38 ldap_int_thread_initialize( void )
39 {
40         return 0;
41 }
42
43 int
44 ldap_int_thread_destroy( void )
45 {
46 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
47         /* LinuxThreads: kill clones */
48         pthread_kill_other_threads_np();
49 #endif
50         return 0;
51 }
52
53 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
54 int
55 ldap_pvt_thread_set_concurrency(int n)
56 {
57 #ifdef HAVE_PTHREAD_SETCONCURRENCY
58         return pthread_setconcurrency( n );
59 #elif HAVE_THR_SETCONCURRENCY
60         return thr_setconcurrency( n );
61 #else
62         return 0;
63 #endif
64 }
65 #endif
66
67 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
68 int
69 ldap_pvt_thread_get_concurrency(void)
70 {
71 #ifdef HAVE_PTHREAD_GETCONCURRENCY
72         return pthread_getconcurrency();
73 #elif HAVE_THR_GETCONCURRENCY
74         return thr_getconcurrency();
75 #else
76         return 0;
77 #endif
78 }
79 #endif
80
81 /* detachstate appeared in Draft 6, but without manifest constants.
82  * in Draft 7 they were called PTHREAD_CREATE_UNDETACHED and ...DETACHED.
83  * in Draft 8 on, ...UNDETACHED became ...JOINABLE.
84  */
85 #ifndef PTHREAD_CREATE_JOINABLE
86 #ifdef PTHREAD_CREATE_UNDETACHED
87 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
88 #else
89 #define PTHREAD_CREATE_JOINABLE 0
90 #endif
91 #endif
92
93 #ifndef PTHREAD_CREATE_DETACHED
94 #define PTHREAD_CREATE_DETACHED 1
95 #endif
96
97 int 
98 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
99         int detach,
100         void *(*start_routine)( void * ),
101         void *arg)
102 {
103         int rtn;
104         pthread_attr_t attr;
105
106 /* Always create the thread attrs, so we can set stacksize if we need to */
107 #if HAVE_PTHREADS > 5
108         pthread_attr_init(&attr);
109 #else
110         pthread_attr_create(&attr);
111 #endif
112
113 #if defined(LDAP_PVT_THREAD_STACK_SIZE) && LDAP_PVT_THREAD_STACK_SIZE > 0
114         /* this should be tunable */
115         pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
116 #endif
117
118 #if HAVE_PTHREADS > 5
119         detach = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
120 #if HAVE_PTHREADS == 6
121         pthread_attr_setdetachstate(&attr, &detach);
122 #else
123         pthread_attr_setdetachstate(&attr, detach);
124 #endif
125 #endif
126
127 #if HAVE_PTHREADS < 5
128         rtn = pthread_create( thread, attr, start_routine, arg );
129 #else
130         rtn = pthread_create( thread, &attr, start_routine, arg );
131 #endif
132 #if HAVE_PTHREADS > 5
133         pthread_attr_destroy(&attr);
134 #else
135         pthread_attr_delete(&attr);
136         if( detach ) {
137                 pthread_detach( thread );
138         }
139 #endif
140
141 #if HAVE_PTHREADS < 7
142         if ( rtn < 0 ) rtn = errno;
143 #endif
144         return rtn;
145 }
146
147 void 
148 ldap_pvt_thread_exit( void *retval )
149 {
150         pthread_exit( retval );
151 }
152
153 int 
154 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
155 {
156 #if HAVE_PTHREADS < 7
157         void *dummy;
158
159         if (thread_return==NULL)
160           thread_return=&dummy;
161
162         if ( pthread_join( thread, thread_return ) < 0 ) return errno;
163         return 0;
164 #else
165         return pthread_join( thread, thread_return );
166 #endif
167 }
168
169 int 
170 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
171 {
172 #if ( HAVE_PTHREAD_KILL && HAVE_PTHREADS > 6 )
173         /* MacOS 10.1 is detected as v10 but has no pthread_kill() */
174         return pthread_kill( thread, signo );
175 #elif ( HAVE_PTHREAD_KILL && HAVE_PTHREADS > 4 )
176         if ( pthread_kill( thread, signo ) < 0 ) return errno;
177         return 0;
178 #else
179         /* pthread package with DCE */
180         if (kill( getpid(), signo )<0)
181                 return errno;
182         return 0;
183 #endif
184 }
185
186 int 
187 ldap_pvt_thread_yield( void )
188 {
189 #if HAVE_THR_YIELD
190         return thr_yield();
191
192 #elif HAVE_PTHREADS == 10
193         return sched_yield();
194
195 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
196         sched_yield();
197         return 0;
198
199 #elif HAVE_PTHREADS == 6
200         pthread_yield(NULL);
201         return 0;
202 #else
203         pthread_yield();
204         return 0;
205 #endif
206 }
207
208 int 
209 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
210 {
211 #if HAVE_PTHREADS < 7
212         if ( pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) < 0 )
213                 return errno;
214         return 0;
215 #else
216         return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
217 #endif
218 }
219
220 int 
221 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
222 {
223 #if HAVE_PTHREADS < 7
224         if ( pthread_cond_destroy( cond ) < 0 ) return errno;
225         return 0;
226 #else
227         return pthread_cond_destroy( cond );
228 #endif
229 }
230         
231 int 
232 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
233 {
234 #if HAVE_PTHREADS < 7
235         if ( pthread_cond_signal( cond ) < 0 ) return errno;
236         return 0;
237 #else
238         return pthread_cond_signal( cond );
239 #endif
240 }
241
242 int
243 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
244 {
245 #if HAVE_PTHREADS < 7
246         if ( pthread_cond_broadcast( cond ) < 0 ) return errno;
247         return 0;
248 #else
249         return pthread_cond_broadcast( cond );
250 #endif
251 }
252
253 int 
254 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
255                       ldap_pvt_thread_mutex_t *mutex )
256 {
257 #if HAVE_PTHREADS < 7
258         if ( pthread_cond_wait( cond, mutex ) < 0 ) return errno;
259         return 0;
260 #else
261         return pthread_cond_wait( cond, mutex );
262 #endif
263 }
264
265 int 
266 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
267 {
268 #if HAVE_PTHREADS < 7
269         if ( pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT )<0)
270                 return errno;
271         return 0;
272 #else
273         return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
274 #endif
275 }
276
277 int 
278 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
279 {
280 #if HAVE_PTHREADS < 7
281         if ( pthread_mutex_destroy( mutex ) < 0 ) return errno;
282         return 0;
283 #else
284         return pthread_mutex_destroy( mutex );
285 #endif
286 }
287
288 int 
289 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
290 {
291 #if HAVE_PTHREADS < 7
292         if ( pthread_mutex_lock( mutex ) < 0 ) return errno;
293         return 0;
294 #else
295         return pthread_mutex_lock( mutex );
296 #endif
297 }
298
299 int 
300 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
301 {
302 #if HAVE_PTHREADS < 7
303         if ( pthread_mutex_trylock( mutex ) < 0 ) return errno;
304         return 0;
305 #else
306         return pthread_mutex_trylock( mutex );
307 #endif
308 }
309
310 int 
311 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
312 {
313 #if HAVE_PTHREADS < 7
314         if ( pthread_mutex_unlock( mutex ) < 0 ) return errno;
315         return 0;
316 #else
317         return pthread_mutex_unlock( mutex );
318 #endif
319 }
320
321 ldap_pvt_thread_t ldap_pvt_thread_self( void )
322 {
323         return pthread_self();
324 }
325
326 #ifdef LDAP_THREAD_HAVE_RDWR
327 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
328 int 
329 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
330 {
331 #if HAVE_PTHREADS < 7
332         if ( pthread_rwlock_init( rw, NULL ) < 0 ) return errno;
333         return 0;
334 #else
335         return pthread_rwlock_init( rw, NULL );
336 #endif
337 }
338
339 int 
340 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
341 {
342 #if HAVE_PTHREADS < 7
343         if ( pthread_rwlock_destroy( rw ) < 0 ) return errno;
344         return 0;
345 #else
346         return pthread_rwlock_destroy( rw );
347 #endif
348 }
349
350 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
351 {
352 #if HAVE_PTHREADS < 7
353         if ( pthread_rwlock_rdlock( rw ) < 0 ) return errno;
354         return 0;
355 #else
356         return pthread_rwlock_rdlock( rw );
357 #endif
358 }
359
360 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
361 {
362 #if HAVE_PTHREADS < 7
363         if ( pthread_rwlock_tryrdlock( rw ) < 0 ) return errno;
364         return 0;
365 #else
366         return pthread_rwlock_tryrdlock( rw );
367 #endif
368 }
369
370 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
371 {
372 #if HAVE_PTHREADS < 7
373         if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
374         return 0;
375 #else
376         return pthread_rwlock_unlock( rw );
377 #endif
378 }
379
380 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
381 {
382 #if HAVE_PTHREADS < 7
383         if ( pthread_rwlock_wrlock( rw ) < 0 ) return errno;
384         return 0;
385 #else
386         return pthread_rwlock_wrlock( rw );
387 #endif
388 }
389
390 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
391 {
392 #if HAVE_PTHREADS < 7
393         if ( pthread_rwlock_trywrlock( rw ) < 0 ) return errno;
394         return 0;
395 #else
396         return pthread_rwlock_trywrlock( rw );
397 #endif
398 }
399
400 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
401 {
402 #if HAVE_PTHREADS < 7
403         if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
404         return 0;
405 #else
406         return pthread_rwlock_unlock( rw );
407 #endif
408 }
409
410 #endif /* HAVE_PTHREAD_RDLOCK_DESTROY */
411 #endif /* LDAP_THREAD_HAVE_RDWR */
412 #endif /* HAVE_PTHREADS */
413