]> git.sur5r.net Git - openldap/blob - libraries/liblthread/thread.c
Update slapd to use lutil_passwd() for both user and root passwords.
[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 #ifndef HAVE_PTHREAD_KILL
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 /* HAVE_PTHREAD_KILL */
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( void )
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 int
184 pthread_attr_init( pthread_attr_t *attr )
185 {
186         *attr = 0;
187         return( 0 );
188 }
189
190 int
191 pthread_attr_destroy( pthread_attr_t *attr )
192 {
193         *attr = 0;
194         return( 0 );
195 }
196
197 int
198 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
199 {
200         *detachstate = *attr;
201         return( 0 );
202 }
203
204 int
205 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
206 {
207         *attr = detachstate;
208         return( 0 );
209 }
210
211 /* ARGSUSED */
212 int
213 pthread_create(
214     pthread_t           *tid,
215     pthread_attr_t      *attr,
216     VFP                 func,
217     void                *arg
218 )
219 {
220         return( thr_create( NULL, 0, func, arg, *attr, tid ) );
221 }
222
223 void
224 pthread_yield( void )
225 {
226         thr_yield();
227 }
228
229 void
230 pthread_exit()
231 {
232         thr_exit( NULL );
233 }
234
235 void
236 pthread_join( pthread_t tid, int *status )
237 {
238         thr_join( tid, NULL, (void **) status );
239 }
240
241 void
242 pthread_kill( pthread_t tid, int sig )
243 {
244         thr_kill( tid, sig );
245 }
246
247 /* ARGSUSED */
248 int
249 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
250 {
251         return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
252 }
253
254 int
255 pthread_mutex_destroy( pthread_mutex_t *mp )
256 {
257         return( mutex_destroy( mp ) );
258 }
259
260 int
261 pthread_mutex_lock( pthread_mutex_t *mp )
262 {
263         return( mutex_lock( mp ) );
264 }
265
266 int
267 pthread_mutex_unlock( pthread_mutex_t *mp )
268 {
269         return( mutex_unlock( mp ) );
270 }
271
272 int
273 pthread_mutex_trylock( pthread_mutex_t *mp )
274 {
275         return( mutex_trylock( mp ) );
276 }
277
278 int
279 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
280 {
281         return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
282 }
283
284 int
285 pthread_cond_destroy( pthread_cond_t *cv )
286 {
287         return( cond_destroy( cv ) );
288 }
289
290 int
291 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
292 {
293         return( cond_wait( cv, mp ) );
294 }
295
296 int
297 pthread_cond_signal( pthread_cond_t *cv )
298 {
299         return( cond_signal( cv ) );
300 }
301
302 int
303 pthread_cond_broadcast( pthread_cond_t *cv )
304 {
305         return( cond_broadcast( cv ) );
306 }
307
308 #elif defined( HAVE_LWP )
309
310 /*************
311  *           *
312  * SunOS LWP *
313  *           *
314  *************/
315
316 int
317 pthread_attr_init( pthread_attr_t *attr )
318 {
319         *attr = 0;
320         return( 0 );
321 }
322
323 int
324 pthread_attr_destroy( pthread_attr_t *attr )
325 {
326         return( 0 );
327 }
328
329 int
330 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
331 {
332         *detachstate = *attr;
333         return( 0 );
334 }
335
336 int
337 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
338 {
339         *attr = detachstate;
340         return( 0 );
341 }
342
343 static void
344 lwp_create_stack( VFP func, void *arg, int stackno )
345 {
346         (*func)( arg );
347
348         free_stack( stackno );
349 }
350
351 /* ARGSUSED */
352 int
353 pthread_create(
354     pthread_t           *tid,
355     pthread_attr_t      *attr,
356     VFP                 func,
357     void                *arg
358 )
359 {
360         stkalign_t      *stack;
361         int             stackno;
362
363         if ( (stack = get_stack( &stackno )) == NULL ) {
364                 return( -1 );
365         }
366         return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
367             arg, stackno ) );
368 }
369
370 void
371 pthread_yield( void )
372 {
373         lwp_yield( SELF );
374 }
375
376 void
377 pthread_exit()
378 {
379         lwp_destroy( SELF );
380 }
381
382 void
383 pthread_join( pthread_t tid, int *status )
384 {
385         lwp_join( tid );
386 }
387
388 /* ARGSUSED */
389 void
390 pthread_kill( pthread_t tid, int sig )
391 {
392         return;
393 }
394
395 /* ARGSUSED */
396 int
397 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
398 {
399         return( mon_create( mp ) );
400 }
401
402 int
403 pthread_mutex_destroy( pthread_mutex_t *mp )
404 {
405         return( mon_destroy( *mp ) );
406 }
407
408 int
409 pthread_mutex_lock( pthread_mutex_t *mp )
410 {
411         return( mon_enter( *mp ) );
412 }
413
414 int
415 pthread_mutex_unlock( pthread_mutex_t *mp )
416 {
417         return( mon_exit( *mp ) );
418 }
419
420 int
421 pthread_mutex_trylock( pthread_mutex_t *mp )
422 {
423         return( mon_cond_enter( *mp ) );
424 }
425
426 int
427 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
428 {
429         /*
430          * lwp cv_create requires the monitor id be passed in
431          * when the cv is created, pthreads passes it when the
432          * condition is waited for.  so, we fake the creation
433          * here and actually do it when the cv is waited for
434          * later.
435          */
436
437         cv->lcv_created = 0;
438
439         return( 0 );
440 }
441
442 int
443 pthread_cond_destroy( pthread_cond_t *cv )
444 {
445         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
446 }
447
448 int
449 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
450 {
451         if ( ! cv->lcv_created ) {
452                 cv_create( &cv->lcv_cv, *mp );
453                 cv->lcv_created = 1;
454         }
455
456         return( cv_wait( cv->lcv_cv ) );
457 }
458
459 int
460 pthread_cond_signal( pthread_cond_t *cv )
461 {
462         return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
463 }
464
465 int
466 pthread_cond_broadcast( pthread_cond_t *cv )
467 {
468         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
469 }
470
471
472 #elif defined( HAVE_NT_THREADS )
473
474 int
475 pthread_attr_init( pthread_attr_t *attr )
476 {
477         *attr = 0;
478         return( 0 );
479 }
480
481 int
482 pthread_attr_destroy( pthread_attr_t *attr )
483 {
484         return( 0 );
485 }
486
487 int
488 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
489 {
490         *detachstate = *attr;
491         return( 0 );
492 }
493
494 int
495 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
496 {
497         *attr = detachstate;
498         return( 0 );
499 }
500
501 int
502 pthread_create(
503     pthread_t           *tid,
504     pthread_attr_t      *attr,
505     VFP                 func,
506     void                *arg
507 )
508 {
509         *tid = (pthread_t)_beginthread( (void *) func, 0, arg );
510          return ( (unsigned long)*tid == -1 ? -1 : 0 );
511 }
512
513 void
514 pthread_yield()
515 {
516
517 }
518
519 void
520 pthread_exit( void )
521 {
522         _endthread( );
523 }
524
525 void
526 pthread_join( pthread_t tid, int *pStatus )
527 {
528         DWORD status;
529         status = WaitForSingleObject( tid, INFINITE );
530         if ( pStatus != NULL)
531         {
532                 if ( status != WAIT_FAILED )
533                         *pStatus = 0;
534                 else
535                         *pStatus = WAIT_ABANDONED;
536         }
537 }
538
539
540 void
541 pthread_kill( pthread_t tid, int sig )
542 {
543         return;
544 }
545
546
547 int
548 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
549 {
550         *mp = CreateMutex( NULL, 0, NULL );
551         return ( 0 );
552 }
553
554 int
555 pthread_mutex_destroy( pthread_mutex_t *mp )
556 {
557         CloseHandle( *mp );
558         return ( 0 );
559 }
560
561 int
562 pthread_mutex_lock( pthread_mutex_t *mp )
563 {
564         WaitForSingleObject( *mp, INFINITE );
565         return ( 0 );
566 }
567
568 int
569 pthread_mutex_unlock( pthread_mutex_t *mp )
570 {
571         ReleaseMutex( *mp );
572         return ( 0 );
573 }
574
575 int
576 pthread_mutex_trylock( pthread_mutex_t *mp )
577 {
578         DWORD status;
579
580         status = WaitForSingleObject( *mp, 0 );
581         if ( (status == WAIT_FAILED) || (status == WAIT_TIMEOUT) )
582                 return 0;
583         else
584                 return 1;
585 }
586
587 int
588 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
589 {
590         *cv = CreateEvent( NULL, FALSE, FALSE, NULL );
591         return( 0 );
592 }
593
594 int
595 pthread_cond_destroy( pthread_cond_t *cv )
596 {
597         CloseHandle( *cv );
598         return( 0 );
599 }
600
601 int
602 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
603 {
604         ReleaseMutex( *mp );
605         WaitForSingleObject( *cv, INFINITE );
606         WaitForSingleObject( *mp, INFINITE );
607         return( 0 );
608 }
609
610 int
611 pthread_cond_signal( pthread_cond_t *cv )
612 {
613         SetEvent( *cv );
614         return( 0 );
615 }
616
617 int
618 pthread_cond_broadcast( pthread_cond_t *cv )
619 {
620         SetEvent( *cv );
621         return( 0 );
622 }
623
624 #else
625
626 /***********************************************************************
627  *                                                                     *
628  * no threads package defined for this system - fake ok returns from   *
629  * all threads routines (making it single-threaded).                   *
630  *                                                                     *
631  ***********************************************************************/
632
633 /* ARGSUSED */
634 int
635 pthread_attr_init( pthread_attr_t *attr )
636 {
637         return( 0 );
638 }
639
640 /* ARGSUSED */
641 int
642 pthread_attr_destroy( pthread_attr_t *attr )
643 {
644         return( 0 );
645 }
646
647 /* ARGSUSED */
648 int
649 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
650 {
651         return( 0 );
652 }
653
654 /* ARGSUSED */
655 int
656 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
657 {
658         return( 0 );
659 }
660
661 /* ARGSUSED */
662 int
663 pthread_create(
664     pthread_t           *tid,
665     pthread_attr_t      *attr,
666     VFP                 func,
667     void                *arg
668 )
669 {
670         (*func)( arg );
671
672         return( 0 );
673 }
674
675 void
676 pthread_yield( void )
677 {
678         return;
679 }
680
681 void
682 pthread_exit()
683 {
684         return;
685 }
686
687 /* ARGSUSED */
688 void
689 pthread_kill( pthread_t tid, int sig )
690 {
691         return;
692 }
693
694 void
695 pthread_join( pthread_t tid, int *status )
696 {
697         return;
698 }
699
700 /* ARGSUSED */
701 int
702 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
703 {
704         return( 0 );
705 }
706
707 /* ARGSUSED */
708 int
709 pthread_mutex_destroy( pthread_mutex_t *mp )
710 {
711         return( 0 );
712 }
713
714 /* ARGSUSED */
715 int
716 pthread_mutex_lock( pthread_mutex_t *mp )
717 {
718         return( 0 );
719 }
720
721 /* ARGSUSED */
722 int
723 pthread_mutex_unlock( pthread_mutex_t *mp )
724 {
725         return( 0 );
726 }
727
728 /* ARGSUSED */
729 int
730 pthread_mutex_trylock( pthread_mutex_t *mp )
731 {
732         return( 0 );
733 }
734
735 /* ARGSUSED */
736 int
737 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
738 {
739         return( 0 );
740 }
741
742 /* ARGSUSED */
743 int
744 pthread_cond_destroy( pthread_cond_t *cv )
745 {
746         return( 0 );
747 }
748
749 /* ARGSUSED */
750 int
751 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
752 {
753         return( 0 );
754 }
755
756 /* ARGSUSED */
757 int
758 pthread_cond_signal( pthread_cond_t *cv )
759 {
760         return( 0 );
761 }
762
763 /* ARGSUSED */
764 int
765 pthread_cond_broadcast( pthread_cond_t *cv )
766 {
767         return( 0 );
768 }
769
770 #endif /* no threads package */