3 * Copyright 1998-2002 The OpenLDAP Foundation, Redwood City, California, USA
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.
12 /* thr_posix.c - wrapper around posix and posixish thread implementations.
17 #if defined( HAVE_PTHREADS )
21 #include "ldap_pvt_thread.h"
24 #if HAVE_PTHREADS == 4
25 # define LDAP_INT_THREAD_ATTR_DEFAULT pthread_attr_default
26 # define LDAP_INT_THREAD_CONDATTR_DEFAULT pthread_condattr_default
27 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT pthread_mutexattr_default
29 # define LDAP_INT_THREAD_ATTR_DEFAULT NULL
30 # define LDAP_INT_THREAD_CONDATTR_DEFAULT NULL
31 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT NULL
36 ldap_int_thread_initialize( void )
42 ldap_int_thread_destroy( void )
44 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
45 /* LinuxThreads: kill clones */
46 pthread_kill_other_threads_np();
51 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
53 ldap_pvt_thread_set_concurrency(int n)
55 #ifdef HAVE_PTHREAD_SETCONCURRENCY
56 return pthread_setconcurrency( n );
57 #elif HAVE_THR_SETCONCURRENCY
58 return thr_setconcurrency( n );
65 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
67 ldap_pvt_thread_get_concurrency(void)
69 #ifdef HAVE_PTHREAD_GETCONCURRENCY
70 return pthread_getconcurrency();
71 #elif HAVE_THR_GETCONCURRENCY
72 return thr_getconcurrency();
79 /* These are first defined in Draft 7 */
80 #ifndef PTHREAD_CREATE_JOINABLE
81 #define PTHREAD_CREATE_JOINABLE 0
84 #ifndef PTHREAD_CREATE_DETACHED
85 #define PTHREAD_CREATE_DETACHED 1
89 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
91 void *(*start_routine)( void * ),
97 pthread_attr_init(&attr);
99 pthread_attr_create(&attr);
102 #if defined(LDAP_PVT_THREAD_STACK_SIZE) && LDAP_PVT_THREAD_STACK_SIZE > 0
103 /* this should be tunable */
104 pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
107 #if HAVE_PTHREADS > 4
108 detach = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
109 #if HAVE_PTHREADS == 6
110 pthread_attr_setdetachstate(&attr, &detach);
112 pthread_attr_setdetachstate(&attr, detach);
115 rtn = pthread_create( thread, &attr, start_routine, arg );
116 #if HAVE_PTHREADS > 4
117 pthread_attr_destroy(&attr);
119 pthread_attr_delete(&attr);
121 pthread_detach( thread );
125 #if HAVE_PTHREADS < 7
126 if ( rtn < 0 ) rtn = errno;
132 ldap_pvt_thread_exit( void *retval )
134 pthread_exit( retval );
138 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
140 #if HAVE_PTHREADS < 7
143 if (thread_return==NULL)
144 thread_return=&dummy;
146 if ( pthread_join( thread, thread_return ) < 0 ) return errno;
149 return pthread_join( thread, thread_return );
154 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
156 #if HAVE_PTHREADS > 6
157 return pthread_kill( thread, signo );
158 #elif HAVE_PTHREADS == 6
159 if ( pthread_kill( thread, signo ) < 0 ) return errno;
162 /* pthread package with DCE */
163 if (kill( getpid(), signo )<0)
170 ldap_pvt_thread_yield( void )
172 #if HAVE_PTHREADS == 10
173 return sched_yield();
175 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
182 #elif HAVE_PTHREADS == 6
192 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
194 #if HAVE_PTHREADS < 7
195 if ( pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) < 0 )
199 return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
204 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
206 #if HAVE_PTHREADS < 7
207 if ( pthread_cond_destroy( cond ) < 0 ) return errno;
210 return pthread_cond_destroy( cond );
215 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
217 #if HAVE_PTHREADS < 7
218 if ( pthread_cond_signal( cond ) < 0 ) return errno;
221 return pthread_cond_signal( cond );
226 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
228 #if HAVE_PTHREADS < 7
229 if ( pthread_cond_broadcast( cond ) < 0 ) return errno;
232 return pthread_cond_broadcast( cond );
237 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
238 ldap_pvt_thread_mutex_t *mutex )
240 #if HAVE_PTHREADS < 7
241 if ( pthread_cond_wait( cond, mutex ) < 0 ) return errno;
244 return pthread_cond_wait( cond, mutex );
249 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
251 #if HAVE_PTHREADS < 7
252 if ( pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT )<0)
256 return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
261 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
263 #if HAVE_PTHREADS < 7
264 if ( pthread_mutex_destroy( mutex ) < 0 ) return errno;
267 return pthread_mutex_destroy( mutex );
272 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
274 #if HAVE_PTHREADS < 7
275 if ( pthread_mutex_lock( mutex ) < 0 ) return errno;
278 return pthread_mutex_lock( mutex );
283 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
285 #if HAVE_PTHREADS < 7
286 if ( pthread_mutex_trylock( mutex ) < 0 ) return errno;
289 return pthread_mutex_trylock( mutex );
294 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
296 #if HAVE_PTHREADS < 7
297 if ( pthread_mutex_unlock( mutex ) < 0 ) return errno;
300 return pthread_mutex_unlock( mutex );
304 #ifdef LDAP_THREAD_HAVE_RDWR
305 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
307 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
309 #if HAVE_PTHREADS < 7
310 if ( pthread_rwlock_init( rw, NULL ) < 0 ) return errno;
313 return pthread_rwlock_init( rw, NULL );
318 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
320 #if HAVE_PTHREADS < 7
321 if ( pthread_rwlock_destroy( rw ) < 0 ) return errno;
324 return pthread_rwlock_destroy( rw );
328 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
330 #if HAVE_PTHREADS < 7
331 if ( pthread_rwlock_rdlock( rw ) < 0 ) return errno;
334 return pthread_rwlock_rdlock( rw );
338 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
340 #if HAVE_PTHREADS < 7
341 if ( pthread_rwlock_tryrdlock( rw ) < 0 ) return errno;
344 return pthread_rwlock_tryrdlock( rw );
348 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
350 #if HAVE_PTHREADS < 7
351 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
354 return pthread_rwlock_unlock( rw );
358 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
360 #if HAVE_PTHREADS < 7
361 if ( pthread_rwlock_wrlock( rw ) < 0 ) return errno;
364 return pthread_rwlock_wrlock( rw );
368 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
370 #if HAVE_PTHREADS < 7
371 if ( pthread_rwlock_trywrlock( rw ) < 0 ) return errno;
374 return pthread_rwlock_trywrlock( rw );
378 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
380 #if HAVE_PTHREADS < 7
381 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
384 return pthread_rwlock_unlock( rw );
388 #endif /* HAVE_PTHREAD_RDLOCK_DESTROY */
389 #endif /* LDAP_THREAD_HAVE_RDWR */
390 #endif /* HAVE_PTHREADS */