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-2006 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 #if REPLACE_SCHED_YIELD
24 #ifndef HAVE_NANOSLEEP
25 #include <ac/socket.h>
30 #include "ldap_pvt_thread.h" /* Get the thread interface */
31 #define LDAP_THREAD_IMPLEMENTATION
32 #define LDAP_THREAD_RDWR_IMPLEMENTATION
33 #include "ldap_thr_debug.h" /* May rename the symbols defined below */
36 # define LDAP_INT_THREAD_ATTR_DEFAULT pthread_attr_default
37 # define LDAP_INT_THREAD_CONDATTR_DEFAULT pthread_condattr_default
38 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT pthread_mutexattr_default
40 # define LDAP_INT_THREAD_ATTR_DEFAULT NULL
41 # define LDAP_INT_THREAD_CONDATTR_DEFAULT NULL
42 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT NULL
45 #ifdef LDAP_THREAD_DEBUG
46 # if defined LDAP_INT_THREAD_MUTEXATTR /* May be defined in CPPFLAGS */
47 # elif defined HAVE_PTHREAD_KILL_OTHER_THREADS_NP
48 /* LinuxThreads hack */
49 # define LDAP_INT_THREAD_MUTEXATTR PTHREAD_MUTEX_ERRORCHECK_NP
51 # define LDAP_INT_THREAD_MUTEXATTR PTHREAD_MUTEX_ERRORCHECK
53 static pthread_mutexattr_t mutex_attr;
54 # undef LDAP_INT_THREAD_MUTEXATTR_DEFAULT
55 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT &mutex_attr
59 ldap_int_thread_initialize( void )
61 #ifdef LDAP_INT_THREAD_MUTEXATTR
62 pthread_mutexattr_init( &mutex_attr );
63 pthread_mutexattr_settype( &mutex_attr, LDAP_INT_THREAD_MUTEXATTR );
69 ldap_int_thread_destroy( void )
71 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
72 /* LinuxThreads: kill clones */
73 pthread_kill_other_threads_np();
75 #ifdef LDAP_INT_THREAD_MUTEXATTR
76 pthread_mutexattr_destroy( &mutex_attr );
81 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
83 ldap_pvt_thread_set_concurrency(int n)
85 #ifdef HAVE_PTHREAD_SETCONCURRENCY
86 return pthread_setconcurrency( n );
87 #elif HAVE_THR_SETCONCURRENCY
88 return thr_setconcurrency( n );
95 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
97 ldap_pvt_thread_get_concurrency(void)
99 #ifdef HAVE_PTHREAD_GETCONCURRENCY
100 return pthread_getconcurrency();
101 #elif HAVE_THR_GETCONCURRENCY
102 return thr_getconcurrency();
109 /* detachstate appeared in Draft 6, but without manifest constants.
110 * in Draft 7 they were called PTHREAD_CREATE_UNDETACHED and ...DETACHED.
111 * in Draft 8 on, ...UNDETACHED became ...JOINABLE.
113 #ifndef PTHREAD_CREATE_JOINABLE
114 #ifdef PTHREAD_CREATE_UNDETACHED
115 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
117 #define PTHREAD_CREATE_JOINABLE 0
121 #ifndef PTHREAD_CREATE_DETACHED
122 #define PTHREAD_CREATE_DETACHED 1
126 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
128 void *(*start_routine)( void * ),
134 /* Always create the thread attrs, so we can set stacksize if we need to */
135 #if HAVE_PTHREADS > 5
136 pthread_attr_init(&attr);
138 pthread_attr_create(&attr);
141 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
142 /* this should be tunable */
143 pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
146 #if HAVE_PTHREADS > 5
147 detach = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
148 #if HAVE_PTHREADS == 6
149 pthread_attr_setdetachstate(&attr, &detach);
151 pthread_attr_setdetachstate(&attr, detach);
155 #if HAVE_PTHREADS < 5
156 rtn = pthread_create( thread, attr, start_routine, arg );
158 rtn = pthread_create( thread, &attr, start_routine, arg );
160 #if HAVE_PTHREADS > 5
161 pthread_attr_destroy(&attr);
163 pthread_attr_delete(&attr);
165 pthread_detach( thread );
169 #if HAVE_PTHREADS < 7
170 if ( rtn < 0 ) rtn = errno;
176 ldap_pvt_thread_exit( void *retval )
178 pthread_exit( retval );
182 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
184 #if HAVE_PTHREADS < 7
187 if (thread_return==NULL)
188 thread_return=&dummy;
190 if ( pthread_join( thread, thread_return ) < 0 ) return errno;
193 return pthread_join( thread, thread_return );
198 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
200 #if ( HAVE_PTHREAD_KILL && HAVE_PTHREADS > 6 )
201 /* MacOS 10.1 is detected as v10 but has no pthread_kill() */
202 return pthread_kill( thread, signo );
203 #elif ( HAVE_PTHREAD_KILL && HAVE_PTHREADS > 4 )
204 if ( pthread_kill( thread, signo ) < 0 ) return errno;
207 /* pthread package with DCE */
208 if (kill( getpid(), signo )<0)
215 ldap_pvt_thread_yield( void )
217 #ifdef REPLACE_SCHED_YIELD
218 #ifdef HAVE_NANOSLEEP
219 struct timespec t = { 0, 0 };
222 struct timeval tv = {0,0};
223 select( 0, NULL, NULL, NULL, &tv );
229 #elif HAVE_PTHREADS == 10
230 return sched_yield();
232 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
236 #elif HAVE_PTHREADS == 6
246 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
248 #if HAVE_PTHREADS < 7
249 if ( pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) < 0 )
253 return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
258 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
260 #if HAVE_PTHREADS < 7
261 if ( pthread_cond_destroy( cond ) < 0 ) return errno;
264 return pthread_cond_destroy( cond );
269 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
271 #if HAVE_PTHREADS < 7
272 if ( pthread_cond_signal( cond ) < 0 ) return errno;
275 return pthread_cond_signal( cond );
280 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
282 #if HAVE_PTHREADS < 7
283 if ( pthread_cond_broadcast( cond ) < 0 ) return errno;
286 return pthread_cond_broadcast( cond );
291 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
292 ldap_pvt_thread_mutex_t *mutex )
294 #if HAVE_PTHREADS < 7
295 if ( pthread_cond_wait( cond, mutex ) < 0 ) return errno;
298 return pthread_cond_wait( cond, mutex );
303 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
305 #if HAVE_PTHREADS < 7
306 if ( pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT )<0)
310 return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
315 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
317 #if HAVE_PTHREADS < 7
318 if ( pthread_mutex_destroy( mutex ) < 0 ) return errno;
321 return pthread_mutex_destroy( mutex );
326 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
328 #if HAVE_PTHREADS < 7
329 if ( pthread_mutex_lock( mutex ) < 0 ) return errno;
332 return pthread_mutex_lock( mutex );
337 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
339 #if HAVE_PTHREADS < 7
340 if ( pthread_mutex_trylock( mutex ) < 0 ) return errno;
343 return pthread_mutex_trylock( mutex );
348 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
350 #if HAVE_PTHREADS < 7
351 if ( pthread_mutex_unlock( mutex ) < 0 ) return errno;
354 return pthread_mutex_unlock( mutex );
358 ldap_pvt_thread_t ldap_pvt_thread_self( void )
360 return pthread_self();
363 #ifdef LDAP_THREAD_HAVE_RDWR
364 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
366 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
368 #if HAVE_PTHREADS < 7
369 if ( pthread_rwlock_init( rw, NULL ) < 0 ) return errno;
372 return pthread_rwlock_init( rw, NULL );
377 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
379 #if HAVE_PTHREADS < 7
380 if ( pthread_rwlock_destroy( rw ) < 0 ) return errno;
383 return pthread_rwlock_destroy( rw );
387 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
389 #if HAVE_PTHREADS < 7
390 if ( pthread_rwlock_rdlock( rw ) < 0 ) return errno;
393 return pthread_rwlock_rdlock( rw );
397 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
399 #if HAVE_PTHREADS < 7
400 if ( pthread_rwlock_tryrdlock( rw ) < 0 ) return errno;
403 return pthread_rwlock_tryrdlock( rw );
407 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
409 #if HAVE_PTHREADS < 7
410 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
413 return pthread_rwlock_unlock( rw );
417 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
419 #if HAVE_PTHREADS < 7
420 if ( pthread_rwlock_wrlock( rw ) < 0 ) return errno;
423 return pthread_rwlock_wrlock( rw );
427 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
429 #if HAVE_PTHREADS < 7
430 if ( pthread_rwlock_trywrlock( rw ) < 0 ) return errno;
433 return pthread_rwlock_trywrlock( rw );
437 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
439 #if HAVE_PTHREADS < 7
440 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
443 return pthread_rwlock_unlock( rw );
447 #endif /* HAVE_PTHREAD_RWLOCK_DESTROY */
448 #endif /* LDAP_THREAD_HAVE_RDWR */
449 #endif /* HAVE_PTHREADS */