]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_posix.c
Some tweaks for OS/390 pthreads. Func signatures are different, return
[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_PTHREAD_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_PTHREAD_OS390
115         if ( rtn == -1 ) rtn = errno;
116 #endif
117
118 #if !defined( PTHREAD_CREATE_JOINABLE ) && !defined( PTHREAD_UNDETACHED )
119         if( detach ) {
120                 (void) pthread_detach( *thread );
121         }
122 #endif
123         pthread_attr_destroy(&attr);
124
125 #else
126         rtn = pthread_create( thread, LDAP_INT_THREAD_ATTR_DEFAULT,
127                 start_routine, arg );
128
129         if( detach ) {
130                 pthread_detach( thread );
131         }
132 #endif
133
134         return rtn;
135 }
136
137 void 
138 ldap_pvt_thread_exit( void *retval )
139 {
140         pthread_exit( retval );
141 }
142
143 int 
144 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
145 {
146 #if !defined( HAVE_PTHREADS_FINAL )
147         void *dummy;
148         if (thread_return==NULL)
149           thread_return=&dummy;
150 #endif  
151 #ifdef HAVE_PTHREAD_OS390
152         int st = pthread_join( thread, thread_return ); 
153         if ( st == -1 ) st = errno;
154         return st;
155 #else
156         return pthread_join( thread, thread_return );
157 #endif
158 }
159
160 int 
161 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
162 {
163 #ifdef HAVE_PTHREAD_KILL
164 #ifdef HAVE_PTHREAD_OS390
165         int st = pthread_kill( thread, signo );
166         if ( st == -1 ) st = errno;
167         return st;
168 #else
169         return pthread_kill( thread, signo );
170 #endif
171 #else
172         /* pthread package with DCE */
173         if (kill( getpid(), signo )<0)
174                 return errno;
175         return 0;
176 #endif
177 }
178
179 int 
180 ldap_pvt_thread_yield( void )
181 {
182 #ifdef _POSIX_THREAD_IS_GNU_PTH
183         sched_yield();
184         return 0;
185
186 #elif HAVE_SCHED_YIELD
187         return sched_yield();
188
189 #elif HAVE_PTHREAD_YIELD
190 #if HAVE_PTHREAD_OS390
191         pthread_yield(NULL);
192 #else
193         pthread_yield();
194 #endif
195         return 0;
196
197 #elif HAVE_THR_YIELD
198         return thr_yield();
199
200 #else
201         return 0;
202 #endif   
203 }
204
205 int 
206 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
207 {
208         return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
209 }
210
211 int 
212 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
213 {
214         return pthread_cond_destroy( cond );
215 }
216         
217 int 
218 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
219 {
220         return pthread_cond_signal( cond );
221 }
222
223 int
224 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
225 {
226         return pthread_cond_broadcast( cond );
227 }
228
229 int 
230 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
231                       ldap_pvt_thread_mutex_t *mutex )
232 {
233         return pthread_cond_wait( cond, mutex );
234 }
235
236 int 
237 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
238 {
239         return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
240 }
241
242 int 
243 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
244 {
245         return pthread_mutex_destroy( mutex );
246 }
247
248 int 
249 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
250 {
251         return pthread_mutex_lock( mutex );
252 }
253
254 int 
255 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
256 {
257         return pthread_mutex_trylock( mutex );
258 }
259
260 int 
261 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
262 {
263         return pthread_mutex_unlock( mutex );
264 }
265
266 #ifdef LDAP_THREAD_HAVE_RDWR
267 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
268 int 
269 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
270 {
271         return pthread_rwlock_init( rw, NULL );
272 }
273
274 int 
275 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
276 {
277         return pthread_rwlock_destroy( rw );
278 }
279
280 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
281 {
282         return pthread_rwlock_rdlock( rw );
283 }
284
285 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
286 {
287         return pthread_rwlock_tryrdlock( rw );
288 }
289
290 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
291 {
292         return pthread_rwlock_unlock( rw );
293 }
294
295 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
296 {
297         return pthread_rwlock_wrlock( rw );
298 }
299
300 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
301 {
302         return pthread_rwlock_trywrlock( rw );
303 }
304
305 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
306 {
307         return pthread_rwlock_unlock( rw );
308 }
309
310 #endif /* HAVE_PTHREAD_RDLOCK_DESTROY */
311 #endif /* LDAP_THREAD_HAVE_RDWR */
312 #endif /* HAVE_PTHREADS */
313