2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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);
829 for (i=0; i<pool->ltp_numqs; i++) {
830 pq = pool->ltp_wqs[i];
832 LDAP_FREE(pq->ltp_free);
835 LDAP_FREE(pool->ltp_wqs);
838 ldap_int_has_thread_pool = 0;
842 /* Thread loop. Accept and handle submitted tasks. */
844 ldap_int_thread_pool_wrapper (
847 struct ldap_int_thread_poolq_s *pq = xpool;
848 struct ldap_int_thread_pool_s *pool = pq->ltp_pool;
849 ldap_int_thread_task_t *task;
850 ldap_int_tpool_plist_t *work_list;
851 ldap_int_thread_userctx_t ctx, *kctx;
852 unsigned i, keyslot, hash;
853 int pool_lock = 0, freeme = 0;
855 assert(pool != NULL);
857 for ( i=0; i<MAXKEYS; i++ ) {
858 ctx.ltu_key[i].ltk_key = NULL;
862 ctx.ltu_id = ldap_pvt_thread_self();
863 TID_HASH(ctx.ltu_id, hash);
865 ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
867 if (pool->ltp_pause) {
868 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
869 /* thread_keys[] is read-only when paused */
870 while (pool->ltp_pause)
871 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
872 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
875 /* find a key slot to give this thread ID and store a
876 * pointer to our keys there; start at the thread ID
877 * itself (mod LDAP_MAXTHR) and look for an empty slot.
879 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
880 for (keyslot = hash & (LDAP_MAXTHR-1);
881 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
882 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
883 thread_keys[keyslot].ctx = &ctx;
884 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
886 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
888 pq->ltp_active_count++;
891 work_list = pq->ltp_work_list; /* help the compiler a bit */
892 task = LDAP_STAILQ_FIRST(work_list);
893 if (task == NULL) { /* paused or no pending tasks */
894 if (--(pq->ltp_active_count) < 1) {
895 if (pool->ltp_pause) {
896 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
897 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
899 if (--(pool->ltp_active_queues) < 1) {
900 /* Notify pool_pause it is the sole active thread. */
901 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
907 if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) {
908 /* Not paused, and either finishing or too many
909 * threads running (can happen if ltp_max_count
910 * was reduced). Let this thread die.
915 /* We could check an idle timer here, and let the
916 * thread die if it has been inactive for a while.
917 * Only die if there are other open threads (i.e.,
918 * always have at least one thread open).
919 * The check should be like this:
920 * if (pool->ltp_open_count>1 && pool->ltp_starting==0)
921 * check timer, wait if ltp_pause, leave thread;
923 * Just use pthread_cond_timedwait() if we want to
927 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
928 if (!pool->ltp_pause) {
929 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
930 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
934 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
936 work_list = pq->ltp_work_list;
937 task = LDAP_STAILQ_FIRST(work_list);
938 } while (task == NULL);
941 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
942 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
945 pq->ltp_active_count++;
948 LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q);
949 pq->ltp_pending_count--;
950 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
952 task->ltt_start_routine(&ctx, task->ltt_arg);
954 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
955 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l);
959 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
961 /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey()
962 * during this call, since it prevents new pauses. */
963 ldap_pvt_thread_pool_context_reset(&ctx);
965 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
966 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
968 pq->ltp_open_count--;
969 if (pq->ltp_open_count == 0) {
970 if (pool->ltp_finishing)
971 /* let pool_destroy know we're all done */
972 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
978 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
980 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
983 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
984 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
985 LDAP_FREE(pq->ltp_free);
988 ldap_pvt_thread_exit(NULL);
992 /* Arguments > ltp_pause to handle_pause(,PAUSE_ARG()). arg=PAUSE_ARG
993 * ensures (arg-ltp_pause) sets GO_* at need and keeps DO_PAUSE/GO_*.
997 #define CHECK_PAUSE 32 /* if ltp_pause: GO_IDLE; wait; GO_UNIDLE */
998 #define DO_PAUSE 64 /* CHECK_PAUSE; pause the pool */
999 #define PAUSE_ARG(a) \
1000 ((a) | ((a) & (GO_IDLE|GO_UNIDLE) ? GO_IDLE-1 : CHECK_PAUSE))
1003 handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type )
1005 struct ldap_int_thread_pool_s *pool;
1006 struct ldap_int_thread_poolq_s *pq;
1007 int ret = 0, pause, max_ltp_pause;
1017 if (pause_type == CHECK_PAUSE && !pool->ltp_pause)
1021 ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context();
1025 /* Let pool_unidle() ignore requests for new pauses */
1026 max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED;
1028 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1030 pause = pool->ltp_pause; /* NOT_PAUSED, WANT_PAUSE or PAUSED */
1032 /* If ltp_pause and not GO_IDLE|GO_UNIDLE: Set GO_IDLE,GO_UNIDLE */
1033 pause_type -= pause;
1035 if (pause_type & GO_IDLE) {
1037 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1038 pq->ltp_pending_count++;
1039 pq->ltp_active_count--;
1040 if (pause && pq->ltp_active_count < 1) {
1043 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1045 pool->ltp_active_queues--;
1046 if (pool->ltp_active_queues < 1)
1047 /* Tell the task waiting to DO_PAUSE it can proceed */
1048 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
1052 if (pause_type & GO_UNIDLE) {
1053 /* Wait out pause if any, then cancel GO_IDLE */
1054 if (pause > max_ltp_pause) {
1057 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
1058 } while (pool->ltp_pause > max_ltp_pause);
1060 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1061 pq->ltp_pending_count--;
1062 pq->ltp_active_count++;
1063 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1066 if (pause_type & DO_PAUSE) {
1068 /* Tell everyone else to pause or finish, then await that */
1070 assert(!pool->ltp_pause);
1071 pool->ltp_pause = WANT_PAUSE;
1072 pool->ltp_active_queues = 0;
1074 for (i=0; i<pool->ltp_numqs; i++)
1075 if (pool->ltp_wqs[i] == pq) break;
1077 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1078 /* temporarily remove ourself from active count */
1079 pq->ltp_active_count--;
1083 pq = pool->ltp_wqs[j];
1085 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1087 /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test,
1088 * and do not finish threads in ldap_pvt_thread_pool_wrapper() */
1089 pq->ltp_open_count = -pq->ltp_open_count;
1090 /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
1091 pq->ltp_work_list = &empty_pending_list;
1093 if (pq->ltp_active_count > 0)
1094 pool->ltp_active_queues++;
1096 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1097 if (pool->ltp_numqs > 1) {
1099 j %= pool->ltp_numqs;
1103 /* Wait for this task to become the sole active task */
1104 while (pool->ltp_active_queues > 0)
1105 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
1107 /* restore us to active count */
1108 pool->ltp_wqs[i]->ltp_active_count++;
1110 assert(pool->ltp_pause == WANT_PAUSE);
1111 pool->ltp_pause = PAUSED;
1113 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1118 /* Consider this task idle: It will not block pool_pause() in other tasks. */
1120 ldap_pvt_thread_pool_idle( ldap_pvt_thread_pool_t *tpool )
1122 handle_pause(tpool, PAUSE_ARG(GO_IDLE));
1125 /* Cancel pool_idle(). If the pool is paused, wait it out first. */
1127 ldap_pvt_thread_pool_unidle( ldap_pvt_thread_pool_t *tpool )
1129 handle_pause(tpool, PAUSE_ARG(GO_UNIDLE));
1133 * If a pause was requested, wait for it. If several threads
1134 * are waiting to pause, let through one or more pauses.
1135 * The calling task must be active, not idle.
1136 * Return 1 if we waited, 0 if not, -1 at parameter error.
1139 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t *tpool )
1141 return handle_pause(tpool, PAUSE_ARG(CHECK_PAUSE));
1145 * Pause the pool. The calling task must be active, not idle.
1146 * Return when all other tasks are paused or idle.
1149 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
1151 return handle_pause(tpool, PAUSE_ARG(DO_PAUSE));
1156 ldap_pvt_thread_pool_resume (
1157 ldap_pvt_thread_pool_t *tpool )
1159 struct ldap_int_thread_pool_s *pool;
1160 struct ldap_int_thread_poolq_s *pq;
1171 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1172 assert(pool->ltp_pause == PAUSED);
1173 pool->ltp_pause = 0;
1174 for (i=0; i<pool->ltp_numqs; i++) {
1175 pq = pool->ltp_wqs[i];
1176 if (pq->ltp_open_count <= 0) /* true when paused, but be paranoid */
1177 pq->ltp_open_count = -pq->ltp_open_count;
1178 pq->ltp_work_list = &pq->ltp_pending_list;
1179 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
1181 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
1182 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1187 * Get the key's data and optionally free function in the given context.
1189 int ldap_pvt_thread_pool_getkey(
1193 ldap_pvt_thread_pool_keyfree_t **kfree )
1195 ldap_int_thread_userctx_t *ctx = xctx;
1198 if ( !ctx || !key || !data ) return EINVAL;
1200 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
1201 if ( ctx->ltu_key[i].ltk_key == key ) {
1202 *data = ctx->ltu_key[i].ltk_data;
1203 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
1211 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
1213 for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
1214 ctx->ltu_key[i] = ctx->ltu_key[i+1];
1215 ctx->ltu_key[i].ltk_key = NULL;
1219 * Set or remove data for the key in the given context.
1220 * key can be any unique pointer.
1221 * kfree() is an optional function to free the data (but not the key):
1222 * pool_context_reset() and pool_purgekey() call kfree(key, data),
1223 * but pool_setkey() does not. For pool_setkey() it is the caller's
1224 * responsibility to free any existing data with the same key.
1225 * kfree() must not call functions taking a tpool argument.
1227 int ldap_pvt_thread_pool_setkey(
1231 ldap_pvt_thread_pool_keyfree_t *kfree,
1233 ldap_pvt_thread_pool_keyfree_t **oldkfreep )
1235 ldap_int_thread_userctx_t *ctx = xctx;
1238 if ( !ctx || !key ) return EINVAL;
1240 for ( i=found=0; i<MAXKEYS; i++ ) {
1241 if ( ctx->ltu_key[i].ltk_key == key ) {
1244 } else if ( !ctx->ltu_key[i].ltk_key ) {
1251 *olddatap = ctx->ltu_key[i].ltk_data;
1259 *oldkfreep = ctx->ltu_key[i].ltk_free;
1265 if ( data || kfree ) {
1268 ctx->ltu_key[i].ltk_key = key;
1269 ctx->ltu_key[i].ltk_data = data;
1270 ctx->ltu_key[i].ltk_free = kfree;
1271 } else if ( found ) {
1272 clear_key_idx( ctx, i );
1278 /* Free all elements with this key, no matter which thread they're in.
1279 * May only be called while the pool is paused.
1281 void ldap_pvt_thread_pool_purgekey( void *key )
1284 ldap_int_thread_userctx_t *ctx;
1286 assert ( key != NULL );
1288 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
1289 for ( i=0; i<LDAP_MAXTHR; i++ ) {
1290 ctx = thread_keys[i].ctx;
1291 if ( ctx && ctx != DELETED_THREAD_CTX ) {
1292 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
1293 if ( ctx->ltu_key[j].ltk_key == key ) {
1294 if (ctx->ltu_key[j].ltk_free)
1295 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
1296 ctx->ltu_key[j].ltk_data );
1297 clear_key_idx( ctx, j );
1303 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
1307 * Find the context of the current thread.
1308 * This is necessary if the caller does not have access to the
1309 * thread context handle (for example, a slapd plugin calling
1310 * slapi_search_internal()). No doubt it is more efficient
1311 * for the application to keep track of the thread context
1314 void *ldap_pvt_thread_pool_context( )
1318 ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
1319 return ctx ? ctx : (void *) &ldap_int_main_thrctx;
1323 * Free the context's keys.
1324 * Must not call functions taking a tpool argument (because this
1325 * thread already holds ltp_mutex when called from pool_wrapper()).
1327 void ldap_pvt_thread_pool_context_reset( void *vctx )
1329 ldap_int_thread_userctx_t *ctx = vctx;
1332 for ( i=MAXKEYS-1; i>=0; i--) {
1333 if ( !ctx->ltu_key[i].ltk_key )
1335 if ( ctx->ltu_key[i].ltk_free )
1336 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
1337 ctx->ltu_key[i].ltk_data );
1338 ctx->ltu_key[i].ltk_key = NULL;
1342 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
1344 ldap_int_thread_userctx_t *ctx = vctx;
1348 #endif /* LDAP_THREAD_HAVE_TPOOL */