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