2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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.
72 ldap_int_thread_userctx_t *ctx;
73 /* ctx is valid when not NULL or DELETED_THREAD_CTX */
74 # define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
75 } thread_keys[LDAP_MAXTHR];
77 #define TID_HASH(tid, hash) do { \
78 unsigned const char *ptr_ = (unsigned const char *)&(tid); \
80 for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
81 (hash) += ((hash) << 5) ^ ptr_[i_]; \
85 /* Task for a thread to perform */
86 typedef struct ldap_int_thread_task_s {
88 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s) q;
89 LDAP_SLIST_ENTRY(ldap_int_thread_task_s) l;
91 ldap_pvt_thread_start_t *ltt_start_routine;
93 struct ldap_int_thread_poolq_s *ltt_queue;
94 } ldap_int_thread_task_t;
96 typedef LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ldap_int_tpool_plist_t;
98 struct ldap_int_thread_poolq_s {
101 struct ldap_int_thread_pool_s *ltp_pool;
103 /* protect members below */
104 ldap_pvt_thread_mutex_t ltp_mutex;
106 /* not paused and something to do for pool_<wrapper/pause/destroy>()
107 * Used for normal pool operation, to synch between submitter and
108 * worker threads. Not used for pauses. In normal operation multiple
109 * queues can rendezvous without acquiring the main pool lock.
111 ldap_pvt_thread_cond_t ltp_cond;
113 /* ltp_pause == 0 ? <p_pending_list : &empty_pending_list,
114 * maintained to reduce work for pool_wrapper()
116 ldap_int_tpool_plist_t *ltp_work_list;
118 /* pending tasks, and unused task objects */
119 ldap_int_tpool_plist_t ltp_pending_list;
120 LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list;
122 /* Max number of threads in this queue */
125 /* Max pending + paused + idle tasks, negated when ltp_finishing */
128 int ltp_pending_count; /* Pending + paused + idle tasks */
129 int ltp_active_count; /* Active, not paused/idle tasks */
130 int ltp_open_count; /* Number of threads */
131 int ltp_starting; /* Currently starting threads */
134 struct ldap_int_thread_pool_s {
135 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
137 struct ldap_int_thread_poolq_s **ltp_wqs;
139 /* number of poolqs */
142 /* protect members below */
143 ldap_pvt_thread_mutex_t ltp_mutex;
145 /* paused and waiting for resume
146 * When a pause is in effect all workers switch to waiting on
147 * this cond instead of their per-queue cond.
149 ldap_pvt_thread_cond_t ltp_cond;
151 /* ltp_active_queues < 1 && ltp_pause */
152 ldap_pvt_thread_cond_t ltp_pcond;
154 /* number of active queues */
155 int ltp_active_queues;
157 /* The pool is finishing, waiting for its threads to close.
158 * They close when ltp_pending_list is done. pool_submit()
159 * rejects new tasks. ltp_max_pending = -(its old value).
163 /* Some active task needs to be the sole active task.
164 * Atomic variable so ldap_pvt_thread_pool_pausing() can read it.
166 volatile sig_atomic_t ltp_pause;
168 /* Max number of threads in pool */
171 /* Configured max number of threads in pool, 0 for default (LDAP_MAXTHR) */
172 int ltp_conf_max_count;
174 /* Max pending + paused + idle tasks, negated when ltp_finishing */
178 static ldap_int_tpool_plist_t empty_pending_list =
179 LDAP_STAILQ_HEAD_INITIALIZER(empty_pending_list);
181 static int ldap_int_has_thread_pool = 0;
182 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
183 ldap_int_thread_pool_list =
184 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
186 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
188 static void *ldap_int_thread_pool_wrapper( void *pool );
190 static ldap_pvt_thread_key_t ldap_tpool_key;
192 /* Context of the main thread */
193 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
196 ldap_int_thread_pool_startup ( void )
198 ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
199 ldap_pvt_thread_key_create( &ldap_tpool_key );
200 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
204 ldap_int_thread_pool_shutdown ( void )
206 struct ldap_int_thread_pool_s *pool;
208 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
209 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
211 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
212 ldap_pvt_thread_key_destroy( ldap_tpool_key );
217 /* Create a thread pool */
219 ldap_pvt_thread_pool_init_q (
220 ldap_pvt_thread_pool_t *tpool,
225 ldap_pvt_thread_pool_t pool;
226 struct ldap_int_thread_poolq_s *pq;
227 int i, rc, rem_thr, rem_pend;
229 /* multiple pools are currently not supported (ITS#4943) */
230 assert(!ldap_int_has_thread_pool);
232 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
234 if (! (1 <= max_pending && max_pending <= MAX_PENDING))
235 max_pending = MAX_PENDING;
238 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
239 sizeof(struct ldap_int_thread_pool_s));
241 if (pool == NULL) return(-1);
243 pool->ltp_wqs = LDAP_MALLOC(numqs * sizeof(struct ldap_int_thread_poolq_s *));
244 if (pool->ltp_wqs == NULL) {
249 for (i=0; i<numqs; i++) {
250 char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1);
253 LDAP_FREE(pool->ltp_wqs[i]->ltp_free);
254 LDAP_FREE(pool->ltp_wqs);
258 pool->ltp_wqs[i] = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1));
259 pool->ltp_wqs[i]->ltp_free = ptr;
262 pool->ltp_numqs = numqs;
263 pool->ltp_conf_max_count = max_threads;
265 max_threads = LDAP_MAXTHR;
267 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
270 for (i=0; i<numqs; i++)
271 LDAP_FREE(pool->ltp_wqs[i]->ltp_free);
272 LDAP_FREE(pool->ltp_wqs);
277 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
281 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
285 rem_thr = max_threads % numqs;
286 rem_pend = max_pending % numqs;
287 for ( i=0; i<numqs; i++ ) {
288 pq = pool->ltp_wqs[i];
290 rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
293 rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
296 LDAP_STAILQ_INIT(&pq->ltp_pending_list);
297 pq->ltp_work_list = &pq->ltp_pending_list;
298 LDAP_SLIST_INIT(&pq->ltp_free_list);
300 pq->ltp_max_count = max_threads / numqs;
305 pq->ltp_max_pending = max_pending / numqs;
307 pq->ltp_max_pending++;
312 ldap_int_has_thread_pool = 1;
314 pool->ltp_max_count = max_threads;
315 pool->ltp_max_pending = max_pending;
317 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
318 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
319 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
321 /* Start no threads just yet. That can break if the process forks
322 * later, as slapd does in order to daemonize. On at least POSIX,
323 * only the forking thread would survive in the child. Yet fork()
324 * can't unlock/clean up other threads' locks and data structures,
325 * unless pthread_atfork() handlers have been set up to do so.
333 ldap_pvt_thread_pool_init (
334 ldap_pvt_thread_pool_t *tpool,
338 return ldap_pvt_thread_pool_init_q( tpool, max_threads, max_pending, 1 );
341 /* Submit a task to be performed by the thread pool */
343 ldap_pvt_thread_pool_submit (
344 ldap_pvt_thread_pool_t *tpool,
345 ldap_pvt_thread_start_t *start_routine, void *arg )
347 return ldap_pvt_thread_pool_submit2( tpool, start_routine, arg, NULL );
350 /* Submit a task to be performed by the thread pool */
352 ldap_pvt_thread_pool_submit2 (
353 ldap_pvt_thread_pool_t *tpool,
354 ldap_pvt_thread_start_t *start_routine, void *arg,
357 struct ldap_int_thread_pool_s *pool;
358 struct ldap_int_thread_poolq_s *pq;
359 ldap_int_thread_task_t *task;
360 ldap_pvt_thread_t thr;
371 if ( pool->ltp_numqs > 1 ) {
372 int min = pool->ltp_wqs[0]->ltp_max_pending + pool->ltp_wqs[0]->ltp_max_count;
374 for ( i = 0; i < pool->ltp_numqs; i++ ) {
375 /* take first queue that has nothing active */
376 if ( !pool->ltp_wqs[i]->ltp_active_count ) {
380 cnt = pool->ltp_wqs[i]->ltp_active_count + pool->ltp_wqs[i]->ltp_pending_count;
392 ldap_pvt_thread_mutex_lock(&pool->ltp_wqs[i]->ltp_mutex);
393 if (pool->ltp_wqs[i]->ltp_pending_count < pool->ltp_wqs[i]->ltp_max_pending) {
396 ldap_pvt_thread_mutex_unlock(&pool->ltp_wqs[i]->ltp_mutex);
398 i %= pool->ltp_numqs;
403 pq = pool->ltp_wqs[i];
404 task = LDAP_SLIST_FIRST(&pq->ltp_free_list);
406 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
408 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
413 task->ltt_start_routine = start_routine;
415 task->ltt_queue = pq;
419 pq->ltp_pending_count++;
420 LDAP_STAILQ_INSERT_TAIL(&pq->ltp_pending_list, task, ltt_next.q);
425 /* should we open (create) a thread? */
426 if (pq->ltp_open_count < pq->ltp_active_count+pq->ltp_pending_count &&
427 pq->ltp_open_count < pq->ltp_max_count)
430 pq->ltp_open_count++;
432 if (0 != ldap_pvt_thread_create(
433 &thr, 1, ldap_int_thread_pool_wrapper, pq))
435 /* couldn't create thread. back out of
436 * ltp_open_count and check for even worse things.
439 pq->ltp_open_count--;
441 if (pq->ltp_open_count == 0) {
442 /* no open threads at all?!?
444 ldap_int_thread_task_t *ptr;
446 /* let pool_destroy know there are no more threads */
447 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
449 LDAP_STAILQ_FOREACH(ptr, &pq->ltp_pending_list, ltt_next.q)
450 if (ptr == task) break;
452 /* no open threads, task not handled, so
453 * back out of ltp_pending_count, free the task,
456 pq->ltp_pending_count--;
457 LDAP_STAILQ_REMOVE(&pq->ltp_pending_list, task,
458 ldap_int_thread_task_s, ltt_next.q);
459 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task,
464 /* there is another open thread, so this
465 * task will be handled eventually.
469 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
472 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
476 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
481 no_task( void *ctx, void *arg )
486 /* Cancel a pending task that was previously submitted.
487 * Return 1 if the task was successfully cancelled, 0 if
488 * not found, -1 for invalid parameters
491 ldap_pvt_thread_pool_retract (
494 ldap_int_thread_task_t *task, *ttmp;
495 struct ldap_int_thread_poolq_s *pq;
501 pq = ttmp->ltt_queue;
505 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
506 LDAP_STAILQ_FOREACH(task, &pq->ltp_pending_list, ltt_next.q)
508 /* Could LDAP_STAILQ_REMOVE the task, but that
509 * walks ltp_pending_list again to find it.
511 task->ltt_start_routine = no_task;
512 task->ltt_arg = NULL;
515 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
519 /* Set number of work queues in this pool. Should not be
520 * more than the number of CPUs. */
522 ldap_pvt_thread_pool_queues(
523 ldap_pvt_thread_pool_t *tpool,
526 struct ldap_int_thread_pool_s *pool;
527 struct ldap_int_thread_poolq_s *pq;
528 int i, rc, rem_thr, rem_pend;
530 if (numqs < 1 || tpool == NULL)
538 if (numqs < pool->ltp_numqs) {
539 for (i=numqs; i<pool->ltp_numqs; i++)
540 pool->ltp_wqs[i]->ltp_max_count = 0;
541 } else if (numqs > pool->ltp_numqs) {
542 struct ldap_int_thread_poolq_s **wqs;
543 wqs = LDAP_REALLOC(pool->ltp_wqs, numqs * sizeof(struct ldap_int_thread_poolq_s *));
547 for (i=pool->ltp_numqs; i<numqs; i++) {
548 char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1);
551 pool->ltp_wqs[i] = NULL;
554 pq = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1));
556 pool->ltp_wqs[i] = pq;
558 rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
561 rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
564 LDAP_STAILQ_INIT(&pq->ltp_pending_list);
565 pq->ltp_work_list = &pq->ltp_pending_list;
566 LDAP_SLIST_INIT(&pq->ltp_free_list);
569 rem_thr = pool->ltp_max_count % numqs;
570 rem_pend = pool->ltp_max_pending % numqs;
571 for ( i=0; i<numqs; i++ ) {
572 pq = pool->ltp_wqs[i];
573 pq->ltp_max_count = pool->ltp_max_count / numqs;
578 pq->ltp_max_pending = pool->ltp_max_pending / numqs;
580 pq->ltp_max_pending++;
584 pool->ltp_numqs = numqs;
588 /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */
590 ldap_pvt_thread_pool_maxthreads(
591 ldap_pvt_thread_pool_t *tpool,
594 struct ldap_int_thread_pool_s *pool;
595 struct ldap_int_thread_poolq_s *pq;
598 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
609 pool->ltp_conf_max_count = max_threads;
611 max_threads = LDAP_MAXTHR;
612 pool->ltp_max_count = max_threads;
614 remthr = max_threads % pool->ltp_numqs;
615 max_threads /= pool->ltp_numqs;
617 for (i=0; i<pool->ltp_numqs; i++) {
618 pq = pool->ltp_wqs[i];
619 pq->ltp_max_count = max_threads;
628 /* Inspect the pool */
630 ldap_pvt_thread_pool_query(
631 ldap_pvt_thread_pool_t *tpool,
632 ldap_pvt_thread_pool_param_t param,
635 struct ldap_int_thread_pool_s *pool;
638 if ( tpool == NULL || value == NULL ) {
644 if ( pool == NULL ) {
649 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
650 count = pool->ltp_conf_max_count;
653 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
654 count = pool->ltp_max_pending;
657 if (count == MAX_PENDING)
661 case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
662 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
663 count = (pool->ltp_pause != 0);
664 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
667 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
668 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
669 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
670 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
671 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
675 for (i=0; i<pool->ltp_numqs; i++) {
676 struct ldap_int_thread_poolq_s *pq = pool->ltp_wqs[i];
677 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
679 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
680 count += pq->ltp_open_count;
682 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
683 count += pq->ltp_starting;
685 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
686 count += pq->ltp_active_count;
688 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
689 count += pq->ltp_pending_count;
691 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
692 count += pq->ltp_pending_count + pq->ltp_active_count;
695 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
702 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
705 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
708 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
711 case LDAP_PVT_THREAD_POOL_PARAM_STATE:
713 *((char **)value) = "pausing";
714 else if (!pool->ltp_finishing)
715 *((char **)value) = "running";
718 for (i=0; i<pool->ltp_numqs; i++)
719 if (pool->ltp_wqs[i]->ltp_pending_count) break;
720 if (i<pool->ltp_numqs)
721 *((char **)value) = "finishing";
723 *((char **)value) = "stopping";
727 case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
732 *((int *)value) = count;
735 return ( count == -1 ? -1 : 0 );
739 * true if pool is pausing; does not lock any mutex to check.
740 * 0 if not pause, 1 if pause, -1 if error or no pool.
743 ldap_pvt_thread_pool_pausing( ldap_pvt_thread_pool_t *tpool )
746 struct ldap_int_thread_pool_s *pool;
748 if ( tpool != NULL && (pool = *tpool) != NULL ) {
749 rc = (pool->ltp_pause != 0);
756 * wrapper for ldap_pvt_thread_pool_query(), left around
757 * for backwards compatibility
760 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
764 rc = ldap_pvt_thread_pool_query( tpool,
765 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
774 /* Destroy the pool after making its threads finish */
776 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
778 struct ldap_int_thread_pool_s *pool, *pptr;
779 struct ldap_int_thread_poolq_s *pq;
780 ldap_int_thread_task_t *task;
788 if (pool == NULL) return(-1);
790 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
791 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
792 if (pptr == pool) break;
794 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
795 ldap_int_thread_pool_s, ltp_next);
796 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
798 if (pool != pptr) return(-1);
800 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
802 pool->ltp_finishing = 1;
803 if (pool->ltp_max_pending > 0)
804 pool->ltp_max_pending = -pool->ltp_max_pending;
806 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
807 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
809 for (i=0; i<pool->ltp_numqs; i++) {
810 pq = pool->ltp_wqs[i];
811 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
812 if (pq->ltp_max_pending > 0)
813 pq->ltp_max_pending = -pq->ltp_max_pending;
815 while ((task = LDAP_STAILQ_FIRST(&pq->ltp_pending_list)) != NULL) {
816 LDAP_STAILQ_REMOVE_HEAD(&pq->ltp_pending_list, ltt_next.q);
819 pq->ltp_pending_count = 0;
822 while (pq->ltp_open_count) {
823 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
824 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
827 while ((task = LDAP_SLIST_FIRST(&pq->ltp_free_list)) != NULL)
829 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
832 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
833 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
834 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
837 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
838 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
839 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
840 for (i=0; i<pool->ltp_numqs; i++) {
841 pq = pool->ltp_wqs[i];
843 LDAP_FREE(pq->ltp_free);
846 LDAP_FREE(pool->ltp_wqs);
849 ldap_int_has_thread_pool = 0;
853 /* Thread loop. Accept and handle submitted tasks. */
855 ldap_int_thread_pool_wrapper (
858 struct ldap_int_thread_poolq_s *pq = xpool;
859 struct ldap_int_thread_pool_s *pool = pq->ltp_pool;
860 ldap_int_thread_task_t *task;
861 ldap_int_tpool_plist_t *work_list;
862 ldap_int_thread_userctx_t ctx, *kctx;
863 unsigned i, keyslot, hash;
864 int pool_lock = 0, freeme = 0;
866 assert(pool != NULL);
868 for ( i=0; i<MAXKEYS; i++ ) {
869 ctx.ltu_key[i].ltk_key = NULL;
873 ctx.ltu_id = ldap_pvt_thread_self();
874 TID_HASH(ctx.ltu_id, hash);
876 ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
878 if (pool->ltp_pause) {
879 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
880 /* thread_keys[] is read-only when paused */
881 while (pool->ltp_pause)
882 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
883 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
886 /* find a key slot to give this thread ID and store a
887 * pointer to our keys there; start at the thread ID
888 * itself (mod LDAP_MAXTHR) and look for an empty slot.
890 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
891 for (keyslot = hash & (LDAP_MAXTHR-1);
892 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
893 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
894 thread_keys[keyslot].ctx = &ctx;
895 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
897 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
899 pq->ltp_active_count++;
902 work_list = pq->ltp_work_list; /* help the compiler a bit */
903 task = LDAP_STAILQ_FIRST(work_list);
904 if (task == NULL) { /* paused or no pending tasks */
905 if (--(pq->ltp_active_count) < 1) {
906 if (pool->ltp_pause) {
907 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
908 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
910 if (--(pool->ltp_active_queues) < 1) {
911 /* Notify pool_pause it is the sole active thread. */
912 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
918 if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) {
919 /* Not paused, and either finishing or too many
920 * threads running (can happen if ltp_max_count
921 * was reduced). Let this thread die.
926 /* We could check an idle timer here, and let the
927 * thread die if it has been inactive for a while.
928 * Only die if there are other open threads (i.e.,
929 * always have at least one thread open).
930 * The check should be like this:
931 * if (pool->ltp_open_count>1 && pool->ltp_starting==0)
932 * check timer, wait if ltp_pause, leave thread;
934 * Just use pthread_cond_timedwait() if we want to
938 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
939 if (!pool->ltp_pause) {
940 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
941 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
945 ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
947 work_list = pq->ltp_work_list;
948 task = LDAP_STAILQ_FIRST(work_list);
949 } while (task == NULL);
952 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
953 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
956 pq->ltp_active_count++;
959 LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q);
960 pq->ltp_pending_count--;
961 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
963 task->ltt_start_routine(&ctx, task->ltt_arg);
965 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
966 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l);
970 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
972 /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey()
973 * during this call, since it prevents new pauses. */
974 ldap_pvt_thread_pool_context_reset(&ctx);
976 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
977 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
979 pq->ltp_open_count--;
980 if (pq->ltp_open_count == 0) {
981 if (pool->ltp_finishing)
982 /* let pool_destroy know we're all done */
983 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
989 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
991 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
994 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
995 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
996 LDAP_FREE(pq->ltp_free);
999 ldap_pvt_thread_exit(NULL);
1003 /* Arguments > ltp_pause to handle_pause(,PAUSE_ARG()). arg=PAUSE_ARG
1004 * ensures (arg-ltp_pause) sets GO_* at need and keeps DO_PAUSE/GO_*.
1007 #define GO_UNIDLE 16
1008 #define CHECK_PAUSE 32 /* if ltp_pause: GO_IDLE; wait; GO_UNIDLE */
1009 #define DO_PAUSE 64 /* CHECK_PAUSE; pause the pool */
1010 #define PAUSE_ARG(a) \
1011 ((a) | ((a) & (GO_IDLE|GO_UNIDLE) ? GO_IDLE-1 : CHECK_PAUSE))
1014 handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type )
1016 struct ldap_int_thread_pool_s *pool;
1017 struct ldap_int_thread_poolq_s *pq;
1018 int ret = 0, pause, max_ltp_pause;
1028 if (pause_type == CHECK_PAUSE && !pool->ltp_pause)
1032 ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context();
1038 /* Let pool_unidle() ignore requests for new pauses */
1039 max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED;
1041 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1043 pause = pool->ltp_pause; /* NOT_PAUSED, WANT_PAUSE or PAUSED */
1045 /* If ltp_pause and not GO_IDLE|GO_UNIDLE: Set GO_IDLE,GO_UNIDLE */
1046 pause_type -= pause;
1048 if (pause_type & GO_IDLE) {
1050 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1051 pq->ltp_pending_count++;
1052 pq->ltp_active_count--;
1053 if (pause && pq->ltp_active_count < 1) {
1056 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1058 pool->ltp_active_queues--;
1059 if (pool->ltp_active_queues < 1)
1060 /* Tell the task waiting to DO_PAUSE it can proceed */
1061 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
1065 if (pause_type & GO_UNIDLE) {
1066 /* Wait out pause if any, then cancel GO_IDLE */
1067 if (pause > max_ltp_pause) {
1070 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
1071 } while (pool->ltp_pause > max_ltp_pause);
1073 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1074 pq->ltp_pending_count--;
1075 pq->ltp_active_count++;
1076 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1079 if (pause_type & DO_PAUSE) {
1081 /* Tell everyone else to pause or finish, then await that */
1083 assert(!pool->ltp_pause);
1084 pool->ltp_pause = WANT_PAUSE;
1085 pool->ltp_active_queues = 0;
1087 for (i=0; i<pool->ltp_numqs; i++)
1088 if (pool->ltp_wqs[i] == pq) break;
1090 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1091 /* temporarily remove ourself from active count */
1092 pq->ltp_active_count--;
1096 pq = pool->ltp_wqs[j];
1098 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1100 /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
1101 pq->ltp_work_list = &empty_pending_list;
1103 if (pq->ltp_active_count > 0)
1104 pool->ltp_active_queues++;
1106 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1107 if (pool->ltp_numqs > 1) {
1109 j %= pool->ltp_numqs;
1113 /* Wait for this task to become the sole active task */
1114 while (pool->ltp_active_queues > 0)
1115 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
1117 /* restore us to active count */
1118 pool->ltp_wqs[i]->ltp_active_count++;
1120 assert(pool->ltp_pause == WANT_PAUSE);
1121 pool->ltp_pause = PAUSED;
1123 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1128 /* Consider this task idle: It will not block pool_pause() in other tasks. */
1130 ldap_pvt_thread_pool_idle( ldap_pvt_thread_pool_t *tpool )
1132 handle_pause(tpool, PAUSE_ARG(GO_IDLE));
1135 /* Cancel pool_idle(). If the pool is paused, wait it out first. */
1137 ldap_pvt_thread_pool_unidle( ldap_pvt_thread_pool_t *tpool )
1139 handle_pause(tpool, PAUSE_ARG(GO_UNIDLE));
1143 * If a pause was requested, wait for it. If several threads
1144 * are waiting to pause, let through one or more pauses.
1145 * The calling task must be active, not idle.
1146 * Return 1 if we waited, 0 if not, -1 at parameter error.
1149 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t *tpool )
1151 return handle_pause(tpool, PAUSE_ARG(CHECK_PAUSE));
1155 * Pause the pool. The calling task must be active, not idle.
1156 * Return when all other tasks are paused or idle.
1159 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
1161 return handle_pause(tpool, PAUSE_ARG(DO_PAUSE));
1166 ldap_pvt_thread_pool_resume (
1167 ldap_pvt_thread_pool_t *tpool )
1169 struct ldap_int_thread_pool_s *pool;
1170 struct ldap_int_thread_poolq_s *pq;
1181 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1182 assert(pool->ltp_pause == PAUSED);
1183 pool->ltp_pause = 0;
1184 for (i=0; i<pool->ltp_numqs; i++) {
1185 pq = pool->ltp_wqs[i];
1186 pq->ltp_work_list = &pq->ltp_pending_list;
1187 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
1189 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
1190 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1195 * Get the key's data and optionally free function in the given context.
1197 int ldap_pvt_thread_pool_getkey(
1201 ldap_pvt_thread_pool_keyfree_t **kfree )
1203 ldap_int_thread_userctx_t *ctx = xctx;
1206 if ( !ctx || !key || !data ) return EINVAL;
1208 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
1209 if ( ctx->ltu_key[i].ltk_key == key ) {
1210 *data = ctx->ltu_key[i].ltk_data;
1211 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
1219 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
1221 for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
1222 ctx->ltu_key[i] = ctx->ltu_key[i+1];
1223 ctx->ltu_key[i].ltk_key = NULL;
1227 * Set or remove data for the key in the given context.
1228 * key can be any unique pointer.
1229 * kfree() is an optional function to free the data (but not the key):
1230 * pool_context_reset() and pool_purgekey() call kfree(key, data),
1231 * but pool_setkey() does not. For pool_setkey() it is the caller's
1232 * responsibility to free any existing data with the same key.
1233 * kfree() must not call functions taking a tpool argument.
1235 int ldap_pvt_thread_pool_setkey(
1239 ldap_pvt_thread_pool_keyfree_t *kfree,
1241 ldap_pvt_thread_pool_keyfree_t **oldkfreep )
1243 ldap_int_thread_userctx_t *ctx = xctx;
1246 if ( !ctx || !key ) return EINVAL;
1248 for ( i=found=0; i<MAXKEYS; i++ ) {
1249 if ( ctx->ltu_key[i].ltk_key == key ) {
1252 } else if ( !ctx->ltu_key[i].ltk_key ) {
1259 *olddatap = ctx->ltu_key[i].ltk_data;
1267 *oldkfreep = ctx->ltu_key[i].ltk_free;
1273 if ( data || kfree ) {
1276 ctx->ltu_key[i].ltk_key = key;
1277 ctx->ltu_key[i].ltk_data = data;
1278 ctx->ltu_key[i].ltk_free = kfree;
1279 } else if ( found ) {
1280 clear_key_idx( ctx, i );
1286 /* Free all elements with this key, no matter which thread they're in.
1287 * May only be called while the pool is paused.
1289 void ldap_pvt_thread_pool_purgekey( void *key )
1292 ldap_int_thread_userctx_t *ctx;
1294 assert ( key != NULL );
1296 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
1297 for ( i=0; i<LDAP_MAXTHR; i++ ) {
1298 ctx = thread_keys[i].ctx;
1299 if ( ctx && ctx != DELETED_THREAD_CTX ) {
1300 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
1301 if ( ctx->ltu_key[j].ltk_key == key ) {
1302 if (ctx->ltu_key[j].ltk_free)
1303 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
1304 ctx->ltu_key[j].ltk_data );
1305 clear_key_idx( ctx, j );
1311 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
1315 * Find the context of the current thread.
1316 * This is necessary if the caller does not have access to the
1317 * thread context handle (for example, a slapd plugin calling
1318 * slapi_search_internal()). No doubt it is more efficient
1319 * for the application to keep track of the thread context
1322 void *ldap_pvt_thread_pool_context( )
1326 ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
1327 return ctx ? ctx : (void *) &ldap_int_main_thrctx;
1331 * Free the context's keys.
1332 * Must not call functions taking a tpool argument (because this
1333 * thread already holds ltp_mutex when called from pool_wrapper()).
1335 void ldap_pvt_thread_pool_context_reset( void *vctx )
1337 ldap_int_thread_userctx_t *ctx = vctx;
1340 for ( i=MAXKEYS-1; i>=0; i--) {
1341 if ( !ctx->ltu_key[i].ltk_key )
1343 if ( ctx->ltu_key[i].ltk_free )
1344 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
1345 ctx->ltu_key[i].ltk_data );
1346 ctx->ltu_key[i].ltk_key = NULL;
1350 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
1352 ldap_int_thread_userctx_t *ctx = vctx;
1356 #endif /* LDAP_THREAD_HAVE_TPOOL */