1 /* thread.c - glue routines to provide a consistent thread interface */
7 #if defined( HAVE_PTHREADS )
9 #ifndef HAVE_PTHREAD_KILL
10 /***********************************************************************
12 * pthreads package with DCE - no mapping to do (except to create a *
13 * pthread_kill() routine) *
15 ***********************************************************************/
19 pthread_kill( pthread_t tid, int sig )
21 kill( getpid(), sig );
23 #endif /* HAVE_PTHREAD_KILL */
25 #elif defined( HAVE_MACH_CTHREADS )
27 /***********************************************************************
29 * under NEXTSTEP or OPENSTEP use CThreads *
30 * lukeh@xedoc.com.au *
32 ***********************************************************************/
35 pthread_attr_init( pthread_attr_t *attr )
42 pthread_attr_destroy( pthread_attr_t *attr )
48 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
55 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
70 *tid = cthread_fork(func, arg);
71 return ( *tid == NULL ? -1 : 0 );
81 pthread_exit( any_t a )
87 pthread_join( pthread_t tid, int *pStatus )
90 status = (int) cthread_join ( tid );
99 pthread_kill( pthread_t tid, int sig )
106 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
114 pthread_mutex_destroy( pthread_mutex_t *mp )
121 pthread_mutex_lock( pthread_mutex_t *mp )
128 pthread_mutex_unlock( pthread_mutex_t *mp )
135 pthread_mutex_trylock( pthread_mutex_t *mp )
137 return mutex_try_lock( mp );
141 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
143 condition_init( cv );
148 pthread_cond_destroy( pthread_cond_t *cv )
150 condition_clear( cv );
155 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
157 condition_wait( cv, mp );
162 pthread_cond_signal( pthread_cond_t *cv )
164 condition_signal( cv );
169 pthread_cond_broadcast( pthread_cond_t *cv )
171 condition_broadcast( cv );
175 #elif defined( HAVE_THR )
184 pthread_attr_init( pthread_attr_t *attr )
191 pthread_attr_destroy( pthread_attr_t *attr )
198 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
200 *detachstate = *attr;
205 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
215 pthread_attr_t *attr,
220 return( thr_create( NULL, 0, func, arg, *attr, tid ) );
224 pthread_yield( void )
236 pthread_join( pthread_t tid, int *status )
238 thr_join( tid, NULL, (void **) status );
242 pthread_kill( pthread_t tid, int sig )
244 thr_kill( tid, sig );
249 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
251 return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
255 pthread_mutex_destroy( pthread_mutex_t *mp )
257 return( mutex_destroy( mp ) );
261 pthread_mutex_lock( pthread_mutex_t *mp )
263 return( mutex_lock( mp ) );
267 pthread_mutex_unlock( pthread_mutex_t *mp )
269 return( mutex_unlock( mp ) );
273 pthread_mutex_trylock( pthread_mutex_t *mp )
275 return( mutex_trylock( mp ) );
279 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
281 return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
285 pthread_cond_destroy( pthread_cond_t *cv )
287 return( cond_destroy( cv ) );
291 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
293 return( cond_wait( cv, mp ) );
297 pthread_cond_signal( pthread_cond_t *cv )
299 return( cond_signal( cv ) );
303 pthread_cond_broadcast( pthread_cond_t *cv )
305 return( cond_broadcast( cv ) );
308 #elif defined( HAVE_LWP )
317 pthread_attr_init( pthread_attr_t *attr )
324 pthread_attr_destroy( pthread_attr_t *attr )
330 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
332 *detachstate = *attr;
337 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
344 lwp_create_stack( VFP func, void *arg, int stackno )
348 free_stack( stackno );
355 pthread_attr_t *attr,
363 if ( (stack = get_stack( &stackno )) == NULL ) {
366 return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
371 pthread_yield( void )
383 pthread_join( pthread_t tid, int *status )
390 pthread_kill( pthread_t tid, int sig )
397 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
399 return( mon_create( mp ) );
403 pthread_mutex_destroy( pthread_mutex_t *mp )
405 return( mon_destroy( *mp ) );
409 pthread_mutex_lock( pthread_mutex_t *mp )
411 return( mon_enter( *mp ) );
415 pthread_mutex_unlock( pthread_mutex_t *mp )
417 return( mon_exit( *mp ) );
421 pthread_mutex_trylock( pthread_mutex_t *mp )
423 return( mon_cond_enter( *mp ) );
427 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
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
443 pthread_cond_destroy( pthread_cond_t *cv )
445 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
449 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
451 if ( ! cv->lcv_created ) {
452 cv_create( &cv->lcv_cv, *mp );
456 return( cv_wait( cv->lcv_cv ) );
460 pthread_cond_signal( pthread_cond_t *cv )
462 return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
466 pthread_cond_broadcast( pthread_cond_t *cv )
468 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
472 #elif defined( HAVE_NT_THREADS )
475 pthread_attr_init( pthread_attr_t *attr )
482 pthread_attr_destroy( pthread_attr_t *attr )
488 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
490 *detachstate = *attr;
495 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
504 pthread_attr_t *attr,
509 *tid = (pthread_t)_beginthread( (void *) func, 0, arg );
510 return ( (unsigned long)*tid == -1 ? -1 : 0 );
526 pthread_join( pthread_t tid, int *pStatus )
529 status = WaitForSingleObject( tid, INFINITE );
530 if ( pStatus != NULL)
532 if ( status != WAIT_FAILED )
535 *pStatus = WAIT_ABANDONED;
541 pthread_kill( pthread_t tid, int sig )
548 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
550 *mp = CreateMutex( NULL, 0, NULL );
555 pthread_mutex_destroy( pthread_mutex_t *mp )
562 pthread_mutex_lock( pthread_mutex_t *mp )
564 WaitForSingleObject( *mp, INFINITE );
569 pthread_mutex_unlock( pthread_mutex_t *mp )
576 pthread_mutex_trylock( pthread_mutex_t *mp )
580 status = WaitForSingleObject( *mp, 0 );
581 if ( (status == WAIT_FAILED) || (status == WAIT_TIMEOUT) )
588 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
590 *cv = CreateEvent( NULL, FALSE, FALSE, NULL );
595 pthread_cond_destroy( pthread_cond_t *cv )
602 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
605 WaitForSingleObject( *cv, INFINITE );
606 WaitForSingleObject( *mp, INFINITE );
611 pthread_cond_signal( pthread_cond_t *cv )
618 pthread_cond_broadcast( pthread_cond_t *cv )
626 /***********************************************************************
628 * no threads package defined for this system - fake ok returns from *
629 * all threads routines (making it single-threaded). *
631 ***********************************************************************/
635 pthread_attr_init( pthread_attr_t *attr )
642 pthread_attr_destroy( pthread_attr_t *attr )
649 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
656 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
665 pthread_attr_t *attr,
676 pthread_yield( void )
689 pthread_kill( pthread_t tid, int sig )
695 pthread_join( pthread_t tid, int *status )
702 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
709 pthread_mutex_destroy( pthread_mutex_t *mp )
716 pthread_mutex_lock( pthread_mutex_t *mp )
723 pthread_mutex_unlock( pthread_mutex_t *mp )
730 pthread_mutex_trylock( pthread_mutex_t *mp )
737 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
744 pthread_cond_destroy( pthread_cond_t *cv )
751 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
758 pthread_cond_signal( pthread_cond_t *cv )
765 pthread_cond_broadcast( pthread_cond_t *cv )
770 #endif /* no threads package */