3 * Copyright 1998-2000 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"
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();
80 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
82 void *(*start_routine)( void * ),
86 #if defined(HAVE_PTHREADS_FINAL) && defined(PTHREAD_CREATE_UNDETACHED)
89 pthread_attr_init(&attr);
91 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
93 rtn = pthread_create( thread, &attr, start_routine, arg );
95 rtn = pthread_create( thread, LDAP_INT_THREAD_ATTR_DEFAULT,
100 #ifdef HAVE_PTHREADS_FINAL
101 pthread_detach( *thread );
103 pthread_detach( thread );
110 ldap_pvt_thread_exit( void *retval )
112 pthread_exit( retval );
116 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
118 #if !defined( HAVE_PTHREADS_FINAL )
120 if (thread_return==NULL)
121 thread_return=&dummy;
123 return pthread_join( thread, thread_return );
127 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
129 #ifdef HAVE_PTHREAD_KILL
130 return pthread_kill( thread, signo );
132 /* pthread package with DCE */
133 if (kill( getpid(), signo )<0)
140 ldap_pvt_thread_yield( void )
142 #ifdef _POSIX_THREAD_IS_GNU_PTH
146 #elif HAVE_SCHED_YIELD
147 return sched_yield();
149 #elif HAVE_PTHREAD_YIELD
162 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
164 return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
168 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
170 return pthread_cond_destroy( cond );
174 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
176 return pthread_cond_signal( cond );
180 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
182 return pthread_cond_broadcast( cond );
186 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
187 ldap_pvt_thread_mutex_t *mutex )
189 return pthread_cond_wait( cond, mutex );
193 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
195 return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
199 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
201 return pthread_mutex_destroy( mutex );
205 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
207 return pthread_mutex_lock( mutex );
211 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
213 return pthread_mutex_trylock( mutex );
217 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
219 return pthread_mutex_unlock( mutex );
222 #ifdef LDAP_THREAD_HAVE_RDWR
223 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
225 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
227 return pthread_rwlock_init( rw, NULL );
231 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
233 return pthread_rwlock_destroy( rw );
236 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
238 return pthread_rwlock_rdlock( rw );
241 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
243 return pthread_rwlock_tryrdlock( rw );
246 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
248 return pthread_rwlock_unlock( rw );
251 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
253 return pthread_rwlock_wrlock( rw );
256 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
258 return pthread_rwlock_trywrlock( rw );
261 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
263 return pthread_rwlock_unlock( rw );
266 #endif /* HAVE_PTHREAD_RDLOCK_DESTROY */
267 #endif /* LDAP_THREAD_HAVE_RDWR */
268 #endif /* HAVE_PTHREADS */