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 read-only (used by pool_purgekey and pool_context).
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;
272 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
276 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
277 sizeof(struct ldap_int_thread_pool_s));
279 if (pool == NULL) return(-1);
281 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
284 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
287 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
290 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
291 pool->ltp_max_count = max_threads;
292 pool->ltp_max_pending = max_pending;
293 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
294 LDAP_SLIST_INIT(&pool->ltp_free_list);
295 LDAP_SLIST_INIT(&pool->ltp_active_list);
296 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
297 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
298 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
301 /* THIS WILL NOT WORK on some systems. If the process
302 * forks after starting a thread, there is no guarantee
303 * that the thread will survive the fork. For example,
304 * slapd forks in order to daemonize, and does so after
305 * calling ldap_pvt_thread_pool_init. On some systems,
306 * this initial thread does not run in the child process,
307 * but ltp_open_count == 1, so two things happen:
308 * 1) the first client connection fails, and 2) when
309 * slapd is kill'ed, it never terminates since it waits
310 * for all worker threads to exit. */
312 /* start up one thread, just so there is one. no need to
313 * lock the mutex right now, since no threads are running.
315 pool->ltp_open_count++;
317 ldap_pvt_thread_t thr;
318 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
321 /* couldn't start one? then don't start any */
322 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
323 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
324 ldap_int_thread_pool_s, ltp_next);
325 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
326 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
327 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
328 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
340 ldap_pvt_thread_pool_submit (
341 ldap_pvt_thread_pool_t *tpool,
342 ldap_pvt_thread_start_t *start_routine, void *arg )
344 struct ldap_int_thread_pool_s *pool;
345 ldap_int_thread_ctx_t *ctx;
347 ldap_pvt_thread_t thr;
357 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
358 if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
359 || (pool->ltp_max_pending > 0
360 && pool->ltp_pending_count >= pool->ltp_max_pending))
362 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
366 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
368 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
370 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
371 sizeof(ldap_int_thread_ctx_t));
373 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
378 ctx->ltc_start_routine = start_routine;
381 pool->ltp_pending_count++;
382 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
383 if (pool->ltp_pause) {
384 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
387 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
388 if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
389 && (pool->ltp_open_count <
390 (pool->ltp_max_count ? pool->ltp_max_count : LDAP_MAXTHR)))
392 pool->ltp_open_count++;
393 pool->ltp_starting++;
396 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
398 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
399 ldap_lazy_sem_op_submit( thread_pool_sem );
405 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
407 rc = ldap_pvt_thread_create( &thr, 1,
408 ldap_int_thread_pool_wrapper, pool );
409 pool->ltp_starting--;
411 /* couldn't create thread. back out of
412 * ltp_open_count and check for even worse things.
414 pool->ltp_open_count--;
415 if (pool->ltp_open_count == 0) {
416 /* no open threads at all?!?
418 ldap_int_thread_ctx_t *ptr;
420 /* let pool_destroy know there are no more threads */
421 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
423 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
424 if (ptr == ctx) break;
426 /* no open threads, context not handled, so
427 * back out of ltp_pending_count, free the context,
430 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
431 ldap_int_thread_ctx_s, ltc_next.q);
432 pool->ltp_pending_count--;
433 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
438 /* there is another open thread, so this
439 * context will be handled eventually.
440 * continue on, we have signalled that
441 * the context is waiting.
444 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
451 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
453 struct ldap_int_thread_pool_s *pool;
455 if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
466 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
467 pool->ltp_max_count = max_threads;
468 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
473 ldap_pvt_thread_pool_query ( ldap_pvt_thread_pool_t *tpool, ldap_pvt_thread_pool_param_t param, void *value )
475 struct ldap_int_thread_pool_s *pool;
478 if ( tpool == NULL || value == NULL ) {
484 if ( pool == NULL ) {
488 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
490 case LDAP_PVT_THREAD_POOL_PARAM_MAX:
491 count = pool->ltp_max_count;
494 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
495 count = pool->ltp_max_pending;
498 case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
499 count = pool->ltp_open_count;
502 case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
503 count = pool->ltp_starting;
506 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
507 count = pool->ltp_active_count;
510 case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
511 count = pool->ltp_pending_count;
514 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
515 count = pool->ltp_pending_count + pool->ltp_active_count;
518 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
521 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
524 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
527 case LDAP_PVT_THREAD_POOL_PARAM_STATE: {
530 ldap_int_thread_pool_state_t state;
532 { "running", LDAP_INT_THREAD_POOL_RUNNING },
533 { "finishing", LDAP_INT_THREAD_POOL_FINISHING },
534 { "stopping", LDAP_INT_THREAD_POOL_STOPPING },
539 if ( pool->ltp_pause ) {
540 *((char **)value) = "pausing";
542 for ( i = 0; str2state[ i ].name != NULL; i++ ) {
543 if ( str2state[ i ].state == pool->ltp_state ) {
547 *((char **)value) = str2state[ i ].name;
549 if ( *((char **)value) != NULL ) {
554 ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex );
557 *((int *)value) = count;
560 return ( count == -1 ? -1 : 0 );
564 * wrapper for ldap_pvt_thread_pool_query(), left around
565 * for backwards compatibility
568 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
572 rc = ldap_pvt_thread_pool_query( tpool,
573 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
583 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
585 struct ldap_int_thread_pool_s *pool, *pptr;
586 ldap_int_thread_ctx_t *ctx;
593 if (pool == NULL) return(-1);
595 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
596 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
597 if (pptr == pool) break;
599 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
600 ldap_int_thread_pool_s, ltp_next);
601 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
603 if (pool != pptr) return(-1);
605 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
606 pool->ltp_state = run_pending
607 ? LDAP_INT_THREAD_POOL_FINISHING
608 : LDAP_INT_THREAD_POOL_STOPPING;
610 while (pool->ltp_open_count) {
611 if (!pool->ltp_pause)
612 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
613 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
615 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
617 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
619 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
623 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
625 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
629 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
630 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
631 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
633 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
634 if ( thread_pool_sem ) {
635 LDAP_FREE( thread_pool_sem );
642 ldap_int_thread_pool_wrapper (
645 struct ldap_int_thread_pool_s *pool = xpool;
646 ldap_int_thread_ctx_t *ctx;
647 ldap_int_thread_userctx_t uctx;
648 unsigned i, keyslot, hash;
653 for ( i=0; i<MAXKEYS; i++ ) {
654 uctx.ltu_key[i].ltk_key = NULL;
657 uctx.ltu_id = ldap_pvt_thread_self();
658 TID_HASH(uctx.ltu_id, hash);
660 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
662 /* thread_keys[] is read-only when paused */
663 while (pool->ltp_pause)
664 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
666 /* find a key slot to give this thread ID and store a
667 * pointer to our keys there; start at the thread ID
668 * itself (mod LDAP_MAXTHR) and look for an empty slot.
670 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
671 for (keyslot = hash & (LDAP_MAXTHR-1);
672 !ldap_pvt_thread_equal(thread_keys[keyslot].id, tid_zero);
673 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
674 thread_keys[keyslot].id = uctx.ltu_id;
675 thread_keys[keyslot].ctx = &uctx;
676 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
679 while (pool->ltp_pause)
680 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
682 if (pool->ltp_state == LDAP_INT_THREAD_POOL_STOPPING)
685 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
687 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
690 if (pool->ltp_open_count >
691 (pool->ltp_max_count ? pool->ltp_max_count : LDAP_MAXTHR))
693 /* too many threads running (can happen if the
694 * maximum threads value is set during ongoing
695 * operation using ldap_pvt_thread_pool_maxthreads)
696 * so let this thread die.
701 /* we could check an idle timer here, and let the
702 * thread die if it has been inactive for a while.
703 * only die if there are other open threads (i.e.,
704 * always have at least one thread open). the check
705 * should be like this:
706 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
707 * check timer, wait if ltp_pause, leave thread (break;)
709 * Just use pthread_cond_timedwait if we want to
713 assert(pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING);
714 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
718 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
719 pool->ltp_pending_count--;
721 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
722 pool->ltp_active_count++;
723 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
725 ctx->ltc_start_routine(&uctx, ctx->ltc_arg);
727 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
728 ldap_lazy_sem_post( thread_pool_sem );
730 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
731 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
732 ldap_int_thread_ctx_s, ltc_next.al);
733 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
734 pool->ltp_active_count--;
736 /* let pool_pause know when it is the sole active thread */
737 if (pool->ltp_active_count < 2)
738 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
741 ldap_pvt_thread_pool_context_reset(&uctx);
743 /* Needed if context_reset can let another thread request a pause */
744 while (pool->ltp_pause)
745 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
747 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
748 thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
749 thread_keys[keyslot].id = tid_zero;
750 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
752 pool->ltp_open_count--;
754 /* let pool_destroy know we're all done */
755 if (pool->ltp_open_count < 1)
756 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
758 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
760 ldap_pvt_thread_exit(NULL);
765 ldap_pvt_thread_pool_pause (
766 ldap_pvt_thread_pool_t *tpool )
768 struct ldap_int_thread_pool_s *pool;
778 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
780 /* If someone else has already requested a pause, we have to wait */
781 while (pool->ltp_pause) {
782 pool->ltp_pending_count++;
783 pool->ltp_active_count--;
784 /* let the other pool_pause() know when it can proceed */
785 if (pool->ltp_active_count < 2)
786 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
787 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
788 pool->ltp_pending_count--;
789 pool->ltp_active_count++;
792 /* Wait for everyone else to pause or finish */
794 while (pool->ltp_active_count > 1) {
795 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
798 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
803 ldap_pvt_thread_pool_resume (
804 ldap_pvt_thread_pool_t *tpool )
806 struct ldap_int_thread_pool_s *pool;
816 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
818 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
819 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
823 int ldap_pvt_thread_pool_getkey(
827 ldap_pvt_thread_pool_keyfree_t **kfree )
829 ldap_int_thread_userctx_t *ctx = xctx;
832 if ( !ctx || !key || !data ) return EINVAL;
834 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
835 if ( ctx->ltu_key[i].ltk_key == key ) {
836 *data = ctx->ltu_key[i].ltk_data;
837 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
845 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
848 while ( ++j < MAXKEYS && ctx->ltu_key[j].ltk_key );
850 ctx->ltu_key[i] = ctx->ltu_key[j];
853 ctx->ltu_key[i].ltk_key = NULL;
856 int ldap_pvt_thread_pool_setkey(
860 ldap_pvt_thread_pool_keyfree_t *kfree )
862 ldap_int_thread_userctx_t *ctx = xctx;
865 if ( !ctx || !key ) return EINVAL;
867 for ( i=found=0; i<MAXKEYS; i++ ) {
868 if ( ctx->ltu_key[i].ltk_key == key ) {
871 } else if ( !ctx->ltu_key[i].ltk_key ) {
876 if ( data || kfree ) {
879 ctx->ltu_key[i].ltk_key = key;
880 ctx->ltu_key[i].ltk_data = data;
881 ctx->ltu_key[i].ltk_free = kfree;
882 } else if ( found ) {
883 clear_key_idx( ctx, i );
889 /* Free all elements with this key, no matter which thread they're in.
890 * May only be called while the pool is paused.
892 void ldap_pvt_thread_pool_purgekey( void *key )
895 ldap_int_thread_userctx_t *ctx;
897 assert ( key != NULL );
899 for ( i=0; i<LDAP_MAXTHR; i++ ) {
900 ctx = thread_keys[i].ctx;
901 if ( ctx && ctx != DELETED_THREAD_CTX ) {
902 for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
903 if ( ctx->ltu_key[j].ltk_key == key ) {
904 if (ctx->ltu_key[j].ltk_free)
905 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
906 ctx->ltu_key[j].ltk_data );
907 clear_key_idx( ctx, j );
916 * This is necessary if the caller does not have access to the
917 * thread context handle (for example, a slapd plugin calling
918 * slapi_search_internal()). No doubt it is more efficient
919 * for the application to keep track of the thread context
922 void *ldap_pvt_thread_pool_context( )
924 ldap_pvt_thread_t tid;
926 ldap_int_thread_userctx_t *ctx;
928 tid = ldap_pvt_thread_self();
929 if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
930 return &ldap_int_main_thrctx;
932 TID_HASH( tid, hash );
933 i = hash &= (LDAP_MAXTHR-1);
934 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
936 ctx = thread_keys[i].ctx;
937 if ( ctx != DELETED_THREAD_CTX )
938 if ( ldap_pvt_thread_equal(thread_keys[i].id, tid) || !ctx )
940 } while ( (i = (i+1) & (LDAP_MAXTHR-1)) != hash );
943 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
948 void ldap_pvt_thread_pool_context_reset( void *vctx )
950 ldap_int_thread_userctx_t *ctx = vctx;
953 for ( i=MAXKEYS-1; i>=0; i--) {
954 if ( !ctx->ltu_key[i].ltk_key )
956 if ( ctx->ltu_key[i].ltk_free )
957 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
958 ctx->ltu_key[i].ltk_data );
959 ctx->ltu_key[i].ltk_key = NULL;
963 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
965 ldap_int_thread_userctx_t *ctx = vctx;
969 #endif /* LDAP_THREAD_HAVE_TPOOL */