1 /* thread.c - glue routines to provide a consistent thread interface */
7 #if defined( HAVE_PTHREADS )
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 );
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 )
183 #if !defined(__SunOS_5_6)
185 pthread_attr_init( pthread_attr_t *attr )
192 pthread_attr_destroy( pthread_attr_t *attr )
199 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
201 *detachstate = *attr;
206 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
216 pthread_attr_t *attr,
221 return( thr_create( NULL, 0, func, arg, *attr, tid ) );
223 #endif /* ! sunos56 */
231 #if !defined(__SunOS_5_6)
239 pthread_join( pthread_t tid, int *status )
241 thr_join( tid, NULL, (void **) status );
245 pthread_kill( pthread_t tid, int sig )
247 thr_kill( tid, sig );
252 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
254 return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
258 pthread_mutex_destroy( pthread_mutex_t *mp )
260 return( mutex_destroy( mp ) );
264 pthread_mutex_lock( pthread_mutex_t *mp )
266 return( mutex_lock( mp ) );
270 pthread_mutex_unlock( pthread_mutex_t *mp )
272 return( mutex_unlock( mp ) );
276 pthread_mutex_trylock( pthread_mutex_t *mp )
278 return( mutex_trylock( mp ) );
282 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
284 return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
288 pthread_cond_destroy( pthread_cond_t *cv )
290 return( cond_destroy( cv ) );
294 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
296 return( cond_wait( cv, mp ) );
300 pthread_cond_signal( pthread_cond_t *cv )
302 return( cond_signal( cv ) );
306 pthread_cond_broadcast( pthread_cond_t *cv )
308 return( cond_broadcast( cv ) );
310 #endif /* ! sunos56 */
312 #elif defined( HAVE_LWP )
320 extern stkalign_t *get_stack();
321 static void lwp_create_stack();
324 pthread_attr_init( pthread_attr_t *attr )
331 pthread_attr_destroy( pthread_attr_t *attr )
337 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
339 *detachstate = *attr;
344 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
354 pthread_attr_t *attr,
362 if ( (stack = get_stack( &stackno )) == NULL ) {
365 return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
370 lwp_create_stack( VFP func, void *arg, int stackno )
374 free_stack( stackno );
390 pthread_join( pthread_t tid, int *status )
397 pthread_kill( pthread_t tid, int sig )
404 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
406 return( mon_create( mp ) );
410 pthread_mutex_destroy( pthread_mutex_t *mp )
412 return( mon_destroy( *mp ) );
416 pthread_mutex_lock( pthread_mutex_t *mp )
418 return( mon_enter( *mp ) );
422 pthread_mutex_unlock( pthread_mutex_t *mp )
424 return( mon_exit( *mp ) );
428 pthread_mutex_trylock( pthread_mutex_t *mp )
430 return( mon_cond_enter( *mp ) );
434 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
437 * lwp cv_create requires the monitor id be passed in
438 * when the cv is created, pthreads passes it when the
439 * condition is waited for. so, we fake the creation
440 * here and actually do it when the cv is waited for
450 pthread_cond_destroy( pthread_cond_t *cv )
452 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
456 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
458 if ( ! cv->lcv_created ) {
459 cv_create( &cv->lcv_cv, *mp );
463 return( cv_wait( cv->lcv_cv ) );
467 pthread_cond_signal( pthread_cond_t *cv )
469 return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
473 pthread_cond_broadcast( pthread_cond_t *cv )
475 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
481 /***********************************************************************
483 * no threads package defined for this system - fake ok returns from *
484 * all threads routines (making it single-threaded). *
486 ***********************************************************************/
490 pthread_attr_init( pthread_attr_t *attr )
497 pthread_attr_destroy( pthread_attr_t *attr )
504 pthread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
511 pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate )
520 pthread_attr_t *attr,
544 pthread_kill( pthread_t tid, int sig )
550 pthread_join( pthread_t tid, int *status )
557 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
564 pthread_mutex_destroy( pthread_mutex_t *mp )
571 pthread_mutex_lock( pthread_mutex_t *mp )
578 pthread_mutex_unlock( pthread_mutex_t *mp )
585 pthread_mutex_trylock( pthread_mutex_t *mp )
592 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
599 pthread_cond_destroy( pthread_cond_t *cv )
606 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
613 pthread_cond_signal( pthread_cond_t *cv )
620 pthread_cond_broadcast( pthread_cond_t *cv )
625 #endif /* no threads package */