2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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/stdarg.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
27 #include "ldap_pvt_thread.h" /* Get the thread interface */
28 #include "ldap_queue.h"
29 #define LDAP_THREAD_POOL_IMPLEMENTATION
30 #include "ldap_thr_debug.h" /* May rename symbols defined below */
32 #ifndef LDAP_THREAD_HAVE_TPOOL
34 typedef enum ldap_int_thread_pool_state_e {
35 LDAP_INT_THREAD_POOL_RUNNING,
36 LDAP_INT_THREAD_POOL_FINISHING,
37 LDAP_INT_THREAD_POOL_STOPPING
38 } ldap_int_thread_pool_state_t;
40 /* Thread-specific key with data and optional free function */
41 typedef struct ldap_int_thread_key_s {
44 ldap_pvt_thread_pool_keyfree_t *ltk_free;
45 } ldap_int_thread_key_t;
47 /* Max number of thread-specific keys we store per thread.
48 * We don't expect to use many...
52 /* Max number of threads */
53 #define LDAP_MAXTHR 1024 /* must be a power of 2 */
55 /* Context: thread ID and thread-specific key/data pairs */
56 typedef struct ldap_int_thread_userctx_s {
57 ldap_pvt_thread_t ltu_id;
58 ldap_int_thread_key_t ltu_key[MAXKEYS];
59 } ldap_int_thread_userctx_t;
62 /* Simple {thread ID -> context} hash table; key=ctx->ltu_id.
63 * Protected by ldap_pvt_thread_pool_mutex except during pauses,
64 * when it is read-only (used by pool_purgekey and pool_context).
65 * Protected by tpool->ltp_mutex during pauses.
68 ldap_int_thread_userctx_t *ctx;
69 /* ctx is valid when not NULL or DELETED_THREAD_CTX */
70 # define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
71 } thread_keys[LDAP_MAXTHR];
73 #define TID_HASH(tid, hash) do { \
74 unsigned const char *ptr_ = (unsigned const char *)&(tid); \
76 for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
77 (hash) += ((hash) << 5) ^ ptr_[i_]; \
81 /* Task for a thread to perform */
82 typedef struct ldap_int_thread_task_s {
84 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s) q;
85 LDAP_SLIST_ENTRY(ldap_int_thread_task_s) l;
87 ldap_pvt_thread_start_t *ltt_start_routine;
89 } ldap_int_thread_task_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_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ltp_pending_list;
105 LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list;
107 ldap_int_thread_pool_state_t ltp_state;
109 /* some active request needs to be the sole active request */
112 long ltp_max_count; /* max number of threads in pool, or 0 */
113 long ltp_max_pending; /* max pending or paused requests, or 0 */
114 long ltp_pending_count; /* pending or paused requests */
115 long ltp_active_count; /* active, not paused requests */
116 long ltp_open_count; /* number of threads */
117 long ltp_starting; /* currenlty starting threads */
120 static int ldap_int_has_thread_pool = 0;
121 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
122 ldap_int_thread_pool_list =
123 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
125 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
127 static void *ldap_int_thread_pool_wrapper( void *pool );
129 /* Context of the main thread */
130 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
133 ldap_int_thread_pool_startup ( void )
135 ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
136 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
140 ldap_int_thread_pool_shutdown ( void )
142 struct ldap_int_thread_pool_s *pool;
144 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
145 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
147 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
152 /* Create a thread pool */
154 ldap_pvt_thread_pool_init (
155 ldap_pvt_thread_pool_t *tpool,
159 ldap_pvt_thread_pool_t pool;
162 /* multiple pools are currently not supported (ITS#4943) */
163 assert(!ldap_int_has_thread_pool);
165 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
171 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
172 sizeof(struct ldap_int_thread_pool_s));
174 if (pool == NULL) return(-1);
176 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
179 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
182 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
186 ldap_int_has_thread_pool = 1;
187 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
188 pool->ltp_max_count = max_threads;
189 pool->ltp_max_pending = max_pending;
190 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
191 LDAP_SLIST_INIT(&pool->ltp_free_list);
192 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
193 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
194 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
197 /* THIS WILL NOT WORK on some systems. If the process
198 * forks after starting a thread, there is no guarantee
199 * that the thread will survive the fork. For example,
200 * slapd forks in order to daemonize, and does so after
201 * calling ldap_pvt_thread_pool_init. On some systems,
202 * this initial thread does not run in the child process,
203 * but ltp_open_count == 1, so two things happen:
204 * 1) the first client connection fails, and 2) when
205 * slapd is kill'ed, it never terminates since it waits
206 * for all worker threads to exit. */
208 /* start up one thread, just so there is one. no need to
209 * lock the mutex right now, since no threads are running.
211 pool->ltp_open_count++;
213 ldap_pvt_thread_t thr;
214 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
217 /* couldn't start one? then don't start any */
218 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
219 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
220 ldap_int_thread_pool_s, ltp_next);
221 ldap_int_has_thread_pool = 0;
222 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
223 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
224 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
225 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
236 /* Submit a task to be performed by the thread pool */
238 ldap_pvt_thread_pool_submit (
239 ldap_pvt_thread_pool_t *tpool,
240 ldap_pvt_thread_start_t *start_routine, void *arg )
242 struct ldap_int_thread_pool_s *pool;
243 ldap_int_thread_task_t *task;
244 ldap_pvt_thread_t thr;
254 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
255 if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
256 || (pool->ltp_max_pending
257 && pool->ltp_pending_count >= pool->ltp_max_pending))
259 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
263 task = LDAP_SLIST_FIRST(&pool->ltp_free_list);
265 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l);
267 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
269 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
274 task->ltt_start_routine = start_routine;
277 pool->ltp_pending_count++;
278 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, task, ltt_next.q);
279 if (pool->ltp_pause) {
280 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
283 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
284 if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
285 && (pool->ltp_open_count <
286 (pool->ltp_max_count ? pool->ltp_max_count : LDAP_MAXTHR)))
288 pool->ltp_open_count++;
289 pool->ltp_starting++;
290 if (0 != ldap_pvt_thread_create(
291 &thr, 1, ldap_int_thread_pool_wrapper, pool))
293 /* couldn't create thread. back out of
294 * ltp_open_count and check for even worse things.
296 pool->ltp_starting--;
297 pool->ltp_open_count--;
298 if (pool->ltp_open_count == 0) {
299 /* no open threads at all?!?
301 ldap_int_thread_task_t *ptr;
303 /* let pool_destroy know there are no more threads */
304 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
306 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltt_next.q)
307 if (ptr == task) break;
309 /* no open threads, task not handled, so
310 * back out of ltp_pending_count, free the task,
313 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, task,
314 ldap_int_thread_task_s, ltt_next.q);
315 pool->ltp_pending_count--;
316 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
321 /* there is another open thread, so this
322 * task will be handled eventually.
323 * continue on, we have signalled that
324 * the task is waiting.
329 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
333 /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */
335 ldap_pvt_thread_pool_maxthreads(
336 ldap_pvt_thread_pool_t *tpool,
339 struct ldap_int_thread_pool_s *pool;
341 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
352 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
353 pool->ltp_max_count = max_threads;
354 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
358 /* Inspect the pool */
360 ldap_pvt_thread_pool_query(
361 ldap_pvt_thread_pool_t *tpool,
362 ldap_pvt_thread_pool_param_t param,
365 struct ldap_int_thread_pool_s *pool;
368 if ( tpool == NULL || value == NULL ) {
374 if ( pool == NULL ) {
378 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
380 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
381 count = pool->ltp_max_count;
384 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
385 count = pool->ltp_max_pending;
388 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
389 count = pool->ltp_open_count;
392 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
393 count = pool->ltp_starting;
396 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
397 count = pool->ltp_active_count;
400 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
401 count = pool->ltp_pending_count;
404 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
405 count = pool->ltp_pending_count + pool->ltp_active_count;
408 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
411 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
414 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
417 case LDAP_PVT_THREAD_POOL_PARAM_STATE: {
420 ldap_int_thread_pool_state_t state;
422 { "running", LDAP_INT_THREAD_POOL_RUNNING },
423 { "finishing", LDAP_INT_THREAD_POOL_FINISHING },
424 { "stopping", LDAP_INT_THREAD_POOL_STOPPING },
429 if ( pool->ltp_pause ) {
430 *((char **)value) = "pausing";
432 for ( i = 0; str2state[ i ].name != NULL; i++ ) {
433 if ( str2state[ i ].state == pool->ltp_state ) {
437 *((char **)value) = str2state[ i ].name;
439 if ( *((char **)value) != NULL ) {
444 case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
447 ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex );
450 *((int *)value) = count;
453 return ( count == -1 ? -1 : 0 );
457 * wrapper for ldap_pvt_thread_pool_query(), left around
458 * for backwards compatibility
461 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
465 rc = ldap_pvt_thread_pool_query( tpool,
466 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
475 /* Destroy the pool after making its threads finish */
477 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
479 struct ldap_int_thread_pool_s *pool, *pptr;
480 ldap_int_thread_task_t *task;
487 if (pool == NULL) return(-1);
489 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
490 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
491 if (pptr == pool) break;
493 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
494 ldap_int_thread_pool_s, ltp_next);
495 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
497 if (pool != pptr) return(-1);
499 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
500 pool->ltp_state = run_pending
501 ? LDAP_INT_THREAD_POOL_FINISHING
502 : LDAP_INT_THREAD_POOL_STOPPING;
504 while (pool->ltp_open_count) {
505 if (!pool->ltp_pause)
506 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
507 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
509 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
511 while ((task = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
513 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q);
517 while ((task = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
519 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l);
523 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
524 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
525 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
527 ldap_int_has_thread_pool = 0;
531 /* Thread loop. Accept and handle submitted tasks. */
533 ldap_int_thread_pool_wrapper (
536 struct ldap_int_thread_pool_s *pool = xpool;
537 ldap_int_thread_task_t *task;
538 ldap_int_thread_userctx_t ctx, *kctx;
539 unsigned i, keyslot, hash;
541 assert(pool != NULL);
543 for ( i=0; i<MAXKEYS; i++ ) {
544 ctx.ltu_key[i].ltk_key = NULL;
547 ctx.ltu_id = ldap_pvt_thread_self();
548 TID_HASH(ctx.ltu_id, hash);
550 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
552 /* thread_keys[] is read-only when paused */
553 while (pool->ltp_pause)
554 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
556 /* find a key slot to give this thread ID and store a
557 * pointer to our keys there; start at the thread ID
558 * itself (mod LDAP_MAXTHR) and look for an empty slot.
560 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
561 for (keyslot = hash & (LDAP_MAXTHR-1);
562 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
563 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
564 thread_keys[keyslot].ctx = &ctx;
565 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
567 pool->ltp_starting--;
570 while (pool->ltp_pause)
571 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
573 if (pool->ltp_state == LDAP_INT_THREAD_POOL_STOPPING)
576 task = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
578 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
581 if (pool->ltp_open_count >
582 (pool->ltp_max_count ? pool->ltp_max_count : LDAP_MAXTHR))
584 /* too many threads running (can happen if the
585 * maximum threads value is set during ongoing
586 * operation using ldap_pvt_thread_pool_maxthreads)
587 * so let this thread die.
592 /* we could check an idle timer here, and let the
593 * thread die if it has been inactive for a while.
594 * only die if there are other open threads (i.e.,
595 * always have at least one thread open). the check
596 * should be like this:
597 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
598 * check timer, wait if ltp_pause, leave thread (break;)
600 * Just use pthread_cond_timedwait if we want to
604 assert(pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING);
605 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
609 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q);
610 pool->ltp_pending_count--;
611 pool->ltp_active_count++;
612 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
614 task->ltt_start_routine(&ctx, task->ltt_arg);
616 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
617 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task, ltt_next.l);
618 pool->ltp_active_count--;
619 /* let pool_pause know when it is the sole active thread */
620 if (pool->ltp_active_count < 2)
621 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
624 /* The ltp_mutex lock protects ctx->ltu_key from pool_purgekey()
625 * during this call, since it prevents new pauses. */
626 ldap_pvt_thread_pool_context_reset(&ctx);
628 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
629 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
630 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
632 pool->ltp_open_count--;
633 /* let pool_destroy know we're all done */
634 if (pool->ltp_open_count < 1)
635 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
637 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
639 ldap_pvt_thread_exit(NULL);
643 /* Pause the pool. Return when all other threads are paused. */
645 ldap_pvt_thread_pool_pause (
646 ldap_pvt_thread_pool_t *tpool )
648 struct ldap_int_thread_pool_s *pool;
658 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
660 /* If someone else has already requested a pause, we have to wait */
661 if (pool->ltp_pause) {
662 pool->ltp_pending_count++;
663 pool->ltp_active_count--;
664 /* let the other pool_pause() know when it can proceed */
665 if (pool->ltp_active_count < 2)
666 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
668 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
669 } while (pool->ltp_pause);
670 pool->ltp_pending_count--;
671 pool->ltp_active_count++;
674 /* Wait for everyone else to pause or finish */
676 while (pool->ltp_active_count > 1) {
677 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
680 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
686 ldap_pvt_thread_pool_resume (
687 ldap_pvt_thread_pool_t *tpool )
689 struct ldap_int_thread_pool_s *pool;
699 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
701 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
702 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
707 * Get the key's data and optionally free function in the given context.
709 int ldap_pvt_thread_pool_getkey(
713 ldap_pvt_thread_pool_keyfree_t **kfree )
715 ldap_int_thread_userctx_t *ctx = xctx;
718 if ( !ctx || !key || !data ) return EINVAL;
720 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
721 if ( ctx->ltu_key[i].ltk_key == key ) {
722 *data = ctx->ltu_key[i].ltk_data;
723 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
731 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
733 for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
734 ctx->ltu_key[i] = ctx->ltu_key[i+1];
735 ctx->ltu_key[i].ltk_key = NULL;
739 * Set or remove data for the key in the given context.
740 * key can be any unique pointer.
741 * kfree() is an optional function to free the data (but not the key):
742 * pool_context_reset() and pool_purgekey() call kfree(key, data),
743 * but pool_setkey() does not. For pool_setkey() it is the caller's
744 * responsibility to free any existing data with the same key.
745 * kfree() must not call functions taking a tpool argument.
747 int ldap_pvt_thread_pool_setkey(
751 ldap_pvt_thread_pool_keyfree_t *kfree )
753 ldap_int_thread_userctx_t *ctx = xctx;
756 if ( !ctx || !key ) return EINVAL;
758 for ( i=found=0; i<MAXKEYS; i++ ) {
759 if ( ctx->ltu_key[i].ltk_key == key ) {
762 } else if ( !ctx->ltu_key[i].ltk_key ) {
767 if ( data || kfree ) {
770 ctx->ltu_key[i].ltk_key = key;
771 ctx->ltu_key[i].ltk_data = data;
772 ctx->ltu_key[i].ltk_free = kfree;
773 } else if ( found ) {
774 clear_key_idx( ctx, i );
780 /* Free all elements with this key, no matter which thread they're in.
781 * May only be called while the pool is paused.
783 void ldap_pvt_thread_pool_purgekey( void *key )
786 ldap_int_thread_userctx_t *ctx;
788 assert ( key != NULL );
790 for ( i=0; i<LDAP_MAXTHR; i++ ) {
791 ctx = thread_keys[i].ctx;
792 if ( ctx && ctx != DELETED_THREAD_CTX ) {
793 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
794 if ( ctx->ltu_key[j].ltk_key == key ) {
795 if (ctx->ltu_key[j].ltk_free)
796 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
797 ctx->ltu_key[j].ltk_data );
798 clear_key_idx( ctx, j );
807 * Find the context of the current thread.
808 * This is necessary if the caller does not have access to the
809 * thread context handle (for example, a slapd plugin calling
810 * slapi_search_internal()). No doubt it is more efficient
811 * for the application to keep track of the thread context
814 void *ldap_pvt_thread_pool_context( )
816 ldap_pvt_thread_t tid;
818 ldap_int_thread_userctx_t *ctx;
820 tid = ldap_pvt_thread_self();
821 if ( ldap_pvt_thread_equal( tid, ldap_int_main_thrctx.ltu_id ))
822 return &ldap_int_main_thrctx;
824 TID_HASH( tid, hash );
825 i = hash &= (LDAP_MAXTHR-1);
826 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
828 ctx = thread_keys[i].ctx;
829 if ( ctx == DELETED_THREAD_CTX )
831 if ( !ctx || ldap_pvt_thread_equal(thread_keys[i].ctx->ltu_id, tid) )
833 } while ( (i = (i+1) & (LDAP_MAXTHR-1)) != hash );
836 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
842 * Free the context's keys.
843 * Must not call functions taking a tpool argument (because this
844 * thread already holds ltp_mutex when called from pool_wrapper()).
846 void ldap_pvt_thread_pool_context_reset( void *vctx )
848 ldap_int_thread_userctx_t *ctx = vctx;
851 for ( i=MAXKEYS-1; i>=0; i--) {
852 if ( !ctx->ltu_key[i].ltk_key )
854 if ( ctx->ltu_key[i].ltk_free )
855 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
856 ctx->ltu_key[i].ltk_data );
857 ctx->ltu_key[i].ltk_key = NULL;
861 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
863 ldap_int_thread_userctx_t *ctx = vctx;
867 #endif /* LDAP_THREAD_HAVE_TPOOL */