2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2013 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/signal.h>
21 #include <ac/stdarg.h>
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
28 #include "ldap_pvt_thread.h" /* Get the thread interface */
29 #include "ldap_queue.h"
30 #define LDAP_THREAD_POOL_IMPLEMENTATION
31 #include "ldap_thr_debug.h" /* May rename symbols defined below */
33 #ifndef LDAP_THREAD_HAVE_TPOOL
35 /* Thread-specific key with data and optional free function */
36 typedef struct ldap_int_tpool_key_s {
39 ldap_pvt_thread_pool_keyfree_t *ltk_free;
40 } ldap_int_tpool_key_t;
42 /* Max number of thread-specific keys we store per thread.
43 * We don't expect to use many...
47 /* Max number of threads */
48 #define LDAP_MAXTHR 1024 /* must be a power of 2 */
50 /* (Theoretical) max number of pending requests */
51 #define MAX_PENDING (INT_MAX/2) /* INT_MAX - (room to avoid overflow) */
53 /* pool->ltp_pause values */
54 enum { NOT_PAUSED = 0, WANT_PAUSE = 1, PAUSED = 2 };
56 /* Context: thread ID and thread-specific key/data pairs */
57 typedef struct ldap_int_thread_userctx_s {
58 struct ldap_int_thread_poolq_s *ltu_pq;
59 ldap_pvt_thread_t ltu_id;
60 ldap_int_tpool_key_t ltu_key[MAXKEYS];
61 } ldap_int_thread_userctx_t;
64 /* Simple {thread ID -> context} hash table; key=ctx->ltu_id.
65 * Protected by ldap_pvt_thread_pool_mutex except during pauses,
66 * when it is read-only (used by pool_purgekey and pool_context).
67 * Protected by ldap_pvt_thread_pool_mutex.
70 ldap_int_thread_userctx_t *ctx;
71 /* ctx is valid when not NULL or DELETED_THREAD_CTX */
72 # define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
73 } thread_keys[LDAP_MAXTHR];
75 #define TID_HASH(tid, hash) do { \
76 unsigned const char *ptr_ = (unsigned const char *)&(tid); \
78 for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
79 (hash) += ((hash) << 5) ^ ptr_[i_]; \
83 /* Task for a thread to perform */
84 typedef struct ldap_int_thread_task_s {
86 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s) q;
87 LDAP_SLIST_ENTRY(ldap_int_thread_task_s) l;
89 ldap_pvt_thread_start_t *ltt_start_routine;
91 } ldap_int_thread_task_t;
93 typedef LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ldap_int_tpool_plist_t;
95 struct ldap_int_thread_poolq_s {
96 struct ldap_int_thread_pool_s *ltp_pool;
98 /* protect members below, and protect thread_keys[] during pauses */
99 ldap_pvt_thread_mutex_t ltp_mutex;
101 /* not paused and something to do for pool_<wrapper/pause/destroy>() */
102 ldap_pvt_thread_cond_t ltp_cond;
104 /* ltp_pause == 0 ? <p_pending_list : &empty_pending_list,
105 * maintaned to reduce work for pool_wrapper()
107 ldap_int_tpool_plist_t *ltp_work_list;
109 /* pending tasks, and unused task objects */
110 ldap_int_tpool_plist_t ltp_pending_list;
111 LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list;
113 /* Max number of threads in pool, or 0 for default (LDAP_MAXTHR) */
116 /* Max pending + paused + idle tasks, negated when ltp_finishing */
119 int ltp_pending_count; /* Pending + paused + idle tasks */
120 int ltp_active_count; /* Active, not paused/idle tasks */
121 int ltp_open_count; /* Number of threads, negated when ltp_pause */
122 int ltp_starting; /* Currently starting threads */
125 struct ldap_int_thread_pool_s {
126 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
128 struct ldap_int_thread_poolq_s *ltp_wqs;
130 /* number of poolqs */
133 /* protect members below, and protect thread_keys[] during pauses */
134 ldap_pvt_thread_mutex_t ltp_mutex;
136 /* not paused and something to do for pool_<wrapper/pause/destroy>() */
137 ldap_pvt_thread_cond_t ltp_cond;
139 /* ltp_active_count <= 1 && ltp_pause */
140 ldap_pvt_thread_cond_t ltp_pcond;
142 /* number of active queues */
143 int ltp_active_queues;
145 /* The pool is finishing, waiting for its threads to close.
146 * They close when ltp_pending_list is done. pool_submit()
147 * rejects new tasks. ltp_max_pending = -(its old value).
151 /* Some active task needs to be the sole active task.
152 * Atomic variable so ldap_pvt_thread_pool_pausing() can read it.
154 volatile sig_atomic_t ltp_pause;
156 /* Max number of threads in pool */
159 /* Configured max number of threads in pool, 0 for default (LDAP_MAXTHR) */
160 int ltp_conf_max_count;
162 /* Max pending + paused + idle tasks, negated when ltp_finishing */
166 static ldap_int_tpool_plist_t empty_pending_list =
167 LDAP_STAILQ_HEAD_INITIALIZER(empty_pending_list);
169 static int ldap_int_has_thread_pool = 0;
170 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
171 ldap_int_thread_pool_list =
172 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
174 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
176 static void *ldap_int_thread_pool_wrapper( void *pool );
178 static ldap_pvt_thread_key_t ldap_tpool_key;
180 /* Context of the main thread */
181 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
184 ldap_int_thread_pool_startup ( void )
186 ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
187 ldap_pvt_thread_key_create( &ldap_tpool_key );
188 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
192 ldap_int_thread_pool_shutdown ( void )
194 struct ldap_int_thread_pool_s *pool;
196 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
197 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
199 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
200 ldap_pvt_thread_key_destroy( ldap_tpool_key );
205 /* Create a thread pool */
207 ldap_pvt_thread_pool_init_q (
208 ldap_pvt_thread_pool_t *tpool,
213 ldap_pvt_thread_pool_t pool;
214 struct ldap_int_thread_poolq_s *pq;
215 int i, rc, rem_thr, rem_pend;
217 /* multiple pools are currently not supported (ITS#4943) */
218 assert(!ldap_int_has_thread_pool);
220 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
222 if (! (1 <= max_pending && max_pending <= MAX_PENDING))
223 max_pending = MAX_PENDING;
226 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
227 sizeof(struct ldap_int_thread_pool_s) +
228 numqs * sizeof(struct ldap_int_thread_poolq_s));
230 if (pool == NULL) return(-1);
232 pool->ltp_wqs = (struct ldap_int_thread_poolq_s *)(pool+1);
233 pool->ltp_numqs = numqs;
234 pool->ltp_conf_max_count = max_threads;
236 max_threads = LDAP_MAXTHR;
238 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
242 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
246 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
250 rem_thr = max_threads % numqs;
251 rem_pend = max_pending % numqs;
252 for ( i=0; i<numqs; i++ ) {
253 pq = &pool->ltp_wqs[i];
255 rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
258 rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
261 LDAP_STAILQ_INIT(&pq->ltp_pending_list);
262 pq->ltp_work_list = &pq->ltp_pending_list;
263 LDAP_SLIST_INIT(&pq->ltp_free_list);
265 pq->ltp_max_count = max_threads / numqs;
270 pq->ltp_max_pending = max_pending / numqs;
272 pq->ltp_max_pending++;
277 ldap_int_has_thread_pool = 1;
279 pool->ltp_max_count = max_threads;
280 pool->ltp_max_pending = max_pending;
282 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
283 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
284 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
286 /* Start no threads just yet. That can break if the process forks
287 * later, as slapd does in order to daemonize. On at least POSIX,
288 * only the forking thread would survive in the child. Yet fork()
289 * can't unlock/clean up other threads' locks and data structures,
290 * unless pthread_atfork() handlers have been set up to do so.
298 ldap_pvt_thread_pool_init (
299 ldap_pvt_thread_pool_t *tpool,
303 return ldap_pvt_thread_pool_init_q( tpool, max_threads, max_pending, 1 );
308 struct ldap_int_thread_pool_s *pool,
312 unsigned char *ptr = (unsigned char *)&arg;
313 /* dumb hash of arg to choose a queue */
314 for (j=0; j<sizeof(arg); j++)
316 i %= pool->ltp_numqs;
320 /* Submit a task to be performed by the thread pool */
322 ldap_pvt_thread_pool_submit (
323 ldap_pvt_thread_pool_t *tpool,
324 ldap_pvt_thread_start_t *start_routine, void *arg )
326 struct ldap_int_thread_pool_s *pool;
327 struct ldap_int_thread_poolq_s *pq;
328 ldap_int_thread_task_t *task;
329 ldap_pvt_thread_t thr;
340 if ( pool->ltp_numqs > 1 )
341 i = ldap_int_poolq_hash( pool, arg );
347 ldap_pvt_thread_mutex_lock(&pool->ltp_wqs[i].ltp_mutex);
348 if (pool->ltp_wqs[i].ltp_pending_count < pool->ltp_wqs[i].ltp_max_pending) {
351 ldap_pvt_thread_mutex_unlock(&pool->ltp_wqs[i].ltp_mutex);
353 i %= pool->ltp_numqs;
358 pq = &pool->ltp_wqs[i];
359 task = LDAP_SLIST_FIRST(&pq->ltp_free_list);
361 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
363 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
368 task->ltt_start_routine = start_routine;
371 pq->ltp_pending_count++;
372 LDAP_STAILQ_INSERT_TAIL(&pq->ltp_pending_list, task, ltt_next.q);
377 /* should we open (create) a thread? */
378 if (pq->ltp_open_count < pq->ltp_active_count+pq->ltp_pending_count &&
379 pq->ltp_open_count < pq->ltp_max_count)
382 pq->ltp_open_count++;
384 if (0 != ldap_pvt_thread_create(
385 &thr, 1, ldap_int_thread_pool_wrapper, pq))
387 /* couldn't create thread. back out of
388 * ltp_open_count and check for even worse things.
391 pq->ltp_open_count--;
393 if (pq->ltp_open_count == 0) {
394 /* no open threads at all?!?
396 ldap_int_thread_task_t *ptr;
398 /* let pool_destroy know there are no more threads */
399 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
401 LDAP_STAILQ_FOREACH(ptr, &pq->ltp_pending_list, ltt_next.q)
402 if (ptr == task) break;
404 /* no open threads, task not handled, so
405 * back out of ltp_pending_count, free the task,
408 pq->ltp_pending_count--;
409 LDAP_STAILQ_REMOVE(&pq->ltp_pending_list, task,
410 ldap_int_thread_task_s, ltt_next.q);
411 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task,
416 /* there is another open thread, so this
417 * task will be handled eventually.
421 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
424 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
428 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
433 no_task( void *ctx, void *arg )
438 /* Cancel a pending task that was previously submitted.
439 * Return 1 if the task was successfully cancelled, 0 if
440 * not found, -1 for invalid parameters
443 ldap_pvt_thread_pool_retract (
444 ldap_pvt_thread_pool_t *tpool,
445 ldap_pvt_thread_start_t *start_routine, void *arg )
447 struct ldap_int_thread_pool_s *pool;
448 struct ldap_int_thread_poolq_s *pq;
449 ldap_int_thread_task_t *task;
460 i = ldap_int_poolq_hash( pool, arg );
461 pq = &pool->ltp_wqs[i];
463 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
464 LDAP_STAILQ_FOREACH(task, &pq->ltp_pending_list, ltt_next.q)
465 if (task->ltt_start_routine == start_routine &&
466 task->ltt_arg == arg) {
467 /* Could LDAP_STAILQ_REMOVE the task, but that
468 * walks ltp_pending_list again to find it.
470 task->ltt_start_routine = no_task;
471 task->ltt_arg = NULL;
474 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
478 /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */
480 ldap_pvt_thread_pool_maxthreads(
481 ldap_pvt_thread_pool_t *tpool,
484 struct ldap_int_thread_pool_s *pool;
485 struct ldap_int_thread_poolq_s *pq;
488 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
499 pool->ltp_conf_max_count = max_threads;
501 max_threads = LDAP_MAXTHR;
502 pool->ltp_max_count = max_threads;
504 remthr = max_threads % pool->ltp_numqs;
505 max_threads /= pool->ltp_numqs;
507 for (i=0; i<pool->ltp_numqs; i++) {
508 pq = &pool->ltp_wqs[i];
509 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
510 pq->ltp_max_count = max_threads;
515 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
520 /* Inspect the pool */
522 ldap_pvt_thread_pool_query(
523 ldap_pvt_thread_pool_t *tpool,
524 ldap_pvt_thread_pool_param_t param,
527 struct ldap_int_thread_pool_s *pool;
530 if ( tpool == NULL || value == NULL ) {
536 if ( pool == NULL ) {
541 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
542 count = pool->ltp_conf_max_count;
545 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
546 count = pool->ltp_max_pending;
549 if (count == MAX_PENDING)
553 case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
554 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
555 count = (pool->ltp_pause != 0);
556 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
559 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
560 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
561 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
562 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
563 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
567 for (i=0; i<pool->ltp_numqs; i++) {
568 struct ldap_int_thread_poolq_s *pq = &pool->ltp_wqs[i];
569 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
571 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
572 count += pq->ltp_open_count;
574 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
575 count += pq->ltp_starting;
577 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
578 count += pq->ltp_active_count;
580 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
581 count += pq->ltp_pending_count;
583 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
584 count += pq->ltp_pending_count + pq->ltp_active_count;
587 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
594 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
597 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
600 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
603 case LDAP_PVT_THREAD_POOL_PARAM_STATE:
605 *((char **)value) = "pausing";
606 else if (!pool->ltp_finishing)
607 *((char **)value) = "running";
610 for (i=0; i<pool->ltp_numqs; i++)
611 if (pool->ltp_wqs[i].ltp_pending_count) break;
612 if (i<pool->ltp_numqs)
613 *((char **)value) = "finishing";
615 *((char **)value) = "stopping";
619 case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
624 *((int *)value) = count;
627 return ( count == -1 ? -1 : 0 );
631 * true if pool is pausing; does not lock any mutex to check.
632 * 0 if not pause, 1 if pause, -1 if error or no pool.
635 ldap_pvt_thread_pool_pausing( ldap_pvt_thread_pool_t *tpool )
638 struct ldap_int_thread_pool_s *pool;
640 if ( tpool != NULL && (pool = *tpool) != NULL ) {
641 rc = (pool->ltp_pause != 0);
648 * wrapper for ldap_pvt_thread_pool_query(), left around
649 * for backwards compatibility
652 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
656 rc = ldap_pvt_thread_pool_query( tpool,
657 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
666 /* Destroy the pool after making its threads finish */
668 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
670 struct ldap_int_thread_pool_s *pool, *pptr;
671 struct ldap_int_thread_poolq_s *pq;
672 ldap_int_thread_task_t *task;
680 if (pool == NULL) return(-1);
682 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
683 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
684 if (pptr == pool) break;
686 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
687 ldap_int_thread_pool_s, ltp_next);
688 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
690 if (pool != pptr) return(-1);
692 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
694 pool->ltp_finishing = 1;
695 if (pool->ltp_max_pending > 0)
696 pool->ltp_max_pending = -pool->ltp_max_pending;
698 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
699 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
701 for (i=0; i<pool->ltp_numqs; i++) {
702 pq = &pool->ltp_wqs[i];
703 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
704 if (pq->ltp_max_pending > 0)
705 pq->ltp_max_pending = -pq->ltp_max_pending;
707 while ((task = LDAP_STAILQ_FIRST(&pq->ltp_pending_list)) != NULL) {
708 LDAP_STAILQ_REMOVE_HEAD(&pq->ltp_pending_list, ltt_next.q);
711 pq->ltp_pending_count = 0;
714 while (pq->ltp_open_count) {
715 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
716 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
719 while ((task = LDAP_SLIST_FIRST(&pq->ltp_free_list)) != NULL)
721 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
724 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
725 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
726 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
729 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
730 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
731 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
734 ldap_int_has_thread_pool = 0;
738 /* Thread loop. Accept and handle submitted tasks. */
740 ldap_int_thread_pool_wrapper (
743 struct ldap_int_thread_poolq_s *pq = xpool;
744 struct ldap_int_thread_pool_s *pool = pq->ltp_pool;
745 ldap_int_thread_task_t *task;
746 ldap_int_tpool_plist_t *work_list;
747 ldap_int_thread_userctx_t ctx, *kctx;
748 unsigned i, keyslot, hash;
751 assert(pool != NULL);
753 for ( i=0; i<MAXKEYS; i++ ) {
754 ctx.ltu_key[i].ltk_key = NULL;
758 ctx.ltu_id = ldap_pvt_thread_self();
759 TID_HASH(ctx.ltu_id, hash);
761 ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
763 if (pool->ltp_pause) {
764 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
765 /* thread_keys[] is read-only when paused */
766 while (pool->ltp_pause)
767 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
768 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
771 /* find a key slot to give this thread ID and store a
772 * pointer to our keys there; start at the thread ID
773 * itself (mod LDAP_MAXTHR) and look for an empty slot.
775 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
776 for (keyslot = hash & (LDAP_MAXTHR-1);
777 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
778 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
779 thread_keys[keyslot].ctx = &ctx;
780 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
782 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
784 pq->ltp_active_count++;
787 work_list = pq->ltp_work_list; /* help the compiler a bit */
788 task = LDAP_STAILQ_FIRST(work_list);
789 if (task == NULL) { /* paused or no pending tasks */
790 if (--(pq->ltp_active_count) < 1) {
791 if (pool->ltp_pause) {
792 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
793 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
795 if (--(pool->ltp_active_queues) < 1) {
796 /* Notify pool_pause it is the sole active thread. */
797 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
803 if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) {
804 /* Not paused, and either finishing or too many
805 * threads running (can happen if ltp_max_count
806 * was reduced). Let this thread die.
811 /* We could check an idle timer here, and let the
812 * thread die if it has been inactive for a while.
813 * Only die if there are other open threads (i.e.,
814 * always have at least one thread open).
815 * The check should be like this:
816 * if (pool->ltp_open_count>1 && pool->ltp_starting==0)
817 * check timer, wait if ltp_pause, leave thread;
819 * Just use pthread_cond_timedwait() if we want to
823 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
824 if (!pool->ltp_pause) {
825 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
826 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
830 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
832 work_list = pq->ltp_work_list;
833 task = LDAP_STAILQ_FIRST(work_list);
834 } while (task == NULL);
837 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
838 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
841 pq->ltp_active_count++;
844 LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q);
845 pq->ltp_pending_count--;
846 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
848 task->ltt_start_routine(&ctx, task->ltt_arg);
850 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
851 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l);
855 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
857 /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey()
858 * during this call, since it prevents new pauses. */
859 ldap_pvt_thread_pool_context_reset(&ctx);
861 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
862 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
864 pq->ltp_open_count--;
865 /* let pool_destroy know we're all done */
866 if (pq->ltp_open_count == 0)
867 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
870 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
872 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
874 ldap_pvt_thread_exit(NULL);
878 /* Arguments > ltp_pause to handle_pause(,PAUSE_ARG()). arg=PAUSE_ARG
879 * ensures (arg-ltp_pause) sets GO_* at need and keeps DO_PAUSE/GO_*.
883 #define CHECK_PAUSE 32 /* if ltp_pause: GO_IDLE; wait; GO_UNIDLE */
884 #define DO_PAUSE 64 /* CHECK_PAUSE; pause the pool */
885 #define PAUSE_ARG(a) \
886 ((a) | ((a) & (GO_IDLE|GO_UNIDLE) ? GO_IDLE-1 : CHECK_PAUSE))
889 handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type )
891 struct ldap_int_thread_pool_s *pool;
892 struct ldap_int_thread_poolq_s *pq;
893 int ret = 0, pause, max_ltp_pause;
903 if (pause_type == CHECK_PAUSE && !pool->ltp_pause)
907 ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context();
911 /* Let pool_unidle() ignore requests for new pauses */
912 max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED;
914 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
916 pause = pool->ltp_pause; /* NOT_PAUSED, WANT_PAUSE or PAUSED */
918 /* If ltp_pause and not GO_IDLE|GO_UNIDLE: Set GO_IDLE,GO_UNIDLE */
921 if (pause_type & GO_IDLE) {
923 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
924 pq->ltp_pending_count++;
925 pq->ltp_active_count--;
926 if (pause && pq->ltp_active_count < 1) {
929 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
931 pool->ltp_active_queues--;
932 if (pool->ltp_active_queues < 1)
933 /* Tell the task waiting to DO_PAUSE it can proceed */
934 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
938 if (pause_type & GO_UNIDLE) {
939 /* Wait out pause if any, then cancel GO_IDLE */
940 if (pause > max_ltp_pause) {
943 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
944 } while (pool->ltp_pause > max_ltp_pause);
946 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
947 pq->ltp_pending_count--;
948 pq->ltp_active_count++;
949 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
952 if (pause_type & DO_PAUSE) {
954 /* Tell everyone else to pause or finish, then await that */
956 assert(!pool->ltp_pause);
957 pool->ltp_pause = WANT_PAUSE;
958 pool->ltp_active_queues = 0;
960 for (i=0; i<pool->ltp_numqs; i++)
961 if (&pool->ltp_wqs[i] == pq) break;
963 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
964 /* temporarily remove ourself from active count */
965 pq->ltp_active_count--;
969 pq = &pool->ltp_wqs[j];
971 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
973 /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test,
974 * and do not finish threads in ldap_pvt_thread_pool_wrapper() */
975 pq->ltp_open_count = -pq->ltp_open_count;
976 /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
977 pq->ltp_work_list = &empty_pending_list;
979 if (pq->ltp_active_count > 0)
980 pool->ltp_active_queues++;
982 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
983 if (pool->ltp_numqs > 1) {
985 j %= pool->ltp_numqs;
989 /* Wait for this task to become the sole active task */
990 while (pool->ltp_active_queues > 0)
991 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
993 /* restore us to active count */
994 pool->ltp_wqs[i].ltp_active_count++;
996 assert(pool->ltp_pause == WANT_PAUSE);
997 pool->ltp_pause = PAUSED;
999 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1004 /* Consider this task idle: It will not block pool_pause() in other tasks. */
1006 ldap_pvt_thread_pool_idle( ldap_pvt_thread_pool_t *tpool )
1008 handle_pause(tpool, PAUSE_ARG(GO_IDLE));
1011 /* Cancel pool_idle(). If the pool is paused, wait it out first. */
1013 ldap_pvt_thread_pool_unidle( ldap_pvt_thread_pool_t *tpool )
1015 handle_pause(tpool, PAUSE_ARG(GO_UNIDLE));
1019 * If a pause was requested, wait for it. If several threads
1020 * are waiting to pause, let through one or more pauses.
1021 * The calling task must be active, not idle.
1022 * Return 1 if we waited, 0 if not, -1 at parameter error.
1025 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t *tpool )
1027 return handle_pause(tpool, PAUSE_ARG(CHECK_PAUSE));
1031 * Pause the pool. The calling task must be active, not idle.
1032 * Return when all other tasks are paused or idle.
1035 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
1037 return handle_pause(tpool, PAUSE_ARG(DO_PAUSE));
1042 ldap_pvt_thread_pool_resume (
1043 ldap_pvt_thread_pool_t *tpool )
1045 struct ldap_int_thread_pool_s *pool;
1046 struct ldap_int_thread_poolq_s *pq;
1057 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1058 assert(pool->ltp_pause == PAUSED);
1059 pool->ltp_pause = 0;
1060 for (i=0; i<pool->ltp_numqs; i++) {
1061 pq = &pool->ltp_wqs[i];
1062 if (pq->ltp_open_count <= 0) /* true when paused, but be paranoid */
1063 pq->ltp_open_count = -pq->ltp_open_count;
1064 pq->ltp_work_list = &pq->ltp_pending_list;
1065 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
1067 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
1068 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1073 * Get the key's data and optionally free function in the given context.
1075 int ldap_pvt_thread_pool_getkey(
1079 ldap_pvt_thread_pool_keyfree_t **kfree )
1081 ldap_int_thread_userctx_t *ctx = xctx;
1084 if ( !ctx || !key || !data ) return EINVAL;
1086 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
1087 if ( ctx->ltu_key[i].ltk_key == key ) {
1088 *data = ctx->ltu_key[i].ltk_data;
1089 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
1097 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
1099 for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
1100 ctx->ltu_key[i] = ctx->ltu_key[i+1];
1101 ctx->ltu_key[i].ltk_key = NULL;
1105 * Set or remove data for the key in the given context.
1106 * key can be any unique pointer.
1107 * kfree() is an optional function to free the data (but not the key):
1108 * pool_context_reset() and pool_purgekey() call kfree(key, data),
1109 * but pool_setkey() does not. For pool_setkey() it is the caller's
1110 * responsibility to free any existing data with the same key.
1111 * kfree() must not call functions taking a tpool argument.
1113 int ldap_pvt_thread_pool_setkey(
1117 ldap_pvt_thread_pool_keyfree_t *kfree,
1119 ldap_pvt_thread_pool_keyfree_t **oldkfreep )
1121 ldap_int_thread_userctx_t *ctx = xctx;
1124 if ( !ctx || !key ) return EINVAL;
1126 for ( i=found=0; i<MAXKEYS; i++ ) {
1127 if ( ctx->ltu_key[i].ltk_key == key ) {
1130 } else if ( !ctx->ltu_key[i].ltk_key ) {
1137 *olddatap = ctx->ltu_key[i].ltk_data;
1145 *oldkfreep = ctx->ltu_key[i].ltk_free;
1151 if ( data || kfree ) {
1154 ctx->ltu_key[i].ltk_key = key;
1155 ctx->ltu_key[i].ltk_data = data;
1156 ctx->ltu_key[i].ltk_free = kfree;
1157 } else if ( found ) {
1158 clear_key_idx( ctx, i );
1164 /* Free all elements with this key, no matter which thread they're in.
1165 * May only be called while the pool is paused.
1167 void ldap_pvt_thread_pool_purgekey( void *key )
1170 ldap_int_thread_userctx_t *ctx;
1172 assert ( key != NULL );
1174 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
1175 for ( i=0; i<LDAP_MAXTHR; i++ ) {
1176 ctx = thread_keys[i].ctx;
1177 if ( ctx && ctx != DELETED_THREAD_CTX ) {
1178 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
1179 if ( ctx->ltu_key[j].ltk_key == key ) {
1180 if (ctx->ltu_key[j].ltk_free)
1181 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
1182 ctx->ltu_key[j].ltk_data );
1183 clear_key_idx( ctx, j );
1189 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
1193 * Find the context of the current thread.
1194 * This is necessary if the caller does not have access to the
1195 * thread context handle (for example, a slapd plugin calling
1196 * slapi_search_internal()). No doubt it is more efficient
1197 * for the application to keep track of the thread context
1200 void *ldap_pvt_thread_pool_context( )
1204 ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
1205 return ctx ? ctx : (void *) &ldap_int_main_thrctx;
1209 * Free the context's keys.
1210 * Must not call functions taking a tpool argument (because this
1211 * thread already holds ltp_mutex when called from pool_wrapper()).
1213 void ldap_pvt_thread_pool_context_reset( void *vctx )
1215 ldap_int_thread_userctx_t *ctx = vctx;
1218 for ( i=MAXKEYS-1; i>=0; i--) {
1219 if ( !ctx->ltu_key[i].ltk_key )
1221 if ( ctx->ltu_key[i].ltk_free )
1222 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
1223 ctx->ltu_key[i].ltk_data );
1224 ctx->ltu_key[i].ltk_key = NULL;
1228 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
1230 ldap_int_thread_userctx_t *ctx = vctx;
1234 #endif /* LDAP_THREAD_HAVE_TPOOL */