]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap_r/thr_posix.c
Use 'long' for LBER_INT_T.
[openldap] / libraries / libldap_r / thr_posix.c
index 9bd2b0a29cdb846288407427ba8453441bc5f688..c44bee48c315ff463008ef9177c3b2b500db6dc7 100644 (file)
@@ -1,23 +1,97 @@
-/* thrposix.c - wrapper around posix and posixish thread implementations.
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted only
+ * as authorized by the OpenLDAP Public License.  A copy of this
+ * license is available at http://www.OpenLDAP.org/license.html or
+ * in file LICENSE in the top-level directory of the distribution.
+ */
+
+/* thr_posix.c - wrapper around posix and posixish thread implementations.
  */
 
 #include "portable.h"
-#include "ldap_pvt_thread.h"
 
 #if defined( HAVE_PTHREADS )
 
+#include <ac/errno.h>
+
+#include "ldap_pvt_thread.h"
+
+
+#if HAVE_PTHREADS_D4
+#  define LDAP_PVT_THREAD_ATTR_DEFAULT         pthread_attr_default
+#  define LDAP_PVT_THREAD_CONDATTR_DEFAULT     pthread_condattr_default
+#  define LDAP_PVT_THREAD_MUTEXATTR_DEFAULT    pthread_mutexattr_default
+#else
+#  define LDAP_PVT_THREAD_ATTR_DEFAULT         NULL
+#  define LDAP_PVT_THREAD_CONDATTR_DEFAULT     NULL
+#  define LDAP_PVT_THREAD_MUTEXATTR_DEFAULT    NULL
+#endif
+
+
+int
+ldap_pvt_thread_initialize( void )
+{
+#if defined( LDAP_THREAD_CONCURRENCY ) && HAVE_PTHREAD_SETCONCURRENCY
+       ldap_pvt_thread_set_concurrency( LDAP_THREAD_CONCURRENCY );
+#endif
+       return 0;
+}
+
+int
+ldap_pvt_thread_destroy( void )
+{
+       return 0;
+}
+
+#ifdef HAVE_PTHREAD_SETCONCURRENCY
+int
+ldap_pvt_thread_set_concurrency(int n)
+{
+#ifdef HAVE_PTHREAD_SETCONCURRENCY
+       return pthread_setconcurrency( n );
+#elif HAVE_THR_SETCONCURRENCY
+       return pthread_setconcurrency( n );
+#else
+       return 0;
+#endif
+}
+#endif
+
+#ifdef HAVE_PTHREAD_GETCONCURRENCY
+int
+ldap_pvt_thread_get_concurrency(void)
+{
+#ifdef HAVE_PTHREAD_GETCONCURRENCY
+       return pthread_getconcurrency();
+#elif HAVE_THR_GETCONCURRENCY
+       return pthread_getconcurrency();
+#else
+       return 0;
+#endif
+}
+#endif
+
 int 
-ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
-                       ldap_pvt_thread_attr_t *attr,
-                      void *(*start_routine)( void *), void *arg)
+ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
+       int detach,
+       void *(*start_routine)( void * ),
+       void *arg)
 {
-#if !defined( HAVE_PTHREADS_D4 )
-       /* This is a standard pthreads implementation. */
-       return pthread_create( thread, attr, start_routine, arg );
+       int rtn = pthread_create( thread, LDAP_PVT_THREAD_ATTR_DEFAULT,
+                                 start_routine, arg );
+
+       if( detach ) {
+#ifdef HAVE_PTHREADS_FINAL
+               pthread_detach( *thread );
 #else
-       /* This is a draft 4 or earlier implementation. */
-       return pthread_create( thread, *attr, start_routine, arg );
-#endif 
+               pthread_detach( thread );
+#endif
+       }
+       return rtn;
 }
 
 void 
@@ -44,7 +118,7 @@ ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
        return pthread_kill( thread, signo );
 #else
        /* pthread package with DCE */
-       if (kill( getpid(), sig )<0)
+       if (kill( getpid(), signo )<0)
                return errno;
        return 0;
 #endif
@@ -53,59 +127,35 @@ ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
 int 
 ldap_pvt_thread_yield( void )
 {
-#ifdef HAVE_SCHED_YIELD
+#ifdef _POSIX_THREAD_IS_GNU_PTH
+       sched_yield();
+       return 0;
+
+#elif HAVE_SCHED_YIELD
        return sched_yield();
+
+#elif HAVE_PTHREAD_YIELD
+       pthread_yield();
+       return 0;
+
+#elif HAVE_THR_YIELD
+       return thr_yield();
+
 #else
-       return pthread_yield();
+       return 0;
 #endif   
 }
 
 int 
-ldap_pvt_thread_attr_init( ldap_pvt_thread_attr_t *attr )
+ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
 {
-#if defined( HAVE_PTHREAD_ATTR_INIT )
-       return pthread_attr_init( attr );
-#elif defined( HAVE_PTHREAD_ATTR_CREATE )
-       return pthread_attr_create( attr );
-#else
-       No way to init attr, so cause an error.
-#endif
-}
-       
-int 
-ldap_pvt_thread_attr_destroy( ldap_pvt_thread_attr_t *attr )
-{
-#if defined( HAVE_PTHREAD_ATTR_DESTROY )
-       return pthread_attr_destroy( attr );
-#elif defined( HAVE_PTHREAD_ATTR_DELETE )
-       return pthread_attr_delete( attr );
-#else
-       No way to destroy attr, so cause an error.
-#endif
+       return pthread_cond_init( cond, LDAP_PVT_THREAD_CONDATTR_DEFAULT );
 }
 
 int 
-ldap_pvt_thread_attr_setdetachstate( ldap_pvt_thread_attr_t *attr, int dstate )
+ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
 {
-#if defined( HAVE_PTHREAD_ATTR_SETDETACHSTATE )
-       return pthread_attr_setdetachstate( attr, dstate );
-#elif defined( HAVE_PTHREAD_ATTR_SETDETACH_NP )
-       return pthread_attr_setdetach_np( attr, dstate );
-#else
-       No way to set state, so cause an error.
-#endif
-}
-
-int 
-ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond, 
-                         ldap_pvt_thread_condattr_t *attr )
-{
-#if defined( HAVE_PTHREADS_D4 )
-       return pthread_cond_init( cond, 
-                                attr ? attr : pthread_condattr_default );
-#else  
-       return pthread_cond_init( cond, attr );
-#endif 
+       return pthread_cond_destroy( cond );
 }
        
 int 
@@ -128,15 +178,9 @@ ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
 }
 
 int 
-ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex,
-                          ldap_pvt_thread_mutexattr_t *attr )
-{
-#if defined( HAVE_PTHREADS_D4 )
-       return pthread_mutex_init( mutex,
-                                 attr ? attr : pthread_mutexattr_default );
-#else      
-       return pthread_mutex_init( mutex, attr );
-#endif     
+ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
+{
+       return pthread_mutex_init( mutex, LDAP_PVT_THREAD_MUTEXATTR_DEFAULT );
 }
 
 int