2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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 static ldap_pvt_thread_t tid_zero;
57 ldap_int_thread_key_t *ctx;
58 } thread_keys[LDAP_MAXTHR];
61 typedef struct ldap_int_thread_ctx_s {
63 LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
64 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
65 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
67 ldap_pvt_thread_start_t *ltc_start_routine;
69 } ldap_int_thread_ctx_t;
71 struct ldap_int_thread_pool_s {
72 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
73 ldap_pvt_thread_mutex_t ltp_mutex;
74 ldap_pvt_thread_cond_t ltp_cond;
75 ldap_pvt_thread_cond_t ltp_pcond;
76 LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
77 LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
78 LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
79 ldap_int_thread_pool_state_t ltp_state;
82 long ltp_pending_count;
83 long ltp_active_count;
88 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
89 ldap_int_thread_pool_list =
90 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
92 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
94 static void *ldap_int_thread_pool_wrapper( void *pool );
96 static ldap_pvt_thread_t ldap_int_main_tid;
98 static ldap_int_thread_key_t ldap_int_main_thrctx[LDAP_MAXTHR];
101 ldap_int_thread_pool_startup ( void )
103 ldap_int_main_tid = ldap_pvt_thread_self();
105 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
109 ldap_int_thread_pool_shutdown ( void )
111 struct ldap_int_thread_pool_s *pool;
113 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
114 LDAP_STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
115 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
117 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
121 typedef struct ldap_lazy_sem_t {
122 ldap_pvt_thread_mutex_t ls_mutex;
123 ldap_pvt_thread_cond_t ls_cond;
126 * when more than ls_lazy_count number of resources
127 * becmoes available, the thread wating for the resources will
128 * be waken up in order to prevent frequent blocking/waking-up
130 unsigned int ls_lazy_count;
132 * only one thread(listener) will wait on this semaphore
133 * using a flag instead of a list
138 ldap_lazy_sem_t* thread_pool_sem = NULL;
141 ldap_lazy_sem_init( unsigned int value, unsigned int lazyness )
143 thread_pool_sem = (ldap_lazy_sem_t*) LDAP_CALLOC(1,
144 sizeof( ldap_lazy_sem_t ));
146 if( thread_pool_sem == NULL ) return -1;
148 ldap_pvt_thread_mutex_init( &thread_pool_sem->ls_mutex );
149 ldap_pvt_thread_cond_init( &thread_pool_sem->ls_cond );
150 thread_pool_sem->ls_sem_value = value;
151 thread_pool_sem->ls_lazy_count = lazyness;
152 thread_pool_sem->ls_wait = 0;
157 /* FIXME: move to some approprite header */
158 int ldap_lazy_sem_dec( ldap_lazy_sem_t* ls );
159 int ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls );
162 * ldap_lazy_sem_wait is used if a caller is blockable(listener).
163 * Otherwise use ldap_lazy_sem_dec (worker)
166 ldap_lazy_sem_op_submit( ldap_lazy_sem_t* ls )
168 if ( ls == NULL ) return -1;
170 /* only worker thread has its thread ctx */
171 if ( ldap_pvt_thread_pool_context() ) {
173 return ldap_lazy_sem_dec( ls );
176 return ldap_lazy_sem_wait( ls );
181 * test if given semaphore's count is zero.
182 * If 0, the caller is blocked
183 * If not, the count is decremented.
186 ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls )
188 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
191 if ( ls->ls_sem_value <= 0 ) {
192 /* no more avaliable resources */
194 ldap_pvt_thread_cond_wait( &ls->ls_cond, &ls->ls_mutex );
197 /* avaliable resources */
201 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
207 * decrement the count without blocking
208 * even when the count becomes less than or equal to 0
211 ldap_lazy_sem_dec( ldap_lazy_sem_t* ls )
213 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
217 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
223 * Increment the count by one and test if it is greater or
224 * equal to lazyness. If it is, wake up a blocked thread.
227 ldap_lazy_sem_post( ldap_lazy_sem_t* ls )
229 if( ls == NULL ) return (-1);
231 ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
235 if ( ls->ls_sem_value >= ls->ls_lazy_count ) {
237 ldap_pvt_thread_cond_signal( &ls->ls_cond );
241 ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
247 ldap_pvt_thread_pool_init (
248 ldap_pvt_thread_pool_t *tpool,
252 ldap_pvt_thread_pool_t pool;
256 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
257 sizeof(struct ldap_int_thread_pool_s));
259 if (pool == NULL) return(-1);
261 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
264 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
267 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
270 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
271 pool->ltp_max_count = max_threads;
272 pool->ltp_max_pending = max_pending;
273 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
274 LDAP_SLIST_INIT(&pool->ltp_free_list);
275 LDAP_SLIST_INIT(&pool->ltp_active_list);
276 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
277 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
278 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
281 /* THIS WILL NOT WORK on some systems. If the process
282 * forks after starting a thread, there is no guarantee
283 * that the thread will survive the fork. For example,
284 * slapd forks in order to daemonize, and does so after
285 * calling ldap_pvt_thread_pool_init. On some systems,
286 * this initial thread does not run in the child process,
287 * but ltp_open_count == 1, so two things happen:
288 * 1) the first client connection fails, and 2) when
289 * slapd is kill'ed, it never terminates since it waits
290 * for all worker threads to exit. */
292 /* start up one thread, just so there is one. no need to
293 * lock the mutex right now, since no threads are running.
295 pool->ltp_open_count++;
297 ldap_pvt_thread_t thr;
298 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
301 /* couldn't start one? then don't start any */
302 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
303 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
304 ldap_int_thread_pool_s, ltp_next);
305 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
306 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
307 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
308 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
318 #define TID_HASH(tid, hash) do { unsigned i; \
319 unsigned char *ptr = (unsigned char *)&(tid); \
320 for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
323 ldap_pvt_thread_pool_submit (
324 ldap_pvt_thread_pool_t *tpool,
325 ldap_pvt_thread_start_t *start_routine, void *arg )
327 struct ldap_int_thread_pool_s *pool;
328 ldap_int_thread_ctx_t *ctx;
330 ldap_pvt_thread_t thr;
340 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
341 if ((pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING &&
342 pool->ltp_state != LDAP_INT_THREAD_POOL_PAUSING)
343 || (pool->ltp_max_pending > 0
344 && pool->ltp_pending_count >= pool->ltp_max_pending))
346 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
349 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
351 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
353 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
354 sizeof(ldap_int_thread_ctx_t));
356 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
361 ctx->ltc_start_routine = start_routine;
364 pool->ltp_pending_count++;
365 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
366 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
367 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
370 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
371 if ((pool->ltp_open_count <= 0
373 || pool->ltp_pending_count > 1
375 || pool->ltp_open_count == pool->ltp_active_count)
376 && (pool->ltp_max_count <= 0
377 || pool->ltp_open_count < pool->ltp_max_count))
379 pool->ltp_open_count++;
380 pool->ltp_starting++;
383 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
385 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
386 ldap_lazy_sem_op_submit( thread_pool_sem );
392 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
394 rc = ldap_pvt_thread_create( &thr, 1,
395 ldap_int_thread_pool_wrapper, pool );
398 pool->ltp_starting--;
400 /* assign this thread ID to a key slot; start
401 * at the thread ID itself (mod LDAP_MAXTHR) and
402 * look for an empty slot.
405 for (rc = hash & (LDAP_MAXTHR-1);
406 !ldap_pvt_thread_equal(thread_keys[rc].id, tid_zero);
407 rc = (rc+1) & (LDAP_MAXTHR-1));
408 thread_keys[rc].id = thr;
410 /* couldn't create thread. back out of
411 * ltp_open_count and check for even worse things.
413 pool->ltp_open_count--;
414 pool->ltp_starting--;
415 if (pool->ltp_open_count == 0) {
416 /* no open threads at all?!?
418 ldap_int_thread_ctx_t *ptr;
419 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
420 if (ptr == ctx) break;
422 /* no open threads, context not handled, so
423 * back out of ltp_pending_count, free the context,
426 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
427 ldap_int_thread_ctx_s, ltc_next.q);
428 pool->ltp_pending_count++;
429 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
434 /* there is another open thread, so this
435 * context will be handled eventually.
436 * continue on and signal that the context
440 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
447 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
449 struct ldap_int_thread_pool_s *pool;
459 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
460 pool->ltp_max_count = max_threads;
461 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
466 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
468 struct ldap_int_thread_pool_s *pool;
479 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
480 count = pool->ltp_pending_count + pool->ltp_active_count;
481 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
486 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
488 struct ldap_int_thread_pool_s *pool, *pptr;
490 ldap_int_thread_ctx_t *ctx;
497 if (pool == NULL) return(-1);
499 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
500 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
501 if (pptr == pool) break;
503 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
504 ldap_int_thread_pool_s, ltp_next);
505 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
507 if (pool != pptr) return(-1);
509 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
510 pool->ltp_state = run_pending
511 ? LDAP_INT_THREAD_POOL_FINISHING
512 : LDAP_INT_THREAD_POOL_STOPPING;
514 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
515 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
518 ldap_pvt_thread_yield();
519 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
520 waiting = pool->ltp_open_count;
521 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
522 } while (waiting > 0);
524 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
526 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
530 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
532 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
536 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
537 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
538 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
540 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
541 if ( thread_pool_sem ) {
542 LDAP_FREE( thread_pool_sem );
549 ldap_int_thread_pool_wrapper (
552 struct ldap_int_thread_pool_s *pool = xpool;
553 ldap_int_thread_ctx_t *ctx;
554 ldap_int_thread_key_t ltc_key[MAXKEYS];
555 ldap_pvt_thread_t tid;
556 int i, keyslot, hash;
561 for ( i=0; i<MAXKEYS; i++ ) {
562 ltc_key[i].ltk_key = NULL;
565 tid = ldap_pvt_thread_self();
567 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
569 /* store pointer to our keys */
571 for (i = hash & (LDAP_MAXTHR-1);
572 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
573 i = (i+1) & (LDAP_MAXTHR-1));
574 thread_keys[i].ctx = ltc_key;
577 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
578 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
580 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
582 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
584 if (pool->ltp_max_count > 0
585 && pool->ltp_open_count > pool->ltp_max_count)
587 /* too many threads running (can happen if the
588 * maximum threads value is set during ongoing
589 * operation using ldap_pvt_thread_pool_maxthreads)
590 * so let this thread die.
595 /* we could check an idle timer here, and let the
596 * thread die if it has been inactive for a while.
597 * only die if there are other open threads (i.e.,
598 * always have at least one thread open). the check
599 * should be like this:
600 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
601 * check timer, leave thread (break;)
604 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING
605 || pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING)
607 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
613 pool->ltp_pending_count--;
615 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
616 pool->ltp_active_count++;
617 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
619 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
621 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
622 ldap_lazy_sem_post( thread_pool_sem );
624 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
625 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
626 ldap_int_thread_ctx_s, ltc_next.al);
627 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
628 pool->ltp_active_count--;
630 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
631 if (pool->ltp_active_count < 2) {
632 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
634 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
636 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
638 ldap_pvt_thread_yield();
640 /* if we use an idle timer, here's
641 * a good place to update it
644 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
647 for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
648 if (ltc_key[i].ltk_free)
651 ltc_key[i].ltk_data );
654 thread_keys[keyslot].ctx = NULL;
655 thread_keys[keyslot].id = tid_zero;
657 pool->ltp_open_count--;
658 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
660 ldap_pvt_thread_exit(NULL);
665 ldap_pvt_thread_pool_pause (
666 ldap_pvt_thread_pool_t *tpool )
668 struct ldap_int_thread_pool_s *pool;
678 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
680 /* If someone else has already requested a pause, we have to wait */
681 while (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
682 pool->ltp_pending_count++;
683 pool->ltp_active_count--;
684 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
685 pool->ltp_pending_count--;
686 pool->ltp_active_count++;
688 /* Wait for everyone else to finish */
689 pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
690 while (pool->ltp_active_count > 1) {
691 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
693 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
698 ldap_pvt_thread_pool_resume (
699 ldap_pvt_thread_pool_t *tpool )
701 struct ldap_int_thread_pool_s *pool;
711 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
713 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
714 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
715 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
719 int ldap_pvt_thread_pool_getkey(
723 ldap_pvt_thread_pool_keyfree_t **kfree )
725 ldap_int_thread_key_t *ctx = xctx;
728 if ( !ctx || !data ) return EINVAL;
730 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
731 if ( ctx[i].ltk_key == key ) {
732 *data = ctx[i].ltk_data;
733 if ( kfree ) *kfree = ctx[i].ltk_free;
740 int ldap_pvt_thread_pool_setkey(
744 ldap_pvt_thread_pool_keyfree_t *kfree )
746 ldap_int_thread_key_t *ctx = xctx;
749 if ( !ctx || !key ) return EINVAL;
751 for ( i=0; i<MAXKEYS; i++ ) {
752 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
753 ctx[i].ltk_key = key;
754 ctx[i].ltk_data = data;
755 ctx[i].ltk_free = kfree;
762 /* Free all elements with this key, no matter which thread they're in.
763 * May only be called while the pool is paused.
765 void ldap_pvt_thread_pool_purgekey( void *key )
768 ldap_int_thread_key_t *ctx;
770 for ( i=0; i<LDAP_MAXTHR; i++ ) {
771 if ( thread_keys[i].ctx ) {
772 ctx = thread_keys[i].ctx;
773 for ( j=0; j<MAXKEYS; j++ ) {
774 if ( ctx[j].ltk_key == key ) {
776 ctx[j].ltk_free( ctx[j].ltk_key, ctx[j].ltk_data );
777 ctx[j].ltk_key = NULL;
778 ctx[j].ltk_free = NULL;
787 * This is necessary if the caller does not have access to the
788 * thread context handle (for example, a slapd plugin calling
789 * slapi_search_internal()). No doubt it is more efficient to
790 * for the application to keep track of the thread context
793 void *ldap_pvt_thread_pool_context( )
795 ldap_pvt_thread_t tid;
798 tid = ldap_pvt_thread_self();
799 if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
800 return ldap_int_main_thrctx;
802 TID_HASH( tid, hash );
803 for (i = hash & (LDAP_MAXTHR-1);
804 !ldap_pvt_thread_equal(thread_keys[i].id, tid_zero) &&
805 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
806 i = (i+1) & (LDAP_MAXTHR-1));
808 return thread_keys[i].ctx;
811 void ldap_pvt_thread_pool_context_reset( void *vctx )
813 ldap_int_thread_key_t *ctx = vctx;
816 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++) {
817 if ( ctx[i].ltk_free )
818 ctx[i].ltk_free( ctx[i].ltk_key, ctx[i].ltk_data );
819 ctx[i].ltk_key = NULL;
822 #endif /* LDAP_THREAD_HAVE_TPOOL */