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
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);
271 for (i=0; i<numqs; i++)
272 LDAP_FREE(pool->ltp_wqs[i]->ltp_free);
273 LDAP_FREE(pool->ltp_wqs);
278 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
282 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
286 rem_thr = max_threads % numqs;
287 rem_pend = max_pending % numqs;
288 for ( i=0; i<numqs; i++ ) {
289 pq = pool->ltp_wqs[i];
291 rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
294 rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
297 LDAP_STAILQ_INIT(&pq->ltp_pending_list);
298 pq->ltp_work_list = &pq->ltp_pending_list;
299 LDAP_SLIST_INIT(&pq->ltp_free_list);
301 pq->ltp_max_count = max_threads / numqs;
306 pq->ltp_max_pending = max_pending / numqs;
308 pq->ltp_max_pending++;
313 ldap_int_has_thread_pool = 1;
315 pool->ltp_max_count = max_threads;
316 pool->ltp_max_pending = max_pending;
318 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
319 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
320 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
322 /* Start no threads just yet. That can break if the process forks
323 * later, as slapd does in order to daemonize. On at least POSIX,
324 * only the forking thread would survive in the child. Yet fork()
325 * can't unlock/clean up other threads' locks and data structures,
326 * unless pthread_atfork() handlers have been set up to do so.
334 ldap_pvt_thread_pool_init (
335 ldap_pvt_thread_pool_t *tpool,
339 return ldap_pvt_thread_pool_init_q( tpool, max_threads, max_pending, 1 );
344 struct ldap_int_thread_pool_s *pool,
348 unsigned char *ptr = (unsigned char *)&arg;
349 /* dumb hash of arg to choose a queue */
350 for (j=0; j<sizeof(arg); j++)
352 i %= pool->ltp_numqs;
356 /* Submit a task to be performed by the thread pool */
358 ldap_pvt_thread_pool_submit (
359 ldap_pvt_thread_pool_t *tpool,
360 ldap_pvt_thread_start_t *start_routine, void *arg )
362 struct ldap_int_thread_pool_s *pool;
363 struct ldap_int_thread_poolq_s *pq;
364 ldap_int_thread_task_t *task;
365 ldap_pvt_thread_t thr;
376 if ( pool->ltp_numqs > 1 )
377 i = ldap_int_poolq_hash( pool, arg );
383 ldap_pvt_thread_mutex_lock(&pool->ltp_wqs[i]->ltp_mutex);
384 if (pool->ltp_wqs[i]->ltp_pending_count < pool->ltp_wqs[i]->ltp_max_pending) {
387 ldap_pvt_thread_mutex_unlock(&pool->ltp_wqs[i]->ltp_mutex);
389 i %= pool->ltp_numqs;
394 pq = pool->ltp_wqs[i];
395 task = LDAP_SLIST_FIRST(&pq->ltp_free_list);
397 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
399 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
404 task->ltt_start_routine = start_routine;
407 pq->ltp_pending_count++;
408 LDAP_STAILQ_INSERT_TAIL(&pq->ltp_pending_list, task, ltt_next.q);
413 /* should we open (create) a thread? */
414 if (pq->ltp_open_count < pq->ltp_active_count+pq->ltp_pending_count &&
415 pq->ltp_open_count < pq->ltp_max_count)
418 pq->ltp_open_count++;
420 if (0 != ldap_pvt_thread_create(
421 &thr, 1, ldap_int_thread_pool_wrapper, pq))
423 /* couldn't create thread. back out of
424 * ltp_open_count and check for even worse things.
427 pq->ltp_open_count--;
429 if (pq->ltp_open_count == 0) {
430 /* no open threads at all?!?
432 ldap_int_thread_task_t *ptr;
434 /* let pool_destroy know there are no more threads */
435 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
437 LDAP_STAILQ_FOREACH(ptr, &pq->ltp_pending_list, ltt_next.q)
438 if (ptr == task) break;
440 /* no open threads, task not handled, so
441 * back out of ltp_pending_count, free the task,
444 pq->ltp_pending_count--;
445 LDAP_STAILQ_REMOVE(&pq->ltp_pending_list, task,
446 ldap_int_thread_task_s, ltt_next.q);
447 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task,
452 /* there is another open thread, so this
453 * task will be handled eventually.
457 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
460 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
464 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
469 no_task( void *ctx, void *arg )
474 /* Cancel a pending task that was previously submitted.
475 * Return 1 if the task was successfully cancelled, 0 if
476 * not found, -1 for invalid parameters
479 ldap_pvt_thread_pool_retract (
480 ldap_pvt_thread_pool_t *tpool,
481 ldap_pvt_thread_start_t *start_routine, void *arg )
483 struct ldap_int_thread_pool_s *pool;
484 struct ldap_int_thread_poolq_s *pq;
485 ldap_int_thread_task_t *task;
496 i = ldap_int_poolq_hash( pool, arg );
497 pq = pool->ltp_wqs[i];
499 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
500 LDAP_STAILQ_FOREACH(task, &pq->ltp_pending_list, ltt_next.q)
501 if (task->ltt_start_routine == start_routine &&
502 task->ltt_arg == arg) {
503 /* Could LDAP_STAILQ_REMOVE the task, but that
504 * walks ltp_pending_list again to find it.
506 task->ltt_start_routine = no_task;
507 task->ltt_arg = NULL;
510 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
514 /* Set number of work queues in this pool. Should not be
515 * more than the number of CPUs. */
517 ldap_pvt_thread_pool_queues(
518 ldap_pvt_thread_pool_t *tpool,
521 struct ldap_int_thread_pool_s *pool;
522 struct ldap_int_thread_poolq_s *pq;
523 int i, rc, rem_thr, rem_pend;
525 if (numqs < 1 || tpool == NULL)
533 if (numqs < pool->ltp_numqs) {
534 for (i=numqs; i<pool->ltp_numqs; i++)
535 pool->ltp_wqs[i]->ltp_max_count = 0;
536 } else if (numqs > pool->ltp_numqs) {
537 struct ldap_int_thread_poolq_s **wqs;
538 wqs = LDAP_REALLOC(pool->ltp_wqs, numqs * sizeof(struct ldap_int_thread_poolq_s *));
542 for (i=pool->ltp_numqs; i<numqs; i++) {
543 char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1);
546 pool->ltp_wqs[i] = NULL;
549 pq = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1));
551 pool->ltp_wqs[i] = pq;
553 rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
556 rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
559 LDAP_STAILQ_INIT(&pq->ltp_pending_list);
560 pq->ltp_work_list = &pq->ltp_pending_list;
561 LDAP_SLIST_INIT(&pq->ltp_free_list);
564 rem_thr = pool->ltp_max_count % numqs;
565 rem_pend = pool->ltp_max_pending % numqs;
566 for ( i=0; i<numqs; i++ ) {
567 pq = pool->ltp_wqs[i];
568 pq->ltp_max_count = pool->ltp_max_count / numqs;
573 pq->ltp_max_pending = pool->ltp_max_pending / numqs;
575 pq->ltp_max_pending++;
579 pool->ltp_numqs = numqs;
583 /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */
585 ldap_pvt_thread_pool_maxthreads(
586 ldap_pvt_thread_pool_t *tpool,
589 struct ldap_int_thread_pool_s *pool;
590 struct ldap_int_thread_poolq_s *pq;
593 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
604 pool->ltp_conf_max_count = max_threads;
606 max_threads = LDAP_MAXTHR;
607 pool->ltp_max_count = max_threads;
609 remthr = max_threads % pool->ltp_numqs;
610 max_threads /= pool->ltp_numqs;
612 for (i=0; i<pool->ltp_numqs; i++) {
613 pq = pool->ltp_wqs[i];
614 pq->ltp_max_count = max_threads;
623 /* Inspect the pool */
625 ldap_pvt_thread_pool_query(
626 ldap_pvt_thread_pool_t *tpool,
627 ldap_pvt_thread_pool_param_t param,
630 struct ldap_int_thread_pool_s *pool;
633 if ( tpool == NULL || value == NULL ) {
639 if ( pool == NULL ) {
644 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
645 count = pool->ltp_conf_max_count;
648 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
649 count = pool->ltp_max_pending;
652 if (count == MAX_PENDING)
656 case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
657 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
658 count = (pool->ltp_pause != 0);
659 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
662 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
663 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
664 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
665 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
666 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
670 for (i=0; i<pool->ltp_numqs; i++) {
671 struct ldap_int_thread_poolq_s *pq = pool->ltp_wqs[i];
672 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
674 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
675 count += pq->ltp_open_count;
677 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
678 count += pq->ltp_starting;
680 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
681 count += pq->ltp_active_count;
683 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
684 count += pq->ltp_pending_count;
686 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
687 count += pq->ltp_pending_count + pq->ltp_active_count;
690 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
697 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
700 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
703 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
706 case LDAP_PVT_THREAD_POOL_PARAM_STATE:
708 *((char **)value) = "pausing";
709 else if (!pool->ltp_finishing)
710 *((char **)value) = "running";
713 for (i=0; i<pool->ltp_numqs; i++)
714 if (pool->ltp_wqs[i]->ltp_pending_count) break;
715 if (i<pool->ltp_numqs)
716 *((char **)value) = "finishing";
718 *((char **)value) = "stopping";
722 case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
727 *((int *)value) = count;
730 return ( count == -1 ? -1 : 0 );
734 * true if pool is pausing; does not lock any mutex to check.
735 * 0 if not pause, 1 if pause, -1 if error or no pool.
738 ldap_pvt_thread_pool_pausing( ldap_pvt_thread_pool_t *tpool )
741 struct ldap_int_thread_pool_s *pool;
743 if ( tpool != NULL && (pool = *tpool) != NULL ) {
744 rc = (pool->ltp_pause != 0);
751 * wrapper for ldap_pvt_thread_pool_query(), left around
752 * for backwards compatibility
755 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
759 rc = ldap_pvt_thread_pool_query( tpool,
760 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
769 /* Destroy the pool after making its threads finish */
771 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
773 struct ldap_int_thread_pool_s *pool, *pptr;
774 struct ldap_int_thread_poolq_s *pq;
775 ldap_int_thread_task_t *task;
783 if (pool == NULL) return(-1);
785 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
786 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
787 if (pptr == pool) break;
789 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
790 ldap_int_thread_pool_s, ltp_next);
791 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
793 if (pool != pptr) return(-1);
795 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
797 pool->ltp_finishing = 1;
798 if (pool->ltp_max_pending > 0)
799 pool->ltp_max_pending = -pool->ltp_max_pending;
801 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
802 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
804 for (i=0; i<pool->ltp_numqs; i++) {
805 pq = pool->ltp_wqs[i];
806 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
807 if (pq->ltp_max_pending > 0)
808 pq->ltp_max_pending = -pq->ltp_max_pending;
810 while ((task = LDAP_STAILQ_FIRST(&pq->ltp_pending_list)) != NULL) {
811 LDAP_STAILQ_REMOVE_HEAD(&pq->ltp_pending_list, ltt_next.q);
814 pq->ltp_pending_count = 0;
817 while (pq->ltp_open_count) {
818 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
819 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
822 while ((task = LDAP_SLIST_FIRST(&pq->ltp_free_list)) != NULL)
824 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
827 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
828 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
829 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
832 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
833 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
834 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
835 for (i=0; i<pool->ltp_numqs; i++) {
836 pq = pool->ltp_wqs[i];
838 LDAP_FREE(pq->ltp_free);
841 LDAP_FREE(pool->ltp_wqs);
844 ldap_int_has_thread_pool = 0;
848 /* Thread loop. Accept and handle submitted tasks. */
850 ldap_int_thread_pool_wrapper (
853 struct ldap_int_thread_poolq_s *pq = xpool;
854 struct ldap_int_thread_pool_s *pool = pq->ltp_pool;
855 ldap_int_thread_task_t *task;
856 ldap_int_tpool_plist_t *work_list;
857 ldap_int_thread_userctx_t ctx, *kctx;
858 unsigned i, keyslot, hash;
859 int pool_lock = 0, freeme = 0;
861 assert(pool != NULL);
863 for ( i=0; i<MAXKEYS; i++ ) {
864 ctx.ltu_key[i].ltk_key = NULL;
868 ctx.ltu_id = ldap_pvt_thread_self();
869 TID_HASH(ctx.ltu_id, hash);
871 ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
873 if (pool->ltp_pause) {
874 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
875 /* thread_keys[] is read-only when paused */
876 while (pool->ltp_pause)
877 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
878 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
881 /* find a key slot to give this thread ID and store a
882 * pointer to our keys there; start at the thread ID
883 * itself (mod LDAP_MAXTHR) and look for an empty slot.
885 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
886 for (keyslot = hash & (LDAP_MAXTHR-1);
887 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
888 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
889 thread_keys[keyslot].ctx = &ctx;
890 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
892 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
894 pq->ltp_active_count++;
897 work_list = pq->ltp_work_list; /* help the compiler a bit */
898 task = LDAP_STAILQ_FIRST(work_list);
899 if (task == NULL) { /* paused or no pending tasks */
900 if (--(pq->ltp_active_count) < 1) {
901 if (pool->ltp_pause) {
902 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
903 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
905 if (--(pool->ltp_active_queues) < 1) {
906 /* Notify pool_pause it is the sole active thread. */
907 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
913 if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) {
914 /* Not paused, and either finishing or too many
915 * threads running (can happen if ltp_max_count
916 * was reduced). Let this thread die.
921 /* We could check an idle timer here, and let the
922 * thread die if it has been inactive for a while.
923 * Only die if there are other open threads (i.e.,
924 * always have at least one thread open).
925 * The check should be like this:
926 * if (pool->ltp_open_count>1 && pool->ltp_starting==0)
927 * check timer, wait if ltp_pause, leave thread;
929 * Just use pthread_cond_timedwait() if we want to
933 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
934 if (!pool->ltp_pause) {
935 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
936 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
940 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
942 work_list = pq->ltp_work_list;
943 task = LDAP_STAILQ_FIRST(work_list);
944 } while (task == NULL);
947 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
948 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
951 pq->ltp_active_count++;
954 LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q);
955 pq->ltp_pending_count--;
956 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
958 task->ltt_start_routine(&ctx, task->ltt_arg);
960 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
961 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l);
965 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
967 /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey()
968 * during this call, since it prevents new pauses. */
969 ldap_pvt_thread_pool_context_reset(&ctx);
971 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
972 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
974 pq->ltp_open_count--;
975 if (pq->ltp_open_count == 0) {
976 if (pool->ltp_finishing)
977 /* let pool_destroy know we're all done */
978 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
984 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
986 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
989 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
990 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
991 LDAP_FREE(pq->ltp_free);
994 ldap_pvt_thread_exit(NULL);
998 /* Arguments > ltp_pause to handle_pause(,PAUSE_ARG()). arg=PAUSE_ARG
999 * ensures (arg-ltp_pause) sets GO_* at need and keeps DO_PAUSE/GO_*.
1002 #define GO_UNIDLE 16
1003 #define CHECK_PAUSE 32 /* if ltp_pause: GO_IDLE; wait; GO_UNIDLE */
1004 #define DO_PAUSE 64 /* CHECK_PAUSE; pause the pool */
1005 #define PAUSE_ARG(a) \
1006 ((a) | ((a) & (GO_IDLE|GO_UNIDLE) ? GO_IDLE-1 : CHECK_PAUSE))
1009 handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type )
1011 struct ldap_int_thread_pool_s *pool;
1012 struct ldap_int_thread_poolq_s *pq;
1013 int ret = 0, pause, max_ltp_pause;
1023 if (pause_type == CHECK_PAUSE && !pool->ltp_pause)
1027 ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context();
1031 /* Let pool_unidle() ignore requests for new pauses */
1032 max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED;
1034 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1036 pause = pool->ltp_pause; /* NOT_PAUSED, WANT_PAUSE or PAUSED */
1038 /* If ltp_pause and not GO_IDLE|GO_UNIDLE: Set GO_IDLE,GO_UNIDLE */
1039 pause_type -= pause;
1041 if (pause_type & GO_IDLE) {
1043 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1044 pq->ltp_pending_count++;
1045 pq->ltp_active_count--;
1046 if (pause && pq->ltp_active_count < 1) {
1049 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1051 pool->ltp_active_queues--;
1052 if (pool->ltp_active_queues < 1)
1053 /* Tell the task waiting to DO_PAUSE it can proceed */
1054 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
1058 if (pause_type & GO_UNIDLE) {
1059 /* Wait out pause if any, then cancel GO_IDLE */
1060 if (pause > max_ltp_pause) {
1063 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
1064 } while (pool->ltp_pause > max_ltp_pause);
1066 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1067 pq->ltp_pending_count--;
1068 pq->ltp_active_count++;
1069 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1072 if (pause_type & DO_PAUSE) {
1074 /* Tell everyone else to pause or finish, then await that */
1076 assert(!pool->ltp_pause);
1077 pool->ltp_pause = WANT_PAUSE;
1078 pool->ltp_active_queues = 0;
1080 for (i=0; i<pool->ltp_numqs; i++)
1081 if (pool->ltp_wqs[i] == pq) break;
1083 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1084 /* temporarily remove ourself from active count */
1085 pq->ltp_active_count--;
1089 pq = pool->ltp_wqs[j];
1091 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1093 /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test,
1094 * and do not finish threads in ldap_pvt_thread_pool_wrapper() */
1095 pq->ltp_open_count = -pq->ltp_open_count;
1096 /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
1097 pq->ltp_work_list = &empty_pending_list;
1099 if (pq->ltp_active_count > 0)
1100 pool->ltp_active_queues++;
1102 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1103 if (pool->ltp_numqs > 1) {
1105 j %= pool->ltp_numqs;
1109 /* Wait for this task to become the sole active task */
1110 while (pool->ltp_active_queues > 0)
1111 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
1113 /* restore us to active count */
1114 pool->ltp_wqs[i]->ltp_active_count++;
1116 assert(pool->ltp_pause == WANT_PAUSE);
1117 pool->ltp_pause = PAUSED;
1119 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1124 /* Consider this task idle: It will not block pool_pause() in other tasks. */
1126 ldap_pvt_thread_pool_idle( ldap_pvt_thread_pool_t *tpool )
1128 handle_pause(tpool, PAUSE_ARG(GO_IDLE));
1131 /* Cancel pool_idle(). If the pool is paused, wait it out first. */
1133 ldap_pvt_thread_pool_unidle( ldap_pvt_thread_pool_t *tpool )
1135 handle_pause(tpool, PAUSE_ARG(GO_UNIDLE));
1139 * If a pause was requested, wait for it. If several threads
1140 * are waiting to pause, let through one or more pauses.
1141 * The calling task must be active, not idle.
1142 * Return 1 if we waited, 0 if not, -1 at parameter error.
1145 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t *tpool )
1147 return handle_pause(tpool, PAUSE_ARG(CHECK_PAUSE));
1151 * Pause the pool. The calling task must be active, not idle.
1152 * Return when all other tasks are paused or idle.
1155 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
1157 return handle_pause(tpool, PAUSE_ARG(DO_PAUSE));
1162 ldap_pvt_thread_pool_resume (
1163 ldap_pvt_thread_pool_t *tpool )
1165 struct ldap_int_thread_pool_s *pool;
1166 struct ldap_int_thread_poolq_s *pq;
1177 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1178 assert(pool->ltp_pause == PAUSED);
1179 pool->ltp_pause = 0;
1180 for (i=0; i<pool->ltp_numqs; i++) {
1181 pq = pool->ltp_wqs[i];
1182 if (pq->ltp_open_count <= 0) /* true when paused, but be paranoid */
1183 pq->ltp_open_count = -pq->ltp_open_count;
1184 pq->ltp_work_list = &pq->ltp_pending_list;
1185 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
1187 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
1188 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1193 * Get the key's data and optionally free function in the given context.
1195 int ldap_pvt_thread_pool_getkey(
1199 ldap_pvt_thread_pool_keyfree_t **kfree )
1201 ldap_int_thread_userctx_t *ctx = xctx;
1204 if ( !ctx || !key || !data ) return EINVAL;
1206 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
1207 if ( ctx->ltu_key[i].ltk_key == key ) {
1208 *data = ctx->ltu_key[i].ltk_data;
1209 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
1217 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
1219 for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
1220 ctx->ltu_key[i] = ctx->ltu_key[i+1];
1221 ctx->ltu_key[i].ltk_key = NULL;
1225 * Set or remove data for the key in the given context.
1226 * key can be any unique pointer.
1227 * kfree() is an optional function to free the data (but not the key):
1228 * pool_context_reset() and pool_purgekey() call kfree(key, data),
1229 * but pool_setkey() does not. For pool_setkey() it is the caller's
1230 * responsibility to free any existing data with the same key.
1231 * kfree() must not call functions taking a tpool argument.
1233 int ldap_pvt_thread_pool_setkey(
1237 ldap_pvt_thread_pool_keyfree_t *kfree,
1239 ldap_pvt_thread_pool_keyfree_t **oldkfreep )
1241 ldap_int_thread_userctx_t *ctx = xctx;
1244 if ( !ctx || !key ) return EINVAL;
1246 for ( i=found=0; i<MAXKEYS; i++ ) {
1247 if ( ctx->ltu_key[i].ltk_key == key ) {
1250 } else if ( !ctx->ltu_key[i].ltk_key ) {
1257 *olddatap = ctx->ltu_key[i].ltk_data;
1265 *oldkfreep = ctx->ltu_key[i].ltk_free;
1271 if ( data || kfree ) {
1274 ctx->ltu_key[i].ltk_key = key;
1275 ctx->ltu_key[i].ltk_data = data;
1276 ctx->ltu_key[i].ltk_free = kfree;
1277 } else if ( found ) {
1278 clear_key_idx( ctx, i );
1284 /* Free all elements with this key, no matter which thread they're in.
1285 * May only be called while the pool is paused.
1287 void ldap_pvt_thread_pool_purgekey( void *key )
1290 ldap_int_thread_userctx_t *ctx;
1292 assert ( key != NULL );
1294 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
1295 for ( i=0; i<LDAP_MAXTHR; i++ ) {
1296 ctx = thread_keys[i].ctx;
1297 if ( ctx && ctx != DELETED_THREAD_CTX ) {
1298 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
1299 if ( ctx->ltu_key[j].ltk_key == key ) {
1300 if (ctx->ltu_key[j].ltk_free)
1301 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
1302 ctx->ltu_key[j].ltk_data );
1303 clear_key_idx( ctx, j );
1309 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
1313 * Find the context of the current thread.
1314 * This is necessary if the caller does not have access to the
1315 * thread context handle (for example, a slapd plugin calling
1316 * slapi_search_internal()). No doubt it is more efficient
1317 * for the application to keep track of the thread context
1320 void *ldap_pvt_thread_pool_context( )
1324 ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
1325 return ctx ? ctx : (void *) &ldap_int_main_thrctx;
1329 * Free the context's keys.
1330 * Must not call functions taking a tpool argument (because this
1331 * thread already holds ltp_mutex when called from pool_wrapper()).
1333 void ldap_pvt_thread_pool_context_reset( void *vctx )
1335 ldap_int_thread_userctx_t *ctx = vctx;
1338 for ( i=MAXKEYS-1; i>=0; i--) {
1339 if ( !ctx->ltu_key[i].ltk_key )
1341 if ( ctx->ltu_key[i].ltk_free )
1342 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
1343 ctx->ltu_key[i].ltk_data );
1344 ctx->ltu_key[i].ltk_key = NULL;
1348 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
1350 ldap_int_thread_userctx_t *ctx = vctx;
1354 #endif /* LDAP_THREAD_HAVE_TPOOL */