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 )
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 ***********************************************************************/
332 pthread_attr_init( pthread_attr_t *attr )
339 pthread_attr_destroy( pthread_attr_t *attr )
346 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
348 *detachstate = *attr;
353 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
368 return( thr_create( NULL, 0, func, arg, attr, tid ) );
384 pthread_join( pthread_t tid, int *status )
386 thr_join( tid, NULL, (void **) status );
390 pthread_kill( pthread_t tid, int sig )
392 thr_kill( tid, sig );
397 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
399 return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
403 pthread_mutex_destroy( pthread_mutex_t *mp )
405 return( mutex_destroy( mp ) );
409 pthread_mutex_lock( pthread_mutex_t *mp )
411 return( mutex_lock( mp ) );
415 pthread_mutex_unlock( pthread_mutex_t *mp )
417 return( mutex_unlock( mp ) );
421 pthread_mutex_trylock( pthread_mutex_t *mp )
423 return( mutex_trylock( mp ) );
427 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
429 return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
433 pthread_cond_destroy( pthread_cond_t *cv )
435 return( cond_destroy( cv ) );
439 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
441 return( cond_wait( cv, mp ) );
445 pthread_cond_signal( pthread_cond_t *cv )
447 return( cond_signal( cv ) );
451 pthread_cond_broadcast( pthread_cond_t *cv )
453 return( cond_broadcast( cv ) );
457 #else /* end sunos5 threads */
459 #if defined( THREAD_MIT_PTHREADS )
461 /***********************************************************************
463 * pthreads package by Chris Provenzano of MIT - provides all the *
464 * pthreads calls already, so no mapping to do *
466 ***********************************************************************/
468 #else /* end mit pthreads */
470 #if defined( THREAD_DCE_PTHREADS )
472 /***********************************************************************
474 * pthreads package with DCE - no mapping to do (except to create a *
475 * pthread_kill() routine) *
477 ***********************************************************************/
481 pthread_kill( pthread_t tid, int sig )
483 kill( getpid(), sig );
486 #endif /* dce pthreads */
487 #endif /* mit pthreads */
488 #endif /* sunos5 lwp */
489 #endif /* sunos4 lwp */
493 /***********************************************************************
495 * no threads package defined for this system - fake ok returns from *
496 * all threads routines (making it single-threaded). *
498 ***********************************************************************/
502 pthread_attr_init( pthread_attr_t *attr )
509 pthread_attr_destroy( pthread_attr_t *attr )
516 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
523 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
556 pthread_kill( pthread_t tid, int sig )
562 pthread_join( pthread_t tid, int *status )
569 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
576 pthread_mutex_destroy( pthread_mutex_t *mp )
583 pthread_mutex_lock( pthread_mutex_t *mp )
590 pthread_mutex_unlock( pthread_mutex_t *mp )
597 pthread_mutex_trylock( pthread_mutex_t *mp )
604 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
611 pthread_cond_destroy( pthread_cond_t *cv )
618 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
625 pthread_cond_signal( pthread_cond_t *cv )
632 pthread_cond_broadcast( pthread_cond_t *cv )
637 #endif /* no threads package */