From: Luke Howard Date: Fri, 24 Jan 2003 06:49:13 +0000 (+0000) Subject: Added ldap_pvt_thread_self() and ldap_pvt_thread_pool_context() API. X-Git-Tag: NO_SLAP_OP_BLOCKS~514 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=9490776d7b7b5fbbe3bf04ec2dab303999169687;p=openldap Added ldap_pvt_thread_self() and ldap_pvt_thread_pool_context() API. --- diff --git a/include/ldap_pvt_thread.h b/include/ldap_pvt_thread.h index 643ee2b8bb..d02359ad9e 100644 --- a/include/ldap_pvt_thread.h +++ b/include/ldap_pvt_thread.h @@ -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 */ diff --git a/libraries/libldap_r/thr_cthreads.c b/libraries/libldap_r/thr_cthreads.c index 3b6023a777..73dff64219 100644 --- a/libraries/libldap_r/thr_cthreads.c +++ b/libraries/libldap_r/thr_cthreads.c @@ -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 */ diff --git a/libraries/libldap_r/thr_lwp.c b/libraries/libldap_r/thr_lwp.c index 11488b8142..eff722f083 100644 --- a/libraries/libldap_r/thr_lwp.c +++ b/libraries/libldap_r/thr_lwp.c @@ -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 */ diff --git a/libraries/libldap_r/thr_nt.c b/libraries/libldap_r/thr_nt.c index 33fc4ad951..9cb0025561 100644 --- a/libraries/libldap_r/thr_nt.c +++ b/libraries/libldap_r/thr_nt.c @@ -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 diff --git a/libraries/libldap_r/thr_posix.c b/libraries/libldap_r/thr_posix.c index 5a9c095ff2..825761e330 100644 --- a/libraries/libldap_r/thr_posix.c +++ b/libraries/libldap_r/thr_posix.c @@ -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 diff --git a/libraries/libldap_r/thr_pth.c b/libraries/libldap_r/thr_pth.c index 92814a6bb0..94e4391947 100644 --- a/libraries/libldap_r/thr_pth.c +++ b/libraries/libldap_r/thr_pth.c @@ -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 ) diff --git a/libraries/libldap_r/thr_stub.c b/libraries/libldap_r/thr_stub.c index c775525b64..021c95335c 100644 --- a/libraries/libldap_r/thr_stub.c +++ b/libraries/libldap_r/thr_stub.c @@ -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 */ diff --git a/libraries/libldap_r/thr_thr.c b/libraries/libldap_r/thr_thr.c index eafccffb3f..c86fa542d3 100644 --- a/libraries/libldap_r/thr_thr.c +++ b/libraries/libldap_r/thr_thr.c @@ -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 */ diff --git a/libraries/libldap_r/tpool.c b/libraries/libldap_r/tpool.c index ce9eb28f2c..d64888d482 100644 --- a/libraries/libldap_r/tpool.c +++ b/libraries/libldap_r/tpool.c @@ -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 */