]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_posix.c
Avoid premature free
[openldap] / libraries / libldap_r / thr_posix.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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) && defined(PTHREAD_CREATE_UNDETACHED)
87         pthread_attr_t attr;
88
89         pthread_attr_init(&attr);
90         if (!detach)
91                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
92
93         rtn = pthread_create( thread, &attr, start_routine, arg );
94 #else
95         rtn = pthread_create( thread, LDAP_INT_THREAD_ATTR_DEFAULT,
96                                   start_routine, arg );
97 #endif
98
99         if( detach ) {
100 #ifdef HAVE_PTHREADS_FINAL
101                 pthread_detach( *thread );
102 #else
103                 pthread_detach( thread );
104 #endif
105         }
106         return rtn;
107 }
108
109 void 
110 ldap_pvt_thread_exit( void *retval )
111 {
112         pthread_exit( retval );
113 }
114
115 int 
116 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
117 {
118 #if !defined( HAVE_PTHREADS_FINAL )
119         void *dummy;
120         if (thread_return==NULL)
121           thread_return=&dummy;
122 #endif  
123         return pthread_join( thread, thread_return );
124 }
125
126 int 
127 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
128 {
129 #ifdef HAVE_PTHREAD_KILL
130         return pthread_kill( thread, signo );
131 #else
132         /* pthread package with DCE */
133         if (kill( getpid(), signo )<0)
134                 return errno;
135         return 0;
136 #endif
137 }
138
139 int 
140 ldap_pvt_thread_yield( void )
141 {
142 #ifdef _POSIX_THREAD_IS_GNU_PTH
143         sched_yield();
144         return 0;
145
146 #elif HAVE_SCHED_YIELD
147         return sched_yield();
148
149 #elif HAVE_PTHREAD_YIELD
150         pthread_yield();
151         return 0;
152
153 #elif HAVE_THR_YIELD
154         return thr_yield();
155
156 #else
157         return 0;
158 #endif   
159 }
160
161 int 
162 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
163 {
164         return pthread_cond_init( cond, LDAP_INT_THREAD_CONDATTR_DEFAULT );
165 }
166
167 int 
168 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
169 {
170         return pthread_cond_destroy( cond );
171 }
172         
173 int 
174 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
175 {
176         return pthread_cond_signal( cond );
177 }
178
179 int
180 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
181 {
182         return pthread_cond_broadcast( cond );
183 }
184
185 int 
186 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
187                       ldap_pvt_thread_mutex_t *mutex )
188 {
189         return pthread_cond_wait( cond, mutex );
190 }
191
192 int 
193 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
194 {
195         return pthread_mutex_init( mutex, LDAP_INT_THREAD_MUTEXATTR_DEFAULT );
196 }
197
198 int 
199 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
200 {
201         return pthread_mutex_destroy( mutex );
202 }
203
204 int 
205 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
206 {
207         return pthread_mutex_lock( mutex );
208 }
209
210 int 
211 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
212 {
213         return pthread_mutex_trylock( mutex );
214 }
215
216 int 
217 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
218 {
219         return pthread_mutex_unlock( mutex );
220 }
221
222 #ifdef LDAP_THREAD_HAVE_RDWR
223 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
224 int 
225 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
226 {
227         return pthread_rwlock_init( rw, NULL );
228 }
229
230 int 
231 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
232 {
233         return pthread_rwlock_destroy( rw );
234 }
235
236 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
237 {
238         return pthread_rwlock_rdlock( rw );
239 }
240
241 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
242 {
243         return pthread_rwlock_tryrdlock( rw );
244 }
245
246 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
247 {
248         return pthread_rwlock_unlock( rw );
249 }
250
251 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
252 {
253         return pthread_rwlock_wrlock( rw );
254 }
255
256 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
257 {
258         return pthread_rwlock_trywrlock( rw );
259 }
260
261 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
262 {
263         return pthread_rwlock_unlock( rw );
264 }
265
266 #endif /* HAVE_PTHREAD_RDLOCK_DESTROY */
267 #endif /* LDAP_THREAD_HAVE_RDWR */
268 #endif /* HAVE_PTHREADS */
269