]> git.sur5r.net Git - openldap/blob - libraries/liblthread/thread.c
ignore SIGPIPE
[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  * Some pthreads packages (ie: DCE) don't have pthread_kill()
12  * pthread_kill() routine)
13  *
14  */
15
16 /* ARGSUSED */
17 void
18 pthread_kill( pthread_t tid, int sig )
19 {
20         kill( getpid(), sig );
21 }
22 #endif /* HAVE_PTHREAD_KILL */
23
24 #if !defined(HAVE_SCHED_YIELD) && !defined(HAVE_PTHREAD_YIELD)
25 /*
26  * Some pthreads packages don't have sched_yield() nor
27  * the draft4 pthread_kill() routine, assume it's not
28  * needed.
29  */
30 void
31 pthread_yield( void )
32 {
33         /* assume pthread implementation is preemptive */
34 }
35 #endif /* missing sched_yield() */
36
37 #elif defined( HAVE_MACH_CTHREADS )
38
39 /***********************************************************************
40  *                                                                     *
41  * under NEXTSTEP or OPENSTEP use CThreads                             *
42  * lukeh@xedoc.com.au                                                  *
43  *                                                                     *
44  ***********************************************************************/
45
46 int
47 pthread_attr_init( pthread_attr_t *attr )
48 {
49         *attr = 0;
50         return( 0 );
51 }
52
53 int
54 pthread_attr_destroy( pthread_attr_t *attr )
55 {
56         return( 0 );
57 }
58
59 int
60 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
61 {
62         *detachstate = *attr;
63         return( 0 );
64 }
65
66 int
67 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
68 {
69         *attr = detachstate;
70         return( 0 );
71 }
72
73 /* ARGSUSED */
74 int
75 pthread_create(
76     pthread_t           *tid,
77     pthread_attr_t      *attr,
78     VFP                 func,
79     void                *arg
80 )
81 {
82         *tid = cthread_fork(func, arg);
83          return ( *tid == NULL ? -1 : 0 );
84 }
85
86 void
87 pthread_yield( void )
88 {
89         cthread_yield();
90 }
91
92 void
93 pthread_exit( any_t a )
94 {
95         cthread_exit( a );
96 }
97
98 void
99 pthread_join( pthread_t tid, int *pStatus )
100 {
101         int status;
102         status = (int) cthread_join ( tid );
103         if (pStatus != NULL)
104                 {
105                 *pStatus = status;
106                 }
107 }
108
109 /* ARGSUSED */
110 void
111 pthread_kill( pthread_t tid, int sig )
112 {
113         return;
114 }
115
116 /* ARGSUSED */
117 int
118 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
119 {
120         mutex_init( mp );
121         mp->name = NULL;
122         return ( 0 );
123 }
124
125 int
126 pthread_mutex_destroy( pthread_mutex_t *mp )
127 {
128         mutex_clear( mp );
129         return ( 0 );
130 }
131
132 int
133 pthread_mutex_lock( pthread_mutex_t *mp )
134 {
135         mutex_lock( mp );
136         return ( 0 );
137 }
138
139 int
140 pthread_mutex_unlock( pthread_mutex_t *mp )
141 {
142         mutex_unlock( mp );
143         return ( 0 );
144 }
145
146 int
147 pthread_mutex_trylock( pthread_mutex_t *mp )
148 {
149         return mutex_try_lock( mp );
150 }
151
152 int
153 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
154 {
155         condition_init( cv );
156         return( 0 );
157 }
158
159 int
160 pthread_cond_destroy( pthread_cond_t *cv )
161 {
162         condition_clear( cv );
163         return( 0 );
164 }
165
166 int
167 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
168 {
169         condition_wait( cv, mp );
170         return( 0 );
171 }
172
173 int
174 pthread_cond_signal( pthread_cond_t *cv )
175 {
176         condition_signal( cv );
177         return( 0 );
178 }
179
180 int
181 pthread_cond_broadcast( pthread_cond_t *cv )
182 {
183         condition_broadcast( cv );
184         return( 0 );
185 }
186
187 #elif defined( HAVE_THR )
188
189 /*******************
190  *                 *
191  * Solaris Threads *
192  *                 *
193  *******************/
194
195 int
196 pthread_attr_init( pthread_attr_t *attr )
197 {
198         *attr = 0;
199         return( 0 );
200 }
201
202 int
203 pthread_attr_destroy( pthread_attr_t *attr )
204 {
205         *attr = 0;
206         return( 0 );
207 }
208
209 int
210 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
211 {
212         *detachstate = *attr;
213         return( 0 );
214 }
215
216 int
217 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
218 {
219         *attr = detachstate;
220         return( 0 );
221 }
222
223 /* ARGSUSED */
224 int
225 pthread_create(
226     pthread_t           *tid,
227     pthread_attr_t      *attr,
228     VFP                 func,
229     void                *arg
230 )
231 {
232         return( thr_create( NULL, 0, func, arg, *attr, tid ) );
233 }
234
235 void
236 pthread_yield( void )
237 {
238         thr_yield();
239 }
240
241 void
242 pthread_exit()
243 {
244         thr_exit( NULL );
245 }
246
247 void
248 pthread_join( pthread_t tid, int *status )
249 {
250         thr_join( tid, NULL, (void **) status );
251 }
252
253 void
254 pthread_kill( pthread_t tid, int sig )
255 {
256         thr_kill( tid, sig );
257 }
258
259 /* ARGSUSED */
260 int
261 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
262 {
263         return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
264 }
265
266 int
267 pthread_mutex_destroy( pthread_mutex_t *mp )
268 {
269         return( mutex_destroy( mp ) );
270 }
271
272 int
273 pthread_mutex_lock( pthread_mutex_t *mp )
274 {
275         return( mutex_lock( mp ) );
276 }
277
278 int
279 pthread_mutex_unlock( pthread_mutex_t *mp )
280 {
281         return( mutex_unlock( mp ) );
282 }
283
284 int
285 pthread_mutex_trylock( pthread_mutex_t *mp )
286 {
287         return( mutex_trylock( mp ) );
288 }
289
290 int
291 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
292 {
293         return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
294 }
295
296 int
297 pthread_cond_destroy( pthread_cond_t *cv )
298 {
299         return( cond_destroy( cv ) );
300 }
301
302 int
303 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
304 {
305         return( cond_wait( cv, mp ) );
306 }
307
308 int
309 pthread_cond_signal( pthread_cond_t *cv )
310 {
311         return( cond_signal( cv ) );
312 }
313
314 int
315 pthread_cond_broadcast( pthread_cond_t *cv )
316 {
317         return( cond_broadcast( cv ) );
318 }
319
320 #elif defined( HAVE_LWP )
321
322 /*************
323  *           *
324  * SunOS LWP *
325  *           *
326  *************/
327
328 int
329 pthread_attr_init( pthread_attr_t *attr )
330 {
331         *attr = 0;
332         return( 0 );
333 }
334
335 int
336 pthread_attr_destroy( pthread_attr_t *attr )
337 {
338         return( 0 );
339 }
340
341 int
342 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
343 {
344         *detachstate = *attr;
345         return( 0 );
346 }
347
348 int
349 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
350 {
351         *attr = detachstate;
352         return( 0 );
353 }
354
355 static void
356 lwp_create_stack( VFP func, void *arg, int stackno )
357 {
358         (*func)( arg );
359
360         free_stack( stackno );
361 }
362
363 /* ARGSUSED */
364 int
365 pthread_create(
366     pthread_t           *tid,
367     pthread_attr_t      *attr,
368     VFP                 func,
369     void                *arg
370 )
371 {
372         stkalign_t      *stack;
373         int             stackno;
374
375         if ( (stack = get_stack( &stackno )) == NULL ) {
376                 return( -1 );
377         }
378         return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
379             arg, stackno ) );
380 }
381
382 void
383 pthread_yield( void )
384 {
385         lwp_yield( SELF );
386 }
387
388 void
389 pthread_exit()
390 {
391         lwp_destroy( SELF );
392 }
393
394 void
395 pthread_join( pthread_t tid, int *status )
396 {
397         lwp_join( tid );
398 }
399
400 /* ARGSUSED */
401 void
402 pthread_kill( pthread_t tid, int sig )
403 {
404         return;
405 }
406
407 /* ARGSUSED */
408 int
409 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
410 {
411         return( mon_create( mp ) );
412 }
413
414 int
415 pthread_mutex_destroy( pthread_mutex_t *mp )
416 {
417         return( mon_destroy( *mp ) );
418 }
419
420 int
421 pthread_mutex_lock( pthread_mutex_t *mp )
422 {
423         return( mon_enter( *mp ) );
424 }
425
426 int
427 pthread_mutex_unlock( pthread_mutex_t *mp )
428 {
429         return( mon_exit( *mp ) );
430 }
431
432 int
433 pthread_mutex_trylock( pthread_mutex_t *mp )
434 {
435         return( mon_cond_enter( *mp ) );
436 }
437
438 int
439 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
440 {
441         /*
442          * lwp cv_create requires the monitor id be passed in
443          * when the cv is created, pthreads passes it when the
444          * condition is waited for.  so, we fake the creation
445          * here and actually do it when the cv is waited for
446          * later.
447          */
448
449         cv->lcv_created = 0;
450
451         return( 0 );
452 }
453
454 int
455 pthread_cond_destroy( pthread_cond_t *cv )
456 {
457         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
458 }
459
460 int
461 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
462 {
463         if ( ! cv->lcv_created ) {
464                 cv_create( &cv->lcv_cv, *mp );
465                 cv->lcv_created = 1;
466         }
467
468         return( cv_wait( cv->lcv_cv ) );
469 }
470
471 int
472 pthread_cond_signal( pthread_cond_t *cv )
473 {
474         return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
475 }
476
477 int
478 pthread_cond_broadcast( pthread_cond_t *cv )
479 {
480         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
481 }
482
483
484 #elif defined( HAVE_NT_THREADS )
485
486 int
487 pthread_attr_init( pthread_attr_t *attr )
488 {
489         *attr = 0;
490         return( 0 );
491 }
492
493 int
494 pthread_attr_destroy( pthread_attr_t *attr )
495 {
496         return( 0 );
497 }
498
499 int
500 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
501 {
502         *detachstate = *attr;
503         return( 0 );
504 }
505
506 int
507 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
508 {
509         *attr = detachstate;
510         return( 0 );
511 }
512
513 int
514 pthread_create(
515     pthread_t           *tid,
516     pthread_attr_t      *attr,
517     VFP                 func,
518     void                *arg
519 )
520 {
521         *tid = (pthread_t)_beginthread( (void *) func, 0, arg );
522          return ( (unsigned long)*tid == -1 ? -1 : 0 );
523 }
524
525 void
526 pthread_yield()
527 {
528
529 }
530
531 void
532 pthread_exit( void )
533 {
534         _endthread( );
535 }
536
537 void
538 pthread_join( pthread_t tid, int *pStatus )
539 {
540         DWORD status;
541         status = WaitForSingleObject( tid, INFINITE );
542         if ( pStatus != NULL)
543         {
544                 if ( status != WAIT_FAILED )
545                         *pStatus = 0;
546                 else
547                         *pStatus = WAIT_ABANDONED;
548         }
549 }
550
551
552 void
553 pthread_kill( pthread_t tid, int sig )
554 {
555         return;
556 }
557
558
559 int
560 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
561 {
562         *mp = CreateMutex( NULL, 0, NULL );
563         return ( 0 );
564 }
565
566 int
567 pthread_mutex_destroy( pthread_mutex_t *mp )
568 {
569         CloseHandle( *mp );
570         return ( 0 );
571 }
572
573 int
574 pthread_mutex_lock( pthread_mutex_t *mp )
575 {
576         WaitForSingleObject( *mp, INFINITE );
577         return ( 0 );
578 }
579
580 int
581 pthread_mutex_unlock( pthread_mutex_t *mp )
582 {
583         ReleaseMutex( *mp );
584         return ( 0 );
585 }
586
587 int
588 pthread_mutex_trylock( pthread_mutex_t *mp )
589 {
590         DWORD status;
591
592         status = WaitForSingleObject( *mp, 0 );
593         if ( (status == WAIT_FAILED) || (status == WAIT_TIMEOUT) )
594                 return 0;
595         else
596                 return 1;
597 }
598
599 int
600 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
601 {
602         *cv = CreateEvent( NULL, FALSE, FALSE, NULL );
603         return( 0 );
604 }
605
606 int
607 pthread_cond_destroy( pthread_cond_t *cv )
608 {
609         CloseHandle( *cv );
610         return( 0 );
611 }
612
613 int
614 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
615 {
616         ReleaseMutex( *mp );
617         WaitForSingleObject( *cv, INFINITE );
618         WaitForSingleObject( *mp, INFINITE );
619         return( 0 );
620 }
621
622 int
623 pthread_cond_signal( pthread_cond_t *cv )
624 {
625         SetEvent( *cv );
626         return( 0 );
627 }
628
629 int
630 pthread_cond_broadcast( pthread_cond_t *cv )
631 {
632         SetEvent( *cv );
633         return( 0 );
634 }
635
636 #else
637
638 /***********************************************************************
639  *                                                                     *
640  * no threads package defined for this system - fake ok returns from   *
641  * all threads routines (making it single-threaded).                   *
642  *                                                                     *
643  ***********************************************************************/
644
645 /* ARGSUSED */
646 int
647 pthread_attr_init( pthread_attr_t *attr )
648 {
649         return( 0 );
650 }
651
652 /* ARGSUSED */
653 int
654 pthread_attr_destroy( pthread_attr_t *attr )
655 {
656         return( 0 );
657 }
658
659 /* ARGSUSED */
660 int
661 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
662 {
663         return( 0 );
664 }
665
666 /* ARGSUSED */
667 int
668 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
669 {
670         return( 0 );
671 }
672
673 /* ARGSUSED */
674 int
675 pthread_create(
676     pthread_t           *tid,
677     pthread_attr_t      *attr,
678     VFP                 func,
679     void                *arg
680 )
681 {
682         (*func)( arg );
683
684         return( 0 );
685 }
686
687 void
688 pthread_yield( void )
689 {
690         return;
691 }
692
693 void
694 pthread_exit()
695 {
696         return;
697 }
698
699 /* ARGSUSED */
700 void
701 pthread_kill( pthread_t tid, int sig )
702 {
703         return;
704 }
705
706 void
707 pthread_join( pthread_t tid, int *status )
708 {
709         return;
710 }
711
712 /* ARGSUSED */
713 int
714 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
715 {
716         return( 0 );
717 }
718
719 /* ARGSUSED */
720 int
721 pthread_mutex_destroy( pthread_mutex_t *mp )
722 {
723         return( 0 );
724 }
725
726 /* ARGSUSED */
727 int
728 pthread_mutex_lock( pthread_mutex_t *mp )
729 {
730         return( 0 );
731 }
732
733 /* ARGSUSED */
734 int
735 pthread_mutex_unlock( pthread_mutex_t *mp )
736 {
737         return( 0 );
738 }
739
740 /* ARGSUSED */
741 int
742 pthread_mutex_trylock( pthread_mutex_t *mp )
743 {
744         return( 0 );
745 }
746
747 /* ARGSUSED */
748 int
749 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
750 {
751         return( 0 );
752 }
753
754 /* ARGSUSED */
755 int
756 pthread_cond_destroy( pthread_cond_t *cv )
757 {
758         return( 0 );
759 }
760
761 /* ARGSUSED */
762 int
763 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
764 {
765         return( 0 );
766 }
767
768 /* ARGSUSED */
769 int
770 pthread_cond_signal( pthread_cond_t *cv )
771 {
772         return( 0 );
773 }
774
775 /* ARGSUSED */
776 int
777 pthread_cond_broadcast( pthread_cond_t *cv )
778 {
779         return( 0 );
780 }
781
782 #endif /* no threads package */