]> git.sur5r.net Git - openldap/blobdiff - libraries/liblthread/thread.c
Moved strdup/tempnam to liblutil
[openldap] / libraries / liblthread / thread.c
index 8e4e4995bec00ec1ce8255e1b407b19b83c1d3d7..0e7ebdd30561bd34b7347bb8b424fc64ef9519f9 100644 (file)
@@ -1,11 +1,28 @@
 /* thread.c - glue routines to provide a consistent thread interface */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <lthread.h>
 
-#if defined( THREAD_NEXT_CTHREADS )
+#if defined( HAVE_PTHREADS )
+
+#ifdef HAVE_DCE
+/***********************************************************************
+ *                                                                     *
+ * pthreads package with DCE - no mapping to do (except to create a    *
+ * pthread_kill() routine)                                             *
+ *                                                                     *
+ ***********************************************************************/
+
+/* ARGSUSED */
+void
+pthread_kill( pthread_t tid, int sig )
+{
+       kill( getpid(), sig );
+}
+#endif /* DCE */
+
+#elif defined( HAVE_MACH_CTHREADS )
 
 /***********************************************************************
  *                                                                     *
@@ -155,17 +172,15 @@ pthread_cond_broadcast( pthread_cond_t *cv )
        return( 0 );
 }
 
-#elif defined( THREAD_SUNOS4_LWP )
-
-/***********************************************************************
- *                                                                     *
- * under sunos 4 - use the built in non-preemptive lwp threads package *
- *                                                                     *
- ***********************************************************************/
+#elif defined( HAVE_LWP_THR )
 
-extern stkalign_t      *get_stack();
-static void            lwp_create_stack();
+/*******************
+ *                 *
+ * Solaris Threads *
+ *                 *
+ *******************/
 
+#if !defined(__SunOS_5_6)
 int
 pthread_attr_init( pthread_attr_t *attr )
 {
@@ -176,6 +191,7 @@ pthread_attr_init( pthread_attr_t *attr )
 int
 pthread_attr_destroy( pthread_attr_t *attr )
 {
+       *attr = 0;
        return( 0 );
 }
 
@@ -202,136 +218,108 @@ pthread_create(
     void               *arg
 )
 {
-       stkalign_t      *stack;
-       int             stackno;
-
-       if ( (stack = get_stack( &stackno )) == NULL ) {
-               return( -1 );
-       }
-       return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
-           arg, stackno ) );
-}
-
-static void
-lwp_create_stack( VFP func, void *arg, int stackno )
-{
-       (*func)( arg );
-
-       free_stack( stackno );
+       return( thr_create( NULL, 0, func, arg, *attr, tid ) );
 }
+#endif /* ! sunos56 */
 
 void
 pthread_yield()
 {
-       lwp_yield( SELF );
+       thr_yield();
 }
 
+#if !defined(__SunOS_5_6)
 void
 pthread_exit()
 {
-       lwp_destroy( SELF );
+       thr_exit( NULL );
 }
 
 void
 pthread_join( pthread_t tid, int *status )
 {
-       lwp_join( tid );
+       thr_join( tid, NULL, (void **) status );
 }
 
-/* ARGSUSED */
 void
 pthread_kill( pthread_t tid, int sig )
 {
-       return;
+       thr_kill( tid, sig );
 }
 
 /* ARGSUSED */
 int
 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
 {
-       return( mon_create( mp ) );
+       return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
 }
 
 int
 pthread_mutex_destroy( pthread_mutex_t *mp )
 {
-       return( mon_destroy( *mp ) );
+       return( mutex_destroy( mp ) );
 }
 
 int
 pthread_mutex_lock( pthread_mutex_t *mp )
 {
-       return( mon_enter( *mp ) );
+       return( mutex_lock( mp ) );
 }
 
 int
 pthread_mutex_unlock( pthread_mutex_t *mp )
 {
-       return( mon_exit( *mp ) );
+       return( mutex_unlock( mp ) );
 }
 
 int
 pthread_mutex_trylock( pthread_mutex_t *mp )
 {
-       return( mon_cond_enter( *mp ) );
+       return( mutex_trylock( mp ) );
 }
 
 int
 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
 {
-       /*
-        * lwp cv_create requires the monitor id be passed in
-        * when the cv is created, pthreads passes it when the
-        * condition is waited for.  so, we fake the creation
-        * here and actually do it when the cv is waited for
-        * later.
-        */
-
-       cv->lcv_created = 0;
-
-       return( 0 );
+       return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
 }
 
 int
 pthread_cond_destroy( pthread_cond_t *cv )
 {
-       return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
+       return( cond_destroy( cv ) );
 }
 
 int
 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
 {
-       if ( ! cv->lcv_created ) {
-               cv_create( &cv->lcv_cv, *mp );
-               cv->lcv_created = 1;
-       }
-
-       return( cv_wait( cv->lcv_cv ) );
+       return( cond_wait( cv, mp ) );
 }
 
 int
 pthread_cond_signal( pthread_cond_t *cv )
 {
-       return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
+       return( cond_signal( cv ) );
 }
 
 int
 pthread_cond_broadcast( pthread_cond_t *cv )
 {
-       return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
+       return( cond_broadcast( cv ) );
 }
+#endif /* ! sunos56 */
 
-#else /* end sunos4 */
+#elif defined( HAVE_LWP )
 
-#  if defined( THREAD_SUNOS5_LWP )
+/*************
+ *           *
+ * SunOS LWP *
+ *           *
+ *************/
 
-/***********************************************************************
- *                                                                     *
- * under sunos 5 - use the built in preemptive solaris threads package *
- *                                                                     *
- ***********************************************************************/
+extern stkalign_t      *get_stack();
+static void            lwp_create_stack();
 
