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-2007 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 #define ERRVAL(val) ((val) < 0 ? errno : 0)
61 #define ERRVAL(val) (val)
65 ldap_int_thread_initialize( void )
67 #ifdef LDAP_INT_THREAD_MUTEXATTR
68 pthread_mutexattr_init( &mutex_attr );
69 pthread_mutexattr_settype( &mutex_attr, LDAP_INT_THREAD_MUTEXATTR );
75 ldap_int_thread_destroy( void )
77 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
78 /* LinuxThreads: kill clones */
79 pthread_kill_other_threads_np();
81 #ifdef LDAP_INT_THREAD_MUTEXATTR
82 pthread_mutexattr_destroy( &mutex_attr );
87 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
89 ldap_pvt_thread_set_concurrency(int n)
91 #ifdef HAVE_PTHREAD_SETCONCURRENCY
92 return pthread_setconcurrency( n );
93 #elif defined(HAVE_THR_SETCONCURRENCY)
94 return thr_setconcurrency( n );
101 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
103 ldap_pvt_thread_get_concurrency(void)
105 #ifdef HAVE_PTHREAD_GETCONCURRENCY
106 return pthread_getconcurrency();
107 #elif defined(HAVE_THR_GETCONCURRENCY)
108 return thr_getconcurrency();
115 /* detachstate appeared in Draft 6, but without manifest constants.
116 * in Draft 7 they were called PTHREAD_CREATE_UNDETACHED and ...DETACHED.
117 * in Draft 8 on, ...UNDETACHED became ...JOINABLE.
119 #ifndef PTHREAD_CREATE_JOINABLE
120 #ifdef PTHREAD_CREATE_UNDETACHED
121 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
123 #define PTHREAD_CREATE_JOINABLE 0
127 #ifndef PTHREAD_CREATE_DETACHED
128 #define PTHREAD_CREATE_DETACHED 1
132 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
134 void *(*start_routine)( void * ),
140 /* Always create the thread attrs, so we can set stacksize if we need to */
141 #if HAVE_PTHREADS > 5
142 pthread_attr_init(&attr);
144 pthread_attr_create(&attr);
147 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
148 /* this should be tunable */
149 pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
152 #if HAVE_PTHREADS > 5
153 detach = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
154 #if HAVE_PTHREADS == 6
155 pthread_attr_setdetachstate(&attr, &detach);
157 pthread_attr_setdetachstate(&attr, detach);
161 #if HAVE_PTHREADS < 5
162 rtn = pthread_create( thread, attr, start_routine, arg );
164 rtn = pthread_create( thread, &attr, start_routine, arg );
167 #if HAVE_PTHREADS > 5
168 pthread_attr_destroy(&attr);
170 pthread_attr_delete(&attr);
172 pthread_detach( thread );
176 #if HAVE_PTHREADS < 7
177 if ( rtn < 0 ) rtn = errno;
183 ldap_pvt_thread_exit( void *retval )
185 pthread_exit( retval );
189 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
191 #if HAVE_PTHREADS < 7
193 if (thread_return==NULL)
194 thread_return=&dummy;
196 return ERRVAL( pthread_join( thread, thread_return ) );
200 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
202 #if defined(HAVE_PTHREAD_KILL) && HAVE_PTHREADS > 4
203 /* MacOS 10.1 is detected as v10 but has no pthread_kill() */
204 return ERRVAL( pthread_kill( thread, signo ) );
206 /* pthread package with DCE */
207 if (kill( getpid(), signo )<0)
214 ldap_pvt_thread_yield( void )
216 #ifdef REPLACE_BROKEN_YIELD
217 #ifdef HAVE_NANOSLEEP
218 struct timespec t = { 0, 0 };
221 struct timeval tv = {0,0};
222 select( 0, NULL, NULL, NULL, &tv );
226 #elif defined(HAVE_THR_YIELD)
230 #elif HAVE_PTHREADS == 10
231 return sched_yield();
233 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
237 #elif HAVE_PTHREADS == 6
248 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
250 return ERRVAL( pthread_cond_init(
251 cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) );
255 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
257 return ERRVAL( pthread_cond_destroy( cond ) );
261 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
263 return ERRVAL( pthread_cond_signal( cond ) );
267 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
269 return ERRVAL( pthread_cond_broadcast( cond ) );
273 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
274 ldap_pvt_thread_mutex_t *mutex )
276 return ERRVAL( pthread_cond_wait( cond, mutex ) );
280 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
282 return ERRVAL( pthread_mutex_init(
283 mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT ) );
287 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
289 return ERRVAL( pthread_mutex_destroy( mutex ) );
293 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
295 return ERRVAL( pthread_mutex_lock( mutex ) );
299 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
301 return ERRVAL( pthread_mutex_trylock( mutex ) );
305 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
307 return ERRVAL( pthread_mutex_unlock( mutex ) );
310 ldap_pvt_thread_t ldap_pvt_thread_self( void )
312 return pthread_self();
315 #ifdef LDAP_THREAD_HAVE_RDWR
316 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
318 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
320 return ERRVAL( pthread_rwlock_init( rw, NULL ) );
324 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
326 return ERRVAL( pthread_rwlock_destroy( rw ) );
329 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
331 return ERRVAL( pthread_rwlock_rdlock( rw ) );
334 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
336 return ERRVAL( pthread_rwlock_tryrdlock( rw ) );
339 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
341 return ERRVAL( pthread_rwlock_unlock( rw ) );
344 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
346 return ERRVAL( pthread_rwlock_wrlock( rw ) );
349 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
351 return ERRVAL( pthread_rwlock_trywrlock( rw ) );
354 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
356 return ERRVAL( pthread_rwlock_unlock( rw ) );
359 #endif /* HAVE_PTHREAD_RWLOCK_DESTROY */
360 #endif /* LDAP_THREAD_HAVE_RDWR */
361 #endif /* HAVE_PTHREADS */