]> git.sur5r.net Git - openldap/commitdiff
Added ldap_pvt_thread_self() and ldap_pvt_thread_pool_context() API.
authorLuke Howard <lukeh@openldap.org>
Fri, 24 Jan 2003 06:49:13 +0000 (06:49 +0000)
committerLuke Howard <lukeh@openldap.org>
Fri, 24 Jan 2003 06:49:13 +0000 (06:49 +0000)
include/ldap_pvt_thread.h
libraries/libldap_r/thr_cthreads.c
libraries/libldap_r/thr_lwp.c
libraries/libldap_r/thr_nt.c
libraries/libldap_r/thr_posix.c
libraries/libldap_r/thr_pth.c
libraries/libldap_r/thr_stub.c
libraries/libldap_r/thr_thr.c
libraries/libldap_r/tpool.c

index 643ee2b8bb81630a95ed07a653f1a9433266aa13..d02359ad9e4eb49bfae70b73bb1a5425cc68f638 100644 (file)
@@ -97,6 +97,9 @@ ldap_pvt_thread_mutex_trylock LDAP_P(( ldap_pvt_thread_mutex_t *mutex ));
 LDAP_F( int )
 ldap_pvt_thread_mutex_unlock LDAP_P(( ldap_pvt_thread_mutex_t *mutex ));
 
+LDAP_F( ldap_pvt_thread_t )
+ldap_pvt_thread_self LDAP_P(( void ));
+
 #ifndef LDAP_THREAD_HAVE_RDWR
 typedef struct ldap_int_thread_rdwr_s * ldap_pvt_thread_rdwr_t;
 #endif
@@ -184,6 +187,10 @@ ldap_pvt_thread_pool_setkey LDAP_P((
        void *data,
        ldap_pvt_thread_pool_keyfree_t *kfree ));
 
+LDAP_F( void *)
+ldap_pvt_thread_pool_context LDAP_P((
+       ldap_pvt_thread_pool_t *pool ));
+
 LDAP_END_DECL
 
 #endif /* _LDAP_THREAD_H */
index 3b6023a7770d47fab7da7adc6dcb2e5ec0536582..73dff64219f9ed05581a3fed531df3eadf153232 100644 (file)
@@ -146,4 +146,10 @@ ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
        return mutex_try_lock( mutex );
 }
 
+ldap_pvt_thread_t
+ldap_pvt_thread_self( void )
+{
+       return cthread_self();
+}
+
 #endif /* HAVE_MACH_CTHREADS */
index 11488b8142579895ab3405193801b45505473831..eff722f0836b716028ee6ba3fb6a9a1556633a23 100644 (file)
@@ -360,4 +360,14 @@ ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
        return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
 }
 
+ldap_pvt_thread_t
+ldap_pvt_thread_self( void )
+{
+       thread_t                mylwp;
+
+       lwp_self( &mylwp );
+
+       return mylwp;
+}
+
 #endif /* HAVE_LWP */
index 33fc4ad95128f5e43c2c890736e73b98f089dbba..9cb0025561fca554f8a7f859bbb3daad3273b555 100644 (file)
@@ -152,4 +152,10 @@ ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
                ? -1 : 0;
 }
 
+ldap_pvt_thread_t
+ldap_pvt_thread_self( void )
+{
+       return GetCurrentThread();
+}
+
 #endif
index 5a9c095ff22098df11b56cdc6e5f014c2b0b53c9..825761e33035c9e237b695c977b8dd17fc746616 100644 (file)
@@ -311,6 +311,11 @@ ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
 #endif
 }
 
+ldap_pvt_thread_t ldap_pvt_thread_self( void )
+{
+       return pthread_self();
+}
+
 #ifdef LDAP_THREAD_HAVE_RDWR
 #ifdef HAVE_PTHREAD_RWLOCK_DESTROY
 int 
index 92814a6bb05286bada6cb222c52fac17d2270a44..94e4391947e7036f8160f65383595aed310b4c19 100644 (file)
@@ -146,6 +146,12 @@ ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
        return( pth_mutex_acquire( mutex, 1, NULL ) ? 0 : errno );
 }
 
+ldap_pvt_thread_t
+ldap_pvt_thread_self( void )
+{
+       return pth_self();
+}
+
 #ifdef LDAP_THREAD_HAVE_RDWR
 int 
 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