-#if !defined(__SunOS_5_6)
 int
 pthread_attr_init( pthread_attr_t *attr )
 {
@@ -342,7 +330,6 @@ pthread_attr_init( pthread_attr_t *attr )
 int
 pthread_attr_destroy( pthread_attr_t *attr )
 {
-       *attr = 0;
        return( 0 );
 }
 
@@ -369,152 +356,128 @@ pthread_create(
     void               *arg
 )
 {
-       return( thr_create( NULL, 0, func, arg, *attr, tid ) );
+       stkalign_t      *stack;
+       int             stackno;
+
+       if ( (stack = get_stack( &stackno )) == NULL ) {
+               return( -1 );
+       }
+       return( lwp_create( tid, lwp_create_stack, MINPRIO, 0, stack, 3, func,
+           arg, stackno ) );
+}
+
+static void
+lwp_create_stack( VFP func, void *arg, int stackno )
+{
+       (*func)( arg );
+
+       free_stack( stackno );
 }
-#endif /* ! sunos56 */
 
 void
 pthread_yield()
 {
-       thr_yield();
+       lwp_yield( SELF );
 }
 
-#if !defined(__SunOS_5_6)
 void
 pthread_exit()
 {
-       thr_exit( NULL );
+       lwp_destroy( SELF );
 }
 
 void
 pthread_join( pthread_t tid, int *status )
 {
-       thr_join( tid, NULL, (void **) status );
+       lwp_join( tid );
 }
 
+/* ARGSUSED */
 void
 pthread_kill( pthread_t tid, int sig )
 {
-       thr_kill( tid, sig );
+       return;
 }
 
 /* ARGSUSED */
 int
 pthread_mutex_init( pthread_mutex_t *mp, pthread_mutexattr_t *attr )
 {
-       return( mutex_init( mp, attr ? *attr : USYNC_THREAD, NULL ) );
+       return( mon_create( mp ) );
 }
 
 int
 pthread_mutex_destroy( pthread_mutex_t *mp )
 {
-       return( mutex_destroy( mp ) );
+       return( mon_destroy( *mp ) );
 }
 
 int
 pthread_mutex_lock( pthread_mutex_t *mp )
 {
-       return( mutex_lock( mp ) );
+       return( mon_enter( *mp ) );
 }
 
 int
 pthread_mutex_unlock( pthread_mutex_t *mp )
 {
-       return( mutex_unlock( mp ) );
+       return( mon_exit( *mp ) );
 }
 
 int
 pthread_mutex_trylock( pthread_mutex_t *mp )
 {
-       return( mutex_trylock( mp ) );
+       return( mon_cond_enter( *mp ) );
 }
 
 int
 pthread_cond_init( pthread_cond_t *cv, pthread_condattr_t *attr )
 {
-       return( cond_init( cv, attr ? *attr : USYNC_THREAD, NULL ) );
+       /*
+        * lwp cv_create requires the monitor id be passed in
+        * when the cv is created, pthreads passes it when the
+        * condition is waited for.  so, we fake the creation
+        * here and actually do it when the cv is waited for
+        * later.
+        */
+
+       cv->lcv_created = 0;
+
+       return( 0 );
 }
 
 int
 pthread_cond_destroy( pthread_cond_t *cv )
 {
-       return( cond_destroy( cv ) );
+       return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
 }
 
 int
 pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp )
 {
-       return( cond_wait( cv, mp ) );
+       if ( ! cv->lcv_created ) {
+               cv_create( &cv->lcv_cv, *mp );
+               cv->lcv_created = 1;
+       }
+
+       return( cv_wait( cv->lcv_cv ) );
 }
 
 int
 pthread_cond_signal( pthread_cond_t *cv )
 {
-       return( cond_signal( cv ) );
+       return( cv->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
 }
 
 int
 pthread_cond_broadcast( pthread_cond_t *cv )
 {
-       return( cond_broadcast( cv ) );
+       return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
 }
-#endif /* ! sunos56 */
-
-
-#else /* end sunos5 threads */
-
-#if defined( THREAD_MIT_PTHREADS )
-
-/***********************************************************************
- *                                                                     *
- * pthreads package by Chris Provenzano of MIT - provides all the      *
- * pthreads calls already, so no mapping to do                         *
- *                                                                     *
- ***********************************************************************/
 
-#else /* end mit pthreads */
-
-#if defined( THREAD_DCE_PTHREADS )
-
-/***********************************************************************
- *                                                                     *
- * pthreads package with DCE - no mapping to do (except to create a    *
- * pthread_kill() routine)                                             *
- *                                                                     *
- ***********************************************************************/
-
-/* ARGSUSED */
-void
-pthread_kill( pthread_t tid, int sig )
-{
-       kill( getpid(), sig );
-}
 
 #else
 
-#if defined ( POSIX_THREADS )
-
-#ifdef HAVE_SCHED_YIELD
-#ifdef HAVE_SCHED_H
-#include <sched.h>
-#endif /* HAVE_SCHED_H */
-
-/* POSIX Threads (final) does have a pthread_yield function */
-void pthread_yield( void )
-{
-       sched_yield();
-}
-
-#endif /* HAVE_SCHED_YIELD */
-
-#endif /* posix threads */
-#endif /* dce pthreads */
-#endif /* mit pthreads */
-#endif /* sunos5 lwp */
-#endif /* sunos4 lwp */
-
-#ifndef _THREAD
-
 /***********************************************************************
  *                                                                     *
  * no threads package defined for this system - fake ok returns from   *