]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap_r/thr_lwp.c
Use getpassphrase() instead of getpass() if available.
[openldap] / libraries / libldap_r / thr_lwp.c
index a6998701343e805d4177b98c1b0a4419460adda8..e13efd5b104bd418e06af05457fbe9c5130fef15 100644 (file)
@@ -1,5 +1,6 @@
+/* $OpenLDAP$ */
 /*
- * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
+ * Copyright 1998-2000 The OpenLDAP Foundation, Redwood City, California, USA
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted only
@@ -16,7 +17,6 @@
  */
 
 #include "portable.h"
-#include "ldap_pvt_thread.h"
 
 #if defined( HAVE_LWP )
 
  * SunOS LWP *
  *           *
  *************/
+
+/* This implementation NEEDS WORK.   It currently does not compile */
+
 #include <stdio.h>
 
 #include <ac/time.h>
 #include <ac/socket.h>
 
-#include "lber.h"
-#include "ldap.h"
-#include "ldap_log.h"
+#include "ldap-int.h"
+
+#include "ldap_pvt_thread.h"
 
 #include <lwp/lwp.h>
 #include <lwp/stackdep.h>
 #define MAX_STACK      51200
 #define MAX_THREADS    20
 
+/*
+ * Initialize LWP by spinning of a schedular
+ */
+int
+ldap_pvt_thread_initialize( void )
+{
+       thread_t                tid;
+       stkalign_t              *stack;
+       int                     stackno;
+
+       if (( stack = get_stack( &stackno )) == NULL ) {
+               return -1;
+       }
+
+       lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
+       return 0;
+}
+
+int
+ldap_pvt_thread_destroy( void )
+{
+       /* need to destory lwp_scheduler thread and clean up private
+               variables */
+       return 0;
+}
+
 struct stackinfo {
        int             stk_inuse;
        stkalign_t      *stk_stack;
@@ -47,13 +76,19 @@ struct stackinfo {
 
 static struct stackinfo        *stacks;
 
-stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
+static stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
 {
        int     i;
 
        if ( stacks == NULL ) {
-               stacks = (struct stackinfo *) ch_calloc( 1, MAX_THREADS *
+               stacks = (struct stackinfo *) LDAP_CALLOC( 1, MAX_THREADS *
                    sizeof(struct stackinfo) );
+
+               if( stacks == NULL ) {
+                       Debug( LDAP_DEBUG_ANY, "stacks allocation failed",
+                               0, 0, 0 );
+                       return NULL;
+               }
        }
 
        for ( i = 0; i < MAX_THREADS; i++ ) {
@@ -70,9 +105,15 @@ stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
        }
 
        if ( stacks[i].stk_stack == NULL ) {
-               stacks[i].stk_stack = (stkalign_t *) malloc(
+               stacks[i].stk_stack = (stkalign_t *) LDAP_MALLOC(
                    (MAX_STACK / sizeof(stkalign_t) + 1 )
                    * sizeof(stkalign_t) );
+
+               if( stacks[i].stk_stack == NULL ) {
+                       Debug( LDAP_DEBUG_ANY, "stack allocation failed",
+                               0, 0, 0 );
+                       return( NULL );
+               }
        }
 
        *stacknop = i;
@@ -80,11 +121,11 @@ stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
        return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
 }
 
-void
+static void
 ldap_pvt_thread_free_stack( int        stackno )
 {
        if ( stackno < 0 || stackno > MAX_THREADS ) {
-               Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d",
+               Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d\n",
                    stackno, 0, 0 );
        }
 
@@ -101,8 +142,9 @@ lwp_create_stack( void *(*func)(), void *arg, int stackno )
 
 int 
 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
-                      ldap_pvt_thread_attr_t *attr,
-                      void *(*start_routine)( void *), void *arg)
+       int detach,
+       void *(*start_routine)( void *),
+       void *arg)
 {
        stkalign_t      *stack;
        int             stackno;
@@ -120,56 +162,130 @@ ldap_pvt_thread_exit( void *retval )
        lwp_destroy( SELF );
 }
 
-int 
-ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
+unsigned int
+ldap_pvt_thread_sleep(
+       unsigned int interval
+)
 {
-       lwp_join( thread );
-       return 0;
-}
+       thread_t                mylwp;
+       tl_t            *t, *nt;
+       time_t          now;
 
-int 
-ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
-{
-       return 0;
-}
 
-int 
-ldap_pvt_thread_yield( void )
-{
-       lwp_yield( SELF );
+       if ( lwp_self( &mylwp ) < 0 ) {
+               return -1;
+       }
+
+       time( &now );
+
+       mon_enter( &sglob->tsl_mon );
+
+       if ( sglob->tsl_list != NULL ) {
+               for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
+                       if ( SAMETHREAD( t->tl_tid, mylwp )) {
+                               /* We're already sleeping? */
+                               t->tl_wake = now + interval;
+                               mon_exit( &sglob->tsl_mon );
+                               lwp_suspend( mylwp );
+                               return 0;
+                       }
+               }
+       }
+
+       nt = (tl_t *) LDAP_MALLOC( sizeof( tl_t ));
+
+       if( nt == NULL ) return -1;
+
+       nt->tl_next = sglob->tsl_list;
+       nt->tl_wake = now + interval;
+       nt->tl_tid = mylwp;
+       sglob->tsl_list = nt;
+
+       mon_exit( &sglob->tsl_mon );
+
+       lwp_suspend( mylwp );
        return 0;
 }
 
-int 
-ldap_pvt_thread_attr_init( ldap_pvt_thread_attr_t *attr )
+/*
+ * The lwp_scheduler thread periodically checks to see if any threads
+ * are due to be resumed.  If there are, it resumes them.  Otherwise,
+ * it computes the lesser of ( 1 second ) or ( the minimum time until
+ * a thread need to be resumed ) and puts itself to sleep for that amount
+ * of time.
+ */
+static void
+lwp_scheduler(
+       int             stackno
+)
 {
-       *attr = 0;
-       return( 0 );
+       time_t                  now, min;
+       struct timeval          interval;
+       tl_t                    *t;
+
+       while ( !sglob->slurpd_shutdown ) {
+               mon_enter( &sglob->tsl_mon );
+
+               time( &now );
+               min = 0L;
+               if ( sglob->tsl_list != NULL ) {
+                       for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
+                               if (( t->tl_wake  > 0L ) && ( t->tl_wake < now )) {
+                                       lwp_resume( t->tl_tid );
+                                       t->tl_wake = 0L;
+                               }
+
+                               if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
+                                       min =  t->tl_wake;
+                               }
+                       }
+               }
+
+               mon_exit( &sglob->tsl_mon );
+
+               interval.tv_usec = 0L;
+               if ( min == 0L ) {
+                       interval.tv_sec = 1L;
+               } else {
+                       interval.tv_sec = min;
+               }
+
+               lwp_sleep( &interval );
+       }
+
+       mon_enter( &sglob->tsl_mon );
+
+       for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
+               lwp_resume( t->tl_tid );
+       }
+
+       mon_exit( &sglob->tsl_mon );
+
+       free_stack( stackno );
 }
 
 int 
-ldap_pvt_thread_attr_destroy( ldap_pvt_thread_attr_t *attr )
+ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
 {
-       return( 0 );
+       lwp_join( thread );
+       return 0;
 }
 
 int 
-ldap_pvt_thread_attr_setdetachstate( ldap_pvt_thread_attr_t *attr, int dstate )
+ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
 {
-       *attr = dstate;
-       return( 0 );
+       return 0;
 }
 
-int
-ldap_pvt_thread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
+int 
+ldap_pvt_thread_yield( void )
 {
-       *detachstate = *attr;
-       return( 0 );
+       lwp_yield( SELF );
+       return 0;
 }
 
 int 
-ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond, 
-                     ldap_pvt_thread_condattr_t *attr )
+ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
 {
        /*
         * lwp cv_create requires the monitor id be passed in
@@ -203,8 +319,7 @@ 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 )
+ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
 {
        return( mon_create( mutex ) );
 }