1 /* thr_posix.c - wrapper around posix and posixish thread implementations. */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
19 #if defined( HAVE_PTHREADS )
23 #include "ldap_pvt_thread.h"
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
31 # define LDAP_INT_THREAD_ATTR_DEFAULT NULL
32 # define LDAP_INT_THREAD_CONDATTR_DEFAULT NULL
33 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT NULL
38 ldap_int_thread_initialize( void )
44 ldap_int_thread_destroy( void )
46 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
47 /* LinuxThreads: kill clones */
48 pthread_kill_other_threads_np();
53 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
55 ldap_pvt_thread_set_concurrency(int n)
57 #ifdef HAVE_PTHREAD_SETCONCURRENCY
58 return pthread_setconcurrency( n );
59 #elif HAVE_THR_SETCONCURRENCY
60 return thr_setconcurrency( n );
67 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
69 ldap_pvt_thread_get_concurrency(void)
71 #ifdef HAVE_PTHREAD_GETCONCURRENCY
72 return pthread_getconcurrency();
73 #elif HAVE_THR_GETCONCURRENCY
74 return thr_getconcurrency();
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.
85 #ifndef PTHREAD_CREATE_JOINABLE
86 #ifdef PTHREAD_CREATE_UNDETACHED
87 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
89 #define PTHREAD_CREATE_JOINABLE 0
93 #ifndef PTHREAD_CREATE_DETACHED
94 #define PTHREAD_CREATE_DETACHED 1
98 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
100 void *(*start_routine)( void * ),
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);
110 pthread_attr_create(&attr);
113 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
114 /* this should be tunable */
115 pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
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);
123 pthread_attr_setdetachstate(&attr, detach);
127 #if HAVE_PTHREADS < 5
128 rtn = pthread_create( thread, attr, start_routine, arg );
130 rtn = pthread_create( thread, &attr, start_routine, arg );
132 #if HAVE_PTHREADS > 5
133 pthread_attr_destroy(&attr);
135 pthread_attr_delete(&attr);
137 pthread_detach( thread );
141 #if HAVE_PTHREADS < 7
142 if ( rtn < 0 ) rtn = errno;
148 ldap_pvt_thread_exit( void *retval )
150 pthread_exit( retval );
154 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
156 #if HAVE_PTHREADS < 7
159 if (thread_return==NULL)
160 thread_return=&dummy;
162 if ( pthread_join( thread, thread_return ) < 0 ) return errno;
165 return pthread_join( thread, thread_return );
170 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
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;
179 /* pthread package with DCE */
180 if (kill( getpid(), signo )<0)
187 ldap_pvt_thread_yield( void )
192 #elif HAVE_PTHREADS == 10
193 return sched_yield();
195 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
199 #elif HAVE_PTHREADS == 6
209 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
211 #if HAVE_PTHREADS < 7
212 if ( pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) < 0 )
216 return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
221 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
223 #if HAVE_PTHREADS < 7
224 if ( pthread_cond_destroy( cond ) < 0 ) return errno;
227 return pthread_cond_destroy( cond );
232 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
234 #if HAVE_PTHREADS < 7
235 if ( pthread_cond_signal( cond ) < 0 ) return errno;
238 return pthread_cond_signal( cond );
243 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
245 #if HAVE_PTHREADS < 7
246 if ( pthread_cond_broadcast( cond ) < 0 ) return errno;
249 return pthread_cond_broadcast( cond );
254 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
255 ldap_pvt_thread_mutex_t *mutex )
257 #if HAVE_PTHREADS < 7
258 if ( pthread_cond_wait( cond, mutex ) < 0 ) return errno;
261 return pthread_cond_wait( cond, mutex );
266 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
268 #if HAVE_PTHREADS < 7
269 if ( pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT )<0)
273 return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
278 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
280 #if HAVE_PTHREADS < 7
281 if ( pthread_mutex_destroy( mutex ) < 0 ) return errno;
284 return pthread_mutex_destroy( mutex );
289 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
291 #if HAVE_PTHREADS < 7
292 if ( pthread_mutex_lock( mutex ) < 0 ) return errno;
295 return pthread_mutex_lock( mutex );
300 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
302 #if HAVE_PTHREADS < 7
303 if ( pthread_mutex_trylock( mutex ) < 0 ) return errno;
306 return pthread_mutex_trylock( mutex );
311 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
313 #if HAVE_PTHREADS < 7
314 if ( pthread_mutex_unlock( mutex ) < 0 ) return errno;
317 return pthread_mutex_unlock( mutex );
321 ldap_pvt_thread_t ldap_pvt_thread_self( void )
323 return pthread_self();
326 #ifdef LDAP_THREAD_HAVE_RDWR
327 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
329 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
331 #if HAVE_PTHREADS < 7
332 if ( pthread_rwlock_init( rw, NULL ) < 0 ) return errno;
335 return pthread_rwlock_init( rw, NULL );
340 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
342 #if HAVE_PTHREADS < 7
343 if ( pthread_rwlock_destroy( rw ) < 0 ) return errno;
346 return pthread_rwlock_destroy( rw );
350 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
352 #if HAVE_PTHREADS < 7
353 if ( pthread_rwlock_rdlock( rw ) < 0 ) return errno;
356 return pthread_rwlock_rdlock( rw );
360 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
362 #if HAVE_PTHREADS < 7
363 if ( pthread_rwlock_tryrdlock( rw ) < 0 ) return errno;
366 return pthread_rwlock_tryrdlock( rw );
370 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
372 #if HAVE_PTHREADS < 7
373 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
376 return pthread_rwlock_unlock( rw );
380 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
382 #if HAVE_PTHREADS < 7
383 if ( pthread_rwlock_wrlock( rw ) < 0 ) return errno;
386 return pthread_rwlock_wrlock( rw );
390 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
392 #if HAVE_PTHREADS < 7
393 if ( pthread_rwlock_trywrlock( rw ) < 0 ) return errno;
396 return pthread_rwlock_trywrlock( rw );
400 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
402 #if HAVE_PTHREADS < 7
403 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
406 return pthread_rwlock_unlock( rw );
410 #endif /* HAVE_PTHREAD_RDLOCK_DESTROY */
411 #endif /* LDAP_THREAD_HAVE_RDWR */
412 #endif /* HAVE_PTHREADS */