Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap_r / thr_posix.c
blob35f13a606004ee7b8733ad4441573a630cdf7042
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.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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>.
17 #include "portable.h"
19 #if defined( HAVE_PTHREADS )
21 #include <ac/errno.h>
23 #ifdef REPLACE_BROKEN_YIELD
24 #ifndef HAVE_NANOSLEEP
25 #include <ac/socket.h>
26 #endif
27 #include <ac/time.h>
28 #endif
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 #if HAVE_PTHREADS < 6
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
40 #else
41 # define LDAP_INT_THREAD_ATTR_DEFAULT NULL
42 # define LDAP_INT_THREAD_CONDATTR_DEFAULT NULL
43 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT NULL
44 #endif
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
51 # else
52 # define LDAP_INT_THREAD_MUTEXATTR PTHREAD_MUTEX_ERRORCHECK
53 # endif
54 static pthread_mutexattr_t mutex_attr;
55 # undef LDAP_INT_THREAD_MUTEXATTR_DEFAULT
56 # define LDAP_INT_THREAD_MUTEXATTR_DEFAULT &mutex_attr
57 #endif
59 #if HAVE_PTHREADS < 7
60 #define ERRVAL(val) ((val) < 0 ? errno : 0)
61 #else
62 #define ERRVAL(val) (val)
63 #endif
65 int
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 );
71 #endif
72 return 0;
75 int
76 ldap_int_thread_destroy( void )
78 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
79 /* LinuxThreads: kill clones */
80 pthread_kill_other_threads_np();
81 #endif
82 #ifdef LDAP_INT_THREAD_MUTEXATTR
83 pthread_mutexattr_destroy( &mutex_attr );
84 #endif
85 return 0;
88 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
89 int
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 );
96 #else
97 return 0;
98 #endif
100 #endif
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();
110 #else
111 return 0;
112 #endif
114 #endif
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
123 #else
124 #define PTHREAD_CREATE_JOINABLE 0
125 #endif
126 #endif
128 #ifndef PTHREAD_CREATE_DETACHED
129 #define PTHREAD_CREATE_DETACHED 1
130 #endif
132 int
133 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
134 int detach,
135 void *(*start_routine)( void * ),
136 void *arg)
138 int rtn;
139 pthread_attr_t attr;
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);
144 #else
145 pthread_attr_create(&attr);
146 #endif
148 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
149 /* this should be tunable */
150 pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
151 #endif
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);
157 #else
158 pthread_attr_setdetachstate(&attr, detach);
159 #endif
160 #endif
162 #if HAVE_PTHREADS < 5
163 rtn = pthread_create( thread, attr, start_routine, arg );
164 #else
165 rtn = pthread_create( thread, &attr, start_routine, arg );
166 #endif
168 #if HAVE_PTHREADS > 5
169 pthread_attr_destroy(&attr);
170 #else
171 pthread_attr_delete(&attr);
172 if( detach ) {
173 pthread_detach( thread );
175 #endif
177 #if HAVE_PTHREADS < 7
178 if ( rtn < 0 ) rtn = errno;
179 #endif
180 return rtn;
183 void
184 ldap_pvt_thread_exit( void *retval )
186 pthread_exit( retval );
189 int
190 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
192 #if HAVE_PTHREADS < 7
193 void *dummy;
194 if (thread_return==NULL)
195 thread_return=&dummy;
196 #endif
197 return ERRVAL( pthread_join( thread, thread_return ) );
200 int
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 ) );
206 #else
207 /* pthread package with DCE */
208 if (kill( getpid(), signo )<0)
209 return errno;
210 return 0;
211 #endif
214 int
215 ldap_pvt_thread_yield( void )
217 #ifdef REPLACE_BROKEN_YIELD
218 #ifdef HAVE_NANOSLEEP
219 struct timespec t = { 0, 0 };
220 nanosleep(&t, NULL);
221 #else
222 struct timeval tv = {0,0};
223 select( 0, NULL, NULL, NULL, &tv );
224 #endif
225 return 0;
227 #elif defined(HAVE_THR_YIELD)
228 thr_yield();
229 return 0;
231 #elif HAVE_PTHREADS == 10
232 return sched_yield();
234 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
235 sched_yield();
236 return 0;
238 #elif HAVE_PTHREADS == 6
239 pthread_yield(NULL);
240 return 0;
242 #else
243 pthread_yield();
244 return 0;
245 #endif
248 int
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 ) );
255 int
256 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
258 return ERRVAL( pthread_cond_destroy( cond ) );
261 int
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 ) );
273 int
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 ) );
280 int
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 ) );
287 int
288 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
290 return ERRVAL( pthread_mutex_destroy( mutex ) );
293 int
294 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
296 return ERRVAL( pthread_mutex_lock( mutex ) );
299 int
300 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
302 return ERRVAL( pthread_mutex_trylock( mutex ) );
305 int
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 );
338 return 0;
341 #ifdef LDAP_THREAD_HAVE_RDWR
342 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
343 int
344 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
346 return ERRVAL( pthread_rwlock_init( rw, NULL ) );
349 int
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 */