]> git.sur5r.net Git - openldap/blob - libraries/liblthread/thread.c
Modified build environment to correctly support bin,sbin,libexec,etc
[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 #include <sched.h>
495
496 void pthread_yield( void )
497 {
498         sched_yield();
499 }
500
501 #endif /* posix threads */
502 #endif /* dce pthreads */
503 #endif /* mit pthreads */
504 #endif /* sunos5 lwp */
505 #endif /* sunos4 lwp */
506
507 #ifndef _THREAD
508
509 /***********************************************************************
510  *                                                                     *
511  * no threads package defined for this system - fake ok returns from   *
512  * all threads routines (making it single-threaded).                   *
513  *                                                                     *
514  ***********************************************************************/
515
516 /* ARGSUSED */
517 int
518 pthread_attr_init( pthread_attr_t *attr )
519 {
520         return( 0 );
521 }
522
523 /* ARGSUSED */
524 int
525 pthread_attr_destroy( pthread_attr_t *attr )
526 {
527         return( 0 );
528 }
529
530 /* ARGSUSED */
531 int
532 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
533 {
534         return( 0 );
535 }
536
537 /* ARGSUSED */
538 int
539 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
540 {
541         return( 0 );
542 }
543
544 /* ARGSUSED */
545 int
546 pthread_create(
547     pthread_t           *tid,
548     pthread_attr_t      *attr,
549     VFP                 func,
550     void                *arg
551 )
552 {
553         (*func)( arg );
554
555         return( 0 );
556 }
557
558 void
559 pthread_yield()
560 {
561         return;
562 }
563
564 void
565 pthread_exit()
566 {
567         return;
568 }
569
570 /* ARGSUSED */
571 void
572 pthread_kill( pthread_t tid, int sig )
573 {
574         return;
575 }
576
577 void
578 pthread_join( pthread_t tid, int *status )
579 {
580         return;
581 }
582
583 /* ARGSUSED */
584 int
585 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
586 {
587         return( 0 );
588 }
589
590 /* ARGSUSED */
591 int
592 pthread_mutex_destroy( pthread_mutex_t *mp )
593 {
594         return( 0 );
595 }
596
597 /* ARGSUSED */
598 int
599 pthread_mutex_lock( pthread_mutex_t *mp )
600 {
601         return( 0 );
602 }
603
604 /* ARGSUSED */
605 int
606 pthread_mutex_unlock( pthread_mutex_t *mp )
607 {
608         return( 0 );
609 }
610
611 /* ARGSUSED */
612 int
613 pthread_mutex_trylock( pthread_mutex_t *mp )
614 {
615         return( 0 );
616 }
617
618 /* ARGSUSED */
619 int
620 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
621 {
622         return( 0 );
623 }
624
625 /* ARGSUSED */
626 int
627 pthread_cond_destroy( pthread_cond_t *cv )
628 {
629         return( 0 );
630 }
631
632 /* ARGSUSED */
633 int
634 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
635 {
636         return( 0 );
637 }
638
639 /* ARGSUSED */
640 int
641 pthread_cond_signal( pthread_cond_t *cv )
642 {
643         return( 0 );
644 }
645
646 /* ARGSUSED */
647 int
648 pthread_cond_broadcast( pthread_cond_t *cv )
649 {
650         return( 0 );
651 }
652
653 #endif /* no threads package */