]> git.sur5r.net Git - openldap/commitdiff
Use thread-specific data in pool_context()
authorHoward Chu <hyc@openldap.org>
Thu, 25 Oct 2007 06:48:44 +0000 (06:48 +0000)
committerHoward Chu <hyc@openldap.org>
Thu, 25 Oct 2007 06:48:44 +0000 (06:48 +0000)
libraries/libldap_r/tpool.c

index 8309089cc746e6254f8be9a4c511fe78d789b016..5d883c8fa7f8d6b6221a34689c192b9129de6e24 100644 (file)
@@ -126,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;
 
@@ -133,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);
 }
 
@@ -145,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);
 }
 
@@ -552,6 +556,8 @@ ldap_int_thread_pool_wrapper (
        ctx.ltu_id = ldap_pvt_thread_self();
        TID_HASH(ctx.ltu_id, hash);
 
+       ldap_pvt_thread_setdata( ldap_tpool_key, &ctx );
+
        ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
 
        /* thread_keys[] is read-only when paused */
@@ -819,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;
 }
 
 /*