index c775525b648e21b53921033660ef394e6ae335cb..021c95335c27e37a102ba65f9876bc9c9b81ba39 100644 (file)
@@ -191,4 +191,11 @@ int ldap_pvt_thread_pool_setkey (
 {
        return(0);
 }
+
+ldap_pvt_thread_t
+ldap_pvt_thread_self( void )
+{
+       return(0);
+}
+
 #endif /* NO_THREADS */
index eafccffb3f00f8576811c5eba19e7b6ca89a958d..c86fa542d3a7cc35171eed26b216b39fddede6b4 100644 (file)
@@ -149,4 +149,10 @@ ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
        return( mutex_trylock( mp ) );
 }
 
+ldap_pvt_thread_t
+ldap_pvt_thread_self( void )
+{
+       return thr_self();
+}
+
 #endif /* HAVE_THR */
index ce9eb28f2c2ff35649c23eabe89d4d2b16320de0..d64888d482cebd474a80e32fe02799cd5a5a1256 100644 (file)
@@ -46,9 +46,11 @@ typedef struct ldap_int_thread_ctx_s {
        union {
        LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
        LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
+       LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
        } ltc_next;
        ldap_pvt_thread_start_t *ltc_start_routine;
        void *ltc_arg;
+       ldap_pvt_thread_t ltc_thread_id;
        ldap_int_thread_key_t ltc_key[MAXKEYS];
 } ldap_int_thread_ctx_t;
 
@@ -58,6 +60,7 @@ struct ldap_int_thread_pool_s {
        ldap_pvt_thread_cond_t ltp_cond;
        LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
        LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
+       LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
        long ltp_state;
        long ltp_max_count;
        long ltp_max_pending;
@@ -120,6 +123,7 @@ ldap_pvt_thread_pool_init (
        pool->ltp_max_pending = max_pending;
        LDAP_STAILQ_INIT(&pool->ltp_pending_list);
        LDAP_SLIST_INIT(&pool->ltp_free_list);
+       LDAP_SLIST_INIT(&pool->ltp_active_list);
        ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
        LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
        ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
@@ -373,6 +377,11 @@ ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
                LDAP_FREE(ctx);
        }
 
+       while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_active_list)) != NULL)
+       {
+               LDAP_SLIST_REMOVE_HEAD(&pool->ltp_active_list, ltc_next.al);
+       }
+
        ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
        ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
        LDAP_FREE(pool);
@@ -425,9 +434,11 @@ ldap_int_thread_pool_wrapper (
                }
 
                pool->ltp_pending_count--;
+               LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
                pool->ltp_active_count++;
                ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
 
+               ctx->ltc_thread_id = ldap_pvt_thread_self();
                ctx->ltc_start_routine(ctx, ctx->ltc_arg);
 
                ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
@@ -442,6 +453,10 @@ ldap_int_thread_pool_wrapper (
 
                ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
                pool->ltp_active_count--;
+               ctx = LDAP_SLIST_FIRST(&pool->ltp_active_list);
+               if (ctx) {
+                       LDAP_SLIST_REMOVE_HEAD(&pool->ltp_active_list, ltc_next.al);
+               }
        }
 
        pool->ltp_open_count--;
@@ -494,4 +509,36 @@ int ldap_pvt_thread_pool_setkey(
        }
        return ENOMEM;
 }
+
+/*
+ * This is necessary if the caller does not have access to the
+ * thread context handle (for example, a slapd plugin calling
+ * slapi_search_internal()). No doubt it is more efficient to
+ * for the application to keep track of the thread context
+ * handles itself.
+ */
+void *ldap_pvt_thread_pool_context( ldap_pvt_thread_pool_t *tpool )
+{
+       ldap_pvt_thread_pool_t pool;
+       ldap_pvt_thread_t tid;
+       ldap_int_thread_ctx_t *ptr;
+
+       pool = *tpool;
+       if (pool == NULL) {
+               return NULL;
+       }
+
+       tid = ldap_pvt_thread_self();
+
+       ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
+       LDAP_SLIST_FOREACH(ptr, &pool->ltp_active_list, ltc_next.al)
+               if (ptr != NULL && ptr->ltc_thread_id == tid) break;
+       if (ptr != NULL && ptr->ltc_thread_id != tid) {
+               ptr = NULL;
+       }
+       ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
+
+       return ptr;
+}
+
 #endif /* LDAP_HAVE_THREAD_POOL */