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-2018 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 */
34 #include <signal.h> /* For pthread_kill() */
36 extern int ldap_int_stackguard;
39 # define LDAP_INT_THREAD_ATTR_DEFAULT pthread_attr_default
40 # define LDAP_INT_THREAD_CONDATTR_DEFAULT pthread_condattr_default
41 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT pthread_mutexattr_default
43 # define LDAP_INT_THREAD_ATTR_DEFAULT NULL
44 # define LDAP_INT_THREAD_CONDATTR_DEFAULT NULL
45 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT NULL
48 #ifdef LDAP_THREAD_DEBUG
49 # if defined LDAP_INT_THREAD_MUTEXATTR /* May be defined in CPPFLAGS */
50 # elif defined HAVE_PTHREAD_KILL_OTHER_THREADS_NP
51 /* LinuxThreads hack */
52 # define LDAP_INT_THREAD_MUTEXATTR PTHREAD_MUTEX_ERRORCHECK_NP
54 # define LDAP_INT_THREAD_MUTEXATTR PTHREAD_MUTEX_ERRORCHECK
56 static pthread_mutexattr_t mutex_attr;
57 # undef LDAP_INT_THREAD_MUTEXATTR_DEFAULT
58 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT &mutex_attr
61 static pthread_mutexattr_t mutex_attr_recursive;
64 #define ERRVAL(val) ((val) < 0 ? errno : 0)
66 #define ERRVAL(val) (val)
70 ldap_int_thread_initialize( void )
72 #ifdef LDAP_INT_THREAD_MUTEXATTR
73 pthread_mutexattr_init( &mutex_attr );
74 pthread_mutexattr_settype( &mutex_attr, LDAP_INT_THREAD_MUTEXATTR );
76 if (pthread_mutexattr_init(&mutex_attr_recursive))
78 if (pthread_mutexattr_settype(&mutex_attr_recursive, PTHREAD_MUTEX_RECURSIVE))
84 ldap_int_thread_destroy( void )
86 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
87 /* LinuxThreads: kill clones */
88 pthread_kill_other_threads_np();
90 #ifdef LDAP_INT_THREAD_MUTEXATTR
91 pthread_mutexattr_destroy( &mutex_attr );
93 pthread_mutexattr_destroy( &mutex_attr_recursive );
97 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
99 ldap_pvt_thread_set_concurrency(int n)
101 #ifdef HAVE_PTHREAD_SETCONCURRENCY
102 return pthread_setconcurrency( n );
103 #elif defined(HAVE_THR_SETCONCURRENCY)
104 return thr_setconcurrency( n );
111 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
113 ldap_pvt_thread_get_concurrency(void)
115 #ifdef HAVE_PTHREAD_GETCONCURRENCY
116 return pthread_getconcurrency();
117 #elif defined(HAVE_THR_GETCONCURRENCY)
118 return thr_getconcurrency();
125 /* detachstate appeared in Draft 6, but without manifest constants.
126 * in Draft 7 they were called PTHREAD_CREATE_UNDETACHED and ...DETACHED.
127 * in Draft 8 on, ...UNDETACHED became ...JOINABLE.
129 #ifndef PTHREAD_CREATE_JOINABLE
130 #ifdef PTHREAD_CREATE_UNDETACHED
131 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
133 #define PTHREAD_CREATE_JOINABLE 0
137 #ifndef PTHREAD_CREATE_DETACHED
138 #define PTHREAD_CREATE_DETACHED 1
142 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
144 void *(*start_routine)( void * ),
150 /* Always create the thread attrs, so we can set stacksize if we need to */
151 #if HAVE_PTHREADS > 5
152 pthread_attr_init(&attr);
154 pthread_attr_create(&attr);
157 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
158 /* this should be tunable */
159 pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
160 if ( ldap_int_stackguard )
161 pthread_attr_setguardsize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
164 #if HAVE_PTHREADS > 5
165 detach = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
166 #if HAVE_PTHREADS == 6
167 pthread_attr_setdetachstate(&attr, &detach);
169 pthread_attr_setdetachstate(&attr, detach);
173 #if HAVE_PTHREADS < 5
174 rtn = pthread_create( thread, attr, start_routine, arg );
176 rtn = pthread_create( thread, &attr, start_routine, arg );
179 #if HAVE_PTHREADS > 5
180 pthread_attr_destroy(&attr);
182 pthread_attr_delete(&attr);
184 pthread_detach( thread );
188 #if HAVE_PTHREADS < 7
189 if ( rtn < 0 ) rtn = errno;
195 ldap_pvt_thread_exit( void *retval )
197 pthread_exit( retval );
201 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
203 #if HAVE_PTHREADS < 7
205 if (thread_return==NULL)
206 thread_return=&dummy;
208 return ERRVAL( pthread_join( thread, thread_return ) );
212 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
214 #if defined(HAVE_PTHREAD_KILL) && HAVE_PTHREADS > 4
215 /* MacOS 10.1 is detected as v10 but has no pthread_kill() */
216 return ERRVAL( pthread_kill( thread, signo ) );
218 /* pthread package with DCE */
219 if (kill( getpid(), signo )<0)
226 ldap_pvt_thread_yield( void )
228 #ifdef REPLACE_BROKEN_YIELD
229 #ifdef HAVE_NANOSLEEP
230 struct timespec t = { 0, 0 };
233 struct timeval tv = {0,0};
234 select( 0, NULL, NULL, NULL, &tv );
238 #elif defined(HAVE_THR_YIELD)
242 #elif HAVE_PTHREADS == 10
243 return sched_yield();
245 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
249 #elif HAVE_PTHREADS == 6
260 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
262 return ERRVAL( pthread_cond_init(
263 cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) );
267 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
269 return ERRVAL( pthread_cond_destroy( cond ) );
273 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
275 return ERRVAL( pthread_cond_signal( cond ) );
279 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
281 return ERRVAL( pthread_cond_broadcast( cond ) );
285 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
286 ldap_pvt_thread_mutex_t *mutex )
288 return ERRVAL( pthread_cond_wait( cond, mutex ) );
292 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
294 return ERRVAL( pthread_mutex_init(
295 mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT ) );
299 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
301 return ERRVAL( pthread_mutex_destroy( mutex ) );
305 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
307 return ERRVAL( pthread_mutex_lock( mutex ) );
311 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
313 return ERRVAL( pthread_mutex_trylock( mutex ) );
317 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
319 return ERRVAL( pthread_mutex_unlock( mutex ) );
323 ldap_pvt_thread_mutex_recursive_init( ldap_pvt_thread_mutex_t *mutex )
325 return ERRVAL( pthread_mutex_init( mutex, &mutex_attr_recursive ) );
328 int ldap_pvt_thread_mutex_recursive_destroy( ldap_pvt_thread_mutex_recursive_t *mutex )
329 LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_destroy")));
330 int ldap_pvt_thread_mutex_recursive_lock( ldap_pvt_thread_mutex_recursive_t *mutex )
331 LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_lock")));
332 int ldap_pvt_thread_mutex_recursive_trylock( ldap_pvt_thread_mutex_recursive_t *mutex )
333 LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_trylock")));
334 int ldap_pvt_thread_mutex_recursive_unlock( ldap_pvt_thread_mutex_recursive_t *mutex )
335 LDAP_GCCATTR((alias("ldap_pvt_thread_mutex_unlock")));
337 ldap_pvt_thread_t ldap_pvt_thread_self( void )
339 return pthread_self();
343 ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key )
345 return pthread_key_create( key, NULL );
349 ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key )
351 return pthread_key_delete( key );
355 ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data )
357 return pthread_setspecific( key, data );
361 ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data )
363 *data = pthread_getspecific( key );
367 #ifdef LDAP_THREAD_HAVE_RDWR
368 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
370 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
372 return ERRVAL( pthread_rwlock_init( rw, NULL ) );
376 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
378 return ERRVAL( pthread_rwlock_destroy( rw ) );
381 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
383 return ERRVAL( pthread_rwlock_rdlock( rw ) );
386 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
388 return ERRVAL( pthread_rwlock_tryrdlock( rw ) );
391 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
393 return ERRVAL( pthread_rwlock_unlock( rw ) );
396 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
398 return ERRVAL( pthread_rwlock_wrlock( rw ) );
401 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
403 return ERRVAL( pthread_rwlock_trywrlock( rw ) );
406 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
408 return ERRVAL( pthread_rwlock_unlock( rw ) );
411 #endif /* HAVE_PTHREAD_RWLOCK_DESTROY */
412 #endif /* LDAP_THREAD_HAVE_RDWR */
413 #endif /* HAVE_PTHREADS */