]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_posix.c
fef18fe9cafc76bcf7c370a6df77d4b657568f7f
[openldap] / libraries / libldap_r / thr_posix.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, Redwood City, California, USA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11
12 /* thr_posix.c - wrapper around posix and posixish thread implementations.
13  */
14
15 #include "portable.h"
16
17 #if defined( HAVE_PTHREADS )
18
19 #include <ac/errno.h>
20
21 #include "ldap_pvt_thread.h"
22
23
24 #if HAVE_PTHREADS_D4
25 #  define LDAP_INT_THREAD_ATTR_DEFAULT          pthread_attr_default
26 #  define LDAP_INT_THREAD_CONDATTR_DEFAULT      pthread_condattr_default
27 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT     pthread_mutexattr_default
28 #else
29 #  define LDAP_INT_THREAD_ATTR_DEFAULT          NULL
30 #  define LDAP_INT_THREAD_CONDATTR_DEFAULT      NULL
31 #  define LDAP_INT_THREAD_MUTEXATTR_DEFAULT     NULL
32 #endif
33
34
35 int
36 ldap_int_thread_initialize( void )
37 {
38         return 0;
39 }
40
41 int
42 ldap_int_thread_destroy( void )
43 {
44 #ifdef HAVE_PTHREAD_KILL_OTHER_THREADS_NP
45         /* LinuxThreads: kill clones */
46         pthread_kill_other_threads_np();
47 #endif
48         return 0;
49 }
50
51 #ifdef LDAP_THREAD_HAVE_SETCONCURRENCY
52 int
53 ldap_pvt_thread_set_concurrency(int n)
54 {
55 #ifdef HAVE_PTHREAD_SETCONCURRENCY
56         return pthread_setconcurrency( n );
57 #elif HAVE_THR_SETCONCURRENCY
58         return thr_setconcurrency( n );
59 #else
60         return 0;
61 #endif
62 }
63 #endif
64
65 #ifdef LDAP_THREAD_HAVE_GETCONCURRENCY
66 int
67 ldap_pvt_thread_get_concurrency(void)
68 {
69 #ifdef HAVE_PTHREAD_GETCONCURRENCY
70         return pthread_getconcurrency();
71 #elif HAVE_THR_GETCONCURRENCY
72         return thr_getconcurrency();
73 #else
74         return 0;
75 #endif
76 }
77 #endif
78
79 int 
80 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
81         int detach,
82         void *(*start_routine)( void * ),
83         void *arg)
84 {
85         int rtn;
86 #if defined( HAVE_PTHREADS_FINAL )
87         pthread_attr_t attr;
88         pthread_attr_init(&attr);
89
90 #if defined( PTHREAD_CREATE_JOINABLE ) || defined( PTHREAD_UNDETACHED )
91         if (!detach) {
92 #if defined( PTHREAD_CREATE_JOINABLE )
93                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
94 #else
95                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
96 #endif
97 #ifdef PTHREAD_CREATE_DETACHED
98         } else {
99                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
100 #elif HAVE_PTHREADS_OS390
101         } else {
102                 int st = __DETACHED;
103                 pthread_attr_setdetachstate(&attr, &st);
104 #endif
105         }
106 #endif
107
108 #if defined(LDAP_PVT_THREAD_STACK_SIZE) && LDAP_PVT_THREAD_STACK_SIZE > 0
109         /* this should be tunable */
110         pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE );
111 #endif
112
113         rtn = pthread_create( thread, &attr, start_routine, arg );
114 #ifdef HAVE_PTHREADS_OS390
115         if ( rtn == -1 ) rtn = errno;
116 #endif
117
118 #if !defined( PTHREAD_CREATE_JOINABLE ) && !defined( PTHREAD_UNDETACHED )
119         if( detach ) {
120 #ifdef HAVE_PTHREADS_OS390
121                 (void) pthread_detach( thread );
122 #else
123                 (void) pthread_detach( *thread );
124 #endif
125         }
126 #endif
127         pthread_attr_destroy(&attr);
128
129 #else
130         rtn = pthread_create( thread, LDAP_INT_THREAD_ATTR_DEFAULT,
131                 start_routine, arg );
132
133         if( detach ) {
134                 pthread_detach( thread );
135         }
136 #endif
137
138         return rtn;
139 }
140
141 void 
142 ldap_pvt_thread_exit( void *retval )
143 {
144         pthread_exit( retval );
145 }
146
147 int 
148 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
149 {
150 #if !defined( HAVE_PTHREADS_FINAL )
151         void *dummy;
152         if (thread_return==NULL)
153           thread_return=&dummy;
154 #endif  
155 #ifdef HAVE_PTHREADS_OS390
156         int st = pthread_join( thread, thread_return ); 
157         if ( st == -1 ) st = errno;
158         return st;
159 #else
160         return pthread_join( thread, thread_return );
161 #endif
162 }
163
164 int 
165 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
166 {
167 #ifdef HAVE_PTHREAD_KILL
168 #ifdef HAVE_PTHREADS_OS390
169         int st = pthread_kill( thread, signo );
170         if ( st == -1 ) st = errno;
171         return st;
172 #else
173         return pthread_kill( thread, signo );
174 #endif
175 #else
176         /* pthread package with DCE */
177         if (kill( getpid(), signo )<0)
178                 return errno;
179         return 0;
180 #endif
181 }
182
183 int 
184 ldap_pvt_thread_yield( void )
185 {
186 #ifdef _POSIX_THREAD_IS_GNU_PTH
187         sched_yield();
188         return 0;
189
190 #elif HAVE_SCHED_YIELD
191         return sched_yield();
192
193 #elif HAVE_PTHREAD_YIELD
194 #if HAVE_PTHREADS_OS390
195         pthread_yield(NULL);
196 #else
197         pthread_yield();
198 #endif
199         return 0;
200
201 #elif HAVE_THR_YIELD
202         return thr_yield();
203
204 #else
205         return 0;
206 #endif   
207 }
208
209 int 
210 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
211 {
212         return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
213 }
214
215 int 
216 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
217 {
218         return pthread_cond_destroy( cond );
219 }
220         
221 int 
222 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
223 {
224         return pthread_cond_signal( cond );
225 }
226
227 int
228 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
229 {
230         return pthread_cond_broadcast( cond );
231 }
232
233 int 
234 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
235                       ldap_pvt_thread_mutex_t *mutex )
236 {
237         return pthread_cond_wait( cond, mutex );
238 }
239
240 int 
241 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
242 {
243         return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
244 }
245
246 int 
247 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
248 {
249         return pthread_mutex_destroy( mutex );
250 }
251
252 int 
253 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
254 {
255         return pthread_mutex_lock( mutex );
256 }
257
258 int 
259 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
260 {
261         return pthread_mutex_trylock( mutex );
262 }
263
264 int 
265 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
266 {
267         return pthread_mutex_unlock( mutex );
268 }
269
270 #ifdef LDAP_THREAD_HAVE_RDWR
271 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
272 int 
273 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
274 {
275         return pthread_rwlock_init( rw, NULL );
276 }
277
278 int 
279 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
280 {
281         return pthread_rwlock_destroy( rw );
282 }
283
284 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
285 {
286         return pthread_rwlock_rdlock( rw );
287 }
288
289 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
290 {
291         return pthread_rwlock_tryrdlock( rw );
292 }
293
294 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
295 {
296         return pthread_rwlock_unlock( rw );
297 }
298
299 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
300 {
301         return pthread_rwlock_wrlock( rw );
302 }
303
304 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
305 {
306         return pthread_rwlock_trywrlock( rw );
307 }
308
309 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
310 {
311         return pthread_rwlock_unlock( rw );
312 }
313
314 #endif /* HAVE_PTHREAD_RDLOCK_DESTROY */
315 #endif /* LDAP_THREAD_HAVE_RDWR */
316 #endif /* HAVE_PTHREADS */
317