]> git.sur5r.net Git - openldap/blob - libraries/liblthread/thread.c
The world compiles and links....
[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 #ifdef HAVE_DCE
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 /* DCE */
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()
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()
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 extern stkalign_t       *get_stack();
321 static void             lwp_create_stack();
322
323 int
324 pthread_attr_init( pthread_attr_t *attr )
325 {
326         *attr = 0;
327         return( 0 );
328 }
329
330 int
331 pthread_attr_destroy( pthread_attr_t *attr )
332 {
333         return( 0 );
334 }
335
336 int
337 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
338 {
339         *detachstate = *attr;
340         return( 0 );
341 }
342
343 int
344 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
345 {
346         *attr = detachstate;
347         return( 0 );
348 }
349
350 /* ARGSUSED */
351 int
352 pthread_create(
353     pthread_t           *tid,
354     pthread_attr_t      *attr,
355     VFP                 func,
356     void                *arg
357 )
358 {
359         stkalign_t      *stack;
360         int             stackno;
361
362         if ( (stack = get_stack( &stackno )) == NULL ) {
363                 return( -1 );
364         }
365         return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
366             arg, stackno ) );
367 }
368
369 static void
370 lwp_create_stack( VFP func, void *arg, int stackno )
371 {
372         (*func)( arg );
373
374         free_stack( stackno );
375 }
376
377 void
378 pthread_yield()
379 {
380         lwp_yield( SELF );
381 }
382
383 void
384 pthread_exit()
385 {
386         lwp_destroy( SELF );
387 }
388
389 void
390 pthread_join( pthread_t tid, int *status )
391 {
392         lwp_join( tid );
393 }
394
395 /* ARGSUSED */
396 void
397 pthread_kill( pthread_t tid, int sig )
398 {
399         return;
400 }
401
402 /* ARGSUSED */
403 int
404 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
405 {
406         return( mon_create( mp ) );
407 }
408
409 int
410 pthread_mutex_destroy( pthread_mutex_t *mp )
411 {
412         return( mon_destroy( *mp ) );
413 }
414
415 int
416 pthread_mutex_lock( pthread_mutex_t *mp )
417 {
418         return( mon_enter( *mp ) );
419 }
420
421 int
422 pthread_mutex_unlock( pthread_mutex_t *mp )
423 {
424         return( mon_exit( *mp ) );
425 }
426
427 int
428 pthread_mutex_trylock( pthread_mutex_t *mp )
429 {
430         return( mon_cond_enter( *mp ) );
431 }
432
433 int
434 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
435 {
436         /*
437          * lwp cv_create requires the monitor id be passed in
438          * when the cv is created, pthreads passes it when the
439          * condition is waited for.  so, we fake the creation
440          * here and actually do it when the cv is waited for
441          * later.
442          */
443
444         cv->lcv_created = 0;
445
446         return( 0 );
447 }
448
449 int
450 pthread_cond_destroy( pthread_cond_t *cv )
451 {
452         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
453 }
454
455 int
456 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
457 {
458         if ( ! cv->lcv_created ) {
459                 cv_create( &cv->lcv_cv, *mp );
460                 cv->lcv_created = 1;
461         }
462
463         return( cv_wait( cv->lcv_cv ) );
464 }
465
466 int
467 pthread_cond_signal( pthread_cond_t *cv )
468 {
469         return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
470 }
471
472 int
473 pthread_cond_broadcast( pthread_cond_t *cv )
474 {
475         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
476 }
477
478
479 #else
480
481 /***********************************************************************
482  *                                                                     *
483  * no threads package defined for this system - fake ok returns from   *
484  * all threads routines (making it single-threaded).                   *
485  *                                                                     *
486  ***********************************************************************/
487
488 /* ARGSUSED */
489 int
490 pthread_attr_init( pthread_attr_t *attr )
491 {
492         return( 0 );
493 }
494
495 /* ARGSUSED */
496 int
497 pthread_attr_destroy( pthread_attr_t *attr )
498 {
499         return( 0 );
500 }
501
502 /* ARGSUSED */
503 int
504 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
505 {
506         return( 0 );
507 }
508
509 /* ARGSUSED */
510 int
511 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
512 {
513         return( 0 );
514 }
515
516 /* ARGSUSED */
517 int
518 pthread_create(
519     pthread_t           *tid,
520     pthread_attr_t      *attr,
521     VFP                 func,
522     void                *arg
523 )
524 {
525         (*func)( arg );
526
527         return( 0 );
528 }
529
530 void
531 pthread_yield()
532 {
533         return;
534 }
535
536 void
537 pthread_exit()
538 {
539         return;
540 }
541
542 /* ARGSUSED */
543 void
544 pthread_kill( pthread_t tid, int sig )
545 {
546         return;
547 }
548
549 void
550 pthread_join( pthread_t tid, int *status )
551 {
552         return;
553 }
554
555 /* ARGSUSED */
556 int
557 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
558 {
559         return( 0 );
560 }
561
562 /* ARGSUSED */
563 int
564 pthread_mutex_destroy( pthread_mutex_t *mp )
565 {
566         return( 0 );
567 }
568
569 /* ARGSUSED */
570 int
571 pthread_mutex_lock( pthread_mutex_t *mp )
572 {
573         return( 0 );
574 }
575
576 /* ARGSUSED */
577 int
578 pthread_mutex_unlock( pthread_mutex_t *mp )
579 {
580         return( 0 );
581 }
582
583 /* ARGSUSED */
584 int
585 pthread_mutex_trylock( pthread_mutex_t *mp )
586 {
587         return( 0 );
588 }
589
590 /* ARGSUSED */
591 int
592 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
593 {
594         return( 0 );
595 }
596
597 /* ARGSUSED */
598 int
599 pthread_cond_destroy( pthread_cond_t *cv )
600 {
601         return( 0 );
602 }
603
604 /* ARGSUSED */
605 int
606 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
607 {
608         return( 0 );
609 }
610
611 /* ARGSUSED */
612 int
613 pthread_cond_signal( pthread_cond_t *cv )
614 {
615         return( 0 );
616 }
617
618 /* ARGSUSED */
619 int
620 pthread_cond_broadcast( pthread_cond_t *cv )
621 {
622         return( 0 );
623 }
624
625 #endif /* no threads package */