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_PAUSING
39 } ldap_int_thread_pool_state_t;
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...
51 #define LDAP_MAXTHR 1024 /* must be a power of 2 */
53 typedef struct ldap_int_thread_userctx_s {
54 ldap_pvt_thread_t ltu_id;
55 ldap_int_thread_key_t ltu_key[MAXKEYS];
56 } ldap_int_thread_userctx_t;
58 static ldap_pvt_thread_t tid_zero;
62 ldap_int_thread_userctx_t *ctx;
63 } thread_keys[LDAP_MAXTHR];
66 typedef struct ldap_int_thread_ctx_s {
68 LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
69 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
70 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
72 ldap_pvt_thread_start_t *ltc_start_routine;
74 } ldap_int_thread_ctx_t;
76 struct ldap_int_thread_pool_s {
77 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
78 ldap_pvt_thread_mutex_t ltp_mutex;
79 ldap_pvt_thread_cond_t ltp_cond;
80 ldap_pvt_thread_cond_t ltp_pcond;
81 LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
82 LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
83 LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
84 ldap_int_thread_pool_state_t ltp_state;
87 long ltp_pending_count;
88 long ltp_active_count;
93 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
94 ldap_int_thread_pool_list =
95 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
97 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
99 static void *ldap_int_thread_pool_wrapper( void *pool );
101 static ldap_pvt_thread_t ldap_int_main_tid;
103 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
106 ldap_int_thread_pool_startup ( void )
108 ldap_int_main_tid = ldap_pvt_thread_self();
109 ldap_int_main_thrctx.ltu_id = ldap_int_main_tid;
111 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
115 ldap_int_thread_pool_shutdown ( void )
117 struct ldap_int_thread_pool_s *pool;
119 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
120 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
122 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
126 typedef struct ldap_lazy_sem_t {
127 ldap_pvt_thread_mutex_t ls_mutex;
128 ldap_pvt_thread_cond_t ls_cond;
131 * when more than ls_lazy_count number of resources
132 * becmoes available, the thread wating for the resources will
133 * be waken up in order to prevent frequent blocking/waking-up
135 unsigned int ls_lazy_count;
137 * only one thread(listener) will wait on this semaphore
138 * using a flag instead of a list
143 ldap_lazy_sem_t* thread_pool_sem = NULL;
146 ldap_lazy_sem_init( unsigned int value, unsigned int lazyness )
148 thread_pool_sem = (ldap_lazy_sem_t*) LDAP_CALLOC(1,
149 sizeof( ldap_lazy_sem_t ));
151 if( thread_pool_sem == NULL ) return -1;
153 ldap_pvt_thread_mutex_init( &thread_pool_sem->ls_mutex );
154 ldap_pvt_thread_cond_init( &thread_pool_sem->ls_cond );
155 thread_pool_sem->ls_sem_value = value;
156 thread_pool_sem->ls_lazy_count = lazyness;
157 thread_pool_sem->ls_wait = 0;
162 /* FIXME: move to some approprite header */
163 int ldap_lazy_sem_dec( ldap_lazy_sem_t* ls );
164 int ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls );
167 * ldap_lazy_sem_wait is used if a caller is blockable(listener).
168 * Otherwise use ldap_lazy_sem_dec (worker)
171 ldap_lazy_sem_op_submit( ldap_lazy_sem_t* ls )
173 if ( ls == NULL ) return -1;
175 /* only worker thread has its thread ctx */
176 if ( ldap_pvt_thread_pool_context() ) {
178 return ldap_lazy_sem_dec( ls );
181 return ldap_lazy_sem_wait( ls );
186 * test if given semaphore's count is zero.
187 * If 0, the caller is blocked
188 * If not, the count is decremented.
191 ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls )
193 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
196 if ( ls->ls_sem_value <= 0 ) {
197 /* no more avaliable resources */
199 ldap_pvt_thread_cond_wait( &ls->ls_cond, &ls->ls_mutex );
202 /* avaliable resources */
206 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
212 * decrement the count without blocking
213 * even when the count becomes less than or equal to 0
216 ldap_lazy_sem_dec( ldap_lazy_sem_t* ls )
218 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
222 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
228 * Increment the count by one and test if it is greater or
229 * equal to lazyness. If it is, wake up a blocked thread.
232 ldap_lazy_sem_post( ldap_lazy_sem_t* ls )
234 if( ls == NULL ) return (-1);
236 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
240 if ( ls->ls_sem_value >= ls->ls_lazy_count ) {
242 ldap_pvt_thread_cond_signal( &ls->ls_cond );
246 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
252 ldap_pvt_thread_pool_init (
253 ldap_pvt_thread_pool_t *tpool,
257 ldap_pvt_thread_pool_t pool;
261 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
262 sizeof(struct ldap_int_thread_pool_s));
264 if (pool == NULL) return(-1);
266 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
269 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
272 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
275 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
276 pool->ltp_max_count = max_threads;
277 pool->ltp_max_pending = max_pending;
278 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
279 LDAP_SLIST_INIT(&pool->ltp_free_list);
280 LDAP_SLIST_INIT(&pool->ltp_active_list);
281 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
282 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
283 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
286 /* THIS WILL NOT WORK on some systems. If the process
287 * forks after starting a thread, there is no guarantee
288 * that the thread will survive the fork. For example,
289 * slapd forks in order to daemonize, and does so after
290 * calling ldap_pvt_thread_pool_init. On some systems,
291 * this initial thread does not run in the child process,
292 * but ltp_open_count == 1, so two things happen:
293 * 1) the first client connection fails, and 2) when
294 * slapd is kill'ed, it never terminates since it waits
295 * for all worker threads to exit. */
297 /* start up one thread, just so there is one. no need to
298 * lock the mutex right now, since no threads are running.
300 pool->ltp_open_count++;
302 ldap_pvt_thread_t thr;
303 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
306 /* couldn't start one? then don't start any */
307 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
308 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
309 ldap_int_thread_pool_s, ltp_next);
310 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
311 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
312 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
313 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
323 #define TID_HASH(tid, hash) do { unsigned i; \
324 unsigned char *ptr = (unsigned char *)&(tid); \
325 for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
328 ldap_pvt_thread_pool_submit (
329 ldap_pvt_thread_pool_t *tpool,
330 ldap_pvt_thread_start_t *start_routine, void *arg )
332 struct ldap_int_thread_pool_s *pool;
333 ldap_int_thread_ctx_t *ctx;
335 ldap_pvt_thread_t thr;
345 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
346 if ((pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING &&
347 pool->ltp_state != LDAP_INT_THREAD_POOL_PAUSING)
348 || (pool->ltp_max_pending > 0
349 && pool->ltp_pending_count >= pool->ltp_max_pending))
351 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
354 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
356 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
358 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
359 sizeof(ldap_int_thread_ctx_t));
361 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
366 ctx->ltc_start_routine = start_routine;
369 pool->ltp_pending_count++;
370 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
371 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
372 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
375 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
376 if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
377 && (pool->ltp_open_count < pool->ltp_max_count ||
378 pool->ltp_max_count <= 0 ))
380 pool->ltp_open_count++;
381 pool->ltp_starting++;
384 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
386 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
387 ldap_lazy_sem_op_submit( thread_pool_sem );
393 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
395 rc = ldap_pvt_thread_create( &thr, 1,
396 ldap_int_thread_pool_wrapper, pool );
399 pool->ltp_starting--;
401 /* assign this thread ID to a key slot; start
402 * at the thread ID itself (mod LDAP_MAXTHR) and
403 * look for an empty slot.
406 for (rc = hash & (LDAP_MAXTHR-1);
407 !ldap_pvt_thread_equal(thread_keys[rc].id, tid_zero);
408 rc = (rc+1) & (LDAP_MAXTHR-1));
409 thread_keys[rc].id = thr;
411 /* couldn't create thread. back out of
412 * ltp_open_count and check for even worse things.
414 pool->ltp_open_count--;
415 pool->ltp_starting--;
416 if (pool->ltp_open_count == 0) {
417 /* no open threads at all?!?
419 ldap_int_thread_ctx_t *ptr;
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 },
529 { "pausing", LDAP_INT_THREAD_POOL_PAUSING },
534 for ( i = 0; str2state[ i ].name != NULL; i++ ) {
535 if ( str2state[ i ].state == pool->ltp_state ) {
539 *((char **)value) = str2state[ i ].name;
540 if ( str2state[ i ].name != NULL ) {
545 ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex );
548 *((int *)value) = count;
551 return ( count == -1 ? -1 : 0 );
555 * wrapper for ldap_pvt_thread_pool_query(), left around
556 * for backwards compatibility
559 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
563 rc = ldap_pvt_thread_pool_query( tpool,
564 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
574 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
576 struct ldap_int_thread_pool_s *pool, *pptr;
577 ldap_int_thread_ctx_t *ctx;
584 if (pool == NULL) return(-1);
586 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
587 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
588 if (pptr == pool) break;
590 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
591 ldap_int_thread_pool_s, ltp_next);
592 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
594 if (pool != pptr) return(-1);
596 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
597 pool->ltp_state = run_pending
598 ? LDAP_INT_THREAD_POOL_FINISHING
599 : LDAP_INT_THREAD_POOL_STOPPING;
601 if ( pool->ltp_open_count ) {
602 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
603 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
605 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
607 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
609 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
613 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
615 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
619 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
620 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
621 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
623 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
624 if ( thread_pool_sem ) {
625 LDAP_FREE( thread_pool_sem );
632 ldap_int_thread_pool_wrapper (
635 struct ldap_int_thread_pool_s *pool = xpool;
636 ldap_int_thread_ctx_t *ctx;
637 ldap_int_thread_userctx_t uctx;
638 int i, keyslot, hash;
643 for ( i=0; i<MAXKEYS; i++ ) {
644 uctx.ltu_key[i].ltk_key = NULL;
647 uctx.ltu_id = ldap_pvt_thread_self();
649 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
651 /* store pointer to our keys */
652 TID_HASH(uctx.ltu_id, hash);
653 for (i = hash & (LDAP_MAXTHR-1);
654 !ldap_pvt_thread_equal(thread_keys[i].id, uctx.ltu_id);
655 i = (i+1) & (LDAP_MAXTHR-1));
656 thread_keys[i].ctx = &uctx;
659 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
660 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
662 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
664 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
666 if (pool->ltp_max_count > 0
667 && pool->ltp_open_count > pool->ltp_max_count)
669 /* too many threads running (can happen if the
670 * maximum threads value is set during ongoing
671 * operation using ldap_pvt_thread_pool_maxthreads)
672 * so let this thread die.
677 /* we could check an idle timer here, and let the
678 * thread die if it has been inactive for a while.
679 * only die if there are other open threads (i.e.,
680 * always have at least one thread open). the check
681 * should be like this:
682 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
683 * check timer, leave thread (break;)
685 * Just use pthread_cond_timedwait if we want to
689 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING
690 || pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING)
692 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
698 pool->ltp_pending_count--;
700 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
701 pool->ltp_active_count++;
702 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
704 ctx->ltc_start_routine(&uctx, ctx->ltc_arg);
706 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
707 ldap_lazy_sem_post( thread_pool_sem );
709 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
710 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
711 ldap_int_thread_ctx_s, ltc_next.al);
712 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
713 pool->ltp_active_count--;
715 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
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);
723 for ( i=0; i<MAXKEYS && uctx.ltu_key[i].ltk_key; i++ ) {
724 if (uctx.ltu_key[i].ltk_free)
725 uctx.ltu_key[i].ltk_free(
726 uctx.ltu_key[i].ltk_key,
727 uctx.ltu_key[i].ltk_data );
730 thread_keys[keyslot].ctx = NULL;
731 thread_keys[keyslot].id = tid_zero;
733 pool->ltp_open_count--;
735 /* let pool_destroy know we're all done */
736 if (pool->ltp_open_count < 1)
737 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
739 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
741 ldap_pvt_thread_exit(NULL);
746 ldap_pvt_thread_pool_pause (
747 ldap_pvt_thread_pool_t *tpool )
749 struct ldap_int_thread_pool_s *pool;
759 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
761 /* If someone else has already requested a pause, we have to wait */
762 while (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
763 pool->ltp_pending_count++;
764 pool->ltp_active_count--;
765 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
766 pool->ltp_pending_count--;
767 pool->ltp_active_count++;
769 /* Wait for everyone else to finish */
770 pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
771 while (pool->ltp_active_count > 1) {
772 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
774 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
779 ldap_pvt_thread_pool_resume (
780 ldap_pvt_thread_pool_t *tpool )
782 struct ldap_int_thread_pool_s *pool;
792 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
794 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
795 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
796 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
800 int ldap_pvt_thread_pool_getkey(
804 ldap_pvt_thread_pool_keyfree_t **kfree )
806 ldap_int_thread_userctx_t *ctx = xctx;
809 if ( !ctx || !data ) return EINVAL;
811 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
812 if ( ctx->ltu_key[i].ltk_key == key ) {
813 *data = ctx->ltu_key[i].ltk_data;
814 if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
821 int ldap_pvt_thread_pool_setkey(
825 ldap_pvt_thread_pool_keyfree_t *kfree )
827 ldap_int_thread_userctx_t *ctx = xctx;
830 if ( !ctx || !key ) return EINVAL;
832 for ( i=0; i<MAXKEYS; i++ ) {
833 if ( !ctx->ltu_key[i].ltk_key || ctx->ltu_key[i].ltk_key == key ) {
834 ctx->ltu_key[i].ltk_key = key;
835 ctx->ltu_key[i].ltk_data = data;
836 ctx->ltu_key[i].ltk_free = kfree;
843 /* Free all elements with this key, no matter which thread they're in.
844 * May only be called while the pool is paused.
846 void ldap_pvt_thread_pool_purgekey( void *key )
849 ldap_int_thread_userctx_t *ctx;
851 for ( i=0; i<LDAP_MAXTHR; i++ ) {
852 if ( thread_keys[i].ctx ) {
853 ctx = thread_keys[i].ctx;
854 for ( j=0; j<MAXKEYS; j++ ) {
855 if ( ctx->ltu_key[j].ltk_key == key ) {
856 if (ctx->ltu_key[j].ltk_free)
857 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
858 ctx->ltu_key[j].ltk_data );
859 ctx->ltu_key[j].ltk_key = NULL;
860 ctx->ltu_key[j].ltk_free = NULL;
869 * This is necessary if the caller does not have access to the
870 * thread context handle (for example, a slapd plugin calling
871 * slapi_search_internal()). No doubt it is more efficient to
872 * for the application to keep track of the thread context
875 void *ldap_pvt_thread_pool_context( )
877 ldap_pvt_thread_t tid;
880 tid = ldap_pvt_thread_self();
881 if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
882 return &ldap_int_main_thrctx;
884 TID_HASH( tid, hash );
885 for (i = hash & (LDAP_MAXTHR-1);
886 !ldap_pvt_thread_equal(thread_keys[i].id, tid_zero) &&
887 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
888 i = (i+1) & (LDAP_MAXTHR-1));
890 return thread_keys[i].ctx;
893 void ldap_pvt_thread_pool_context_reset( void *vctx )
895 ldap_int_thread_userctx_t *ctx = vctx;
898 for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++) {
899 if ( ctx->ltu_key[i].ltk_free )
900 ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
901 ctx->ltu_key[i].ltk_data );
902 ctx->ltu_key[i].ltk_key = NULL;
906 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
908 ldap_int_thread_userctx_t *ctx = vctx;
912 #endif /* LDAP_THREAD_HAVE_TPOOL */