]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_posix.c
Restore pre-C99 preprocessor support (since rev 1.165)
[openldap] / libraries / libldap_r / thr_posix.c
1 /* thr_posix.c - wrapper around posix and posixish thread implementations.  */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
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.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #if defined( HAVE_PTHREADS )
20
21 #include <ac/errno.h>
22
23 #ifdef REPLACE_BROKEN_YIELD
24 #ifndef HAVE_NANOSLEEP
25 #include <ac/socket.h>
26 #endif
27 #include <ac/time.h>
28 #endif
29
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
35 #if HAVE_PTHREADS < 6
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
39 #else
40 #  define LDAP_INT_THREAD_ATTR_DEFAULT          NULL
41 #  define LDAP_INT_THREAD_CONDATTR_DEFAULT      NULL
42 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT NULL
43 #endif
44
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
50 #  else
51 #    define LDAP_INT_THREAD_MUTEXATTR   PTHREAD_MUTEX_ERRORCHECK
52 #  endif
53 static pthread_mutexattr_t mutex_attr;
54 #  undef  LDAP_INT_THREAD_MUTEXATTR_DEFAULT
55 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT &mutex_attr
56 #endif
57
58 #if HAVE_PTHREADS < 7
59 #define ERRVAL(val)     ((val) < 0 ? errno : 0)
60 #else
61 #define ERRVAL(val)     (val)
62 #endif
63
64 int
65 ldap_int_thread_initialize( void )
66 {
67 #ifdef LDAP_INT_THREAD_MUTEXATTR
68         pthread_mutexattr_init( &mutex_attr );
69         pthread_mutexattr_settype( &mutex_attr, LDAP_INT_THREAD_MUTEXATTR );
70 #endif
71         return 0;
72 }
73
74 int
75 ldap_int_thread_destroy( void )
76 {
77 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
78         /* LinuxThreads: kill clones */
79         pthread_kill_other_threads_np();
80 #endif
81 #ifdef LDAP_INT_THREAD_MUTEXATTR
82         pthread_mutexattr_destroy( &mutex_attr );
83 #endif
84         return 0;
85 }
86
87 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
88 int
89 ldap_pvt_thread_set_concurrency(int n)
90 {
91 #ifdef HAVE_PTHREAD_SETCONCURRENCY
92         return pthread_setconcurrency( n );
93 #elif defined(HAVE_THR_SETCONCURRENCY)
94         return thr_setconcurrency( n );
95 #else
96         return 0;
97 #endif
98 }
99 #endif
100
101 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
102 int
103 ldap_pvt_thread_get_concurrency(void)
104 {
105 #ifdef HAVE_PTHREAD_GETCONCURRENCY
106         return pthread_getconcurrency();
107 #elif defined(HAVE_THR_GETCONCURRENCY)
108         return thr_getconcurrency();
109 #else
110         return 0;
111 #endif
112 }
113 #endif
114
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.
118  */
119 #ifndef PTHREAD_CREATE_JOINABLE
120 #ifdef PTHREAD_CREATE_UNDETACHED
121 #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
122 #else
123 #define PTHREAD_CREATE_JOINABLE 0
124 #endif
125 #endif
126
127 #ifndef PTHREAD_CREATE_DETACHED
128 #define PTHREAD_CREATE_DETACHED 1
129 #endif
130
131 int 
132 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
133         int detach,
134         void *(*start_routine)( void * ),
135         void *arg)
136 {
137         int rtn;
138         pthread_attr_t attr;
139
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);
143 #else
144         pthread_attr_create(&attr);
145 #endif
146
147 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
148         /* this should be tunable */
149         pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
150 #endif
151
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);
156 #else
157         pthread_attr_setdetachstate(&attr, detach);
158 #endif
159 #endif
160
161 #if HAVE_PTHREADS < 5
162         rtn = pthread_create( thread, attr, start_routine, arg );
163 #else
164         rtn = pthread_create( thread, &attr, start_routine, arg );
165 #endif
166
167 #if HAVE_PTHREADS > 5
168         pthread_attr_destroy(&attr);
169 #else
170         pthread_attr_delete(&attr);
171         if( detach ) {
172                 pthread_detach( thread );
173         }
174 #endif
175
176 #if HAVE_PTHREADS < 7
177         if ( rtn < 0 ) rtn = errno;
178 #endif
179         return rtn;
180 }
181
182 void 
183 ldap_pvt_thread_exit( void *retval )
184 {
185         pthread_exit( retval );
186 }
187
188 int 
189 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
190 {
191 #if HAVE_PTHREADS < 7
192         void *dummy;
193         if (thread_return==NULL)
194           thread_return=&dummy;
195 #endif
196         return ERRVAL( pthread_join( thread, thread_return ) );
197 }
198
199 int 
200 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
201 {
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 ) );
205 #else
206         /* pthread package with DCE */
207         if (kill( getpid(), signo )<0)
208                 return errno;
209         return 0;
210 #endif
211 }
212
213 int 
214 ldap_pvt_thread_yield( void )
215 {
216 #ifdef REPLACE_BROKEN_YIELD
217 #ifdef HAVE_NANOSLEEP
218         struct timespec t = { 0, 0 };
219         nanosleep(&t, NULL);
220 #else
221         struct timeval tv = {0,0};
222         select( 0, NULL, NULL, NULL, &tv );
223 #endif
224         return 0;
225
226 #elif defined(HAVE_THR_YIELD)
227         thr_yield();
228         return 0;
229
230 #elif HAVE_PTHREADS == 10
231         return sched_yield();
232
233 #elif defined(_POSIX_THREAD_IS_GNU_PTH)
234         sched_yield();
235         return 0;
236
237 #elif HAVE_PTHREADS == 6
238         pthread_yield(NULL);
239         return 0;
240
241 #else
242         pthread_yield();
243         return 0;
244 #endif
245 }
246
247 int 
248 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
249 {
250         return ERRVAL( pthread_cond_init(
251                 cond, LDAP_INT_THREAD_CONDATTR_DEFAULT ) );
252 }
253
254 int 
255 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
256 {
257         return ERRVAL( pthread_cond_destroy( cond ) );
258 }
259         
260 int 
261 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
262 {
263         return ERRVAL( pthread_cond_signal( cond ) );
264 }
265
266 int
267 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
268 {
269         return ERRVAL( pthread_cond_broadcast( cond ) );
270 }
271
272 int 
273 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
274                       ldap_pvt_thread_mutex_t *mutex )
275 {
276         return ERRVAL( pthread_cond_wait( cond, mutex ) );
277 }
278
279 int 
280 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
281 {
282         return ERRVAL( pthread_mutex_init(
283                 mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT ) );
284 }
285
286 int 
287 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
288 {
289         return ERRVAL( pthread_mutex_destroy( mutex ) );
290 }
291
292 int 
293 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
294 {
295         return ERRVAL( pthread_mutex_lock( mutex ) );
296 }
297
298 int 
299 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
300 {
301         return ERRVAL( pthread_mutex_trylock( mutex ) );
302 }
303
304 int 
305 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
306 {
307         return ERRVAL( pthread_mutex_unlock( mutex ) );
308 }
309
310 ldap_pvt_thread_t ldap_pvt_thread_self( void )
311 {
312         return pthread_self();
313 }
314
315 int
316 ldap_pvt_thread_key_create( ldap_pvt_thread_key_t *key )
317 {
318         return pthread_key_create( key, NULL );
319 }
320
321 int
322 ldap_pvt_thread_key_destroy( ldap_pvt_thread_key_t key )
323 {
324         return pthread_key_delete( key );
325 }
326
327 int
328 ldap_pvt_thread_key_setdata( ldap_pvt_thread_key_t key, void *data )
329 {
330         return pthread_setspecific( key, data );
331 }
332
333 int
334 ldap_pvt_thread_key_getdata( ldap_pvt_thread_key_t key, void **data )
335 {
336         *data = pthread_getspecific( key );
337         return 0;
338 }
339
340 #ifdef LDAP_THREAD_HAVE_RDWR
341 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
342 int 
343 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
344 {
345         return ERRVAL( pthread_rwlock_init( rw, NULL ) );
346 }
347
348 int 
349 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
350 {
351         return ERRVAL( pthread_rwlock_destroy( rw ) );
352 }
353
354 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
355 {
356         return ERRVAL( pthread_rwlock_rdlock( rw ) );
357 }
358
359 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
360 {
361         return ERRVAL( pthread_rwlock_tryrdlock( rw ) );
362 }
363
364 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
365 {
366         return ERRVAL( pthread_rwlock_unlock( rw ) );
367 }
368
369 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
370 {
371         return ERRVAL( pthread_rwlock_wrlock( rw ) );
372 }
373
374 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
375 {
376         return ERRVAL( pthread_rwlock_trywrlock( rw ) );
377 }
378
379 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
380 {
381         return ERRVAL( pthread_rwlock_unlock( rw ) );
382 }
383
384 #endif /* HAVE_PTHREAD_RWLOCK_DESTROY */
385 #endif /* LDAP_THREAD_HAVE_RDWR */
386 #endif /* HAVE_PTHREADS */
387