]> git.sur5r.net Git - openldap/blob - libraries/liblthread/thread.c
f6e516f5059240cc6a8e484fefc5a8889fad8f65
[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 int
332 pthread_attr_init( pthread_attr_t *attr )
333 {
334         *attr = 0;
335         return( 0 );
336 }
337
338 int
339 pthread_attr_destroy( pthread_attr_t *attr )
340 {
341         *attr = 0;
342         return( 0 );
343 }
344
345 int
346 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
347 {
348         *detachstate = *attr;
349         return( 0 );
350 }
351
352 int
353 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
354 {
355         *attr = detachstate;
356         return( 0 );
357 }
358
359 /* ARGSUSED */
360 int
361 pthread_create(
362     pthread_t           *tid,
363     pthread_attr_t      attr,
364     VFP                 func,
365     void                *arg
366 )
367 {
368         return( thr_create( NULL, 0, func, arg, attr, tid ) );
369 }
370
371 void
372 pthread_yield()
373 {
374         thr_yield();
375 }
376
377 void
378 pthread_exit()
379 {
380         thr_exit( NULL );
381 }
382
383 void
384 pthread_join( pthread_t tid, int *status )
385 {
386         thr_join( tid, NULL, (void **) status );
387 }
388
389 void
390 pthread_kill( pthread_t tid, int sig )
391 {
392         thr_kill( tid, sig );
393 }
394
395 /* ARGSUSED */
396 int
397 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
398 {
399         return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
400 }
401
402 int
403 pthread_mutex_destroy( pthread_mutex_t *mp )
404 {
405         return( mutex_destroy( mp ) );
406 }
407
408 int
409 pthread_mutex_lock( pthread_mutex_t *mp )
410 {
411         return( mutex_lock( mp ) );
412 }
413
414 int
415 pthread_mutex_unlock( pthread_mutex_t *mp )
416 {
417         return( mutex_unlock( mp ) );
418 }
419
420 int
421 pthread_mutex_trylock( pthread_mutex_t *mp )
422 {
423         return( mutex_trylock( mp ) );
424 }
425
426 int
427 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
428 {
429         return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
430 }
431
432 int
433 pthread_cond_destroy( pthread_cond_t *cv )
434 {
435         return( cond_destroy( cv ) );
436 }
437
438 int
439 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
440 {
441         return( cond_wait( cv, mp ) );
442 }
443
444 int
445 pthread_cond_signal( pthread_cond_t *cv )
446 {
447         return( cond_signal( cv ) );
448 }
449
450 int
451 pthread_cond_broadcast( pthread_cond_t *cv )
452 {
453         return( cond_broadcast( cv ) );
454 }
455
456
457 #else /* end sunos5 threads */
458
459 #if defined( THREAD_MIT_PTHREADS )
460
461 /***********************************************************************
462  *                                                                     *
463  * pthreads package by Chris Provenzano of MIT - provides all the      *
464  * pthreads calls already, so no mapping to do                         *
465  *                                                                     *
466  ***********************************************************************/
467
468 #else /* end mit pthreads */
469
470 #if defined( THREAD_DCE_PTHREADS )
471
472 /***********************************************************************
473  *                                                                     *
474  * pthreads package with DCE - no mapping to do (except to create a    *
475  * pthread_kill() routine)                                             *
476  *                                                                     *
477  ***********************************************************************/
478
479 /* ARGSUSED */
480 void
481 pthread_kill( pthread_t tid, int sig )
482 {
483         kill( getpid(), sig );
484 }
485
486 #else
487
488 #if defined ( POSIX_THREADS )
489
490 void p_thread_yield( void )
491 {
492         sched_yield();
493 }
494
495 #endif /* posix threads */
496 #endif /* dce pthreads */
497 #endif /* mit pthreads */
498 #endif /* sunos5 lwp */
499 #endif /* sunos4 lwp */
500
501 #ifndef _THREAD
502
503 /***********************************************************************
504  *                                                                     *
505  * no threads package defined for this system - fake ok returns from   *
506  * all threads routines (making it single-threaded).                   *
507  *                                                                     *
508  ***********************************************************************/
509
510 /* ARGSUSED */
511 int
512 pthread_attr_init( pthread_attr_t *attr )
513 {
514         return( 0 );
515 }
516
517 /* ARGSUSED */
518 int
519 pthread_attr_destroy( pthread_attr_t *attr )
520 {
521         return( 0 );
522 }
523
524 /* ARGSUSED */
525 int
526 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
527 {
528         return( 0 );
529 }
530
531 /* ARGSUSED */
532 int
533 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
534 {
535         return( 0 );
536 }
537
538 /* ARGSUSED */
539 int
540 pthread_create(
541     pthread_t           *tid,
542     pthread_attr_t      attr,
543     VFP                 func,
544     void                *arg
545 )
546 {
547         (*func)( arg );
548
549         return( 0 );
550 }
551
552 void
553 pthread_yield()
554 {
555         return;
556 }
557
558 void
559 pthread_exit()
560 {
561         return;
562 }
563
564 /* ARGSUSED */
565 void
566 pthread_kill( pthread_t tid, int sig )
567 {
568         return;
569 }
570
571 void
572 pthread_join( pthread_t tid, int *status )
573 {
574         return;
575 }
576
577 /* ARGSUSED */
578 int
579 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
580 {
581         return( 0 );
582 }
583
584 /* ARGSUSED */
585 int
586 pthread_mutex_destroy( pthread_mutex_t *mp )
587 {
588         return( 0 );
589 }
590
591 /* ARGSUSED */
592 int
593 pthread_mutex_lock( pthread_mutex_t *mp )
594 {
595         return( 0 );
596 }
597
598 /* ARGSUSED */
599 int
600 pthread_mutex_unlock( pthread_mutex_t *mp )
601 {
602         return( 0 );
603 }
604
605 /* ARGSUSED */
606 int
607 pthread_mutex_trylock( pthread_mutex_t *mp )
608 {
609         return( 0 );
610 }
611
612 /* ARGSUSED */
613 int
614 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
615 {
616         return( 0 );
617 }
618
619 /* ARGSUSED */
620 int
621 pthread_cond_destroy( pthread_cond_t *cv )
622 {
623         return( 0 );
624 }
625
626 /* ARGSUSED */
627 int
628 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
629 {
630         return( 0 );
631 }
632
633 /* ARGSUSED */
634 int
635 pthread_cond_signal( pthread_cond_t *cv )
636 {
637         return( 0 );
638 }
639
640 /* ARGSUSED */
641 int
642 pthread_cond_broadcast( pthread_cond_t *cv )
643 {
644         return( 0 );
645 }
646
647 #endif /* no threads package */