2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/signal.h>
21 #include <ac/stdarg.h>
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
28 #include "ldap_pvt_thread.h" /* Get the thread interface */
29 #include "ldap_queue.h"
30 #define LDAP_THREAD_POOL_IMPLEMENTATION
31 #include "ldap_thr_debug.h" /* May rename symbols defined below */
33 #ifndef LDAP_THREAD_HAVE_TPOOL
35 /* Thread-specific key with data and optional free function */
36 typedef struct ldap_int_tpool_key_s {
39 ldap_pvt_thread_pool_keyfree_t *ltk_free;
40 } ldap_int_tpool_key_t;
42 /* Max number of thread-specific keys we store per thread.
43 * We don't expect to use many...
47 /* Max number of threads */
48 #define LDAP_MAXTHR 1024 /* must be a power of 2 */
50 /* (Theoretical) max number of pending requests */
51 #define MAX_PENDING (INT_MAX/2) /* INT_MAX - (room to avoid overflow) */
53 /* Context: thread ID and thread-specific key/data pairs */
54 typedef struct ldap_int_thread_userctx_s {
55 ldap_pvt_thread_t ltu_id;
56 ldap_int_tpool_key_t ltu_key[MAXKEYS];
57 } ldap_int_thread_userctx_t;
60 /* Simple {thread ID -> context} hash table; key=ctx->ltu_id.
61 * Protected by ldap_pvt_thread_pool_mutex except during pauses,
62 * when it is read-only (used by pool_purgekey and pool_context).
63 * Protected by tpool->ltp_mutex during pauses.
66 ldap_int_thread_userctx_t *ctx;
67 /* ctx is valid when not NULL or DELETED_THREAD_CTX */
68 # define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
69 } thread_keys[LDAP_MAXTHR];
71 #define TID_HASH(tid, hash) do { \
72 unsigned const char *ptr_ = (unsigned const char *)&(tid); \
74 for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
75 (hash) += ((hash) << 5) ^ ptr_[i_]; \
79 /* Task for a thread to perform */
80 typedef struct ldap_int_thread_task_s {
82 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s) q;
83 LDAP_SLIST_ENTRY(ldap_int_thread_task_s) l;
85 ldap_pvt_thread_start_t *ltt_start_routine;
87 } ldap_int_thread_task_t;
89 typedef LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ldap_int_tpool_plist_t;
91 struct ldap_int_thread_pool_s {
92 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
94 /* protect members below, and protect thread_keys[] during pauses */
95 ldap_pvt_thread_mutex_t ltp_mutex;
97 /* not paused and something to do for pool_<wrapper/pause/destroy>() */
98 ldap_pvt_thread_cond_t ltp_cond;
100 /* ltp_active_count <= 1 && ltp_pause */
101 ldap_pvt_thread_cond_t ltp_pcond;
103 /* pending tasks, and unused task objects */
104 ldap_int_tpool_plist_t ltp_pending_list;
105 LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list;
107 /* The pool is finishing, waiting for its threads to close.
108 * They close when ltp_pending_list is done. pool_submit()
109 * rejects new tasks. ltp_max_pending = -(its old value).
113 /* Some active task needs to be the sole active task.
114 * Atomic variable so ldap_pvt_thread_pool_pausing() can read it.
116 volatile sig_atomic_t ltp_pause;
118 /* Max number of threads in pool, or 0 for default (LDAP_MAXTHR) */
121 /* Max number of pending + paused requests, negated when ltp_finishing */
124 int ltp_pending_count; /* Pending or paused requests */
125 int ltp_active_count; /* Active, not paused requests */
126 int ltp_open_count; /* Number of threads */
127 int ltp_starting; /* Currenlty starting threads */
130 * State maintained to reduce the time ltp_mutex must be locked
131 * in ldap_pvt_thread_pool_<submit/wrapper>().
134 /* ltp_pause == 0 ? <p_pending_list : &empty_pending_list */
135 ldap_int_tpool_plist_t *ltp_work_list;
137 /* >0 if paused or we may open a thread, <0 if we should close a thread. */
138 /* Updated when ltp_<finishing/pause/max_count/open_count> change. */
139 int ltp_vary_open_count;
140 # define SET_VARY_OPEN_COUNT(pool) \
141 ((pool)->ltp_vary_open_count = \
142 (pool)->ltp_pause ? 1 : \
143 (pool)->ltp_finishing ? -1 : \
144 ((pool)->ltp_max_count ? (pool)->ltp_max_count : LDAP_MAXTHR) \
145 - (pool)->ltp_open_count)
148 static ldap_int_tpool_plist_t empty_pending_list =
149 LDAP_STAILQ_HEAD_INITIALIZER(empty_pending_list);
151 static int ldap_int_has_thread_pool = 0;
152 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
153 ldap_int_thread_pool_list =
154 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
156 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
158 static void *ldap_int_thread_pool_wrapper( void *pool );
160 static ldap_pvt_thread_key_t ldap_tpool_key;
162 /* Context of the main thread */
163 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
166 ldap_int_thread_pool_startup ( void )
168 ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
169 ldap_pvt_thread_key_create( &ldap_tpool_key );
170 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
174 ldap_int_thread_pool_shutdown ( void )
176 struct ldap_int_thread_pool_s *pool;
178 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
179 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
181 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
182 ldap_pvt_thread_key_destroy( ldap_tpool_key );
187 /* Create a thread pool */
189 ldap_pvt_thread_pool_init (
190 ldap_pvt_thread_pool_t *tpool,
194 ldap_pvt_thread_pool_t pool;
197 /* multiple pools are currently not supported (ITS#4943) */
198 assert(!ldap_int_has_thread_pool);
200 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
202 if (! (1 <= max_pending && max_pending <= MAX_PENDING))
203 max_pending = MAX_PENDING;
206 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
207 sizeof(struct ldap_int_thread_pool_s));
209 if (pool == NULL) return(-1);
211 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
214 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
217 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
221 ldap_int_has_thread_pool = 1;
223 pool->ltp_max_count = max_threads;
224 SET_VARY_OPEN_COUNT(pool);
225 pool->ltp_max_pending = max_pending;
227 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
228 pool->ltp_work_list = &pool->ltp_pending_list;
229 LDAP_SLIST_INIT(&pool->ltp_free_list);
231 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
232 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
233 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
236 /* THIS WILL NOT WORK on some systems. If the process
237 * forks after starting a thread, there is no guarantee
238 * that the thread will survive the fork. For example,
239 * slapd forks in order to daemonize, and does so after
240 * calling ldap_pvt_thread_pool_init. On some systems,
241 * this initial thread does not run in the child process,
242 * but ltp_open_count == 1, so two things happen:
243 * 1) the first client connection fails, and 2) when
244 * slapd is kill'ed, it never terminates since it waits
245 * for all worker threads to exit. */
247 /* start up one thread, just so there is one. no need to
248 * lock the mutex right now, since no threads are running.
250 pool->ltp_open_count++;
251 SET_VARY_OPEN_COUNT(pool);
253 ldap_pvt_thread_t thr;
254 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
257 /* couldn't start one? then don't start any */
258 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
259 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
260 ldap_int_thread_pool_s, ltp_next);
261 ldap_int_has_thread_pool = 0;
262 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
263 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
264 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
265 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
276 /* Submit a task to be performed by the thread pool */
278 ldap_pvt_thread_pool_submit (
279 ldap_pvt_thread_pool_t *tpool,
280 ldap_pvt_thread_start_t *start_routine, void *arg )
282 struct ldap_int_thread_pool_s *pool;
283 ldap_int_thread_task_t *task;
284 ldap_pvt_thread_t thr;
294 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
296 if (pool->ltp_pending_count >= pool->ltp_max_pending)
299 task = LDAP_SLIST_FIRST(&pool->ltp_free_list);
301 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l);
303 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
308 task->ltt_start_routine = start_routine;
311 pool->ltp_pending_count++;
312 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, task, ltt_next.q);
313 if (pool->ltp_pause) {
314 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
317 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
318 if (pool->ltp_vary_open_count > 0 &&
319 pool->ltp_open_count < pool->ltp_active_count+pool->ltp_pending_count)
321 pool->ltp_starting++;
322 pool->ltp_open_count++;
323 SET_VARY_OPEN_COUNT(pool);
325 if (0 != ldap_pvt_thread_create(
326 &thr, 1, ldap_int_thread_pool_wrapper, pool))
328 /* couldn't create thread. back out of
329 * ltp_open_count and check for even worse things.
331 pool->ltp_starting--;
332 pool->ltp_open_count--;
333 SET_VARY_OPEN_COUNT(pool);
335 if (pool->ltp_open_count == 0) {
336 /* no open threads at all?!?
338 ldap_int_thread_task_t *ptr;
340 /* let pool_destroy know there are no more threads */
341 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
343 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltt_next.q)
344 if (ptr == task) break;
346 /* no open threads, task not handled, so
347 * back out of ltp_pending_count, free the task,
350 pool->ltp_pending_count--;
351 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, task,
352 ldap_int_thread_task_s, ltt_next.q);
353 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task,
358 /* there is another open thread, so this
359 * task will be handled eventually.
360 * continue on, we have signalled that
361 * the task is waiting.
366 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
370 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
374 /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */
376 ldap_pvt_thread_pool_maxthreads(
377 ldap_pvt_thread_pool_t *tpool,
380 struct ldap_int_thread_pool_s *pool;
382 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
393 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
395 pool->ltp_max_count = max_threads;
396 SET_VARY_OPEN_COUNT(pool);
398 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
402 /* Inspect the pool */
404 ldap_pvt_thread_pool_query(
405 ldap_pvt_thread_pool_t *tpool,
406 ldap_pvt_thread_pool_param_t param,
409 struct ldap_int_thread_pool_s *pool;
412 if ( tpool == NULL || value == NULL ) {
418 if ( pool == NULL ) {
422 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
424 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
425 count = pool->ltp_max_count;
428 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
429 count = pool->ltp_max_pending;
432 if (count == MAX_PENDING)
436 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
437 count = pool->ltp_open_count;
440 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
441 count = pool->ltp_starting;
444 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
445 count = pool->ltp_active_count;
448 case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
449 count = pool->ltp_pause;
452 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
453 count = pool->ltp_pending_count;
456 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
457 count = pool->ltp_pending_count + pool->ltp_active_count;
460 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
463 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
466 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
469 case LDAP_PVT_THREAD_POOL_PARAM_STATE:
471 pool->ltp_pause ? "pausing" :
472 !pool->ltp_finishing ? "running" :
473 pool->ltp_pending_count ? "finishing" : "stopping";
476 case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
479 ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex );
482 *((int *)value) = count;
485 return ( count == -1 ? -1 : 0 );
489 * true if pool is pausing; does not lock any mutex to check.
490 * 0 if not pause, 1 if pause, -1 if error or no pool.
493 ldap_pvt_thread_pool_pausing( ldap_pvt_thread_pool_t *tpool )
496 struct ldap_int_thread_pool_s *pool;
498 if ( tpool != NULL && (pool = *tpool) != NULL ) {
499 rc = pool->ltp_pause;
506 * wrapper for ldap_pvt_thread_pool_query(), left around
507 * for backwards compatibility
510 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
514 rc = ldap_pvt_thread_pool_query( tpool,
515 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
525 flush_pending_list( struct ldap_int_thread_pool_s *pool )
527 ldap_int_thread_task_t *task;
529 while ((task = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL) {
530 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q);
535 /* Destroy the pool after making its threads finish */
537 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
539 struct ldap_int_thread_pool_s *pool, *pptr;
540 ldap_int_thread_task_t *task;
547 if (pool == NULL) return(-1);
549 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
550 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
551 if (pptr == pool) break;
553 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
554 ldap_int_thread_pool_s, ltp_next);
555 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
557 if (pool != pptr) return(-1);
559 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
561 pool->ltp_finishing = 1;
562 SET_VARY_OPEN_COUNT(pool);
563 if (pool->ltp_max_pending > 0)
564 pool->ltp_max_pending = -pool->ltp_max_pending;
566 flush_pending_list(pool);
568 while (pool->ltp_open_count) {
569 if (!pool->ltp_pause)
570 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
571 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
573 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
575 flush_pending_list(pool);
577 while ((task = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
579 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l);
583 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
584 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
585 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
588 ldap_int_has_thread_pool = 0;
592 /* Thread loop. Accept and handle submitted tasks. */
594 ldap_int_thread_pool_wrapper (
597 struct ldap_int_thread_pool_s *pool = xpool;
598 ldap_int_thread_task_t *task;
599 ldap_int_thread_userctx_t ctx, *kctx;
600 unsigned i, keyslot, hash;
602 assert(pool != NULL);
604 for ( i=0; i<MAXKEYS; i++ ) {
605 ctx.ltu_key[i].ltk_key = NULL;
608 ctx.ltu_id = ldap_pvt_thread_self();
609 TID_HASH(ctx.ltu_id, hash);
611 ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
613 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
615 /* thread_keys[] is read-only when paused */
616 while (pool->ltp_pause)
617 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
619 /* find a key slot to give this thread ID and store a
620 * pointer to our keys there; start at the thread ID
621 * itself (mod LDAP_MAXTHR) and look for an empty slot.
623 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
624 for (keyslot = hash & (LDAP_MAXTHR-1);
625 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
626 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
627 thread_keys[keyslot].ctx = &ctx;
628 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
630 pool->ltp_starting--;
633 task = LDAP_STAILQ_FIRST(pool->ltp_work_list);
634 if (task == NULL) { /* paused or no pending tasks */
635 if (pool->ltp_vary_open_count < 0) {
636 /* not paused, and either finishing or too many
637 * threads running (can happen if ltp_max_count
638 * was reduced) so let this thread die.
643 /* we could check an idle timer here, and let the
644 * thread die if it has been inactive for a while.
645 * only die if there are other open threads (i.e.,
646 * always have at least one thread open). the check
647 * should be like this:
648 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
649 * check timer, wait if ltp_pause, leave thread (break;)
651 * Just use pthread_cond_timedwait if we want to
655 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
659 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q);
660 pool->ltp_pending_count--;
661 pool->ltp_active_count++;
662 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
664 task->ltt_start_routine(&ctx, task->ltt_arg);
666 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
667 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task, ltt_next.l);
668 pool->ltp_active_count--;
669 /* let pool_pause know when it is the sole active thread */
670 if (pool->ltp_active_count < 2)
671 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
674 /* The ltp_mutex lock protects ctx->ltu_key from pool_purgekey()
675 * during this call, since it prevents new pauses. */
676 ldap_pvt_thread_pool_context_reset(&ctx);
678 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
679 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
680 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
682 pool->ltp_open_count--;
683 SET_VARY_OPEN_COUNT(pool);
684 /* let pool_destroy know we're all done */
685 if (pool->ltp_open_count < 1)
686 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
688 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
690 ldap_pvt_thread_exit(NULL);
694 /* Pause the pool. Return when all other threads are paused. */
696 ldap_pvt_thread_pool_pause (
697 ldap_pvt_thread_pool_t *tpool )
699 struct ldap_int_thread_pool_s *pool;
709 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
711 /* If someone else has already requested a pause, we have to wait */
712 if (pool->ltp_pause) {
713 pool->ltp_pending_count++;
714 pool->ltp_active_count--;
715 /* let the other pool_pause() know when it can proceed */
716 if (pool->ltp_active_count < 2)
717 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
719 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
720 } while (pool->ltp_pause);
721 pool->ltp_pending_count--;
722 pool->ltp_active_count++;
725 /* Wait for everyone else to pause or finish */
727 SET_VARY_OPEN_COUNT(pool);
728 /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
729 pool->ltp_work_list = &empty_pending_list;
731 while (pool->ltp_active_count > 1) {
732 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
735 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
741 ldap_pvt_thread_pool_resume (
742 ldap_pvt_thread_pool_t *tpool )
744 struct ldap_int_thread_pool_s *pool;
754 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
757 SET_VARY_OPEN_COUNT(pool);
758 pool->ltp_work_list = &pool->ltp_pending_list;
760 if (!pool->ltp_finishing)
761 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
763 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
768 * Get the key's data and optionally free function in the given context.
770 int ldap_pvt_thread_pool_getkey(
774 ldap_pvt_thread_pool_keyfree_t **kfree )
776 ldap_int_thread_userctx_t *ctx = xctx;
779 if ( !ctx || !key || !data ) return EINVAL;
781 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
782 if ( ctx->ltu_key[i].ltk_key == key ) {
783 *data = ctx->ltu_key[i].ltk_data;
784 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
792 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
794 for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
795 ctx->ltu_key[i] = ctx->ltu_key[i+1];
796 ctx->ltu_key[i].ltk_key = NULL;
800 * Set or remove data for the key in the given context.
801 * key can be any unique pointer.
802 * kfree() is an optional function to free the data (but not the key):
803 * pool_context_reset() and pool_purgekey() call kfree(key, data),
804 * but pool_setkey() does not. For pool_setkey() it is the caller's
805 * responsibility to free any existing data with the same key.
806 * kfree() must not call functions taking a tpool argument.
808 int ldap_pvt_thread_pool_setkey(
812 ldap_pvt_thread_pool_keyfree_t *kfree,
814 ldap_pvt_thread_pool_keyfree_t **oldkfreep )
816 ldap_int_thread_userctx_t *ctx = xctx;
819 if ( !ctx || !key ) return EINVAL;
821 for ( i=found=0; i<MAXKEYS; i++ ) {
822 if ( ctx->ltu_key[i].ltk_key == key ) {
825 } else if ( !ctx->ltu_key[i].ltk_key ) {
832 *olddatap = ctx->ltu_key[i].ltk_data;
840 *oldkfreep = ctx->ltu_key[i].ltk_free;
846 if ( data || kfree ) {
849 ctx->ltu_key[i].ltk_key = key;
850 ctx->ltu_key[i].ltk_data = data;
851 ctx->ltu_key[i].ltk_free = kfree;
852 } else if ( found ) {
853 clear_key_idx( ctx, i );
859 /* Free all elements with this key, no matter which thread they're in.
860 * May only be called while the pool is paused.
862 void ldap_pvt_thread_pool_purgekey( void *key )
865 ldap_int_thread_userctx_t *ctx;
867 assert ( key != NULL );
869 for ( i=0; i<LDAP_MAXTHR; i++ ) {
870 ctx = thread_keys[i].ctx;
871 if ( ctx && ctx != DELETED_THREAD_CTX ) {
872 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
873 if ( ctx->ltu_key[j].ltk_key == key ) {
874 if (ctx->ltu_key[j].ltk_free)
875 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
876 ctx->ltu_key[j].ltk_data );
877 clear_key_idx( ctx, j );
886 * Find the context of the current thread.
887 * This is necessary if the caller does not have access to the
888 * thread context handle (for example, a slapd plugin calling
889 * slapi_search_internal()). No doubt it is more efficient
890 * for the application to keep track of the thread context
893 void *ldap_pvt_thread_pool_context( )
897 ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
898 return ctx ? ctx : &ldap_int_main_thrctx;
902 * Free the context's keys.
903 * Must not call functions taking a tpool argument (because this
904 * thread already holds ltp_mutex when called from pool_wrapper()).
906 void ldap_pvt_thread_pool_context_reset( void *vctx )
908 ldap_int_thread_userctx_t *ctx = vctx;
911 for ( i=MAXKEYS-1; i>=0; i--) {
912 if ( !ctx->ltu_key[i].ltk_key )
914 if ( ctx->ltu_key[i].ltk_free )
915 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
916 ctx->ltu_key[i].ltk_data );
917 ctx->ltu_key[i].ltk_key = NULL;
921 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
923 ldap_int_thread_userctx_t *ctx = vctx;
927 #endif /* LDAP_THREAD_HAVE_TPOOL */