]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap_r/tpool.c
Emulate mkstemp() using mktemp() if mkstemp() is not found by configure
[openldap] / libraries / libldap_r / tpool.c
index ba44fb22cc7284241844ebaae83eaf6ce8af6f51..6d014d70ff0dc58ca36ca61c490df9c00c762582 100644 (file)
@@ -12,8 +12,8 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdarg.h>
 
+#include <ac/stdarg.h>
 #include <ac/stdlib.h>
 #include <ac/string.h>
 #include <ac/time.h>
@@ -61,7 +61,9 @@ static void *ldap_int_thread_pool_wrapper(
 
 static void *ldap_int_thread_enlist( ldap_int_thread_list_t *list, void *elem );
 static void *ldap_int_thread_delist( ldap_int_thread_list_t *list, void *elem );
+#if 0
 static void *ldap_int_thread_onlist( ldap_int_thread_list_t *list, void *elem );
+#endif
 
 int
 ldap_int_thread_pool_startup ( void )
@@ -85,7 +87,7 @@ ldap_int_thread_pool_shutdown ( void )
 int
 ldap_pvt_thread_pool_init (
        ldap_pvt_thread_pool_t *tpool,
-       int max_concurrency,
+       int max_threads,
        int max_pending )
 {
        ldap_pvt_thread_pool_t pool;
@@ -104,7 +106,7 @@ ldap_pvt_thread_pool_init (
        if (rc != 0)
                return(rc);
        pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
-       pool->ltp_max_count = max_concurrency;
+       pool->ltp_max_count = max_threads;
        pool->ltp_max_pending = max_pending;
        ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
        ldap_int_thread_enlist(&ldap_int_thread_pool_list, pool);
@@ -120,7 +122,7 @@ ldap_pvt_thread_pool_init (
         * but ltp_open_count == 1, so two things happen: 
         * 1) the first client connection fails, and 2) when
         * slapd is kill'ed, it never terminates since it waits
-        * for all worker threads to exit.
+        * for all worker threads to exit. */
 
        /* start up one thread, just so there is one. no need to
         * lock the mutex right now, since no threads are running.
@@ -235,6 +237,25 @@ ldap_pvt_thread_pool_submit (
        return(0);
 }
 
+int
+ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
+{
+       struct ldap_int_thread_pool_s *pool;
+
+       if (tpool == NULL)
+               return(-1);
+
+       pool = *tpool;
+
+       if (pool == NULL)
+               return(-1);
+
+       ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
+       pool->ltp_max_count = max_threads;
+       ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
+       return(0);
+}
+
 int
 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
 {
@@ -296,8 +317,8 @@ ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
                ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
        } while (waiting > 0);
 
-       while (ctx = (ldap_int_thread_ctx_t *)ldap_int_thread_delist(
-               &pool->ltp_pending_list, NULL))
+       while ((ctx = (ldap_int_thread_ctx_t *)ldap_int_thread_delist(
+               &pool->ltp_pending_list, NULL)) != NULL)
        {
                free(ctx);
        }
@@ -325,6 +346,17 @@ ldap_int_thread_pool_wrapper (
                if (ctx == NULL) {
                        if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
                                break;
+                       if (pool->ltp_max_count > 0
+                               && pool->ltp_open_count > pool->ltp_max_count)
+                       {
+                               /* too many threads running (can happen if the
+                                * maximum threads value is set during ongoing
+                                * operation using ldap_pvt_thread_pool_maxthreads)
+                                * so let this thread die.
+                                */
+                               break;
+                       }
+
                        /* we could check an idle timer here, and let the
                         * thread die if it has been inactive for a while.
                         * only die if there are other open threads (i.e.,
@@ -403,7 +435,7 @@ ldap_int_thread_delist( ldap_int_thread_list_t *list, void *elem )
        }
        return(NULL);
 }
-
+#if 0
 static void *
 ldap_int_thread_onlist( ldap_int_thread_list_t *list, void *elem )
 {
@@ -418,5 +450,5 @@ ldap_int_thread_onlist( ldap_int_thread_list_t *list, void *elem )
 
        return(NULL);
 }
-
+#endif
 #endif /* LDAP_HAVE_THREAD_POOL */