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