X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap_r%2Ftpool.c;h=87fc0b3187e4f36427b614b40c3df5a9c39f128f;hb=0ef9e6107baf45d29e194442991132df6c190adb;hp=87e959449134e27655421229d308455db581e0b8;hpb=b3345a21e027a710829fce527d6a6056e737f1df;p=openldap diff --git a/libraries/libldap_r/tpool.c b/libraries/libldap_r/tpool.c index 87e9594491..87fc0b3187 100644 --- a/libraries/libldap_r/tpool.c +++ b/libraries/libldap_r/tpool.c @@ -32,6 +32,10 @@ #ifndef LDAP_THREAD_HAVE_TPOOL +#ifndef CACHELINE +#define CACHELINE 64 +#endif + /* Thread-specific key with data and optional free function */ typedef struct ldap_int_tpool_key_s { void *ltk_key; @@ -55,6 +59,7 @@ enum { NOT_PAUSED = 0, WANT_PAUSE = 1, PAUSED = 2 }; /* Context: thread ID and thread-specific key/data pairs */ typedef struct ldap_int_thread_userctx_s { + struct ldap_int_thread_poolq_s *ltu_pq; ldap_pvt_thread_t ltu_id; ldap_int_tpool_key_t ltu_key[MAXKEYS]; } ldap_int_thread_userctx_t; @@ -63,7 +68,7 @@ typedef struct ldap_int_thread_userctx_s { /* Simple {thread ID -> context} hash table; key=ctx->ltu_id. * Protected by ldap_pvt_thread_pool_mutex except during pauses, * when it is read-only (used by pool_purgekey and pool_context). - * Protected by tpool->ltp_mutex during pauses. + * Protected by ldap_pvt_thread_pool_mutex. */ static struct { ldap_int_thread_userctx_t *ctx; @@ -91,18 +96,21 @@ typedef struct ldap_int_thread_task_s { typedef LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ldap_int_tpool_plist_t; -struct ldap_int_thread_pool_s { - LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next; +struct ldap_int_thread_poolq_s { + void *ltp_free; - /* protect members below, and protect thread_keys[] during pauses */ + struct ldap_int_thread_pool_s *ltp_pool; + + /* protect members below */ ldap_pvt_thread_mutex_t ltp_mutex; - /* not paused and something to do for pool_() */ + /* not paused and something to do for pool_() + * Used for normal pool operation, to synch between submitter and + * worker threads. Not used for pauses. In normal operation multiple + * queues can rendezvous without acquiring the main pool lock. + */ ldap_pvt_thread_cond_t ltp_cond; - /* ltp_active_count <= 1 && ltp_pause */ - ldap_pvt_thread_cond_t ltp_pcond; - /* ltp_pause == 0 ? <p_pending_list : &empty_pending_list, * maintaned to reduce work for pool_wrapper() */ @@ -112,6 +120,41 @@ struct ldap_int_thread_pool_s { ldap_int_tpool_plist_t ltp_pending_list; LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list; + /* Max number of threads in this queue */ + int ltp_max_count; + + /* Max pending + paused + idle tasks, negated when ltp_finishing */ + int ltp_max_pending; + + int ltp_pending_count; /* Pending + paused + idle tasks */ + int ltp_active_count; /* Active, not paused/idle tasks */ + int ltp_open_count; /* Number of threads, negated when ltp_pause */ + int ltp_starting; /* Currently starting threads */ +}; + +struct ldap_int_thread_pool_s { + LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next; + + struct ldap_int_thread_poolq_s **ltp_wqs; + + /* number of poolqs */ + int ltp_numqs; + + /* protect members below */ + ldap_pvt_thread_mutex_t ltp_mutex; + + /* paused and waiting for resume + * When a pause is in effect all workers switch to waiting on + * this cond instead of their per-queue cond. + */ + ldap_pvt_thread_cond_t ltp_cond; + + /* ltp_active_queues < 1 && ltp_pause */ + ldap_pvt_thread_cond_t ltp_pcond; + + /* number of active queues */ + int ltp_active_queues; + /* The pool is finishing, waiting for its threads to close. * They close when ltp_pending_list is done. pool_submit() * rejects new tasks. ltp_max_pending = -(its old value). @@ -120,34 +163,17 @@ struct ldap_int_thread_pool_s { /* Some active task needs to be the sole active task. * Atomic variable so ldap_pvt_thread_pool_pausing() can read it. - * Note: Pauses adjust ltp_, - * so pool_() mostly can avoid testing ltp_pause. */ volatile sig_atomic_t ltp_pause; - /* Max number of threads in pool, or 0 for default (LDAP_MAXTHR) */ + /* Max number of threads in pool */ int ltp_max_count; + /* Configured max number of threads in pool, 0 for default (LDAP_MAXTHR) */ + int ltp_conf_max_count; + /* Max pending + paused + idle tasks, negated when ltp_finishing */ int ltp_max_pending; - - int ltp_pending_count; /* Pending + paused + idle tasks */ - int ltp_active_count; /* Active, not paused/idle tasks */ - int ltp_open_count; /* Number of threads, negated when ltp_pause */ - int ltp_starting; /* Currenlty starting threads */ - - /* >0 if paused or we may open a thread, <0 if we should close a thread. - * Updated when ltp_ change. - * Maintained to reduce the time ltp_mutex must be locked in - * ldap_pvt_thread_pool_(). - */ - int ltp_vary_open_count; -# define SET_VARY_OPEN_COUNT(pool) \ - ((pool)->ltp_vary_open_count = \ - (pool)->ltp_pause ? 1 : \ - (pool)->ltp_finishing ? -1 : \ - ((pool)->ltp_max_count ? (pool)->ltp_max_count : LDAP_MAXTHR) \ - - (pool)->ltp_open_count) }; static ldap_int_tpool_plist_t empty_pending_list = @@ -191,13 +217,15 @@ ldap_int_thread_pool_shutdown ( void ) /* Create a thread pool */ int -ldap_pvt_thread_pool_init ( +ldap_pvt_thread_pool_init_q ( ldap_pvt_thread_pool_t *tpool, int max_threads, - int max_pending ) + int max_pending, + int numqs ) { ldap_pvt_thread_pool_t pool; - int rc; + struct ldap_int_thread_poolq_s *pq; + int i, rc, rem_thr, rem_pend; /* multiple pools are currently not supported (ITS#4943) */ assert(!ldap_int_has_thread_pool); @@ -213,26 +241,74 @@ ldap_pvt_thread_pool_init ( if (pool == NULL) return(-1); + pool->ltp_wqs = LDAP_MALLOC(numqs * sizeof(struct ldap_int_thread_poolq_s *)); + if (pool->ltp_wqs == NULL) { + LDAP_FREE(pool); + return(-1); + } + + for (i=0; i=0; i--) + LDAP_FREE(pool->ltp_wqs[i]->ltp_free); + LDAP_FREE(pool->ltp_wqs); + LDAP_FREE(pool); + return(-1); + } + pool->ltp_wqs[i] = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1)); + pool->ltp_wqs[i]->ltp_free = ptr; + } + + pool->ltp_numqs = numqs; + pool->ltp_conf_max_count = max_threads; + if ( !max_threads ) + max_threads = LDAP_MAXTHR; + rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex); if (rc != 0) return(rc); + rc = ldap_pvt_thread_cond_init(&pool->ltp_cond); if (rc != 0) return(rc); + rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond); if (rc != 0) return(rc); + rem_thr = max_threads % numqs; + rem_pend = max_pending % numqs; + for ( i=0; iltp_wqs[i]; + pq->ltp_pool = pool; + rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex); + if (rc != 0) + return(rc); + rc = ldap_pvt_thread_cond_init(&pq->ltp_cond); + if (rc != 0) + return(rc); + LDAP_STAILQ_INIT(&pq->ltp_pending_list); + pq->ltp_work_list = &pq->ltp_pending_list; + LDAP_SLIST_INIT(&pq->ltp_free_list); + + pq->ltp_max_count = max_threads / numqs; + if ( rem_thr ) { + pq->ltp_max_count++; + rem_thr--; + } + pq->ltp_max_pending = max_pending / numqs; + if ( rem_pend ) { + pq->ltp_max_pending++; + rem_pend--; + } + } + ldap_int_has_thread_pool = 1; pool->ltp_max_count = max_threads; - SET_VARY_OPEN_COUNT(pool); pool->ltp_max_pending = max_pending; - LDAP_STAILQ_INIT(&pool->ltp_pending_list); - pool->ltp_work_list = &pool->ltp_pending_list; - LDAP_SLIST_INIT(&pool->ltp_free_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); @@ -248,6 +324,28 @@ ldap_pvt_thread_pool_init ( return(0); } +int +ldap_pvt_thread_pool_init ( + ldap_pvt_thread_pool_t *tpool, + int max_threads, + int max_pending ) +{ + return ldap_pvt_thread_pool_init_q( tpool, max_threads, max_pending, 1 ); +} + +static int +ldap_int_poolq_hash( + struct ldap_int_thread_pool_s *pool, + void *arg ) +{ + int i = 0, j; + unsigned char *ptr = (unsigned char *)&arg; + /* dumb hash of arg to choose a queue */ + for (j=0; jltp_numqs; + return i; +} /* Submit a task to be performed by the thread pool */ int @@ -256,8 +354,10 @@ ldap_pvt_thread_pool_submit ( ldap_pvt_thread_start_t *start_routine, void *arg ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; ldap_int_thread_task_t *task; ldap_pvt_thread_t thr; + int i, j; if (tpool == NULL) return(-1); @@ -267,14 +367,28 @@ ldap_pvt_thread_pool_submit ( if (pool == NULL) return(-1); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); + if ( pool->ltp_numqs > 1 ) + i = ldap_int_poolq_hash( pool, arg ); + else + i = 0; - if (pool->ltp_pending_count >= pool->ltp_max_pending) - goto failed; + j = i; + while(1) { + ldap_pvt_thread_mutex_lock(&pool->ltp_wqs[i]->ltp_mutex); + if (pool->ltp_wqs[i]->ltp_pending_count < pool->ltp_wqs[i]->ltp_max_pending) { + break; + } + ldap_pvt_thread_mutex_unlock(&pool->ltp_wqs[i]->ltp_mutex); + i++; + i %= pool->ltp_numqs; + if ( i == j ) + return -1; + } - task = LDAP_SLIST_FIRST(&pool->ltp_free_list); + pq = pool->ltp_wqs[i]; + task = LDAP_SLIST_FIRST(&pq->ltp_free_list); if (task) { - LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l); + LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l); } else { task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task)); if (task == NULL) @@ -284,49 +398,47 @@ ldap_pvt_thread_pool_submit ( task->ltt_start_routine = start_routine; task->ltt_arg = arg; - pool->ltp_pending_count++; - LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, task, ltt_next.q); + pq->ltp_pending_count++; + LDAP_STAILQ_INSERT_TAIL(&pq->ltp_pending_list, task, ltt_next.q); - /* true if ltp_pause != 0 or we should open (create) a thread */ - if (pool->ltp_vary_open_count > 0 && - pool->ltp_open_count < pool->ltp_active_count+pool->ltp_pending_count) - { - if (pool->ltp_pause) - goto done; + if (pool->ltp_pause) + goto done; - pool->ltp_starting++; - pool->ltp_open_count++; - SET_VARY_OPEN_COUNT(pool); + /* should we open (create) a thread? */ + if (pq->ltp_open_count < pq->ltp_active_count+pq->ltp_pending_count && + pq->ltp_open_count < pq->ltp_max_count) + { + pq->ltp_starting++; + pq->ltp_open_count++; if (0 != ldap_pvt_thread_create( - &thr, 1, ldap_int_thread_pool_wrapper, pool)) + &thr, 1, ldap_int_thread_pool_wrapper, pq)) { /* couldn't create thread. back out of * ltp_open_count and check for even worse things. */ - pool->ltp_starting--; - pool->ltp_open_count--; - SET_VARY_OPEN_COUNT(pool); + pq->ltp_starting--; + pq->ltp_open_count--; - if (pool->ltp_open_count == 0) { + if (pq->ltp_open_count == 0) { /* no open threads at all?!? */ ldap_int_thread_task_t *ptr; /* let pool_destroy know there are no more threads */ - ldap_pvt_thread_cond_signal(&pool->ltp_cond); + ldap_pvt_thread_cond_signal(&pq->ltp_cond); - LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltt_next.q) + LDAP_STAILQ_FOREACH(ptr, &pq->ltp_pending_list, ltt_next.q) if (ptr == task) break; if (ptr == task) { /* no open threads, task not handled, so * back out of ltp_pending_count, free the task, * report the error. */ - pool->ltp_pending_count--; - LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, task, + pq->ltp_pending_count--; + LDAP_STAILQ_REMOVE(&pq->ltp_pending_list, task, ldap_int_thread_task_s, ltt_next.q); - LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task, + LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l); goto failed; } @@ -336,14 +448,14 @@ ldap_pvt_thread_pool_submit ( */ } } - ldap_pvt_thread_cond_signal(&pool->ltp_cond); + ldap_pvt_thread_cond_signal(&pq->ltp_cond); done: - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); return(0); failed: - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); return(-1); } @@ -363,7 +475,9 @@ ldap_pvt_thread_pool_retract ( ldap_pvt_thread_start_t *start_routine, void *arg ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; ldap_int_thread_task_t *task; + int i; if (tpool == NULL) return(-1); @@ -373,8 +487,11 @@ ldap_pvt_thread_pool_retract ( if (pool == NULL) return(-1); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - LDAP_STAILQ_FOREACH(task, &pool->ltp_pending_list, ltt_next.q) + i = ldap_int_poolq_hash( pool, arg ); + pq = pool->ltp_wqs[i]; + + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + LDAP_STAILQ_FOREACH(task, &pq->ltp_pending_list, ltt_next.q) if (task->ltt_start_routine == start_routine && task->ltt_arg == arg) { /* Could LDAP_STAILQ_REMOVE the task, but that @@ -384,10 +501,79 @@ ldap_pvt_thread_pool_retract ( task->ltt_arg = NULL; break; } - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); return task != NULL; } +/* Set number of work queues in this pool. Should not be + * more than the number of CPUs. */ +int +ldap_pvt_thread_pool_queues( + ldap_pvt_thread_pool_t *tpool, + int numqs ) +{ + struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; + int i, rc, rem_thr, rem_pend; + + if (numqs < 1 || tpool == NULL) + return(-1); + + pool = *tpool; + + if (pool == NULL) + return(-1); + + if (numqs < pool->ltp_numqs) { + for (i=numqs; iltp_numqs; i++) + pool->ltp_wqs[i]->ltp_max_count = 0; + } else if (numqs > pool->ltp_numqs) { + struct ldap_int_thread_poolq_s **wqs; + wqs = LDAP_REALLOC(pool->ltp_wqs, numqs * sizeof(struct ldap_int_thread_poolq_s *)); + if (wqs == NULL) + return(-1); + pool->ltp_wqs = wqs; + for (i=pool->ltp_numqs; iltp_wqs[i] = NULL; + return(-1); + } + pq = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1)); + pq->ltp_free = ptr; + pool->ltp_wqs[i] = pq; + pq->ltp_pool = pool; + rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex); + if (rc != 0) + return(rc); + rc = ldap_pvt_thread_cond_init(&pq->ltp_cond); + if (rc != 0) + return(rc); + LDAP_STAILQ_INIT(&pq->ltp_pending_list); + pq->ltp_work_list = &pq->ltp_pending_list; + LDAP_SLIST_INIT(&pq->ltp_free_list); + } + } + rem_thr = pool->ltp_max_count % numqs; + rem_pend = pool->ltp_max_pending % numqs; + for ( i=0; iltp_wqs[i]; + pq->ltp_max_count = pool->ltp_max_count / numqs; + if ( rem_thr ) { + pq->ltp_max_count++; + rem_thr--; + } + pq->ltp_max_pending = pool->ltp_max_pending / numqs; + if ( rem_pend ) { + pq->ltp_max_pending++; + rem_pend--; + } + } + pool->ltp_numqs = numqs; + return 0; +} + /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */ int ldap_pvt_thread_pool_maxthreads( @@ -395,6 +581,8 @@ ldap_pvt_thread_pool_maxthreads( int max_threads ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; + int remthr, i; if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR)) max_threads = 0; @@ -407,12 +595,22 @@ ldap_pvt_thread_pool_maxthreads( if (pool == NULL) return(-1); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - + pool->ltp_conf_max_count = max_threads; + if ( !max_threads ) + max_threads = LDAP_MAXTHR; pool->ltp_max_count = max_threads; - SET_VARY_OPEN_COUNT(pool); - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + remthr = max_threads % pool->ltp_numqs; + max_threads /= pool->ltp_numqs; + + for (i=0; iltp_numqs; i++) { + pq = pool->ltp_wqs[i]; + pq->ltp_max_count = max_threads; + if (remthr) { + pq->ltp_max_count++; + remthr--; + } + } return(0); } @@ -436,10 +634,9 @@ ldap_pvt_thread_pool_query( return 0; } - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); switch ( param ) { case LDAP_PVT_THREAD_POOL_PARAM_MAX: - count = pool->ltp_max_count; + count = pool->ltp_conf_max_count; break; case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING: @@ -450,30 +647,45 @@ ldap_pvt_thread_pool_query( count = 0; break; - case LDAP_PVT_THREAD_POOL_PARAM_OPEN: - count = pool->ltp_open_count; - if (count < 0) - count = -count; - break; - - case LDAP_PVT_THREAD_POOL_PARAM_STARTING: - count = pool->ltp_starting; - break; - - case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE: - count = pool->ltp_active_count; - break; - case LDAP_PVT_THREAD_POOL_PARAM_PAUSING: + ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); count = (pool->ltp_pause != 0); + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); break; + case LDAP_PVT_THREAD_POOL_PARAM_OPEN: + case LDAP_PVT_THREAD_POOL_PARAM_STARTING: + case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE: case LDAP_PVT_THREAD_POOL_PARAM_PENDING: - count = pool->ltp_pending_count; - break; - case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD: - count = pool->ltp_pending_count + pool->ltp_active_count; + { + int i; + count = 0; + for (i=0; iltp_numqs; i++) { + struct ldap_int_thread_poolq_s *pq = pool->ltp_wqs[i]; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + switch(param) { + case LDAP_PVT_THREAD_POOL_PARAM_OPEN: + count += pq->ltp_open_count; + break; + case LDAP_PVT_THREAD_POOL_PARAM_STARTING: + count += pq->ltp_starting; + break; + case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE: + count += pq->ltp_active_count; + break; + case LDAP_PVT_THREAD_POOL_PARAM_PENDING: + count += pq->ltp_pending_count; + break; + case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD: + count += pq->ltp_pending_count + pq->ltp_active_count; + break; + } + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + } + if (count < 0) + count = -count; + } break; case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX: @@ -486,16 +698,24 @@ ldap_pvt_thread_pool_query( break; case LDAP_PVT_THREAD_POOL_PARAM_STATE: - *((char **)value) = - pool->ltp_pause ? "pausing" : - !pool->ltp_finishing ? "running" : - pool->ltp_pending_count ? "finishing" : "stopping"; + if (pool->ltp_pause) + *((char **)value) = "pausing"; + else if (!pool->ltp_finishing) + *((char **)value) = "running"; + else { + int i; + for (i=0; iltp_numqs; i++) + if (pool->ltp_wqs[i]->ltp_pending_count) break; + if (iltp_numqs) + *((char **)value) = "finishing"; + else + *((char **)value) = "stopping"; + } break; case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN: break; } - ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex ); if ( count > -1 ) { *((int *)value) = count; @@ -545,7 +765,9 @@ int ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending ) { struct ldap_int_thread_pool_s *pool, *pptr; + struct ldap_int_thread_poolq_s *pq; ldap_int_thread_task_t *task; + int i; if (tpool == NULL) return(-1); @@ -567,31 +789,40 @@ ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending ) ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); pool->ltp_finishing = 1; - SET_VARY_OPEN_COUNT(pool); if (pool->ltp_max_pending > 0) pool->ltp_max_pending = -pool->ltp_max_pending; - if (!run_pending) { - while ((task = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL) { - LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q); - LDAP_FREE(task); + ldap_pvt_thread_cond_broadcast(&pool->ltp_cond); + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + + for (i=0; iltp_numqs; i++) { + pq = pool->ltp_wqs[i]; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + if (pq->ltp_max_pending > 0) + pq->ltp_max_pending = -pq->ltp_max_pending; + if (!run_pending) { + while ((task = LDAP_STAILQ_FIRST(&pq->ltp_pending_list)) != NULL) { + LDAP_STAILQ_REMOVE_HEAD(&pq->ltp_pending_list, ltt_next.q); + LDAP_FREE(task); + } + pq->ltp_pending_count = 0; } - pool->ltp_pending_count = 0; - } - while (pool->ltp_open_count) { - if (!pool->ltp_pause) - ldap_pvt_thread_cond_broadcast(&pool->ltp_cond); - ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); - } + while (pq->ltp_open_count) { + ldap_pvt_thread_cond_broadcast(&pq->ltp_cond); + ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex); + } - while ((task = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL) - { - LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l); - LDAP_FREE(task); + while ((task = LDAP_SLIST_FIRST(&pq->ltp_free_list)) != NULL) + { + LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l); + LDAP_FREE(task); + } + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + ldap_pvt_thread_cond_destroy(&pq->ltp_cond); + ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex); } - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); ldap_pvt_thread_cond_destroy(&pool->ltp_pcond); ldap_pvt_thread_cond_destroy(&pool->ltp_cond); ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex); @@ -606,11 +837,13 @@ static void * ldap_int_thread_pool_wrapper ( void *xpool ) { - struct ldap_int_thread_pool_s *pool = xpool; + struct ldap_int_thread_poolq_s *pq = xpool; + struct ldap_int_thread_pool_s *pool = pq->ltp_pool; ldap_int_thread_task_t *task; ldap_int_tpool_plist_t *work_list; ldap_int_thread_userctx_t ctx, *kctx; unsigned i, keyslot, hash; + int pool_lock = 0, freeme = 0; assert(pool != NULL); @@ -618,16 +851,19 @@ ldap_int_thread_pool_wrapper ( ctx.ltu_key[i].ltk_key = NULL; } + ctx.ltu_pq = pq; 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 */ - while (pool->ltp_pause) - ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); + if (pool->ltp_pause) { + ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); + /* thread_keys[] is read-only when paused */ + while (pool->ltp_pause) + ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + } /* find a key slot to give this thread ID and store a * pointer to our keys there; start at the thread ID @@ -640,20 +876,28 @@ ldap_int_thread_pool_wrapper ( thread_keys[keyslot].ctx = &ctx; ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex); - pool->ltp_starting--; - pool->ltp_active_count++; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pq->ltp_starting--; + pq->ltp_active_count++; for (;;) { - work_list = pool->ltp_work_list; /* help the compiler a bit */ + work_list = pq->ltp_work_list; /* help the compiler a bit */ task = LDAP_STAILQ_FIRST(work_list); if (task == NULL) { /* paused or no pending tasks */ - if (--(pool->ltp_active_count) < 2) { - /* Notify pool_pause it is the sole active thread. */ - ldap_pvt_thread_cond_signal(&pool->ltp_pcond); + if (--(pq->ltp_active_count) < 1) { + if (pool->ltp_pause) { + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); + pool_lock = 1; + if (--(pool->ltp_active_queues) < 1) { + /* Notify pool_pause it is the sole active thread. */ + ldap_pvt_thread_cond_signal(&pool->ltp_pcond); + } + } } do { - if (pool->ltp_vary_open_count < 0) { + if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) { /* Not paused, and either finishing or too many * threads running (can happen if ltp_max_count * was reduced). Let this thread die. @@ -672,44 +916,67 @@ ldap_int_thread_pool_wrapper ( * Just use pthread_cond_timedwait() if we want to * check idle time. */ - ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); - - work_list = pool->ltp_work_list; + if (pool_lock) { + ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); + if (!pool->ltp_pause) { + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pool_lock = 0; + } + } else + ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex); + + work_list = pq->ltp_work_list; task = LDAP_STAILQ_FIRST(work_list); } while (task == NULL); - pool->ltp_active_count++; + if (pool_lock) { + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pool_lock = 0; + } + pq->ltp_active_count++; } LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q); - pool->ltp_pending_count--; - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + pq->ltp_pending_count--; + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); task->ltt_start_routine(&ctx, task->ltt_arg); - ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task, ltt_next.l); + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l); } done: - assert(!pool->ltp_pause); /* thread_keys writable, ltp_open_count >= 0 */ + ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex); - /* The ltp_mutex lock protects ctx->ltu_key from pool_purgekey() + /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey() * during this call, since it prevents new pauses. */ ldap_pvt_thread_pool_context_reset(&ctx); - ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex); thread_keys[keyslot].ctx = DELETED_THREAD_CTX; ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex); - pool->ltp_open_count--; - SET_VARY_OPEN_COUNT(pool); - /* let pool_destroy know we're all done */ - if (pool->ltp_open_count == 0) - ldap_pvt_thread_cond_signal(&pool->ltp_cond); + pq->ltp_open_count--; + if (pq->ltp_open_count == 0) { + if (pool->ltp_finishing) + /* let pool_destroy know we're all done */ + ldap_pvt_thread_cond_signal(&pq->ltp_cond); + else + freeme = 1; + } - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + if (pool_lock) + ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + else + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + if (freeme) { + ldap_pvt_thread_cond_destroy(&pq->ltp_cond); + ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex); + LDAP_FREE(pq->ltp_free); + } ldap_pvt_thread_exit(NULL); return(NULL); } @@ -728,6 +995,7 @@ static int handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; int ret = 0, pause, max_ltp_pause; if (tpool == NULL) @@ -741,6 +1009,11 @@ handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type ) if (pause_type == CHECK_PAUSE && !pool->ltp_pause) return(0); + { + ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context(); + pq = ctx->ltu_pq; + } + /* Let pool_unidle() ignore requests for new pauses */ max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED; @@ -752,11 +1025,19 @@ handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type ) pause_type -= pause; if (pause_type & GO_IDLE) { - pool->ltp_pending_count++; - pool->ltp_active_count--; - if (pause && pool->ltp_active_count < 2) { + int do_pool = 0; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pq->ltp_pending_count++; + pq->ltp_active_count--; + if (pause && pq->ltp_active_count < 1) { + do_pool = 1; + } + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + if (do_pool) { + pool->ltp_active_queues--; + if (pool->ltp_active_queues < 1) /* Tell the task waiting to DO_PAUSE it can proceed */ - ldap_pvt_thread_cond_signal(&pool->ltp_pcond); + ldap_pvt_thread_cond_signal(&pool->ltp_pcond); } } @@ -768,30 +1049,61 @@ handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type ) ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex); } while (pool->ltp_pause > max_ltp_pause); } - pool->ltp_pending_count--; - pool->ltp_active_count++; + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + pq->ltp_pending_count--; + pq->ltp_active_count++; + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); } if (pause_type & DO_PAUSE) { + int i, j; /* Tell everyone else to pause or finish, then await that */ ret = 0; assert(!pool->ltp_pause); pool->ltp_pause = WANT_PAUSE; - /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test, - * and do not finish threads in ldap_pvt_thread_pool_wrapper() */ - pool->ltp_open_count = -pool->ltp_open_count; - SET_VARY_OPEN_COUNT(pool); - /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */ - pool->ltp_work_list = &empty_pending_list; + pool->ltp_active_queues = 0; + + for (i=0; iltp_numqs; i++) + if (pool->ltp_wqs[i] == pq) break; + + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + /* temporarily remove ourself from active count */ + pq->ltp_active_count--; + + j=i; + do { + pq = pool->ltp_wqs[j]; + if (j != i) + ldap_pvt_thread_mutex_lock(&pq->ltp_mutex); + + /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test, + * and do not finish threads in ldap_pvt_thread_pool_wrapper() */ + pq->ltp_open_count = -pq->ltp_open_count; + /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */ + pq->ltp_work_list = &empty_pending_list; + + if (pq->ltp_active_count > 0) + pool->ltp_active_queues++; + + ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex); + if (pool->ltp_numqs > 1) { + j++; + j %= pool->ltp_numqs; + } + } while (j != i); + /* Wait for this task to become the sole active task */ - while (pool->ltp_active_count > 1) { + while (pool->ltp_active_queues > 0) ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex); - } + + /* restore us to active count */ + pool->ltp_wqs[i]->ltp_active_count++; + assert(pool->ltp_pause == WANT_PAUSE); pool->ltp_pause = PAUSED; } - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); + return(ret); } @@ -837,6 +1149,8 @@ ldap_pvt_thread_pool_resume ( ldap_pvt_thread_pool_t *tpool ) { struct ldap_int_thread_pool_s *pool; + struct ldap_int_thread_poolq_s *pq; + int i; if (tpool == NULL) return(-1); @@ -847,16 +1161,16 @@ ldap_pvt_thread_pool_resume ( return(0); ldap_pvt_thread_mutex_lock(&pool->ltp_mutex); - assert(pool->ltp_pause == PAUSED); pool->ltp_pause = 0; - if (pool->ltp_open_count <= 0) /* true when paused, but be paranoid */ - pool->ltp_open_count = -pool->ltp_open_count; - SET_VARY_OPEN_COUNT(pool); - pool->ltp_work_list = &pool->ltp_pending_list; - + for (i=0; iltp_numqs; i++) { + pq = pool->ltp_wqs[i]; + if (pq->ltp_open_count <= 0) /* true when paused, but be paranoid */ + pq->ltp_open_count = -pq->ltp_open_count; + pq->ltp_work_list = &pq->ltp_pending_list; + ldap_pvt_thread_cond_broadcast(&pq->ltp_cond); + } ldap_pvt_thread_cond_broadcast(&pool->ltp_cond); - ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex); return(0); } @@ -963,6 +1277,7 @@ void ldap_pvt_thread_pool_purgekey( void *key ) assert ( key != NULL ); + ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex); for ( i=0; i