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