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 #ifdef REPLACE_BROKEN_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_BROKEN_YIELD
218 #ifdef HAVE_NANOSLEEP
219 struct timespec t = { 0, 0 };
222 struct timeval tv = {0,0};
223 select( 0, NULL, NULL, NULL, &tv );
230 #elif HAVE_PTHREADS == 10
231 return sched_yield();
233 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
237 #elif HAVE_PTHREADS == 6
247 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
249 #if HAVE_PTHREADS < 7
250 if ( pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) < 0 )
254 return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
259 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
261 #if HAVE_PTHREADS < 7
262 if ( pthread_cond_destroy( cond ) < 0 ) return errno;
265 return pthread_cond_destroy( cond );
270 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
272 #if HAVE_PTHREADS < 7
273 if ( pthread_cond_signal( cond ) < 0 ) return errno;
276 return pthread_cond_signal( cond );
281 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
283 #if HAVE_PTHREADS < 7
284 if ( pthread_cond_broadcast( cond ) < 0 ) return errno;
287 return pthread_cond_broadcast( cond );
292 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
293 ldap_pvt_thread_mutex_t *mutex )
295 #if HAVE_PTHREADS < 7
296 if ( pthread_cond_wait( cond, mutex ) < 0 ) return errno;
299 return pthread_cond_wait( cond, mutex );
304 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
306 #if HAVE_PTHREADS < 7
307 if ( pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT )<0)
311 return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
316 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
318 #if HAVE_PTHREADS < 7
319 if ( pthread_mutex_destroy( mutex ) < 0 ) return errno;
322 return pthread_mutex_destroy( mutex );
327 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
329 #if HAVE_PTHREADS < 7
330 if ( pthread_mutex_lock( mutex ) < 0 ) return errno;
333 return pthread_mutex_lock( mutex );
338 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
340 #if HAVE_PTHREADS < 7
341 if ( pthread_mutex_trylock( mutex ) < 0 ) return errno;
344 return pthread_mutex_trylock( mutex );
349 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
351 #if HAVE_PTHREADS < 7
352 if ( pthread_mutex_unlock( mutex ) < 0 ) return errno;
355 return pthread_mutex_unlock( mutex );
359 ldap_pvt_thread_t ldap_pvt_thread_self( void )
361 return pthread_self();
364 #ifdef LDAP_THREAD_HAVE_RDWR
365 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
367 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
369 #if HAVE_PTHREADS < 7
370 if ( pthread_rwlock_init( rw, NULL ) < 0 ) return errno;
373 return pthread_rwlock_init( rw, NULL );
378 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
380 #if HAVE_PTHREADS < 7
381 if ( pthread_rwlock_destroy( rw ) < 0 ) return errno;
384 return pthread_rwlock_destroy( rw );
388 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
390 #if HAVE_PTHREADS < 7
391 if ( pthread_rwlock_rdlock( rw ) < 0 ) return errno;
394 return pthread_rwlock_rdlock( rw );
398 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
400 #if HAVE_PTHREADS < 7
401 if ( pthread_rwlock_tryrdlock( rw ) < 0 ) return errno;
404 return pthread_rwlock_tryrdlock( rw );
408 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
410 #if HAVE_PTHREADS < 7
411 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
414 return pthread_rwlock_unlock( rw );
418 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
420 #if HAVE_PTHREADS < 7
421 if ( pthread_rwlock_wrlock( rw ) < 0 ) return errno;
424 return pthread_rwlock_wrlock( rw );
428 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
430 #if HAVE_PTHREADS < 7
431 if ( pthread_rwlock_trywrlock( rw ) < 0 ) return errno;
434 return pthread_rwlock_trywrlock( rw );
438 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
440 #if HAVE_PTHREADS < 7
441 if ( pthread_rwlock_unlock( rw ) < 0 ) return errno;
444 return pthread_rwlock_unlock( rw );
448 #endif /* HAVE_PTHREAD_RWLOCK_DESTROY */
449 #endif /* LDAP_THREAD_HAVE_RDWR */
450 #endif /* HAVE_PTHREADS */