1 /* thr_posix.c - wrapper around posix and posixish thread implementations. */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap_r/thr_posix.c,v 1.46.2.5 2008/02/11 23:26:42 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 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() */
37 # define LDAP_INT_THREAD_ATTR_DEFAULT pthread_attr_default
38 # define LDAP_INT_THREAD_CONDATTR_DEFAULT pthread_condattr_default
39 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT pthread_mutexattr_default
41 # define LDAP_INT_THREAD_ATTR_DEFAULT NULL
42 # define LDAP_INT_THREAD_CONDATTR_DEFAULT NULL
43 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT NULL
46 #ifdef LDAP_THREAD_DEBUG
47 # if defined LDAP_INT_THREAD_MUTEXATTR /* May be defined in CPPFLAGS */
48 # elif defined HAVE_PTHREAD_KILL_OTHER_THREADS_NP
49 /* LinuxThreads hack */
50 # define LDAP_INT_THREAD_MUTEXATTR PTHREAD_MUTEX_ERRORCHECK_NP
52 # define LDAP_INT_THREAD_MUTEXATTR PTHREAD_MUTEX_ERRORCHECK
54 static pthread_mutexattr_t mutex_attr
;
55 # undef LDAP_INT_THREAD_MUTEXATTR_DEFAULT
56 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT &mutex_attr
60 #define ERRVAL(val) ((val) < 0 ? errno : 0)
62 #define ERRVAL(val) (val)
66 ldap_int_thread_initialize( void )
68 #ifdef LDAP_INT_THREAD_MUTEXATTR
69 pthread_mutexattr_init( &mutex_attr
);
70 pthread_mutexattr_settype( &mutex_attr
, LDAP_INT_THREAD_MUTEXATTR
);
76 ldap_int_thread_destroy( void )
78 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
79 /* LinuxThreads: kill clones */
80 pthread_kill_other_threads_np();
82 #ifdef LDAP_INT_THREAD_MUTEXATTR
83 pthread_mutexattr_destroy( &mutex_attr
);
88 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
90 ldap_pvt_thread_set_concurrency(int n
)
92 #ifdef HAVE_PTHREAD_SETCONCURRENCY
93 return pthread_setconcurrency( n
);
94 #elif defined(HAVE_THR_SETCONCURRENCY)
95 return thr_setconcurrency( n
);
102 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
104 ldap_pvt_thread_get_concurrency(void)
106 #ifdef HAVE_PTHREAD_GETCONCURRENCY
107 return pthread_getconcurrency();
108 #elif defined(HAVE_THR_GETCONCURRENCY)
109 return thr_getconcurrency();
116 /* detachstate appeared in Draft 6, but without manifest constants.
117 * in Draft 7 they were called PTHREAD_CREATE_UNDETACHED and ...DETACHED.
118 * in Draft 8 on, ...UNDETACHED became ...JOINABLE.
120 #ifndef PTHREAD_CREATE_JOINABLE
121 #ifdef PTHREAD_CREATE_UNDETACHED
122 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
124 #define PTHREAD_CREATE_JOINABLE 0
128 #ifndef PTHREAD_CREATE_DETACHED
129 #define PTHREAD_CREATE_DETACHED 1
133 ldap_pvt_thread_create( ldap_pvt_thread_t
* thread
,
135 void *(*start_routine
)( void * ),
141 /* Always create the thread attrs, so we can set stacksize if we need to */
142 #if HAVE_PTHREADS > 5
143 pthread_attr_init(&attr
);
145 pthread_attr_create(&attr
);
148 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
149 /* this should be tunable */
150 pthread_attr_setstacksize( &attr
, LDAP_PVT_THREAD_STACK_SIZE
);
153 #if HAVE_PTHREADS > 5
154 detach
= detach
? PTHREAD_CREATE_DETACHED
: PTHREAD_CREATE_JOINABLE
;
155 #if HAVE_PTHREADS == 6
156 pthread_attr_setdetachstate(&attr
, &detach
);
158 pthread_attr_setdetachstate(&attr
, detach
);
162 #if HAVE_PTHREADS < 5
163 rtn
= pthread_create( thread
, attr
, start_routine
, arg
);
165 rtn
= pthread_create( thread
, &attr
, start_routine
, arg
);
168 #if HAVE_PTHREADS > 5
169 pthread_attr_destroy(&attr
);
171 pthread_attr_delete(&attr
);
173 pthread_detach( thread
);
177 #if HAVE_PTHREADS < 7
178 if ( rtn
< 0 ) rtn
= errno
;
184 ldap_pvt_thread_exit( void *retval
)
186 pthread_exit( retval
);
190 ldap_pvt_thread_join( ldap_pvt_thread_t thread
, void **thread_return
)
192 #if HAVE_PTHREADS < 7
194 if (thread_return
==NULL
)
195 thread_return
=&dummy
;
197 return ERRVAL( pthread_join( thread
, thread_return
) );
201 ldap_pvt_thread_kill( ldap_pvt_thread_t thread
, int signo
)
203 #if defined(HAVE_PTHREAD_KILL) && HAVE_PTHREADS > 4
204 /* MacOS 10.1 is detected as v10 but has no pthread_kill() */
205 return ERRVAL( pthread_kill( thread
, signo
) );
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
);
227 #elif defined(HAVE_THR_YIELD)
231 #elif HAVE_PTHREADS == 10
232 return sched_yield();
234 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
238 #elif HAVE_PTHREADS == 6
249 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t
*cond
)
251 return ERRVAL( pthread_cond_init(
252 cond
, LDAP_INT_THREAD_CONDATTR_DEFAULT
) );
256 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t
*cond
)
258 return ERRVAL( pthread_cond_destroy( cond
) );
262 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t
*cond
)
264 return ERRVAL( pthread_cond_signal( cond
) );
268 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t
*cond
)
270 return ERRVAL( pthread_cond_broadcast( cond
) );
274 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t
*cond
,
275 ldap_pvt_thread_mutex_t
*mutex
)
277 return ERRVAL( pthread_cond_wait( cond
, mutex
) );
281 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t
*mutex
)
283 return ERRVAL( pthread_mutex_init(
284 mutex
, LDAP_INT_THREAD_MUTEXATTR_DEFAULT
) );
288 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t
*mutex
)
290 return ERRVAL( pthread_mutex_destroy( mutex
) );
294 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t
*mutex
)
296 return ERRVAL( pthread_mutex_lock( mutex
) );
300 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t
*mutex
)
302 return ERRVAL( pthread_mutex_trylock( mutex
) );
306 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t
*mutex
)
308 return ERRVAL( pthread_mutex_unlock( mutex
) );
311 ldap_pvt_thread_t
ldap_pvt_thread_self( void )
313 return pthread_self();
317 ldap_pvt_thread_key_create( ldap_pvt_thread_key_t
*key
)
319 return pthread_key_create( key
, NULL
);
323 ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key
)
325 return pthread_key_delete( key
);
329 ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key
, void *data
)
331 return pthread_setspecific( key
, data
);
335 ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key
, void **data
)
337 *data
= pthread_getspecific( key
);
341 #ifdef LDAP_THREAD_HAVE_RDWR
342 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
344 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t
*rw
)
346 return ERRVAL( pthread_rwlock_init( rw
, NULL
) );
350 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t
*rw
)
352 return ERRVAL( pthread_rwlock_destroy( rw
) );
355 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t
*rw
)
357 return ERRVAL( pthread_rwlock_rdlock( rw
) );
360 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t
*rw
)
362 return ERRVAL( pthread_rwlock_tryrdlock( rw
) );
365 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t
*rw
)
367 return ERRVAL( pthread_rwlock_unlock( rw
) );
370 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t
*rw
)
372 return ERRVAL( pthread_rwlock_wrlock( rw
) );
375 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t
*rw
)
377 return ERRVAL( pthread_rwlock_trywrlock( rw
) );
380 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t
*rw
)
382 return ERRVAL( pthread_rwlock_unlock( rw
) );
385 #endif /* HAVE_PTHREAD_RWLOCK_DESTROY */
386 #endif /* LDAP_THREAD_HAVE_RDWR */
387 #endif /* HAVE_PTHREADS */