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
39 /* Thread-specific key with data and optional free function */
40 typedef struct ldap_int_tpool_key_s {
43 ldap_pvt_thread_pool_keyfree_t *ltk_free;
44 } ldap_int_tpool_key_t;
46 /* Max number of thread-specific keys we store per thread.
47 * We don't expect to use many...
51 /* Max number of threads */
52 #define LDAP_MAXTHR 1024 /* must be a power of 2 */
54 /* (Theoretical) max number of pending requests */
55 #define MAX_PENDING (INT_MAX/2) /* INT_MAX - (room to avoid overflow) */
57 /* pool->ltp_pause values */
58 enum { NOT_PAUSED = 0, WANT_PAUSE = 1, PAUSED = 2 };
60 /* Context: thread ID and thread-specific key/data pairs */
61 typedef struct ldap_int_thread_userctx_s {
62 struct ldap_int_thread_poolq_s *ltu_pq;
63 ldap_pvt_thread_t ltu_id;
64 ldap_int_tpool_key_t ltu_key[MAXKEYS];
65 } ldap_int_thread_userctx_t;
68 /* Simple {thread ID -> context} hash table; key=ctx->ltu_id.
69 * Protected by ldap_pvt_thread_pool_mutex except during pauses,
70 * when it is read-only (used by pool_purgekey and pool_context).
71 * Protected by ldap_pvt_thread_pool_mutex.
74 ldap_int_thread_userctx_t *ctx;
75 /* ctx is valid when not NULL or DELETED_THREAD_CTX */
76 # define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
77 } thread_keys[LDAP_MAXTHR];
79 #define TID_HASH(tid, hash) do { \
80 unsigned const char *ptr_ = (unsigned const char *)&(tid); \
82 for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
83 (hash) += ((hash) << 5) ^ ptr_[i_]; \
87 /* Task for a thread to perform */
88 typedef struct ldap_int_thread_task_s {
90 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s) q;
91 LDAP_SLIST_ENTRY(ldap_int_thread_task_s) l;
93 ldap_pvt_thread_start_t *ltt_start_routine;
95 } ldap_int_thread_task_t;
97 typedef LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ldap_int_tpool_plist_t;
99 struct ldap_int_thread_poolq_s {
102 struct ldap_int_thread_pool_s *ltp_pool;
104 /* protect members below */
105 ldap_pvt_thread_mutex_t ltp_mutex;
107 /* not paused and something to do for pool_<wrapper/pause/destroy>()
108 * Used for normal pool operation, to synch between submitter and
109 * worker threads. Not used for pauses. In normal operation multiple
110 * queues can rendezvous without acquiring the main pool lock.
112 ldap_pvt_thread_cond_t ltp_cond;
114 /* ltp_pause == 0 ? <p_pending_list : &empty_pending_list,
115 * maintaned to reduce work for pool_wrapper()
117 ldap_int_tpool_plist_t *ltp_work_list;
119 /* pending tasks, and unused task objects */
120 ldap_int_tpool_plist_t ltp_pending_list;
121 LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list;
123 /* Max number of threads in this queue */
126 /* Max pending + paused + idle tasks, negated when ltp_finishing */
129 int ltp_pending_count; /* Pending + paused + idle tasks */
130 int ltp_active_count; /* Active, not paused/idle tasks */
131 int ltp_open_count; /* Number of threads, negated when ltp_pause */
132 int ltp_starting; /* Currently starting threads */
135 struct ldap_int_thread_pool_s {
136 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
138 struct ldap_int_thread_poolq_s **ltp_wqs;
140 /* number of poolqs */
143 /* protect members below */
144 ldap_pvt_thread_mutex_t ltp_mutex;
146 /* paused and waiting for resume
147 * When a pause is in effect all workers switch to waiting on
148 * this cond instead of their per-queue cond.
150 ldap_pvt_thread_cond_t ltp_cond;
152 /* ltp_active_queues < 1 && ltp_pause */
153 ldap_pvt_thread_cond_t ltp_pcond;
155 /* number of active queues */
156 int ltp_active_queues;
158 /* The pool is finishing, waiting for its threads to close.
159 * They close when ltp_pending_list is done. pool_submit()
160 * rejects new tasks. ltp_max_pending = -(its old value).
164 /* Some active task needs to be the sole active task.
165 * Atomic variable so ldap_pvt_thread_pool_pausing() can read it.
167 volatile sig_atomic_t ltp_pause;
169 /* Max number of threads in pool */
172 /* Configured max number of threads in pool, 0 for default (LDAP_MAXTHR) */
173 int ltp_conf_max_count;
175 /* Max pending + paused + idle tasks, negated when ltp_finishing */
179 static ldap_int_tpool_plist_t empty_pending_list =
180 LDAP_STAILQ_HEAD_INITIALIZER(empty_pending_list);
182 static int ldap_int_has_thread_pool = 0;
183 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
184 ldap_int_thread_pool_list =
185 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
187 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
189 static void *ldap_int_thread_pool_wrapper( void *pool );
191 static ldap_pvt_thread_key_t ldap_tpool_key;
193 /* Context of the main thread */
194 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
197 ldap_int_thread_pool_startup ( void )
199 ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
200 ldap_pvt_thread_key_create( &ldap_tpool_key );
201 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
205 ldap_int_thread_pool_shutdown ( void )
207 struct ldap_int_thread_pool_s *pool;
209 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
210 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
212 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
213 ldap_pvt_thread_key_destroy( ldap_tpool_key );
218 /* Create a thread pool */
220 ldap_pvt_thread_pool_init_q (
221 ldap_pvt_thread_pool_t *tpool,
226 ldap_pvt_thread_pool_t pool;
227 struct ldap_int_thread_poolq_s *pq;
228 int i, rc, rem_thr, rem_pend;
230 /* multiple pools are currently not supported (ITS#4943) */
231 assert(!ldap_int_has_thread_pool);
233 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
235 if (! (1 <= max_pending && max_pending <= MAX_PENDING))
236 max_pending = MAX_PENDING;
239 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
240 sizeof(struct ldap_int_thread_pool_s));
242 if (pool == NULL) return(-1);
244 pool->ltp_wqs = LDAP_MALLOC(numqs * sizeof(struct ldap_int_thread_poolq_s *));
245 if (pool->ltp_wqs == NULL) {
250 for (i=0; i<numqs; i++) {
251 char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1);
254 LDAP_FREE(pool->ltp_wqs[i]->ltp_free);
255 LDAP_FREE(pool->ltp_wqs);
259 pool->ltp_wqs[i] = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1));
260 pool->ltp_wqs[i]->ltp_free = ptr;
263 pool->ltp_numqs = numqs;
264 pool->ltp_conf_max_count = max_threads;
266 max_threads = LDAP_MAXTHR;
268 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
272 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
276 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
280 rem_thr = max_threads % numqs;
281 rem_pend = max_pending % numqs;
282 for ( i=0; i<numqs; i++ ) {
283 pq = pool->ltp_wqs[i];
285 rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
288 rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
291 LDAP_STAILQ_INIT(&pq->ltp_pending_list);
292 pq->ltp_work_list = &pq->ltp_pending_list;
293 LDAP_SLIST_INIT(&pq->ltp_free_list);
295 pq->ltp_max_count = max_threads / numqs;
300 pq->ltp_max_pending = max_pending / numqs;
302 pq->ltp_max_pending++;
307 ldap_int_has_thread_pool = 1;
309 pool->ltp_max_count = max_threads;
310 pool->ltp_max_pending = max_pending;
312 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
313 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
314 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
316 /* Start no threads just yet. That can break if the process forks
317 * later, as slapd does in order to daemonize. On at least POSIX,
318 * only the forking thread would survive in the child. Yet fork()
319 * can't unlock/clean up other threads' locks and data structures,
320 * unless pthread_atfork() handlers have been set up to do so.
328 ldap_pvt_thread_pool_init (
329 ldap_pvt_thread_pool_t *tpool,
333 return ldap_pvt_thread_pool_init_q( tpool, max_threads, max_pending, 1 );
338 struct ldap_int_thread_pool_s *pool,
342 unsigned char *ptr = (unsigned char *)&arg;
343 /* dumb hash of arg to choose a queue */
344 for (j=0; j<sizeof(arg); j++)
346 i %= pool->ltp_numqs;
350 /* Submit a task to be performed by the thread pool */
352 ldap_pvt_thread_pool_submit (
353 ldap_pvt_thread_pool_t *tpool,
354 ldap_pvt_thread_start_t *start_routine, void *arg )
356 struct ldap_int_thread_pool_s *pool;
357 struct ldap_int_thread_poolq_s *pq;
358 ldap_int_thread_task_t *task;
359 ldap_pvt_thread_t thr;
370 if ( pool->ltp_numqs > 1 )
371 i = ldap_int_poolq_hash( pool, arg );
377 ldap_pvt_thread_mutex_lock(&pool->ltp_wqs[i]->ltp_mutex);
378 if (pool->ltp_wqs[i]->ltp_pending_count < pool->ltp_wqs[i]->ltp_max_pending) {
381 ldap_pvt_thread_mutex_unlock(&pool->ltp_wqs[i]->ltp_mutex);
383 i %= pool->ltp_numqs;
388 pq = pool->ltp_wqs[i];
389 task = LDAP_SLIST_FIRST(&pq->ltp_free_list);
391 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
393 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
398 task->ltt_start_routine = start_routine;
401 pq->ltp_pending_count++;
402 LDAP_STAILQ_INSERT_TAIL(&pq->ltp_pending_list, task, ltt_next.q);
407 /* should we open (create) a thread? */
408 if (pq->ltp_open_count < pq->ltp_active_count+pq->ltp_pending_count &&
409 pq->ltp_open_count < pq->ltp_max_count)
412 pq->ltp_open_count++;
414 if (0 != ldap_pvt_thread_create(
415 &thr, 1, ldap_int_thread_pool_wrapper, pq))
417 /* couldn't create thread. back out of
418 * ltp_open_count and check for even worse things.
421 pq->ltp_open_count--;
423 if (pq->ltp_open_count == 0) {
424 /* no open threads at all?!?
426 ldap_int_thread_task_t *ptr;
428 /* let pool_destroy know there are no more threads */
429 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
431 LDAP_STAILQ_FOREACH(ptr, &pq->ltp_pending_list, ltt_next.q)
432 if (ptr == task) break;
434 /* no open threads, task not handled, so
435 * back out of ltp_pending_count, free the task,
438 pq->ltp_pending_count--;
439 LDAP_STAILQ_REMOVE(&pq->ltp_pending_list, task,
440 ldap_int_thread_task_s, ltt_next.q);
441 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task,
446 /* there is another open thread, so this
447 * task will be handled eventually.
451 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
454 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
458 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
463 no_task( void *ctx, void *arg )
468 /* Cancel a pending task that was previously submitted.
469 * Return 1 if the task was successfully cancelled, 0 if
470 * not found, -1 for invalid parameters
473 ldap_pvt_thread_pool_retract (
474 ldap_pvt_thread_pool_t *tpool,
475 ldap_pvt_thread_start_t *start_routine, void *arg )
477 struct ldap_int_thread_pool_s *pool;
478 struct ldap_int_thread_poolq_s *pq;
479 ldap_int_thread_task_t *task;
490 i = ldap_int_poolq_hash( pool, arg );
491 pq = pool->ltp_wqs[i];
493 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
494 LDAP_STAILQ_FOREACH(task, &pq->ltp_pending_list, ltt_next.q)
495 if (task->ltt_start_routine == start_routine &&
496 task->ltt_arg == arg) {
497 /* Could LDAP_STAILQ_REMOVE the task, but that
498 * walks ltp_pending_list again to find it.
500 task->ltt_start_routine = no_task;
501 task->ltt_arg = NULL;
504 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
508 /* Set number of work queues in this pool. Should not be
509 * more than the number of CPUs. */
511 ldap_pvt_thread_pool_queues(
512 ldap_pvt_thread_pool_t *tpool,
515 struct ldap_int_thread_pool_s *pool;
516 struct ldap_int_thread_poolq_s *pq;
517 int i, rc, rem_thr, rem_pend;
519 if (numqs < 1 || tpool == NULL)
527 if (numqs < pool->ltp_numqs) {
528 for (i=numqs; i<pool->ltp_numqs; i++)
529 pool->ltp_wqs[i]->ltp_max_count = 0;
530 } else if (numqs > pool->ltp_numqs) {
531 struct ldap_int_thread_poolq_s **wqs;
532 wqs = LDAP_REALLOC(pool->ltp_wqs, numqs * sizeof(struct ldap_int_thread_poolq_s *));
536 for (i=pool->ltp_numqs; i<numqs; i++) {
537 char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1);
540 pool->ltp_wqs[i] = NULL;
543 pq = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1));
545 pool->ltp_wqs[i] = pq;
547 rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
550 rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
553 LDAP_STAILQ_INIT(&pq->ltp_pending_list);
554 pq->ltp_work_list = &pq->ltp_pending_list;
555 LDAP_SLIST_INIT(&pq->ltp_free_list);
558 rem_thr = pool->ltp_max_count % numqs;
559 rem_pend = pool->ltp_max_pending % numqs;
560 for ( i=0; i<numqs; i++ ) {
561 pq = pool->ltp_wqs[i];
562 pq->ltp_max_count = pool->ltp_max_count / numqs;
567 pq->ltp_max_pending = pool->ltp_max_pending / numqs;
569 pq->ltp_max_pending++;
573 pool->ltp_numqs = numqs;
577 /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */
579 ldap_pvt_thread_pool_maxthreads(
580 ldap_pvt_thread_pool_t *tpool,
583 struct ldap_int_thread_pool_s *pool;
584 struct ldap_int_thread_poolq_s *pq;
587 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
598 pool->ltp_conf_max_count = max_threads;
600 max_threads = LDAP_MAXTHR;
601 pool->ltp_max_count = max_threads;
603 remthr = max_threads % pool->ltp_numqs;
604 max_threads /= pool->ltp_numqs;
606 for (i=0; i<pool->ltp_numqs; i++) {
607 pq = pool->ltp_wqs[i];
608 pq->ltp_max_count = max_threads;
617 /* Inspect the pool */
619 ldap_pvt_thread_pool_query(
620 ldap_pvt_thread_pool_t *tpool,
621 ldap_pvt_thread_pool_param_t param,
624 struct ldap_int_thread_pool_s *pool;
627 if ( tpool == NULL || value == NULL ) {
633 if ( pool == NULL ) {
638 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
639 count = pool->ltp_conf_max_count;
642 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
643 count = pool->ltp_max_pending;
646 if (count == MAX_PENDING)
650 case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
651 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
652 count = (pool->ltp_pause != 0);
653 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
656 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
657 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
658 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
659 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
660 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
664 for (i=0; i<pool->ltp_numqs; i++) {
665 struct ldap_int_thread_poolq_s *pq = pool->ltp_wqs[i];
666 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
668 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
669 count += pq->ltp_open_count;
671 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
672 count += pq->ltp_starting;
674 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
675 count += pq->ltp_active_count;
677 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
678 count += pq->ltp_pending_count;
680 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
681 count += pq->ltp_pending_count + pq->ltp_active_count;
684 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
691 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
694 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
697 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
700 case LDAP_PVT_THREAD_POOL_PARAM_STATE:
702 *((char **)value) = "pausing";
703 else if (!pool->ltp_finishing)
704 *((char **)value) = "running";
707 for (i=0; i<pool->ltp_numqs; i++)
708 if (pool->ltp_wqs[i]->ltp_pending_count) break;
709 if (i<pool->ltp_numqs)
710 *((char **)value) = "finishing";
712 *((char **)value) = "stopping";
716 case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
721 *((int *)value) = count;
724 return ( count == -1 ? -1 : 0 );
728 * true if pool is pausing; does not lock any mutex to check.
729 * 0 if not pause, 1 if pause, -1 if error or no pool.
732 ldap_pvt_thread_pool_pausing( ldap_pvt_thread_pool_t *tpool )
735 struct ldap_int_thread_pool_s *pool;
737 if ( tpool != NULL && (pool = *tpool) != NULL ) {
738 rc = (pool->ltp_pause != 0);
745 * wrapper for ldap_pvt_thread_pool_query(), left around
746 * for backwards compatibility
749 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
753 rc = ldap_pvt_thread_pool_query( tpool,
754 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
763 /* Destroy the pool after making its threads finish */
765 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
767 struct ldap_int_thread_pool_s *pool, *pptr;
768 struct ldap_int_thread_poolq_s *pq;
769 ldap_int_thread_task_t *task;
777 if (pool == NULL) return(-1);
779 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
780 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
781 if (pptr == pool) break;
783 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
784 ldap_int_thread_pool_s, ltp_next);
785 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
787 if (pool != pptr) return(-1);
789 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
791 pool->ltp_finishing = 1;
792 if (pool->ltp_max_pending > 0)
793 pool->ltp_max_pending = -pool->ltp_max_pending;
795 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
796 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
798 for (i=0; i<pool->ltp_numqs; i++) {
799 pq = pool->ltp_wqs[i];
800 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
801 if (pq->ltp_max_pending > 0)
802 pq->ltp_max_pending = -pq->ltp_max_pending;
804 while ((task = LDAP_STAILQ_FIRST(&pq->ltp_pending_list)) != NULL) {
805 LDAP_STAILQ_REMOVE_HEAD(&pq->ltp_pending_list, ltt_next.q);
808 pq->ltp_pending_count = 0;
811 while (pq->ltp_open_count) {
812 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
813 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
816 while ((task = LDAP_SLIST_FIRST(&pq->ltp_free_list)) != NULL)
818 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
821 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
822 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
823 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
826 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
827 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
828 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
831 ldap_int_has_thread_pool = 0;
835 /* Thread loop. Accept and handle submitted tasks. */
837 ldap_int_thread_pool_wrapper (
840 struct ldap_int_thread_poolq_s *pq = xpool;
841 struct ldap_int_thread_pool_s *pool = pq->ltp_pool;
842 ldap_int_thread_task_t *task;
843 ldap_int_tpool_plist_t *work_list;
844 ldap_int_thread_userctx_t ctx, *kctx;
845 unsigned i, keyslot, hash;
846 int pool_lock = 0, freeme = 0;
848 assert(pool != NULL);
850 for ( i=0; i<MAXKEYS; i++ ) {
851 ctx.ltu_key[i].ltk_key = NULL;
855 ctx.ltu_id = ldap_pvt_thread_self();
856 TID_HASH(ctx.ltu_id, hash);
858 ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
860 if (pool->ltp_pause) {
861 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
862 /* thread_keys[] is read-only when paused */
863 while (pool->ltp_pause)
864 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
865 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
868 /* find a key slot to give this thread ID and store a
869 * pointer to our keys there; start at the thread ID
870 * itself (mod LDAP_MAXTHR) and look for an empty slot.
872 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
873 for (keyslot = hash & (LDAP_MAXTHR-1);
874 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
875 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
876 thread_keys[keyslot].ctx = &ctx;
877 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
879 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
881 pq->ltp_active_count++;
884 work_list = pq->ltp_work_list; /* help the compiler a bit */
885 task = LDAP_STAILQ_FIRST(work_list);
886 if (task == NULL) { /* paused or no pending tasks */
887 if (--(pq->ltp_active_count) < 1) {
888 if (pool->ltp_pause) {
889 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
890 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
892 if (--(pool->ltp_active_queues) < 1) {
893 /* Notify pool_pause it is the sole active thread. */
894 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
900 if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) {
901 /* Not paused, and either finishing or too many
902 * threads running (can happen if ltp_max_count
903 * was reduced). Let this thread die.
908 /* We could check an idle timer here, and let the
909 * thread die if it has been inactive for a while.
910 * Only die if there are other open threads (i.e.,
911 * always have at least one thread open).
912 * The check should be like this:
913 * if (pool->ltp_open_count>1 && pool->ltp_starting==0)
914 * check timer, wait if ltp_pause, leave thread;
916 * Just use pthread_cond_timedwait() if we want to
920 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
921 if (!pool->ltp_pause) {
922 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
923 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
927 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
929 work_list = pq->ltp_work_list;
930 task = LDAP_STAILQ_FIRST(work_list);
931 } while (task == NULL);
934 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
935 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
938 pq->ltp_active_count++;
941 LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q);
942 pq->ltp_pending_count--;
943 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
945 task->ltt_start_routine(&ctx, task->ltt_arg);
947 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
948 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l);
952 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
954 /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey()
955 * during this call, since it prevents new pauses. */
956 ldap_pvt_thread_pool_context_reset(&ctx);
958 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
959 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
961 pq->ltp_open_count--;
962 if (pq->ltp_open_count == 0) {
963 if (pool->ltp_finishing)
964 /* let pool_destroy know we're all done */
965 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
971 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
973 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
976 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
977 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
978 LDAP_FREE(pq->ltp_free);
980 ldap_pvt_thread_exit(NULL);
984 /* Arguments > ltp_pause to handle_pause(,PAUSE_ARG()). arg=PAUSE_ARG
985 * ensures (arg-ltp_pause) sets GO_* at need and keeps DO_PAUSE/GO_*.
989 #define CHECK_PAUSE 32 /* if ltp_pause: GO_IDLE; wait; GO_UNIDLE */
990 #define DO_PAUSE 64 /* CHECK_PAUSE; pause the pool */
991 #define PAUSE_ARG(a) \
992 ((a) | ((a) & (GO_IDLE|GO_UNIDLE) ? GO_IDLE-1 : CHECK_PAUSE))
995 handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type )
997 struct ldap_int_thread_pool_s *pool;
998 struct ldap_int_thread_poolq_s *pq;
999 int ret = 0, pause, max_ltp_pause;
1009 if (pause_type == CHECK_PAUSE && !pool->ltp_pause)
1013 ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context();
1017 /* Let pool_unidle() ignore requests for new pauses */
1018 max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED;
1020 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1022 pause = pool->ltp_pause; /* NOT_PAUSED, WANT_PAUSE or PAUSED */
1024 /* If ltp_pause and not GO_IDLE|GO_UNIDLE: Set GO_IDLE,GO_UNIDLE */
1025 pause_type -= pause;
1027 if (pause_type & GO_IDLE) {
1029 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1030 pq->ltp_pending_count++;
1031 pq->ltp_active_count--;
1032 if (pause && pq->ltp_active_count < 1) {
1035 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1037 pool->ltp_active_queues--;
1038 if (pool->ltp_active_queues < 1)
1039 /* Tell the task waiting to DO_PAUSE it can proceed */
1040 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
1044 if (pause_type & GO_UNIDLE) {
1045 /* Wait out pause if any, then cancel GO_IDLE */
1046 if (pause > max_ltp_pause) {
1049 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
1050 } while (pool->ltp_pause > max_ltp_pause);
1052 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1053 pq->ltp_pending_count--;
1054 pq->ltp_active_count++;
1055 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1058 if (pause_type & DO_PAUSE) {
1060 /* Tell everyone else to pause or finish, then await that */
1062 assert(!pool->ltp_pause);
1063 pool->ltp_pause = WANT_PAUSE;
1064 pool->ltp_active_queues = 0;
1066 for (i=0; i<pool->ltp_numqs; i++)
1067 if (pool->ltp_wqs[i] == pq) break;
1069 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1070 /* temporarily remove ourself from active count */
1071 pq->ltp_active_count--;
1075 pq = pool->ltp_wqs[j];
1077 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1079 /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test,
1080 * and do not finish threads in ldap_pvt_thread_pool_wrapper() */
1081 pq->ltp_open_count = -pq->ltp_open_count;
1082 /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
1083 pq->ltp_work_list = &empty_pending_list;
1085 if (pq->ltp_active_count > 0)
1086 pool->ltp_active_queues++;
1088 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1089 if (pool->ltp_numqs > 1) {
1091 j %= pool->ltp_numqs;
1095 /* Wait for this task to become the sole active task */
1096 while (pool->ltp_active_queues > 0)
1097 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
1099 /* restore us to active count */
1100 pool->ltp_wqs[i]->ltp_active_count++;
1102 assert(pool->ltp_pause == WANT_PAUSE);
1103 pool->ltp_pause = PAUSED;
1105 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1110 /* Consider this task idle: It will not block pool_pause() in other tasks. */
1112 ldap_pvt_thread_pool_idle( ldap_pvt_thread_pool_t *tpool )
1114 handle_pause(tpool, PAUSE_ARG(GO_IDLE));
1117 /* Cancel pool_idle(). If the pool is paused, wait it out first. */
1119 ldap_pvt_thread_pool_unidle( ldap_pvt_thread_pool_t *tpool )
1121 handle_pause(tpool, PAUSE_ARG(GO_UNIDLE));
1125 * If a pause was requested, wait for it. If several threads
1126 * are waiting to pause, let through one or more pauses.
1127 * The calling task must be active, not idle.
1128 * Return 1 if we waited, 0 if not, -1 at parameter error.
1131 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t *tpool )
1133 return handle_pause(tpool, PAUSE_ARG(CHECK_PAUSE));
1137 * Pause the pool. The calling task must be active, not idle.
1138 * Return when all other tasks are paused or idle.
1141 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
1143 return handle_pause(tpool, PAUSE_ARG(DO_PAUSE));
1148 ldap_pvt_thread_pool_resume (
1149 ldap_pvt_thread_pool_t *tpool )
1151 struct ldap_int_thread_pool_s *pool;
1152 struct ldap_int_thread_poolq_s *pq;
1163 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1164 assert(pool->ltp_pause == PAUSED);
1165 pool->ltp_pause = 0;
1166 for (i=0; i<pool->ltp_numqs; i++) {
1167 pq = pool->ltp_wqs[i];
1168 if (pq->ltp_open_count <= 0) /* true when paused, but be paranoid */
1169 pq->ltp_open_count = -pq->ltp_open_count;
1170 pq->ltp_work_list = &pq->ltp_pending_list;
1171 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
1173 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
1174 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1179 * Get the key's data and optionally free function in the given context.
1181 int ldap_pvt_thread_pool_getkey(
1185 ldap_pvt_thread_pool_keyfree_t **kfree )
1187 ldap_int_thread_userctx_t *ctx = xctx;
1190 if ( !ctx || !key || !data ) return EINVAL;
1192 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
1193 if ( ctx->ltu_key[i].ltk_key == key ) {
1194 *data = ctx->ltu_key[i].ltk_data;
1195 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
1203 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
1205 for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
1206 ctx->ltu_key[i] = ctx->ltu_key[i+1];
1207 ctx->ltu_key[i].ltk_key = NULL;
1211 * Set or remove data for the key in the given context.
1212 * key can be any unique pointer.
1213 * kfree() is an optional function to free the data (but not the key):
1214 * pool_context_reset() and pool_purgekey() call kfree(key, data),
1215 * but pool_setkey() does not. For pool_setkey() it is the caller's
1216 * responsibility to free any existing data with the same key.
1217 * kfree() must not call functions taking a tpool argument.
1219 int ldap_pvt_thread_pool_setkey(
1223 ldap_pvt_thread_pool_keyfree_t *kfree,
1225 ldap_pvt_thread_pool_keyfree_t **oldkfreep )
1227 ldap_int_thread_userctx_t *ctx = xctx;
1230 if ( !ctx || !key ) return EINVAL;
1232 for ( i=found=0; i<MAXKEYS; i++ ) {
1233 if ( ctx->ltu_key[i].ltk_key == key ) {
1236 } else if ( !ctx->ltu_key[i].ltk_key ) {
1243 *olddatap = ctx->ltu_key[i].ltk_data;
1251 *oldkfreep = ctx->ltu_key[i].ltk_free;
1257 if ( data || kfree ) {
1260 ctx->ltu_key[i].ltk_key = key;
1261 ctx->ltu_key[i].ltk_data = data;
1262 ctx->ltu_key[i].ltk_free = kfree;
1263 } else if ( found ) {
1264 clear_key_idx( ctx, i );
1270 /* Free all elements with this key, no matter which thread they're in.
1271 * May only be called while the pool is paused.
1273 void ldap_pvt_thread_pool_purgekey( void *key )
1276 ldap_int_thread_userctx_t *ctx;
1278 assert ( key != NULL );
1280 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
1281 for ( i=0; i<LDAP_MAXTHR; i++ ) {
1282 ctx = thread_keys[i].ctx;
1283 if ( ctx && ctx != DELETED_THREAD_CTX ) {
1284 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
1285 if ( ctx->ltu_key[j].ltk_key == key ) {
1286 if (ctx->ltu_key[j].ltk_free)
1287 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
1288 ctx->ltu_key[j].ltk_data );
1289 clear_key_idx( ctx, j );
1295 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
1299 * Find the context of the current thread.
1300 * This is necessary if the caller does not have access to the
1301 * thread context handle (for example, a slapd plugin calling
1302 * slapi_search_internal()). No doubt it is more efficient
1303 * for the application to keep track of the thread context
1306 void *ldap_pvt_thread_pool_context( )
1310 ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
1311 return ctx ? ctx : (void *) &ldap_int_main_thrctx;
1315 * Free the context's keys.
1316 * Must not call functions taking a tpool argument (because this
1317 * thread already holds ltp_mutex when called from pool_wrapper()).
1319 void ldap_pvt_thread_pool_context_reset( void *vctx )
1321 ldap_int_thread_userctx_t *ctx = vctx;
1324 for ( i=MAXKEYS-1; i>=0; i--) {
1325 if ( !ctx->ltu_key[i].ltk_key )
1327 if ( ctx->ltu_key[i].ltk_free )
1328 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
1329 ctx->ltu_key[i].ltk_data );
1330 ctx->ltu_key[i].ltk_key = NULL;
1334 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
1336 ldap_int_thread_userctx_t *ctx = vctx;
1340 #endif /* LDAP_THREAD_HAVE_TPOOL */