2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 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 ldap_pvt_thread_t ltu_id;
59 ldap_int_tpool_key_t ltu_key[MAXKEYS];
60 } ldap_int_thread_userctx_t;
63 /* Simple {thread ID -> context} hash table; key=ctx->ltu_id.
64 * Protected by ldap_pvt_thread_pool_mutex except during pauses,
65 * when it is read-only (used by pool_purgekey and pool_context).
66 * Protected by tpool->ltp_mutex during pauses.
69 ldap_int_thread_userctx_t *ctx;
70 /* ctx is valid when not NULL or DELETED_THREAD_CTX */
71 # define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
72 } thread_keys[LDAP_MAXTHR];
74 #define TID_HASH(tid, hash) do { \
75 unsigned const char *ptr_ = (unsigned const char *)&(tid); \
77 for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
78 (hash) += ((hash) << 5) ^ ptr_[i_]; \
82 /* Task for a thread to perform */
83 typedef struct ldap_int_thread_task_s {
85 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s) q;
86 LDAP_SLIST_ENTRY(ldap_int_thread_task_s) l;
88 ldap_pvt_thread_start_t *ltt_start_routine;
90 } ldap_int_thread_task_t;
92 typedef LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ldap_int_tpool_plist_t;
94 struct ldap_int_thread_pool_s {
95 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
97 /* protect members below, and protect thread_keys[] during pauses */
98 ldap_pvt_thread_mutex_t ltp_mutex;
100 /* not paused and something to do for pool_<wrapper/pause/destroy>() */
101 ldap_pvt_thread_cond_t ltp_cond;
103 /* ltp_active_count <= 1 && ltp_pause */
104 ldap_pvt_thread_cond_t ltp_pcond;
106 /* ltp_pause == 0 ? <p_pending_list : &empty_pending_list,
107 * maintaned to reduce work for pool_wrapper()
109 ldap_int_tpool_plist_t *ltp_work_list;
111 /* pending tasks, and unused task objects */
112 ldap_int_tpool_plist_t ltp_pending_list;
113 LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list;
115 /* The pool is finishing, waiting for its threads to close.
116 * They close when ltp_pending_list is done. pool_submit()
117 * rejects new tasks. ltp_max_pending = -(its old value).
121 /* Some active task needs to be the sole active task.
122 * Atomic variable so ldap_pvt_thread_pool_pausing() can read it.
123 * Note: Pauses adjust ltp_<open_count/vary_open_count/work_list>,
124 * so pool_<submit/wrapper>() mostly can avoid testing ltp_pause.
126 volatile sig_atomic_t ltp_pause;
128 /* Max number of threads in pool, or 0 for default (LDAP_MAXTHR) */
131 /* Max pending + paused + idle tasks, negated when ltp_finishing */
134 int ltp_pending_count; /* Pending + paused + idle tasks */
135 int ltp_active_count; /* Active, not paused/idle tasks */
136 int ltp_open_count; /* Number of threads, negated when ltp_pause */
137 int ltp_starting; /* Currenlty starting threads */
139 /* >0 if paused or we may open a thread, <0 if we should close a thread.
140 * Updated when ltp_<finishing/pause/max_count/open_count> change.
141 * Maintained to reduce the time ltp_mutex must be locked in
142 * ldap_pvt_thread_pool_<submit/wrapper>().
144 int ltp_vary_open_count;
145 # define SET_VARY_OPEN_COUNT(pool) \
146 ((pool)->ltp_vary_open_count = \
147 (pool)->ltp_pause ? 1 : \
148 (pool)->ltp_finishing ? -1 : \
149 ((pool)->ltp_max_count ? (pool)->ltp_max_count : LDAP_MAXTHR) \
150 - (pool)->ltp_open_count)
153 static ldap_int_tpool_plist_t empty_pending_list =
154 LDAP_STAILQ_HEAD_INITIALIZER(empty_pending_list);
156 static int ldap_int_has_thread_pool = 0;
157 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
158 ldap_int_thread_pool_list =
159 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
161 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
163 static void *ldap_int_thread_pool_wrapper( void *pool );
165 static ldap_pvt_thread_key_t ldap_tpool_key;
167 /* Context of the main thread */
168 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
171 ldap_int_thread_pool_startup ( void )
173 ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
174 ldap_pvt_thread_key_create( &ldap_tpool_key );
175 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
179 ldap_int_thread_pool_shutdown ( void )
181 struct ldap_int_thread_pool_s *pool;
183 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
184 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
186 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
187 ldap_pvt_thread_key_destroy( ldap_tpool_key );
192 /* Create a thread pool */
194 ldap_pvt_thread_pool_init (
195 ldap_pvt_thread_pool_t *tpool,
199 ldap_pvt_thread_pool_t pool;
202 /* multiple pools are currently not supported (ITS#4943) */
203 assert(!ldap_int_has_thread_pool);
205 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
207 if (! (1 <= max_pending && max_pending <= MAX_PENDING))
208 max_pending = MAX_PENDING;
211 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
212 sizeof(struct ldap_int_thread_pool_s));
214 if (pool == NULL) return(-1);
216 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
222 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
225 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
228 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
230 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
234 ldap_int_has_thread_pool = 1;
236 pool->ltp_max_count = max_threads;
237 SET_VARY_OPEN_COUNT(pool);
238 pool->ltp_max_pending = max_pending;
240 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
241 pool->ltp_work_list = &pool->ltp_pending_list;
242 LDAP_SLIST_INIT(&pool->ltp_free_list);
244 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
245 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
246 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
248 /* Start no threads just yet. That can break if the process forks
249 * later, as slapd does in order to daemonize. On at least POSIX,
250 * only the forking thread would survive in the child. Yet fork()
251 * can't unlock/clean up other threads' locks and data structures,
252 * unless pthread_atfork() handlers have been set up to do so.
260 /* Submit a task to be performed by the thread pool */
262 ldap_pvt_thread_pool_submit (
263 ldap_pvt_thread_pool_t *tpool,
264 ldap_pvt_thread_start_t *start_routine, void *arg )
266 struct ldap_int_thread_pool_s *pool;
267 ldap_int_thread_task_t *task;
268 ldap_pvt_thread_t thr;
278 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
280 if (pool->ltp_pending_count >= pool->ltp_max_pending)
283 task = LDAP_SLIST_FIRST(&pool->ltp_free_list);
285 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l);
287 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
292 task->ltt_start_routine = start_routine;
295 pool->ltp_pending_count++;
296 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, task, ltt_next.q);
298 /* true if ltp_pause != 0 or we should open (create) a thread */
299 if (pool->ltp_vary_open_count > 0 &&
300 pool->ltp_open_count < pool->ltp_active_count+pool->ltp_pending_count)
305 pool->ltp_starting++;
306 pool->ltp_open_count++;
307 SET_VARY_OPEN_COUNT(pool);
309 if (0 != ldap_pvt_thread_create(
310 &thr, 1, ldap_int_thread_pool_wrapper, pool))
312 /* couldn't create thread. back out of
313 * ltp_open_count and check for even worse things.
315 pool->ltp_starting--;
316 pool->ltp_open_count--;
317 SET_VARY_OPEN_COUNT(pool);
319 if (pool->ltp_open_count == 0) {
320 /* no open threads at all?!?
322 ldap_int_thread_task_t *ptr;
324 /* let pool_destroy know there are no more threads */
325 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
327 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltt_next.q)
328 if (ptr == task) break;
330 /* no open threads, task not handled, so
331 * back out of ltp_pending_count, free the task,
334 pool->ltp_pending_count--;
335 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, task,
336 ldap_int_thread_task_s, ltt_next.q);
337 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task,
342 /* there is another open thread, so this
343 * task will be handled eventually.
347 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
350 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
354 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
359 no_task( void *ctx, void *arg )
364 /* Cancel a pending task that was previously submitted.
365 * Return 1 if the task was successfully cancelled, 0 if
366 * not found, -1 for invalid parameters
369 ldap_pvt_thread_pool_retract (
370 ldap_pvt_thread_pool_t *tpool,
371 ldap_pvt_thread_start_t *start_routine, void *arg )
373 struct ldap_int_thread_pool_s *pool;
374 ldap_int_thread_task_t *task;
384 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
385 LDAP_STAILQ_FOREACH(task, &pool->ltp_pending_list, ltt_next.q)
386 if (task->ltt_start_routine == start_routine &&
387 task->ltt_arg == arg) {
388 /* Could LDAP_STAILQ_REMOVE the task, but that
389 * walks ltp_pending_list again to find it.
391 task->ltt_start_routine = no_task;
392 task->ltt_arg = NULL;
395 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
399 /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */
401 ldap_pvt_thread_pool_maxthreads(
402 ldap_pvt_thread_pool_t *tpool,
405 struct ldap_int_thread_pool_s *pool;
407 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
418 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
420 pool->ltp_max_count = max_threads;
421 SET_VARY_OPEN_COUNT(pool);
423 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
427 /* Inspect the pool */
429 ldap_pvt_thread_pool_query(
430 ldap_pvt_thread_pool_t *tpool,
431 ldap_pvt_thread_pool_param_t param,
434 struct ldap_int_thread_pool_s *pool;
437 if ( tpool == NULL || value == NULL ) {
443 if ( pool == NULL ) {
447 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
449 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
450 count = pool->ltp_max_count;
453 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
454 count = pool->ltp_max_pending;
457 if (count == MAX_PENDING)
461 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
462 count = pool->ltp_open_count;
467 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
468 count = pool->ltp_starting;
471 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
472 count = pool->ltp_active_count;
475 case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
476 count = (pool->ltp_pause != 0);
479 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
480 count = pool->ltp_pending_count;
483 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
484 count = pool->ltp_pending_count + pool->ltp_active_count;
487 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
490 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
493 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
496 case LDAP_PVT_THREAD_POOL_PARAM_STATE:
498 pool->ltp_pause ? "pausing" :
499 !pool->ltp_finishing ? "running" :
500 pool->ltp_pending_count ? "finishing" : "stopping";
503 case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
506 ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex );
509 *((int *)value) = count;
512 return ( count == -1 ? -1 : 0 );
516 * true if pool is pausing; does not lock any mutex to check.
517 * 0 if not pause, 1 if pause, -1 if error or no pool.
520 ldap_pvt_thread_pool_pausing( ldap_pvt_thread_pool_t *tpool )
523 struct ldap_int_thread_pool_s *pool;
525 if ( tpool != NULL && (pool = *tpool) != NULL ) {
526 rc = (pool->ltp_pause != 0);
533 * wrapper for ldap_pvt_thread_pool_query(), left around
534 * for backwards compatibility
537 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
541 rc = ldap_pvt_thread_pool_query( tpool,
542 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
551 /* Destroy the pool after making its threads finish */
553 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
555 struct ldap_int_thread_pool_s *pool, *pptr;
556 ldap_int_thread_task_t *task;
563 if (pool == NULL) return(-1);
565 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
566 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
567 if (pptr == pool) break;
569 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
570 ldap_int_thread_pool_s, ltp_next);
571 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
573 if (pool != pptr) return(-1);
575 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
577 pool->ltp_finishing = 1;
578 SET_VARY_OPEN_COUNT(pool);
579 if (pool->ltp_max_pending > 0)
580 pool->ltp_max_pending = -pool->ltp_max_pending;
583 while ((task = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL) {
584 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q);
587 pool->ltp_pending_count = 0;
590 while (pool->ltp_open_count) {
591 if (!pool->ltp_pause)
592 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
593 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
596 while ((task = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
598 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l);
602 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
603 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
604 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
605 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
608 ldap_int_has_thread_pool = 0;
612 /* Thread loop. Accept and handle submitted tasks. */
614 ldap_int_thread_pool_wrapper (
617 struct ldap_int_thread_pool_s *pool = xpool;
618 ldap_int_thread_task_t *task;
619 ldap_int_tpool_plist_t *work_list;
620 ldap_int_thread_userctx_t ctx, *kctx;
621 unsigned i, keyslot, hash;
623 assert(pool != NULL);
625 for ( i=0; i<MAXKEYS; i++ ) {
626 ctx.ltu_key[i].ltk_key = NULL;
629 ctx.ltu_id = ldap_pvt_thread_self();
630 TID_HASH(ctx.ltu_id, hash);
632 ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
634 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
636 /* thread_keys[] is read-only when paused */
637 while (pool->ltp_pause)
638 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
640 /* find a key slot to give this thread ID and store a
641 * pointer to our keys there; start at the thread ID
642 * itself (mod LDAP_MAXTHR) and look for an empty slot.
644 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
645 for (keyslot = hash & (LDAP_MAXTHR-1);
646 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
647 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
648 thread_keys[keyslot].ctx = &ctx;
649 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
651 pool->ltp_starting--;
652 pool->ltp_active_count++;
655 work_list = pool->ltp_work_list; /* help the compiler a bit */
656 task = LDAP_STAILQ_FIRST(work_list);
657 if (task == NULL) { /* paused or no pending tasks */
658 if (--(pool->ltp_active_count) < 2) {
659 /* Notify pool_pause it is the sole active thread. */
660 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
664 if (pool->ltp_vary_open_count < 0) {
665 /* Not paused, and either finishing or too many
666 * threads running (can happen if ltp_max_count
667 * was reduced). Let this thread die.
672 /* We could check an idle timer here, and let the
673 * thread die if it has been inactive for a while.
674 * Only die if there are other open threads (i.e.,
675 * always have at least one thread open).
676 * The check should be like this:
677 * if (pool->ltp_open_count>1 && pool->ltp_starting==0)
678 * check timer, wait if ltp_pause, leave thread;
680 * Just use pthread_cond_timedwait() if we want to
683 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
685 work_list = pool->ltp_work_list;
686 task = LDAP_STAILQ_FIRST(work_list);
687 } while (task == NULL);
689 pool->ltp_active_count++;
692 LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q);
693 pool->ltp_pending_count--;
694 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
696 task->ltt_start_routine(&ctx, task->ltt_arg);
698 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
699 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task, ltt_next.l);
703 assert(!pool->ltp_pause); /* thread_keys writable, ltp_open_count >= 0 */
705 /* The ltp_mutex lock protects ctx->ltu_key from pool_purgekey()
706 * during this call, since it prevents new pauses. */
707 ldap_pvt_thread_pool_context_reset(&ctx);
709 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
710 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
711 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
713 pool->ltp_open_count--;
714 SET_VARY_OPEN_COUNT(pool);
715 /* let pool_destroy know we're all done */
716 if (pool->ltp_open_count == 0)
717 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
719 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
721 ldap_pvt_thread_exit(NULL);
725 /* Arguments > ltp_pause to handle_pause(,PAUSE_ARG()). arg=PAUSE_ARG
726 * ensures (arg-ltp_pause) sets GO_* at need and keeps DO_PAUSE/GO_*.
730 #define CHECK_PAUSE 32 /* if ltp_pause: GO_IDLE; wait; GO_UNIDLE */
731 #define DO_PAUSE 64 /* CHECK_PAUSE; pause the pool */
732 #define PAUSE_ARG(a) \
733 ((a) | ((a) & (GO_IDLE|GO_UNIDLE) ? GO_IDLE-1 : CHECK_PAUSE))
736 handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type )
738 struct ldap_int_thread_pool_s *pool;
739 int ret = 0, pause, max_ltp_pause;
749 if (pause_type == CHECK_PAUSE && !pool->ltp_pause)
752 /* Let pool_unidle() ignore requests for new pauses */
753 max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED;
755 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
757 pause = pool->ltp_pause; /* NOT_PAUSED, WANT_PAUSE or PAUSED */
759 /* If ltp_pause and not GO_IDLE|GO_UNIDLE: Set GO_IDLE,GO_UNIDLE */
762 if (pause_type & GO_IDLE) {
763 pool->ltp_pending_count++;
764 pool->ltp_active_count--;
765 if (pause && pool->ltp_active_count < 2) {
766 /* Tell the task waiting to DO_PAUSE it can proceed */
767 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
771 if (pause_type & GO_UNIDLE) {
772 /* Wait out pause if any, then cancel GO_IDLE */
773 if (pause > max_ltp_pause) {
776 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
777 } while (pool->ltp_pause > max_ltp_pause);
779 pool->ltp_pending_count--;
780 pool->ltp_active_count++;
783 if (pause_type & DO_PAUSE) {
784 /* Tell everyone else to pause or finish, then await that */
786 assert(!pool->ltp_pause);
787 pool->ltp_pause = WANT_PAUSE;
788 /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test,
789 * and do not finish threads in ldap_pvt_thread_pool_wrapper() */
790 pool->ltp_open_count = -pool->ltp_open_count;
791 SET_VARY_OPEN_COUNT(pool);
792 /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
793 pool->ltp_work_list = &empty_pending_list;
794 /* Wait for this task to become the sole active task */
795 while (pool->ltp_active_count > 1) {
796 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
798 assert(pool->ltp_pause == WANT_PAUSE);
799 pool->ltp_pause = PAUSED;
802 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
806 /* Consider this task idle: It will not block pool_pause() in other tasks. */
808 ldap_pvt_thread_pool_idle( ldap_pvt_thread_pool_t *tpool )
810 handle_pause(tpool, PAUSE_ARG(GO_IDLE));
813 /* Cancel pool_idle(). If the pool is paused, wait it out first. */
815 ldap_pvt_thread_pool_unidle( ldap_pvt_thread_pool_t *tpool )
817 handle_pause(tpool, PAUSE_ARG(GO_UNIDLE));
821 * If a pause was requested, wait for it. If several threads
822 * are waiting to pause, let through one or more pauses.
823 * The calling task must be active, not idle.
824 * Return 1 if we waited, 0 if not, -1 at parameter error.
827 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t *tpool )
829 return handle_pause(tpool, PAUSE_ARG(CHECK_PAUSE));
833 * Pause the pool. The calling task must be active, not idle.
834 * Return when all other tasks are paused or idle.
837 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
839 return handle_pause(tpool, PAUSE_ARG(DO_PAUSE));
844 ldap_pvt_thread_pool_resume (
845 ldap_pvt_thread_pool_t *tpool )
847 struct ldap_int_thread_pool_s *pool;
857 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
859 assert(pool->ltp_pause == PAUSED);
861 if (pool->ltp_open_count <= 0) /* true when paused, but be paranoid */
862 pool->ltp_open_count = -pool->ltp_open_count;
863 SET_VARY_OPEN_COUNT(pool);
864 pool->ltp_work_list = &pool->ltp_pending_list;
866 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
868 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
873 * Get the key's data and optionally free function in the given context.
875 int ldap_pvt_thread_pool_getkey(
879 ldap_pvt_thread_pool_keyfree_t **kfree )
881 ldap_int_thread_userctx_t *ctx = xctx;
884 if ( !ctx || !key || !data ) return EINVAL;
886 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
887 if ( ctx->ltu_key[i].ltk_key == key ) {
888 *data = ctx->ltu_key[i].ltk_data;
889 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
897 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
899 for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
900 ctx->ltu_key[i] = ctx->ltu_key[i+1];
901 ctx->ltu_key[i].ltk_key = NULL;
905 * Set or remove data for the key in the given context.
906 * key can be any unique pointer.
907 * kfree() is an optional function to free the data (but not the key):
908 * pool_context_reset() and pool_purgekey() call kfree(key, data),
909 * but pool_setkey() does not. For pool_setkey() it is the caller's
910 * responsibility to free any existing data with the same key.
911 * kfree() must not call functions taking a tpool argument.
913 int ldap_pvt_thread_pool_setkey(
917 ldap_pvt_thread_pool_keyfree_t *kfree,
919 ldap_pvt_thread_pool_keyfree_t **oldkfreep )
921 ldap_int_thread_userctx_t *ctx = xctx;
924 if ( !ctx || !key ) return EINVAL;
926 for ( i=found=0; i<MAXKEYS; i++ ) {
927 if ( ctx->ltu_key[i].ltk_key == key ) {
930 } else if ( !ctx->ltu_key[i].ltk_key ) {
937 *olddatap = ctx->ltu_key[i].ltk_data;
945 *oldkfreep = ctx->ltu_key[i].ltk_free;
951 if ( data || kfree ) {
954 ctx->ltu_key[i].ltk_key = key;
955 ctx->ltu_key[i].ltk_data = data;
956 ctx->ltu_key[i].ltk_free = kfree;
957 } else if ( found ) {
958 clear_key_idx( ctx, i );
964 /* Free all elements with this key, no matter which thread they're in.
965 * May only be called while the pool is paused.
967 void ldap_pvt_thread_pool_purgekey( void *key )
970 ldap_int_thread_userctx_t *ctx;
972 assert ( key != NULL );
974 for ( i=0; i<LDAP_MAXTHR; i++ ) {
975 ctx = thread_keys[i].ctx;
976 if ( ctx && ctx != DELETED_THREAD_CTX ) {
977 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
978 if ( ctx->ltu_key[j].ltk_key == key ) {
979 if (ctx->ltu_key[j].ltk_free)
980 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
981 ctx->ltu_key[j].ltk_data );
982 clear_key_idx( ctx, j );
991 * Find the context of the current thread.
992 * This is necessary if the caller does not have access to the
993 * thread context handle (for example, a slapd plugin calling
994 * slapi_search_internal()). No doubt it is more efficient
995 * for the application to keep track of the thread context
998 void *ldap_pvt_thread_pool_context( )
1002 ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
1003 return ctx ? ctx : (void *) &ldap_int_main_thrctx;
1007 * Free the context's keys.
1008 * Must not call functions taking a tpool argument (because this
1009 * thread already holds ltp_mutex when called from pool_wrapper()).
1011 void ldap_pvt_thread_pool_context_reset( void *vctx )
1013 ldap_int_thread_userctx_t *ctx = vctx;
1016 for ( i=MAXKEYS-1; i>=0; i--) {
1017 if ( !ctx->ltu_key[i].ltk_key )
1019 if ( ctx->ltu_key[i].ltk_free )
1020 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
1021 ctx->ltu_key[i].ltk_data );
1022 ctx->ltu_key[i].ltk_key = NULL;
1026 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
1028 ldap_int_thread_userctx_t *ctx = vctx;
1032 #endif /* LDAP_THREAD_HAVE_TPOOL */