1 /* thread.c - glue routines to provide a consistent thread interface */
8 #if defined( THREAD_NEXT_CTHREADS )
10 /***********************************************************************
12 * under NEXTSTEP or OPENSTEP use CThreads *
13 * lukeh@xedoc.com.au *
15 ***********************************************************************/
18 pthread_attr_init( pthread_attr_t *attr )
25 pthread_attr_destroy( pthread_attr_t *attr )
31 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
38 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
53 *tid = cthread_fork(func, arg);
54 return ( *tid == NULL ? -1 : 0 );
64 pthread_exit( any_t a )
70 pthread_join( pthread_t tid, int *pStatus )
73 status = (int) cthread_join ( tid );
82 pthread_kill( pthread_t tid, int sig )
89 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
97 pthread_mutex_destroy( pthread_mutex_t *mp )
104 pthread_mutex_lock( pthread_mutex_t *mp )
111 pthread_mutex_unlock( pthread_mutex_t *mp )
118 pthread_mutex_trylock( pthread_mutex_t *mp )
120 return mutex_try_lock( mp );
124 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
126 condition_init( cv );
131 pthread_cond_destroy( pthread_cond_t *cv )
133 condition_clear( cv );
138 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
140 condition_wait( cv, mp );
145 pthread_cond_signal( pthread_cond_t *cv )
147 condition_signal( cv );
152 pthread_cond_broadcast( pthread_cond_t *cv )
154 condition_broadcast( cv );
158 #elif defined( THREAD_SUNOS4_LWP )
160 /***********************************************************************
162 * under sunos 4 - use the built in non-preemptive lwp threads package *
164 ***********************************************************************/
166 extern stkalign_t *get_stack();
167 static void lwp_create_stack();
170 pthread_attr_init( pthread_attr_t *attr )
177 pthread_attr_destroy( pthread_attr_t *attr )
183 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
185 *detachstate = *attr;
190 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
200 pthread_attr_t *attr,
208 if ( (stack = get_stack( &stackno )) == NULL ) {
211 return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
216 lwp_create_stack( VFP func, void *arg, int stackno )
220 free_stack( stackno );
236 pthread_join( pthread_t tid, int *status )
243 pthread_kill( pthread_t tid, int sig )
250 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
252 return( mon_create( mp ) );
256 pthread_mutex_destroy( pthread_mutex_t *mp )
258 return( mon_destroy( *mp ) );
262 pthread_mutex_lock( pthread_mutex_t *mp )
264 return( mon_enter( *mp ) );
268 pthread_mutex_unlock( pthread_mutex_t *mp )
270 return( mon_exit( *mp ) );
274 pthread_mutex_trylock( pthread_mutex_t *mp )
276 return( mon_cond_enter( *mp ) );
280 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
283 * lwp cv_create requires the monitor id be passed in
284 * when the cv is created, pthreads passes it when the
285 * condition is waited for. so, we fake the creation
286 * here and actually do it when the cv is waited for
296 pthread_cond_destroy( pthread_cond_t *cv )
298 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
302 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
304 if ( ! cv->lcv_created ) {
305 cv_create( &cv->lcv_cv, *mp );
309 return( cv_wait( cv->lcv_cv ) );
313 pthread_cond_signal( pthread_cond_t *cv )
315 return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
319 pthread_cond_broadcast( pthread_cond_t *cv )
321 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
324 #else /* end sunos4 */
326 # if defined( THREAD_SUNOS5_LWP )
328 /***********************************************************************
330 * under sunos 5 - use the built in preemptive solaris threads package *
332 ***********************************************************************/
334 #if !defined(__SunOS_5_6)
336 pthread_attr_init( pthread_attr_t *attr )
343 pthread_attr_destroy( pthread_attr_t *attr )
350 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
352 *detachstate = *attr;
357 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
367 pthread_attr_t *attr,
372 return( thr_create( NULL, 0, func, arg, *attr, tid ) );
374 #endif /* ! sunos56 */
382 #if !defined(__SunOS_5_6)
390 pthread_join( pthread_t tid, int *status )
392 thr_join( tid, NULL, (void **) status );
396 pthread_kill( pthread_t tid, int sig )
398 thr_kill( tid, sig );
403 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
405 return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
409 pthread_mutex_destroy( pthread_mutex_t *mp )
411 return( mutex_destroy( mp ) );
415 pthread_mutex_lock( pthread_mutex_t *mp )
417 return( mutex_lock( mp ) );
421 pthread_mutex_unlock( pthread_mutex_t *mp )
423 return( mutex_unlock( mp ) );
427 pthread_mutex_trylock( pthread_mutex_t *mp )
429 return( mutex_trylock( mp ) );
433 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
435 return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
439 pthread_cond_destroy( pthread_cond_t *cv )
441 return( cond_destroy( cv ) );
445 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
447 return( cond_wait( cv, mp ) );
451 pthread_cond_signal( pthread_cond_t *cv )
453 return( cond_signal( cv ) );
457 pthread_cond_broadcast( pthread_cond_t *cv )
459 return( cond_broadcast( cv ) );
461 #endif /* ! sunos56 */
464 #else /* end sunos5 threads */
466 #if defined( THREAD_MIT_PTHREADS )
468 /***********************************************************************
470 * pthreads package by Chris Provenzano of MIT - provides all the *
471 * pthreads calls already, so no mapping to do *
473 ***********************************************************************/
475 #else /* end mit pthreads */
477 #if defined( THREAD_DCE_PTHREADS )
479 /***********************************************************************
481 * pthreads package with DCE - no mapping to do (except to create a *
482 * pthread_kill() routine) *
484 ***********************************************************************/
488 pthread_kill( pthread_t tid, int sig )
490 kill( getpid(), sig );
495 #if defined ( POSIX_THREADS )
497 #ifdef HAVE_SCHED_YIELD
500 #endif /* HAVE_SCHED_H */
502 /* POSIX Threads (final) does have a pthread_yield function */
503 void pthread_yield( void )
507 #endif /* HAVE_SCHED_YIELD */
509 #endif /* posix threads */
510 #endif /* dce pthreads */
511 #endif /* mit pthreads */
512 #endif /* sunos5 lwp */
513 #endif /* sunos4 lwp */
517 /***********************************************************************
519 * no threads package defined for this system - fake ok returns from *
520 * all threads routines (making it single-threaded). *
522 ***********************************************************************/
526 pthread_attr_init( pthread_attr_t *attr )
533 pthread_attr_destroy( pthread_attr_t *attr )
540 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
547 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
556 pthread_attr_t *attr,
580 pthread_kill( pthread_t tid, int sig )
586 pthread_join( pthread_t tid, int *status )
593 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
600 pthread_mutex_destroy( pthread_mutex_t *mp )
607 pthread_mutex_lock( pthread_mutex_t *mp )
614 pthread_mutex_unlock( pthread_mutex_t *mp )
621 pthread_mutex_trylock( pthread_mutex_t *mp )
628 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
635 pthread_cond_destroy( pthread_cond_t *cv )
642 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
649 pthread_cond_signal( pthread_cond_t *cv )
656 pthread_cond_broadcast( pthread_cond_t *cv )
661 #endif /* no threads package */