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 typedef struct ldap_int_thread_key_s {
43 ldap_pvt_thread_pool_keyfree_t *ltk_free;
44 } ldap_int_thread_key_t;
46 /* Max number of thread-specific keys we store per thread.
47 * We don't expect to use many...
50 #define LDAP_MAXTHR 1024 /* must be a power of 2 */
52 typedef struct ldap_int_thread_userctx_s {
53 ldap_pvt_thread_t ltu_id;
54 ldap_int_thread_key_t ltu_key[MAXKEYS];
55 } ldap_int_thread_userctx_t;
57 static ldap_pvt_thread_t tid_zero;
59 /* Thread ID -> context mapping (poor open-addressed hash table).
60 * Protected by ldap_pvt_thread_pool_mutex except during pauses,
61 * when it is reserved for ldap_pvt_thread_pool_purgekey().
65 ldap_int_thread_userctx_t *ctx; /* set when id != tid_zero */
66 # define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
67 } thread_keys[LDAP_MAXTHR];
69 #define TID_HASH(tid, hash) do { \
70 unsigned const char *ptr_ = (unsigned const char *)&(tid); \
72 for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
73 (hash) += ((hash) << 5) ^ ptr_[i_]; \
77 typedef struct ldap_int_thread_ctx_s {
79 LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
80 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
81 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
83 ldap_pvt_thread_start_t *ltc_start_routine;
85 } ldap_int_thread_ctx_t;
87 struct ldap_int_thread_pool_s {
88 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
89 ldap_pvt_thread_mutex_t ltp_mutex;
90 ldap_pvt_thread_cond_t ltp_cond;
91 ldap_pvt_thread_cond_t ltp_pcond;
92 LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
93 LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
94 LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
95 ldap_int_thread_pool_state_t ltp_state;
99 long ltp_pending_count;
100 long ltp_active_count;
105 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
106 ldap_int_thread_pool_list =
107 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
109 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
111 static void *ldap_int_thread_pool_wrapper( void *pool );
113 static ldap_pvt_thread_t ldap_int_main_tid;
115 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
118 ldap_int_thread_pool_startup ( void )
120 ldap_int_main_tid = ldap_pvt_thread_self();
121 ldap_int_main_thrctx.ltu_id = ldap_int_main_tid;
123 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
127 ldap_int_thread_pool_shutdown ( void )
129 struct ldap_int_thread_pool_s *pool;
131 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
132 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
134 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
138 typedef struct ldap_lazy_sem_t {
139 ldap_pvt_thread_mutex_t ls_mutex;
140 ldap_pvt_thread_cond_t ls_cond;
143 * when more than ls_lazy_count number of resources
144 * becmoes available, the thread wating for the resources will
145 * be waken up in order to prevent frequent blocking/waking-up
147 unsigned int ls_lazy_count;
149 * only one thread(listener) will wait on this semaphore
150 * using a flag instead of a list
155 ldap_lazy_sem_t* thread_pool_sem = NULL;
158 ldap_lazy_sem_init( unsigned int value, unsigned int lazyness )
160 thread_pool_sem = (ldap_lazy_sem_t*) LDAP_CALLOC(1,
161 sizeof( ldap_lazy_sem_t ));
163 if( thread_pool_sem == NULL ) return -1;
165 ldap_pvt_thread_mutex_init( &thread_pool_sem->ls_mutex );
166 ldap_pvt_thread_cond_init( &thread_pool_sem->ls_cond );
167 thread_pool_sem->ls_sem_value = value;
168 thread_pool_sem->ls_lazy_count = lazyness;
169 thread_pool_sem->ls_wait = 0;
174 /* FIXME: move to some approprite header */
175 int ldap_lazy_sem_dec( ldap_lazy_sem_t* ls );
176 int ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls );
179 * ldap_lazy_sem_wait is used if a caller is blockable(listener).
180 * Otherwise use ldap_lazy_sem_dec (worker)
183 ldap_lazy_sem_op_submit( ldap_lazy_sem_t* ls )
185 if ( ls == NULL ) return -1;
187 /* only worker thread has its thread ctx */
188 if ( ldap_pvt_thread_pool_context() ) {
190 return ldap_lazy_sem_dec( ls );
193 return ldap_lazy_sem_wait( ls );
198 * test if given semaphore's count is zero.
199 * If 0, the caller is blocked
200 * If not, the count is decremented.
203 ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls )
205 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
208 if ( ls->ls_sem_value <= 0 ) {
209 /* no more avaliable resources */
211 ldap_pvt_thread_cond_wait( &ls->ls_cond, &ls->ls_mutex );
214 /* avaliable resources */
218 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
224 * decrement the count without blocking
225 * even when the count becomes less than or equal to 0
228 ldap_lazy_sem_dec( ldap_lazy_sem_t* ls )
230 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
234 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
240 * Increment the count by one and test if it is greater or
241 * equal to lazyness. If it is, wake up a blocked thread.
244 ldap_lazy_sem_post( ldap_lazy_sem_t* ls )
246 if( ls == NULL ) return (-1);
248 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
252 if ( ls->ls_sem_value >= ls->ls_lazy_count ) {
254 ldap_pvt_thread_cond_signal( &ls->ls_cond );
258 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
264 ldap_pvt_thread_pool_init (
265 ldap_pvt_thread_pool_t *tpool,
269 ldap_pvt_thread_pool_t pool;
273 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
274 sizeof(struct ldap_int_thread_pool_s));
276 if (pool == NULL) return(-1);
278 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
281 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
284 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
287 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
288 pool->ltp_max_count = max_threads;
289 pool->ltp_max_pending = max_pending;
290 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
291 LDAP_SLIST_INIT(&pool->ltp_free_list);
292 LDAP_SLIST_INIT(&pool->ltp_active_list);
293 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
294 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
295 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
298 /* THIS WILL NOT WORK on some systems. If the process
299 * forks after starting a thread, there is no guarantee
300 * that the thread will survive the fork. For example,
301 * slapd forks in order to daemonize, and does so after
302 * calling ldap_pvt_thread_pool_init. On some systems,
303 * this initial thread does not run in the child process,
304 * but ltp_open_count == 1, so two things happen:
305 * 1) the first client connection fails, and 2) when
306 * slapd is kill'ed, it never terminates since it waits
307 * for all worker threads to exit. */
309 /* start up one thread, just so there is one. no need to
310 * lock the mutex right now, since no threads are running.
312 pool->ltp_open_count++;
314 ldap_pvt_thread_t thr;
315 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
318 /* couldn't start one? then don't start any */
319 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
320 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
321 ldap_int_thread_pool_s, ltp_next);
322 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
323 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
324 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
325 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
337 ldap_pvt_thread_pool_submit (
338 ldap_pvt_thread_pool_t *tpool,
339 ldap_pvt_thread_start_t *start_routine, void *arg )
341 struct ldap_int_thread_pool_s *pool;
342 ldap_int_thread_ctx_t *ctx;
344 ldap_pvt_thread_t thr;
354 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
355 if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
356 || (pool->ltp_max_pending > 0
357 && pool->ltp_pending_count >= pool->ltp_max_pending))
359 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
363 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
365 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
367 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
368 sizeof(ldap_int_thread_ctx_t));
370 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
375 ctx->ltc_start_routine = start_routine;
378 pool->ltp_pending_count++;
379 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
380 if (pool->ltp_pause) {
381 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
384 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
385 if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
386 && (pool->ltp_open_count < pool->ltp_max_count ||
387 pool->ltp_max_count <= 0 ))
389 pool->ltp_open_count++;
390 pool->ltp_starting++;
393 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
395 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
396 ldap_lazy_sem_op_submit( thread_pool_sem );
402 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
404 rc = ldap_pvt_thread_create( &thr, 1,
405 ldap_int_thread_pool_wrapper, pool );
406 pool->ltp_starting--;
408 /* couldn't create thread. back out of
409 * ltp_open_count and check for even worse things.
411 pool->ltp_open_count--;
412 if (pool->ltp_open_count == 0) {
413 /* no open threads at all?!?
415 ldap_int_thread_ctx_t *ptr;
417 /* let pool_destroy know there are no more threads */
418 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
420 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
421 if (ptr == ctx) break;
423 /* no open threads, context not handled, so
424 * back out of ltp_pending_count, free the context,
427 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
428 ldap_int_thread_ctx_s, ltc_next.q);
429 pool->ltp_pending_count--;
430 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
435 /* there is another open thread, so this
436 * context will be handled eventually.
437 * continue on and signal that the context
441 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
448 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
450 struct ldap_int_thread_pool_s *pool;
460 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
461 pool->ltp_max_count = max_threads;
462 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
467 ldap_pvt_thread_pool_query ( ldap_pvt_thread_pool_t *tpool, ldap_pvt_thread_pool_param_t param, void *value )
469 struct ldap_int_thread_pool_s *pool;
472 if ( tpool == NULL || value == NULL ) {
478 if ( pool == NULL ) {
482 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
484 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
485 count = pool->ltp_max_count;
488 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
489 count = pool->ltp_max_pending;
492 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
493 count = pool->ltp_open_count;
496 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
497 count = pool->ltp_starting;
500 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
501 count = pool->ltp_active_count;
504 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
505 count = pool->ltp_pending_count;
508 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
509 count = pool->ltp_pending_count + pool->ltp_active_count;
512 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
515 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
518 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
521 case LDAP_PVT_THREAD_POOL_PARAM_STATE: {
524 ldap_int_thread_pool_state_t state;
526 { "running", LDAP_INT_THREAD_POOL_RUNNING },
527 { "finishing", LDAP_INT_THREAD_POOL_FINISHING },
528 { "stopping", LDAP_INT_THREAD_POOL_STOPPING },
533 if ( pool->ltp_pause ) {
534 *((char **)value) = "pausing";
536 for ( i = 0; str2state[ i ].name != NULL; i++ ) {
537 if ( str2state[ i ].state == pool->ltp_state ) {
541 *((char **)value) = str2state[ i ].name;
543 if ( *((char **)value) != NULL ) {
548 ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex );
551 *((int *)value) = count;
554 return ( count == -1 ? -1 : 0 );
558 * wrapper for ldap_pvt_thread_pool_query(), left around
559 * for backwards compatibility
562 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
566 rc = ldap_pvt_thread_pool_query( tpool,
567 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
577 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
579 struct ldap_int_thread_pool_s *pool, *pptr;
580 ldap_int_thread_ctx_t *ctx;
587 if (pool == NULL) return(-1);
589 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
590 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
591 if (pptr == pool) break;
593 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
594 ldap_int_thread_pool_s, ltp_next);
595 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
597 if (pool != pptr) return(-1);
599 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
600 pool->ltp_state = run_pending
601 ? LDAP_INT_THREAD_POOL_FINISHING
602 : LDAP_INT_THREAD_POOL_STOPPING;
604 while (pool->ltp_open_count) {
605 if (!pool->ltp_pause)
606 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
607 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
609 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
611 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
613 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
617 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
619 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
623 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
624 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
625 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
627 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
628 if ( thread_pool_sem ) {
629 LDAP_FREE( thread_pool_sem );
636 ldap_int_thread_pool_wrapper (
639 struct ldap_int_thread_pool_s *pool = xpool;
640 ldap_int_thread_ctx_t *ctx;
641 ldap_int_thread_userctx_t uctx;
642 unsigned i, keyslot, hash;
647 for ( i=0; i<MAXKEYS; i++ ) {
648 uctx.ltu_key[i].ltk_key = NULL;
651 uctx.ltu_id = ldap_pvt_thread_self();
652 TID_HASH(uctx.ltu_id, hash);
654 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
656 /* when paused, thread_keys[] is reserved for pool_purgekey() */
657 while (pool->ltp_pause)
658 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
660 /* find a key slot to give this thread ID and store a
661 * pointer to our keys there; start at the thread ID
662 * itself (mod LDAP_MAXTHR) and look for an empty slot.
664 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
665 for (keyslot = hash & (LDAP_MAXTHR-1);
666 !ldap_pvt_thread_equal(thread_keys[keyslot].id, tid_zero);
667 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
668 thread_keys[keyslot].id = uctx.ltu_id;
669 thread_keys[keyslot].ctx = &uctx;
670 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
673 while (pool->ltp_pause)
674 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
676 if (pool->ltp_state == LDAP_INT_THREAD_POOL_STOPPING)
679 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
681 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
684 if (pool->ltp_max_count > 0
685 && pool->ltp_open_count > pool->ltp_max_count)
687 /* too many threads running (can happen if the
688 * maximum threads value is set during ongoing
689 * operation using ldap_pvt_thread_pool_maxthreads)
690 * so let this thread die.
695 /* we could check an idle timer here, and let the
696 * thread die if it has been inactive for a while.
697 * only die if there are other open threads (i.e.,
698 * always have at least one thread open). the check
699 * should be like this:
700 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
701 * check timer, wait if ltp_pause, leave thread (break;)
703 * Just use pthread_cond_timedwait if we want to
707 assert(pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING);
708 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
712 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
713 pool->ltp_pending_count--;
715 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
716 pool->ltp_active_count++;
717 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
719 ctx->ltc_start_routine(&uctx, ctx->ltc_arg);
721 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
722 ldap_lazy_sem_post( thread_pool_sem );
724 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
725 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
726 ldap_int_thread_ctx_s, ltc_next.al);
727 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
728 pool->ltp_active_count--;
730 /* let pool_pause know when it is the sole active thread */
731 if (pool->ltp_active_count < 2)
732 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
735 ldap_pvt_thread_pool_context_reset(&uctx);
737 /* Needed if context_reset can let another thread request a pause */
738 while (pool->ltp_pause)
739 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
741 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
742 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
743 thread_keys[keyslot].id = tid_zero;
744 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
746 pool->ltp_open_count--;
748 /* let pool_destroy know we're all done */
749 if (pool->ltp_open_count < 1)
750 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
752 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
754 ldap_pvt_thread_exit(NULL);
759 ldap_pvt_thread_pool_pause (
760 ldap_pvt_thread_pool_t *tpool )
762 struct ldap_int_thread_pool_s *pool;
772 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
774 /* If someone else has already requested a pause, we have to wait */
775 while (pool->ltp_pause) {
776 pool->ltp_pending_count++;
777 pool->ltp_active_count--;
778 /* let the other pool_pause() know when it can proceed */
779 if (pool->ltp_active_count < 2)
780 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
781 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
782 pool->ltp_pending_count--;
783 pool->ltp_active_count++;
786 /* Wait for everyone else to pause or finish */
788 while (pool->ltp_active_count > 1) {
789 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
792 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
797 ldap_pvt_thread_pool_resume (
798 ldap_pvt_thread_pool_t *tpool )
800 struct ldap_int_thread_pool_s *pool;
810 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
812 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
813 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
817 int ldap_pvt_thread_pool_getkey(
821 ldap_pvt_thread_pool_keyfree_t **kfree )
823 ldap_int_thread_userctx_t *ctx = xctx;
826 if ( !ctx || !key || !data ) return EINVAL;
828 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
829 if ( ctx->ltu_key[i].ltk_key == key ) {
830 *data = ctx->ltu_key[i].ltk_data;
831 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
839 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
842 while ( ++j < MAXKEYS && ctx->ltu_key[j].ltk_key );
844 ctx->ltu_key[i] = ctx->ltu_key[j];
847 ctx->ltu_key[i].ltk_key = NULL;
850 int ldap_pvt_thread_pool_setkey(
854 ldap_pvt_thread_pool_keyfree_t *kfree )
856 ldap_int_thread_userctx_t *ctx = xctx;
859 if ( !ctx || !key ) return EINVAL;
861 for ( i=found=0; i<MAXKEYS; i++ ) {
862 if ( ctx->ltu_key[i].ltk_key == key ) {
865 } else if ( !ctx->ltu_key[i].ltk_key ) {
870 if ( data || kfree ) {
873 ctx->ltu_key[i].ltk_key = key;
874 ctx->ltu_key[i].ltk_data = data;
875 ctx->ltu_key[i].ltk_free = kfree;
876 } else if ( found ) {
877 clear_key_idx( ctx, i );
883 /* Free all elements with this key, no matter which thread they're in.
884 * May only be called while the pool is paused.
886 void ldap_pvt_thread_pool_purgekey( void *key )
889 ldap_int_thread_userctx_t *ctx;
891 assert ( key != NULL );
893 for ( i=0; i<LDAP_MAXTHR; i++ ) {
894 ctx = thread_keys[i].ctx;
895 if ( ctx && ctx != DELETED_THREAD_CTX ) {
896 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
897 if ( ctx->ltu_key[j].ltk_key == key ) {
898 if (ctx->ltu_key[j].ltk_free)
899 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
900 ctx->ltu_key[j].ltk_data );
901 clear_key_idx( ctx, j );
910 * This is necessary if the caller does not have access to the
911 * thread context handle (for example, a slapd plugin calling
912 * slapi_search_internal()). No doubt it is more efficient
913 * for the application to keep track of the thread context
916 void *ldap_pvt_thread_pool_context( )
918 ldap_pvt_thread_t tid;
920 ldap_int_thread_userctx_t *ctx;
922 tid = ldap_pvt_thread_self();
923 if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
924 return &ldap_int_main_thrctx;
926 TID_HASH( tid, hash );
927 i = hash &= (LDAP_MAXTHR-1);
928 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
930 ctx = thread_keys[i].ctx;
931 if ( ctx != DELETED_THREAD_CTX )
932 if ( ldap_pvt_thread_equal(thread_keys[i].id, tid) || !ctx )
934 } while ( (i = (i+1) & (LDAP_MAXTHR-1)) != hash );
937 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
942 void ldap_pvt_thread_pool_context_reset( void *vctx )
944 ldap_int_thread_userctx_t *ctx = vctx;
947 for ( i=MAXKEYS-1; i>=0; i--) {
948 if ( !ctx->ltu_key[i].ltk_key )
950 if ( ctx->ltu_key[i].ltk_free )
951 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
952 ctx->ltu_key[i].ltk_data );
953 ctx->ltu_key[i].ltk_key = NULL;
957 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
959 ldap_int_thread_userctx_t *ctx = vctx;
963 #endif /* LDAP_THREAD_HAVE_TPOOL */