1 /* thread.c - glue routines to provide a consistent thread interface */
5 #if defined( THREAD_NEXT_CTHREADS )
7 /***********************************************************************
9 * under NEXTSTEP or OPENSTEP use CThreads *
10 * lukeh@xedoc.com.au *
12 ***********************************************************************/
15 pthread_attr_init( pthread_attr_t *attr )
22 pthread_attr_destroy( pthread_attr_t *attr )
28 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
35 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
50 *tid = cthread_fork(func, arg);
51 return ( *tid == NULL ? -1 : 0 );
61 pthread_exit( any_t a )
67 pthread_join( pthread_t tid, int *pStatus )
70 status = (int) cthread_join ( tid );
79 pthread_kill( pthread_t tid, int sig )
86 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
94 pthread_mutex_destroy( pthread_mutex_t *mp )
101 pthread_mutex_lock( pthread_mutex_t *mp )
108 pthread_mutex_unlock( pthread_mutex_t *mp )
115 pthread_mutex_trylock( pthread_mutex_t *mp )
117 return mutex_try_lock( mp );
121 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
123 condition_init( cv );
128 pthread_cond_destroy( pthread_cond_t *cv )
130 condition_clear( cv );
135 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
137 condition_wait( cv, mp );
142 pthread_cond_signal( pthread_cond_t *cv )
144 condition_signal( cv );
149 pthread_cond_broadcast( pthread_cond_t *cv )
151 condition_broadcast( cv );
155 #elif defined( THREAD_SUNOS4_LWP )
157 /***********************************************************************
159 * under sunos 4 - use the built in non-preemptive lwp threads package *
161 ***********************************************************************/
163 extern stkalign_t *get_stack();
164 static void lwp_create_stack();
167 pthread_attr_init( pthread_attr_t *attr )
174 pthread_attr_destroy( pthread_attr_t *attr )
180 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
182 *detachstate = *attr;
187 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
197 pthread_attr_t *attr,
205 if ( (stack = get_stack( &stackno )) == NULL ) {
208 return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
213 lwp_create_stack( VFP func, void *arg, int stackno )
217 free_stack( stackno );
233 pthread_join( pthread_t tid, int *status )
240 pthread_kill( pthread_t tid, int sig )
247 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
249 return( mon_create( mp ) );
253 pthread_mutex_destroy( pthread_mutex_t *mp )
255 return( mon_destroy( *mp ) );
259 pthread_mutex_lock( pthread_mutex_t *mp )
261 return( mon_enter( *mp ) );
265 pthread_mutex_unlock( pthread_mutex_t *mp )
267 return( mon_exit( *mp ) );
271 pthread_mutex_trylock( pthread_mutex_t *mp )
273 return( mon_cond_enter( *mp ) );
277 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
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
293 pthread_cond_destroy( pthread_cond_t *cv )
295 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
299 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
301 if ( ! cv->lcv_created ) {
302 cv_create( &cv->lcv_cv, *mp );
306 return( cv_wait( cv->lcv_cv ) );
310 pthread_cond_signal( pthread_cond_t *cv )
312 return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
316 pthread_cond_broadcast( pthread_cond_t *cv )
318 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
321 #else /* end sunos4 */
323 # if defined( THREAD_SUNOS5_LWP )
325 /***********************************************************************
327 * under sunos 5 - use the built in preemptive solaris threads package *
329 ***********************************************************************/
331 #if !defined(__SunOS_5_6)
333 pthread_attr_init( pthread_attr_t *attr )
340 pthread_attr_destroy( pthread_attr_t *attr )
347 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
349 *detachstate = *attr;
354 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
364 pthread_attr_t *attr,
369 return( thr_create( NULL, 0, func, arg, *attr, tid ) );
371 #endif /* ! sunos56 */
379 #if !defined(__SunOS_5_6)
387 pthread_join( pthread_t tid, int *status )
389 thr_join( tid, NULL, (void **) status );
393 pthread_kill( pthread_t tid, int sig )
395 thr_kill( tid, sig );
400 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
402 return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
406 pthread_mutex_destroy( pthread_mutex_t *mp )
408 return( mutex_destroy( mp ) );
412 pthread_mutex_lock( pthread_mutex_t *mp )
414 return( mutex_lock( mp ) );
418 pthread_mutex_unlock( pthread_mutex_t *mp )
420 return( mutex_unlock( mp ) );
424 pthread_mutex_trylock( pthread_mutex_t *mp )
426 return( mutex_trylock( mp ) );
430 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
432 return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
436 pthread_cond_destroy( pthread_cond_t *cv )
438 return( cond_destroy( cv ) );
442 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
444 return( cond_wait( cv, mp ) );
448 pthread_cond_signal( pthread_cond_t *cv )
450 return( cond_signal( cv ) );
454 pthread_cond_broadcast( pthread_cond_t *cv )
456 return( cond_broadcast( cv ) );
458 #endif /* ! sunos56 */
461 #else /* end sunos5 threads */
463 #if defined( THREAD_MIT_PTHREADS )
465 /***********************************************************************
467 * pthreads package by Chris Provenzano of MIT - provides all the *
468 * pthreads calls already, so no mapping to do *
470 ***********************************************************************/
472 #else /* end mit pthreads */
474 #if defined( THREAD_DCE_PTHREADS )
476 /***********************************************************************
478 * pthreads package with DCE - no mapping to do (except to create a *
479 * pthread_kill() routine) *
481 ***********************************************************************/
485 pthread_kill( pthread_t tid, int sig )
487 kill( getpid(), sig );
492 #if defined ( POSIX_THREADS )
494 #ifdef HAVE_SCHED_YIELD
497 #endif /* HAVE_SCHED_H */
499 /* POSIX Threads (final) does have a pthread_yield function */
500 void pthread_yield( void )
504 #endif /* HAVE_SCHED_YIELD */
506 #endif /* posix threads */
507 #endif /* dce pthreads */
508 #endif /* mit pthreads */
509 #endif /* sunos5 lwp */
510 #endif /* sunos4 lwp */
514 /***********************************************************************
516 * no threads package defined for this system - fake ok returns from *
517 * all threads routines (making it single-threaded). *
519 ***********************************************************************/
523 pthread_attr_init( pthread_attr_t *attr )
530 pthread_attr_destroy( pthread_attr_t *attr )
537 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
544 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
553 pthread_attr_t *attr,
577 pthread_kill( pthread_t tid, int sig )
583 pthread_join( pthread_t tid, int *status )
590 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
597 pthread_mutex_destroy( pthread_mutex_t *mp )
604 pthread_mutex_lock( pthread_mutex_t *mp )
611 pthread_mutex_unlock( pthread_mutex_t *mp )
618 pthread_mutex_trylock( pthread_mutex_t *mp )
625 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
632 pthread_cond_destroy( pthread_cond_t *cv )
639 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
646 pthread_cond_signal( pthread_cond_t *cv )
653 pthread_cond_broadcast( pthread_cond_t *cv )
658 #endif /* no threads package */