]> git.sur5r.net Git - openldap/blob - libraries/liblthread/thread.c
Protoized, moved extern definitions to .h files, fixed related bugs.
[openldap] / libraries / liblthread / thread.c
1 /* thread.c - glue routines to provide a consistent thread interface */
2
3 #include "portable.h"
4
5 #include <lthread.h>
6
7 #if defined( HAVE_PTHREADS )
8
9 #ifndef HAVE_PTHREAD_KILL
10 /***********************************************************************
11  *                                                                     *
12  * pthreads package with DCE - no mapping to do (except to create a    *
13  * pthread_kill() routine)                                             *
14  *                                                                     *
15  ***********************************************************************/
16
17 /* ARGSUSED */
18 void
19 pthread_kill( pthread_t tid, int sig )
20 {
21         kill( getpid(), sig );
22 }
23 #endif /* HAVE_PTHREAD_KILL */
24
25 #elif defined( HAVE_MACH_CTHREADS )
26
27 /***********************************************************************
28  *                                                                     *
29  * under NEXTSTEP or OPENSTEP use CThreads                             *
30  * lukeh@xedoc.com.au                                                  *
31  *                                                                     *
32  ***********************************************************************/
33
34 int
35 pthread_attr_init( pthread_attr_t *attr )
36 {
37         *attr = 0;
38         return( 0 );
39 }
40
41 int
42 pthread_attr_destroy( pthread_attr_t *attr )
43 {
44         return( 0 );
45 }
46
47 int
48 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
49 {
50         *detachstate = *attr;
51         return( 0 );
52 }
53
54 int
55 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
56 {
57         *attr = detachstate;
58         return( 0 );
59 }
60
61 /* ARGSUSED */
62 int
63 pthread_create(
64     pthread_t           *tid,
65     pthread_attr_t      *attr,
66     VFP                 func,
67     void                *arg
68 )
69 {
70         *tid = cthread_fork(func, arg);
71          return ( *tid == NULL ? -1 : 0 );
72 }
73
74 void
75 pthread_yield( void )
76 {
77         cthread_yield();
78 }
79
80 void
81 pthread_exit( any_t a )
82 {
83         cthread_exit( a );
84 }
85
86 void
87 pthread_join( pthread_t tid, int *pStatus )
88 {
89         int status;
90         status = (int) cthread_join ( tid );
91         if (pStatus != NULL)
92                 {
93                 *pStatus = status;
94                 }
95 }
96
97 /* ARGSUSED */
98 void
99 pthread_kill( pthread_t tid, int sig )
100 {
101         return;
102 }
103
104 /* ARGSUSED */
105 int
106 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
107 {
108         mutex_init( mp );
109         mp->name = NULL;
110         return ( 0 );
111 }
112
113 int
114 pthread_mutex_destroy( pthread_mutex_t *mp )
115 {
116         mutex_clear( mp );
117         return ( 0 );
118 }
119
120 int
121 pthread_mutex_lock( pthread_mutex_t *mp )
122 {
123         mutex_lock( mp );
124         return ( 0 );
125 }
126
127 int
128 pthread_mutex_unlock( pthread_mutex_t *mp )
129 {
130         mutex_unlock( mp );
131         return ( 0 );
132 }
133
134 int
135 pthread_mutex_trylock( pthread_mutex_t *mp )
136 {
137         return mutex_try_lock( mp );
138 }
139
140 int
141 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
142 {
143         condition_init( cv );
144         return( 0 );
145 }
146
147 int
148 pthread_cond_destroy( pthread_cond_t *cv )
149 {
150         condition_clear( cv );
151         return( 0 );
152 }
153
154 int
155 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
156 {
157         condition_wait( cv, mp );
158         return( 0 );
159 }
160
161 int
162 pthread_cond_signal( pthread_cond_t *cv )
163 {
164         condition_signal( cv );
165         return( 0 );
166 }
167
168 int
169 pthread_cond_broadcast( pthread_cond_t *cv )
170 {
171         condition_broadcast( cv );
172         return( 0 );
173 }
174
175 #elif defined( HAVE_THR )
176
177 /*******************
178  *                 *
179  * Solaris Threads *
180  *                 *
181  *******************/
182
183 #if !defined(__SunOS_5_6)
184 int
185 pthread_attr_init( pthread_attr_t *attr )
186 {
187         *attr = 0;
188         return( 0 );
189 }
190
191 int
192 pthread_attr_destroy( pthread_attr_t *attr )
193 {
194         *attr = 0;
195         return( 0 );
196 }
197
198 int
199 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
200 {
201         *detachstate = *attr;
202         return( 0 );
203 }
204
205 int
206 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
207 {
208         *attr = detachstate;
209         return( 0 );
210 }
211
212 /* ARGSUSED */
213 int
214 pthread_create(
215     pthread_t           *tid,
216     pthread_attr_t      *attr,
217     VFP                 func,
218     void                *arg
219 )
220 {
221         return( thr_create( NULL, 0, func, arg, *attr, tid ) );
222 }
223 #endif /* ! sunos56 */
224
225 void
226 pthread_yield( void )
227 {
228         thr_yield();
229 }
230
231 #if !defined(__SunOS_5_6)
232 void
233 pthread_exit()
234 {
235         thr_exit( NULL );
236 }
237
238 void
239 pthread_join( pthread_t tid, int *status )
240 {
241         thr_join( tid, NULL, (void **) status );
242 }
243
244 void
245 pthread_kill( pthread_t tid, int sig )
246 {
247         thr_kill( tid, sig );
248 }
249
250 /* ARGSUSED */
251 int
252 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
253 {
254         return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
255 }
256
257 int
258 pthread_mutex_destroy( pthread_mutex_t *mp )
259 {
260         return( mutex_destroy( mp ) );
261 }
262
263 int
264 pthread_mutex_lock( pthread_mutex_t *mp )
265 {
266         return( mutex_lock( mp ) );
267 }
268
269 int
270 pthread_mutex_unlock( pthread_mutex_t *mp )
271 {
272         return( mutex_unlock( mp ) );
273 }
274
275 int
276 pthread_mutex_trylock( pthread_mutex_t *mp )
277 {
278         return( mutex_trylock( mp ) );
279 }
280
281 int
282 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
283 {
284         return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
285 }
286
287 int
288 pthread_cond_destroy( pthread_cond_t *cv )
289 {
290         return( cond_destroy( cv ) );
291 }
292
293 int
294 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
295 {
296         return( cond_wait( cv, mp ) );
297 }
298
299 int
300 pthread_cond_signal( pthread_cond_t *cv )
301 {
302         return( cond_signal( cv ) );
303 }
304
305 int
306 pthread_cond_broadcast( pthread_cond_t *cv )
307 {
308         return( cond_broadcast( cv ) );
309 }
310 #endif /* ! sunos56 */
311
312 #elif defined( HAVE_LWP )
313
314 /*************
315  *           *
316  * SunOS LWP *
317  *           *
318  *************/
319
320 int
321 pthread_attr_init( pthread_attr_t *attr )
322 {
323         *attr = 0;
324         return( 0 );
325 }
326
327 int
328 pthread_attr_destroy( pthread_attr_t *attr )
329 {
330         return( 0 );
331 }
332
333 int
334 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
335 {
336         *detachstate = *attr;
337         return( 0 );
338 }
339
340 int
341 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
342 {
343         *attr = detachstate;
344         return( 0 );
345 }
346
347 static void
348 lwp_create_stack( VFP func, void *arg, int stackno )
349 {
350         (*func)( arg );
351
352         free_stack( stackno );
353 }
354
355 /* ARGSUSED */
356 int
357 pthread_create(
358     pthread_t           *tid,
359     pthread_attr_t      *attr,
360     VFP                 func,
361     void                *arg
362 )
363 {
364         stkalign_t      *stack;
365         int             stackno;
366
367         if ( (stack = get_stack( &stackno )) == NULL ) {
368                 return( -1 );
369         }
370         return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
371             arg, stackno ) );
372 }
373
374 void
375 pthread_yield( void )
376 {
377         lwp_yield( SELF );
378 }
379
380 void
381 pthread_exit()
382 {
383         lwp_destroy( SELF );
384 }
385
386 void
387 pthread_join( pthread_t tid, int *status )
388 {
389         lwp_join( tid );
390 }
391
392 /* ARGSUSED */
393 void
394 pthread_kill( pthread_t tid, int sig )
395 {
396         return;
397 }
398
399 /* ARGSUSED */
400 int
401 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
402 {
403         return( mon_create( mp ) );
404 }
405
406 int
407 pthread_mutex_destroy( pthread_mutex_t *mp )
408 {
409         return( mon_destroy( *mp ) );
410 }
411
412 int
413 pthread_mutex_lock( pthread_mutex_t *mp )
414 {
415         return( mon_enter( *mp ) );
416 }
417
418 int
419 pthread_mutex_unlock( pthread_mutex_t *mp )
420 {
421         return( mon_exit( *mp ) );
422 }
423
424 int
425 pthread_mutex_trylock( pthread_mutex_t *mp )
426 {
427         return( mon_cond_enter( *mp ) );
428 }
429
430 int
431 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
432 {
433         /*
434          * lwp cv_create requires the monitor id be passed in
435          * when the cv is created, pthreads passes it when the
436          * condition is waited for.  so, we fake the creation
437          * here and actually do it when the cv is waited for
438          * later.
439          */
440
441         cv->lcv_created = 0;
442
443         return( 0 );
444 }
445
446 int
447 pthread_cond_destroy( pthread_cond_t *cv )
448 {
449         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
450 }
451
452 int
453 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
454 {
455         if ( ! cv->lcv_created ) {
456                 cv_create( &cv->lcv_cv, *mp );
457                 cv->lcv_created = 1;
458         }
459
460         return( cv_wait( cv->lcv_cv ) );
461 }
462
463 int
464 pthread_cond_signal( pthread_cond_t *cv )
465 {
466         return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
467 }
468
469 int
470 pthread_cond_broadcast( pthread_cond_t *cv )
471 {
472         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
473 }
474
475
476 #else
477
478 /***********************************************************************
479  *                                                                     *
480  * no threads package defined for this system - fake ok returns from   *
481  * all threads routines (making it single-threaded).                   *
482  *                                                                     *
483  ***********************************************************************/
484
485 /* ARGSUSED */
486 int
487 pthread_attr_init( pthread_attr_t *attr )
488 {
489         return( 0 );
490 }
491
492 /* ARGSUSED */
493 int
494 pthread_attr_destroy( pthread_attr_t *attr )
495 {
496         return( 0 );
497 }
498
499 /* ARGSUSED */
500 int
501 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
502 {
503         return( 0 );
504 }
505
506 /* ARGSUSED */
507 int
508 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
509 {
510         return( 0 );
511 }
512
513 /* ARGSUSED */
514 int
515 pthread_create(
516     pthread_t           *tid,
517     pthread_attr_t      *attr,
518     VFP                 func,
519     void                *arg
520 )
521 {
522         (*func)( arg );
523
524         return( 0 );
525 }
526
527 void
528 pthread_yield( void )
529 {
530         return;
531 }
532
533 void
534 pthread_exit()
535 {
536         return;
537 }
538
539 /* ARGSUSED */
540 void
541 pthread_kill( pthread_t tid, int sig )
542 {
543         return;
544 }
545
546 void
547 pthread_join( pthread_t tid, int *status )
548 {
549         return;
550 }
551
552 /* ARGSUSED */
553 int
554 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
555 {
556         return( 0 );
557 }
558
559 /* ARGSUSED */
560 int
561 pthread_mutex_destroy( pthread_mutex_t *mp )
562 {
563         return( 0 );
564 }
565
566 /* ARGSUSED */
567 int
568 pthread_mutex_lock( pthread_mutex_t *mp )
569 {
570         return( 0 );
571 }
572
573 /* ARGSUSED */
574 int
575 pthread_mutex_unlock( pthread_mutex_t *mp )
576 {
577         return( 0 );
578 }
579
580 /* ARGSUSED */
581 int
582 pthread_mutex_trylock( pthread_mutex_t *mp )
583 {
584         return( 0 );
585 }
586
587 /* ARGSUSED */
588 int
589 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
590 {
591         return( 0 );
592 }
593
594 /* ARGSUSED */
595 int
596 pthread_cond_destroy( pthread_cond_t *cv )
597 {
598         return( 0 );
599 }
600
601 /* ARGSUSED */
602 int
603 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
604 {
605         return( 0 );
606 }
607
608 /* ARGSUSED */
609 int
610 pthread_cond_signal( pthread_cond_t *cv )
611 {
612         return( 0 );
613 }
614
615 /* ARGSUSED */
616 int
617 pthread_cond_broadcast( pthread_cond_t *cv )
618 {
619         return( 0 );
620 }
621
622 #endif /* no threads package */