3 * Copyright 1998-2003 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();
79 /* detachstate appeared in Draft 6, but without manifest constants.
80 * in Draft 7 they were called PTHREAD_CREATE_UNDETACHED and ...DETACHED.
81 * in Draft 8 on, ...UNDETACHED became ...JOINABLE.
83 #ifndef PTHREAD_CREATE_JOINABLE
84 #ifdef PTHREAD_CREATE_UNDETACHED
85 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
87 #define PTHREAD_CREATE_JOINABLE 0
91 #ifndef PTHREAD_CREATE_DETACHED
92 #define PTHREAD_CREATE_DETACHED 1
96 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
98 void *(*start_routine)( void * ),
104 /* Always create the thread attrs, so we can set stacksize if we need to */
105 #if HAVE_PTHREADS > 5
106 pthread_attr_init(&attr);
108 pthread_attr_create(&attr);
111 #if defined(LDAP_PVT_THREAD_STACK_SIZE) && LDAP_PVT_THREAD_STACK_SIZE > 0
112 /* this should be tunable */
113 pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
116 #if HAVE_PTHREADS > 5
117 detach = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
118 #if HAVE_PTHREADS == 6
119 pthread_attr_setdetachstate(&attr, &detach);
121 pthread_attr_setdetachstate(&attr, detach);
125 #if HAVE_PTHREADS < 5
126 rtn = pthread_create( thread, attr, start_routine, arg );
128 rtn = pthread_create( thread, &attr, start_routine, arg );
130 #if HAVE_PTHREADS > 5
131 pthread_attr_destroy(&attr);
133 pthread_attr_delete(&attr);
135 pthread_detach( thread );
139 #if HAVE_PTHREADS < 7
140 if ( rtn < 0 ) rtn = errno;
146 ldap_pvt_thread_exit( void *retval )
148 pthread_exit( retval );
152 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
154 #if HAVE_PTHREADS < 7
157 if (thread_return==NULL)
158 thread_return=&dummy;
160 if ( pthread_join( thread, thread_return ) < 0 ) return errno;
163 return pthread_join( thread, thread_return );
168 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
170 #if ( HAVE_PTHREAD_KILL && HAVE_PTHREADS > 6 )
171 /* MacOS 10.1 is detected as v10 but has no pthread_kill() */
172 return pthread_kill( thread, signo );
173 #elif ( HAVE_PTHREAD_KILL && HAVE_PTHREADS > 4 )
174 if ( pthread_kill( thread, signo ) < 0 ) return errno;
177 /* pthread package with DCE */
178 if (kill( getpid(), signo )<0)
185 ldap_pvt_thread_yield( void )
190 #elif HAVE_PTHREADS == 10
191 return sched_yield();
193 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
197 #elif HAVE_PTHREADS == 6
207 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
209 #if HAVE_PTHREADS < 7
210 if ( pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) < 0 )
214 return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
219 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
221 #if HAVE_PTHREADS < 7
222 if ( pthread_cond_destroy( cond ) < 0 ) return errno;
225 return pthread_cond_destroy( cond );
230 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
232 #if HAVE_PTHREADS < 7
233 if ( pthread_cond_signal( cond ) < 0 ) return errno;
236 return pthread_cond_signal( cond );
241 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
243 #if HAVE_PTHREADS < 7
244 if ( pthread_cond_broadcast( cond ) < 0 ) return errno;
247 return pthread_cond_broadcast( cond );
252 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
253 ldap_pvt_thread_mutex_t *mutex )
255 #if HAVE_PTHREADS < 7
256 if ( pthread_cond_wait( cond, mutex ) < 0 ) return errno;
259 return pthread_cond_wait( cond, mutex );
264 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
266 #if HAVE_PTHREADS < 7
267 if ( pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT )<0)
271 return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
276 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
278 #if HAVE_PTHREADS < 7
279 if ( pthread_mutex_destroy( mutex ) < 0 ) return errno;
282 return pthread_mutex_destroy( mutex );
287 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
289 #if HAVE_PTHREADS < 7
290 if ( pthread_mutex_lock( mutex ) < 0 ) return errno;
293 return pthread_mutex_lock( mutex );
298 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
300 #if HAVE_PTHREADS < 7
301 if ( pthread_mutex_trylock( mutex ) < 0 ) return errno;
304 return pthread_mutex_trylock( mutex );
309 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
311 #if HAVE_PTHREADS < 7
312 if ( pthread_mutex_unlock( mutex ) < 0 ) return errno;
315 return pthread_mutex_unlock( mutex );
319 ldap_pvt_thread_t ldap_pvt_thread_self( void )
321 return pthread_self();
324 #ifdef LDAP_THREAD_HAVE_RDWR
325 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
327 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
329 #if HAVE_PTHREADS < 7
330 if ( pthread_rwlock_init( rw, NULL ) < 0 ) return errno;
333 return pthread_rwlock_init( rw, NULL );
338 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
340 #if HAVE_PTHREADS < 7
341 if ( pthread_rwlock_destroy( rw ) < 0 ) return errno;
344 return pthread_rwlock_destroy( rw );
348 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
350 #if HAVE_PTHREADS < 7
351 if ( pthread_rwlock_rdlock( rw ) < 0 ) return errno;
354 return pthread_rwlock_rdlock( rw );
358 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
360 #if HAVE_PTHREADS < 7
361 if ( pthread_rwlock_tryrdlock( rw ) < 0 ) return errno;
364 return pthread_rwlock_tryrdlock( rw );
368 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
370 #if HAVE_PTHREADS < 7
371 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
374 return pthread_rwlock_unlock( rw );
378 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
380 #if HAVE_PTHREADS < 7
381 if ( pthread_rwlock_wrlock( rw ) < 0 ) return errno;
384 return pthread_rwlock_wrlock( rw );
388 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
390 #if HAVE_PTHREADS < 7
391 if ( pthread_rwlock_trywrlock( rw ) < 0 ) return errno;
394 return pthread_rwlock_trywrlock( rw );
398 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
400 #if HAVE_PTHREADS < 7
401 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
404 return pthread_rwlock_unlock( rw );
408 #endif /* HAVE_PTHREAD_RDLOCK_DESTROY */
409 #endif /* LDAP_THREAD_HAVE_RDWR */
410 #endif /* HAVE_PTHREADS */