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