]> git.sur5r.net Git - openldap/blob - libraries/liblthread/thread.c
Fixed liblber ber_get_next trickle bug (ITS#2490)
[openldap] / libraries / liblthread / thread.c
1 /* thread.c - glue routines to provide a consistent thread interface */
2 #include <stdio.h>
3 #include "lthread.h"
4
5 #if defined( THREAD_SUNOS4_LWP )
6
7 /***********************************************************************
8  *                                                                     *
9  * under sunos 4 - use the built in non-preemptive lwp threads package *
10  *                                                                     *
11  ***********************************************************************/
12
13 extern stkalign_t       *get_stack();
14 static void             lwp_create_stack();
15
16 int
17 pthread_attr_init( pthread_attr_t *attr )
18 {
19         *attr = 0;
20         return( 0 );
21 }
22
23 int
24 pthread_attr_destroy( pthread_attr_t *attr )
25 {
26         return( 0 );
27 }
28
29 int
30 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
31 {
32         *detachstate = *attr;
33         return( 0 );
34 }
35
36 int
37 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
38 {
39         *attr = detachstate;
40         return( 0 );
41 }
42
43 /* ARGSUSED */
44 int
45 pthread_create(
46     pthread_t           *tid,
47     pthread_attr_t      attr,
48     VFP                 func,
49     void                *arg
50 )
51 {
52         stkalign_t      *stack;
53         int             stackno;
54
55         if ( (stack = get_stack( &stackno )) == NULL ) {
56                 return( -1 );
57         }
58         return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
59             arg, stackno ) );
60 }
61
62 static void
63 lwp_create_stack( VFP func, void *arg, int stackno )
64 {
65         (*func)( arg );
66
67         free_stack( stackno );
68 }
69
70 void
71 pthread_yield()
72 {
73         lwp_yield( SELF );
74 }
75
76 void
77 pthread_exit()
78 {
79         lwp_destroy( SELF );
80 }
81
82 void
83 pthread_join( pthread_t tid, int *status )
84 {
85         lwp_join( tid );
86 }
87
88 /* ARGSUSED */
89 void
90 pthread_kill( pthread_t tid, int sig )
91 {
92         return;
93 }
94
95 /* ARGSUSED */
96 int
97 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
98 {
99         return( mon_create( mp ) );
100 }
101
102 int
103 pthread_mutex_destroy( pthread_mutex_t *mp )
104 {
105         return( mon_destroy( *mp ) );
106 }
107
108 int
109 pthread_mutex_lock( pthread_mutex_t *mp )
110 {
111         return( mon_enter( *mp ) );
112 }
113
114 int
115 pthread_mutex_unlock( pthread_mutex_t *mp )
116 {
117         return( mon_exit( *mp ) );
118 }
119
120 int
121 pthread_mutex_trylock( pthread_mutex_t *mp )
122 {
123         return( mon_cond_enter( *mp ) );
124 }
125
126 int
127 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
128 {
129         /*
130          * lwp cv_create requires the monitor id be passed in
131          * when the cv is created, pthreads passes it when the
132          * condition is waited for.  so, we fake the creation
133          * here and actually do it when the cv is waited for
134          * later.
135          */
136
137         cv->lcv_created = 0;
138
139         return( 0 );
140 }
141
142 int
143 pthread_cond_destroy( pthread_cond_t *cv )
144 {
145         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
146 }
147
148 int
149 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
150 {
151         if ( ! cv->lcv_created ) {
152                 cv_create( &cv->lcv_cv, *mp );
153                 cv->lcv_created = 1;
154         }
155
156         return( cv_wait( cv->lcv_cv ) );
157 }
158
159 int
160 pthread_cond_signal( pthread_cond_t *cv )
161 {
162         return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
163 }
164
165 int
166 pthread_cond_broadcast( pthread_cond_t *cv )
167 {
168         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
169 }
170
171 #else /* end sunos4 */
172
173 #  if defined( THREAD_SUNOS5_LWP )
174
175 /***********************************************************************
176  *                                                                     *
177  * under sunos 5 - use the built in preemptive solaris threads package *
178  *                                                                     *
179  ***********************************************************************/
180
181 int
182 pthread_attr_init( pthread_attr_t *attr )
183 {
184         *attr = 0;
185         return( 0 );
186 }
187
188 int
189 pthread_attr_destroy( pthread_attr_t *attr )
190 {
191         *attr = 0;
192         return( 0 );
193 }
194
195 int
196 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
197 {
198         *detachstate = *attr;
199         return( 0 );
200 }
201
202 int
203 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
204 {
205         *attr = detachstate;
206         return( 0 );
207 }
208
209 /* ARGSUSED */
210 int
211 pthread_create(
212     pthread_t           *tid,
213     pthread_attr_t      attr,
214     VFP                 func,
215     void                *arg
216 )
217 {
218         return( thr_create( NULL, 0, func, arg, attr, tid ) );
219 }
220
221 void
222 pthread_yield()
223 {
224         thr_yield();
225 }
226
227 void
228 pthread_exit()
229 {
230         thr_exit( NULL );
231 }
232
233 void
234 pthread_join( pthread_t tid, int *status )
235 {
236         thr_join( tid, NULL, (void **) status );
237 }
238
239 void
240 pthread_kill( pthread_t tid, int sig )
241 {
242         thr_kill( tid, sig );
243 }
244
245 /* ARGSUSED */
246 int
247 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
248 {
249         return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
250 }
251
252 int
253 pthread_mutex_destroy( pthread_mutex_t *mp )
254 {
255         return( mutex_destroy( mp ) );
256 }
257
258 int
259 pthread_mutex_lock( pthread_mutex_t *mp )
260 {
261         return( mutex_lock( mp ) );
262 }
263
264 int
265 pthread_mutex_unlock( pthread_mutex_t *mp )
266 {
267         return( mutex_unlock( mp ) );
268 }
269
270 int
271 pthread_mutex_trylock( pthread_mutex_t *mp )
272 {
273         return( mutex_trylock( mp ) );
274 }
275
276 int
277 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
278 {
279         return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
280 }
281
282 int
283 pthread_cond_destroy( pthread_cond_t *cv )
284 {
285         return( cond_destroy( cv ) );
286 }
287
288 int
289 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
290 {
291         return( cond_wait( cv, mp ) );
292 }
293
294 int
295 pthread_cond_signal( pthread_cond_t *cv )
296 {
297         return( cond_signal( cv ) );
298 }
299
300 int
301 pthread_cond_broadcast( pthread_cond_t *cv )
302 {
303         return( cond_broadcast( cv ) );
304 }
305
306
307 #else /* end sunos5 threads */
308
309 #if defined( THREAD_MIT_PTHREADS )
310
311 /***********************************************************************
312  *                                                                     *
313  * pthreads package by Chris Provenzano of MIT - provides all the      *
314  * pthreads calls already, so no mapping to do                         *
315  *                                                                     *
316  ***********************************************************************/
317
318 #else /* end mit pthreads */
319
320 #if defined( THREAD_DCE_PTHREADS )
321
322 /***********************************************************************
323  *                                                                     *
324  * pthreads package with DCE - no mapping to do (except to create a    *
325  * pthread_kill() routine)                                             *
326  *                                                                     *
327  ***********************************************************************/
328
329 /* ARGSUSED */
330 void
331 pthread_kill( pthread_t tid, int sig )
332 {
333         kill( getpid(), sig );
334 }
335
336 #endif /* dce pthreads */
337 #endif /* mit pthreads */
338 #endif /* sunos5 lwp */
339 #endif /* sunos4 lwp */
340
341 #ifndef _THREAD
342
343 /***********************************************************************
344  *                                                                     *
345  * no threads package defined for this system - fake ok returns from   *
346  * all threads routines (making it single-threaded).                   *
347  *                                                                     *
348  ***********************************************************************/
349
350 /* ARGSUSED */
351 int
352 pthread_attr_init( pthread_attr_t *attr )
353 {
354         return( 0 );
355 }
356
357 /* ARGSUSED */
358 int
359 pthread_attr_destroy( pthread_attr_t *attr )
360 {
361         return( 0 );
362 }
363
364 /* ARGSUSED */
365 int
366 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
367 {
368         return( 0 );
369 }
370
371 /* ARGSUSED */
372 int
373 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
374 {
375         return( 0 );
376 }
377
378 /* ARGSUSED */
379 int
380 pthread_create(
381     pthread_t           *tid,
382     pthread_attr_t      attr,
383     VFP                 func,
384     void                *arg
385 )
386 {
387         (*func)( arg );
388
389         return( 0 );
390 }
391
392 void
393 pthread_yield()
394 {
395         return;
396 }
397
398 void
399 pthread_exit()
400 {
401         return;
402 }
403
404 /* ARGSUSED */
405 void
406 pthread_kill( pthread_t tid, int sig )
407 {
408         return;
409 }
410
411 void
412 pthread_join( pthread_t tid, int *status )
413 {
414         return;
415 }
416
417 /* ARGSUSED */
418 int
419 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
420 {
421         return( 0 );
422 }
423
424 /* ARGSUSED */
425 int
426 pthread_mutex_destroy( pthread_mutex_t *mp )
427 {
428         return( 0 );
429 }
430
431 /* ARGSUSED */
432 int
433 pthread_mutex_lock( pthread_mutex_t *mp )
434 {
435         return( 0 );
436 }
437
438 /* ARGSUSED */
439 int
440 pthread_mutex_unlock( pthread_mutex_t *mp )
441 {
442         return( 0 );
443 }
444
445 /* ARGSUSED */
446 int
447 pthread_mutex_trylock( pthread_mutex_t *mp )
448 {
449         return( 0 );
450 }
451
452 /* ARGSUSED */
453 int
454 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
455 {
456         return( 0 );
457 }
458
459 /* ARGSUSED */
460 int
461 pthread_cond_destroy( pthread_cond_t *cv )
462 {
463         return( 0 );
464 }
465
466 /* ARGSUSED */
467 int
468 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
469 {
470         return( 0 );
471 }
472
473 /* ARGSUSED */
474 int
475 pthread_cond_signal( pthread_cond_t *cv )
476 {
477         return( 0 );
478 }
479
480 /* ARGSUSED */
481 int
482 pthread_cond_broadcast( pthread_cond_t *cv )
483 {
484         return( 0 );
485 }
486
487 #endif /* no threads package */