]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap_r/tpool.c
Restore pre-C99 preprocessor support (since rev 1.165)
[openldap] / libraries / libldap_r / tpool.c
index 705016e26760c379e12d46dfb3740d4fa6049d9d..81ebd92ed63b5d59a0a393a429298d1ce980cd7f 100644 (file)
@@ -38,11 +38,11 @@ typedef enum ldap_int_thread_pool_state_e {
 } ldap_int_thread_pool_state_t;
 
 /* Thread-specific key with data and optional free function */
-typedef struct ldap_int_thread_key_s {
+typedef struct ldap_int_tpool_key_s {
        void *ltk_key;
        void *ltk_data;
        ldap_pvt_thread_pool_keyfree_t *ltk_free;
-} ldap_int_thread_key_t;
+} ldap_int_tpool_key_t;
 
 /* Max number of thread-specific keys we store per thread.
  * We don't expect to use many...
@@ -52,13 +52,10 @@ typedef struct ldap_int_thread_key_s {
 /* Max number of threads */
 #define        LDAP_MAXTHR     1024    /* must be a power of 2 */
 
-/* Max number of pending tasks */
-#define LDAP_MAX_PENDING (INT_MAX - LDAP_MAXTHR)
-
 /* Context: thread ID and thread-specific key/data pairs */
 typedef struct ldap_int_thread_userctx_s {
        ldap_pvt_thread_t ltu_id;
-       ldap_int_thread_key_t ltu_key[MAXKEYS];
+       ldap_int_tpool_key_t ltu_key[MAXKEYS];
 } ldap_int_thread_userctx_t;
 
 
@@ -112,8 +109,8 @@ struct ldap_int_thread_pool_s {
        /* some active request needs to be the sole active request */
        int ltp_pause;
 
-       long ltp_max_count;                     /* max number of threads in pool */
-       long ltp_max_pending;           /* max pending or paused requests */
+       long ltp_max_count;                     /* max number of threads in pool, or 0 */
+       long ltp_max_pending;           /* max pending or paused requests, or 0 */
        long ltp_pending_count;         /* pending or paused requests */
        long ltp_active_count;          /* active, not paused requests */
        long ltp_open_count;            /* number of threads */
@@ -129,6 +126,8 @@ static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
 
 static void *ldap_int_thread_pool_wrapper( void *pool );
 
+static ldap_pvt_thread_key_t   ldap_tpool_key;
+
 /* Context of the main thread */
 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
 
@@ -136,6 +135,7 @@ int
 ldap_int_thread_pool_startup ( void )
 {
        ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
+       ldap_pvt_thread_key_create( &ldap_tpool_key );
        return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
 }
 
@@ -148,6 +148,7 @@ ldap_int_thread_pool_shutdown ( void )
                (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
        }
        ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
+       ldap_pvt_thread_key_destroy( ldap_tpool_key );
        return(0);
 }
 
@@ -167,6 +168,8 @@ ldap_pvt_thread_pool_init (
 
        if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
                max_threads = 0;
+       if (max_pending < 0)
+               max_pending = 0;
 
        *tpool = NULL;
        pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
@@ -254,7 +257,7 @@ ldap_pvt_thread_pool_submit (
 
        ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
        if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
-               || (pool->ltp_max_pending > 0
+               || (pool->ltp_max_pending
                        && pool->ltp_pending_count >= pool->ltp_max_pending))
        {
                ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
@@ -398,6 +401,10 @@ ldap_pvt_thread_pool_query(
                count = pool->ltp_active_count;
                break;
 
+       case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
+               count = pool->ltp_pause;
+               break;
+
        case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
                count = pool->ltp_pending_count;
                break;
@@ -525,6 +532,7 @@ ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
        ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
        ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
        LDAP_FREE(pool);
+       *tpool = NULL;
        ldap_int_has_thread_pool = 0;
        return(0);
 }
@@ -548,6 +556,8 @@ ldap_int_thread_pool_wrapper (
        ctx.ltu_id = ldap_pvt_thread_self();
        TID_HASH(ctx.ltu_id, hash);
 
+       ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
+
        ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
 
        /* thread_keys[] is read-only when paused */
@@ -699,7 +709,8 @@ ldap_pvt_thread_pool_resume (
 
        ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
        pool->ltp_pause = 0;
-       ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
+       if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
+               ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
        ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
        return(0);
 }
@@ -814,29 +825,10 @@ void ldap_pvt_thread_pool_purgekey( void *key )
  */
 void *ldap_pvt_thread_pool_context( )
 {
-       ldap_pvt_thread_t tid;
-       unsigned i, hash;
-       ldap_int_thread_userctx_t *ctx;
-
-       tid = ldap_pvt_thread_self();
-       if ( ldap_pvt_thread_equal( tid, ldap_int_main_thrctx.ltu_id ))
-               return &ldap_int_main_thrctx;
-
-       TID_HASH( tid, hash );
-       i = hash &= (LDAP_MAXTHR-1);
-       ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
-       do {
-               ctx = thread_keys[i].ctx;
-               if ( ctx == DELETED_THREAD_CTX )
-                       continue;
-               if ( !ctx || ldap_pvt_thread_equal(thread_keys[i].ctx->ltu_id, tid) )
-                       goto done;
-       } while ( (i = (i+1) & (LDAP_MAXTHR-1)) != hash );
-       ctx = NULL;
- done:
-       ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
+       void *ctx = NULL;
 
-       return ctx;
+       ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
+       return ctx ? ctx : &ldap_int_main_thrctx;
 }
 
 /*