]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap_r/tpool.c
happy new year
[openldap] / libraries / libldap_r / tpool.c
index 2d313f7396dc4172fd977c347cb296061beede08..3c70217f9932f47fc4294536b652de7702428552 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2006 The OpenLDAP Foundation.
+ * Copyright 1998-2007 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -111,7 +111,6 @@ ldap_int_thread_pool_shutdown ( void )
        struct ldap_int_thread_pool_s *pool;
 
        while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
-               LDAP_STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
                (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
        }
        ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
@@ -353,7 +352,6 @@ int
 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
 {
        struct ldap_int_thread_pool_s *pool, *pptr;
-       long waiting;
        ldap_int_thread_ctx_t *ctx;
 
        if (tpool == NULL)
@@ -378,16 +376,12 @@ ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
                ? LDAP_INT_THREAD_POOL_FINISHING
                : LDAP_INT_THREAD_POOL_STOPPING;
 
-       ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
+       if ( pool->ltp_open_count ) {
+               ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
+               ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
+       }
        ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
 
-       do {
-               ldap_pvt_thread_yield();
-               ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
-               waiting = pool->ltp_open_count;
-               ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
-       } while (waiting > 0);
-
        while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
        {
                LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
@@ -461,6 +455,9 @@ ldap_int_thread_pool_wrapper (
                         * should be like this:
                         *   if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
                         *       check timer, leave thread (break;)
+                        *
+                        * Just use pthread_cond_timedwait if we want to
+                        * check idle time.
                         */
 
                        if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING
@@ -492,15 +489,6 @@ ldap_int_thread_pool_wrapper (
                        }
                        ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
                }
-               ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
-
-               ldap_pvt_thread_yield();
-
-               /* if we use an idle timer, here's
-                * a good place to update it
-                */
-
-               ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
        }
 
        for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
@@ -514,6 +502,11 @@ ldap_int_thread_pool_wrapper (
        thread_keys[keyslot].id = tid_zero;
 
        pool->ltp_open_count--;
+
+       /* let pool_destroy know we're all done */
+       if (pool->ltp_open_count < 1)
+               ldap_pvt_thread_cond_signal(&pool->ltp_cond);
+
        ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
 
        ldap_pvt_thread_exit(NULL);