1 /* $OpenLDAP: pkg/ldap/libraries/libldap_r/tpool.c,v 1.52.2.13 2008/03/21 00:46:03 hyc Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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/signal.h>
21 #include <ac/stdarg.h>
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
28 #include "ldap_pvt_thread.h" /* Get the thread interface */
29 #include "ldap_queue.h"
30 #define LDAP_THREAD_POOL_IMPLEMENTATION
31 #include "ldap_thr_debug.h" /* May rename symbols defined below */
33 #ifndef LDAP_THREAD_HAVE_TPOOL
35 /* Thread-specific key with data and optional free function */
36 typedef struct ldap_int_tpool_key_s
{
39 ldap_pvt_thread_pool_keyfree_t
*ltk_free
;
40 } ldap_int_tpool_key_t
;
42 /* Max number of thread-specific keys we store per thread.
43 * We don't expect to use many...
47 /* Max number of threads */
48 #define LDAP_MAXTHR 1024 /* must be a power of 2 */
50 /* (Theoretical) max number of pending requests */
51 #define MAX_PENDING (INT_MAX/2) /* INT_MAX - (room to avoid overflow) */
53 /* Context: thread ID and thread-specific key/data pairs */
54 typedef struct ldap_int_thread_userctx_s
{
55 ldap_pvt_thread_t ltu_id
;
56 ldap_int_tpool_key_t ltu_key
[MAXKEYS
];
57 } ldap_int_thread_userctx_t
;
60 /* Simple {thread ID -> context} hash table; key=ctx->ltu_id.
61 * Protected by ldap_pvt_thread_pool_mutex except during pauses,
62 * when it is read-only (used by pool_purgekey and pool_context).
63 * Protected by tpool->ltp_mutex during pauses.
66 ldap_int_thread_userctx_t
*ctx
;
67 /* ctx is valid when not NULL or DELETED_THREAD_CTX */
68 # define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
69 } thread_keys
[LDAP_MAXTHR
];
71 #define TID_HASH(tid, hash) do { \
72 unsigned const char *ptr_ = (unsigned const char *)&(tid); \
74 for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
75 (hash) += ((hash) << 5) ^ ptr_[i_]; \
79 /* Task for a thread to perform */
80 typedef struct ldap_int_thread_task_s
{
82 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s
) q
;
83 LDAP_SLIST_ENTRY(ldap_int_thread_task_s
) l
;
85 ldap_pvt_thread_start_t
*ltt_start_routine
;
87 } ldap_int_thread_task_t
;
89 typedef LDAP_STAILQ_HEAD(tcq
, ldap_int_thread_task_s
) ldap_int_tpool_plist_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 /* ltp_pause == 0 ? <p_pending_list : &empty_pending_list,
104 * maintaned to reduce work for pool_wrapper()
106 ldap_int_tpool_plist_t
*ltp_work_list
;
108 /* pending tasks, and unused task objects */
109 ldap_int_tpool_plist_t ltp_pending_list
;
110 LDAP_SLIST_HEAD(tcl
, ldap_int_thread_task_s
) ltp_free_list
;
112 /* The pool is finishing, waiting for its threads to close.
113 * They close when ltp_pending_list is done. pool_submit()
114 * rejects new tasks. ltp_max_pending = -(its old value).
118 /* Some active task needs to be the sole active task.
119 * Atomic variable so ldap_pvt_thread_pool_pausing() can read it.
120 * Note: Pauses adjust ltp_<open_count/vary_open_count/work_list>,
121 * so pool_<submit/wrapper>() mostly can avoid testing ltp_pause.
123 volatile sig_atomic_t ltp_pause
;
125 /* Max number of threads in pool, or 0 for default (LDAP_MAXTHR) */
128 /* Max number of pending + paused requests, negated when ltp_finishing */
131 int ltp_pending_count
; /* Pending or paused requests */
132 int ltp_active_count
; /* Active, not paused requests */
133 int ltp_open_count
; /* Number of threads, negated when ltp_pause */
134 int ltp_starting
; /* Currenlty starting threads */
136 /* >0 if paused or we may open a thread, <0 if we should close a thread.
137 * Updated when ltp_<finishing/pause/max_count/open_count> change.
138 * Maintained to reduce the time ltp_mutex must be locked in
139 * ldap_pvt_thread_pool_<submit/wrapper>().
141 int ltp_vary_open_count
;
142 # define SET_VARY_OPEN_COUNT(pool) \
143 ((pool)->ltp_vary_open_count = \
144 (pool)->ltp_pause ? 1 : \
145 (pool)->ltp_finishing ? -1 : \
146 ((pool)->ltp_max_count ? (pool)->ltp_max_count : LDAP_MAXTHR) \
147 - (pool)->ltp_open_count)
150 static ldap_int_tpool_plist_t empty_pending_list
=
151 LDAP_STAILQ_HEAD_INITIALIZER(empty_pending_list
);
153 static int ldap_int_has_thread_pool
= 0;
154 static LDAP_STAILQ_HEAD(tpq
, ldap_int_thread_pool_s
)
155 ldap_int_thread_pool_list
=
156 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list
);
158 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex
;
160 static void *ldap_int_thread_pool_wrapper( void *pool
);
162 static ldap_pvt_thread_key_t ldap_tpool_key
;
164 /* Context of the main thread */
165 static ldap_int_thread_userctx_t ldap_int_main_thrctx
;
168 ldap_int_thread_pool_startup ( void )
170 ldap_int_main_thrctx
.ltu_id
= ldap_pvt_thread_self();
171 ldap_pvt_thread_key_create( &ldap_tpool_key
);
172 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex
);
176 ldap_int_thread_pool_shutdown ( void )
178 struct ldap_int_thread_pool_s
*pool
;
180 while ((pool
= LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list
)) != NULL
) {
181 (ldap_pvt_thread_pool_destroy
)(&pool
, 0); /* ignore thr_debug macro */
183 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex
);
184 ldap_pvt_thread_key_destroy( ldap_tpool_key
);
189 /* Create a thread pool */
191 ldap_pvt_thread_pool_init (
192 ldap_pvt_thread_pool_t
*tpool
,
196 ldap_pvt_thread_pool_t pool
;
199 /* multiple pools are currently not supported (ITS#4943) */
200 assert(!ldap_int_has_thread_pool
);
202 if (! (0 <= max_threads
&& max_threads
<= LDAP_MAXTHR
))
204 if (! (1 <= max_pending
&& max_pending
<= MAX_PENDING
))
205 max_pending
= MAX_PENDING
;
208 pool
= (ldap_pvt_thread_pool_t
) LDAP_CALLOC(1,
209 sizeof(struct ldap_int_thread_pool_s
));
211 if (pool
== NULL
) return(-1);
213 rc
= ldap_pvt_thread_mutex_init(&pool
->ltp_mutex
);
216 rc
= ldap_pvt_thread_cond_init(&pool
->ltp_cond
);
219 rc
= ldap_pvt_thread_cond_init(&pool
->ltp_pcond
);
223 ldap_int_has_thread_pool
= 1;
225 pool
->ltp_max_count
= max_threads
;
226 SET_VARY_OPEN_COUNT(pool
);
227 pool
->ltp_max_pending
= max_pending
;
229 LDAP_STAILQ_INIT(&pool
->ltp_pending_list
);
230 pool
->ltp_work_list
= &pool
->ltp_pending_list
;
231 LDAP_SLIST_INIT(&pool
->ltp_free_list
);
233 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex
);
234 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list
, pool
, ltp_next
);
235 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex
);
238 /* THIS WILL NOT WORK on some systems. If the process
239 * forks after starting a thread, there is no guarantee
240 * that the thread will survive the fork. For example,
241 * slapd forks in order to daemonize, and does so after
242 * calling ldap_pvt_thread_pool_init. On some systems,
243 * this initial thread does not run in the child process,
244 * but ltp_open_count == 1, so two things happen:
245 * 1) the first client connection fails, and 2) when
246 * slapd is kill'ed, it never terminates since it waits
247 * for all worker threads to exit. */
249 /* start up one thread, just so there is one. no need to
250 * lock the mutex right now, since no threads are running.
252 pool
->ltp_open_count
++;
253 SET_VARY_OPEN_COUNT(pool
);
255 ldap_pvt_thread_t thr
;
256 rc
= ldap_pvt_thread_create( &thr
, 1, ldap_int_thread_pool_wrapper
, pool
);
259 /* couldn't start one? then don't start any */
260 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex
);
261 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list
, pool
,
262 ldap_int_thread_pool_s
, ltp_next
);
263 ldap_int_has_thread_pool
= 0;
264 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex
);
265 ldap_pvt_thread_cond_destroy(&pool
->ltp_pcond
);
266 ldap_pvt_thread_cond_destroy(&pool
->ltp_cond
);
267 ldap_pvt_thread_mutex_destroy(&pool
->ltp_mutex
);
278 /* Submit a task to be performed by the thread pool */
280 ldap_pvt_thread_pool_submit (
281 ldap_pvt_thread_pool_t
*tpool
,
282 ldap_pvt_thread_start_t
*start_routine
, void *arg
)
284 struct ldap_int_thread_pool_s
*pool
;
285 ldap_int_thread_task_t
*task
;
286 ldap_pvt_thread_t thr
;
296 ldap_pvt_thread_mutex_lock(&pool
->ltp_mutex
);
298 if (pool
->ltp_pending_count
>= pool
->ltp_max_pending
)
301 task
= LDAP_SLIST_FIRST(&pool
->ltp_free_list
);
303 LDAP_SLIST_REMOVE_HEAD(&pool
->ltp_free_list
, ltt_next
.l
);
305 task
= (ldap_int_thread_task_t
*) LDAP_MALLOC(sizeof(*task
));
310 task
->ltt_start_routine
= start_routine
;
313 pool
->ltp_pending_count
++;
314 LDAP_STAILQ_INSERT_TAIL(&pool
->ltp_pending_list
, task
, ltt_next
.q
);
316 /* true if ltp_pause != 0 or we should open (create) a thread */
317 if (pool
->ltp_vary_open_count
> 0 &&
318 pool
->ltp_open_count
< pool
->ltp_active_count
+pool
->ltp_pending_count
)
323 pool
->ltp_starting
++;
324 pool
->ltp_open_count
++;
325 SET_VARY_OPEN_COUNT(pool
);
327 if (0 != ldap_pvt_thread_create(
328 &thr
, 1, ldap_int_thread_pool_wrapper
, pool
))
330 /* couldn't create thread. back out of
331 * ltp_open_count and check for even worse things.
333 pool
->ltp_starting
--;
334 pool
->ltp_open_count
--;
335 SET_VARY_OPEN_COUNT(pool
);
337 if (pool
->ltp_open_count
== 0) {
338 /* no open threads at all?!?
340 ldap_int_thread_task_t
*ptr
;
342 /* let pool_destroy know there are no more threads */
343 ldap_pvt_thread_cond_signal(&pool
->ltp_cond
);
345 LDAP_STAILQ_FOREACH(ptr
, &pool
->ltp_pending_list
, ltt_next
.q
)
346 if (ptr
== task
) break;
348 /* no open threads, task not handled, so
349 * back out of ltp_pending_count, free the task,
352 pool
->ltp_pending_count
--;
353 LDAP_STAILQ_REMOVE(&pool
->ltp_pending_list
, task
,
354 ldap_int_thread_task_s
, ltt_next
.q
);
355 LDAP_SLIST_INSERT_HEAD(&pool
->ltp_free_list
, task
,
360 /* there is another open thread, so this
361 * task will be handled eventually.
365 ldap_pvt_thread_cond_signal(&pool
->ltp_cond
);
368 ldap_pvt_thread_mutex_unlock(&pool
->ltp_mutex
);
372 ldap_pvt_thread_mutex_unlock(&pool
->ltp_mutex
);
376 /* Set max #threads. value <= 0 means max supported #threads (LDAP_MAXTHR) */
378 ldap_pvt_thread_pool_maxthreads(
379 ldap_pvt_thread_pool_t
*tpool
,
382 struct ldap_int_thread_pool_s
*pool
;
384 if (! (0 <= max_threads
&& max_threads
<= LDAP_MAXTHR
))
395 ldap_pvt_thread_mutex_lock(&pool
->ltp_mutex
);
397 pool
->ltp_max_count
= max_threads
;
398 SET_VARY_OPEN_COUNT(pool
);
400 ldap_pvt_thread_mutex_unlock(&pool
->ltp_mutex
);
404 /* Inspect the pool */
406 ldap_pvt_thread_pool_query(
407 ldap_pvt_thread_pool_t
*tpool
,
408 ldap_pvt_thread_pool_param_t param
,
411 struct ldap_int_thread_pool_s
*pool
;
414 if ( tpool
== NULL
|| value
== NULL
) {
420 if ( pool
== NULL
) {
424 ldap_pvt_thread_mutex_lock(&pool
->ltp_mutex
);
426 case LDAP_PVT_THREAD_POOL_PARAM_MAX
:
427 count
= pool
->ltp_max_count
;
430 case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING
:
431 count
= pool
->ltp_max_pending
;
434 if (count
== MAX_PENDING
)
438 case LDAP_PVT_THREAD_POOL_PARAM_OPEN
:
439 count
= pool
->ltp_open_count
;
444 case LDAP_PVT_THREAD_POOL_PARAM_STARTING
:
445 count
= pool
->ltp_starting
;
448 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE
:
449 count
= pool
->ltp_active_count
;
452 case LDAP_PVT_THREAD_POOL_PARAM_PAUSING
:
453 count
= pool
->ltp_pause
;
456 case LDAP_PVT_THREAD_POOL_PARAM_PENDING
:
457 count
= pool
->ltp_pending_count
;
460 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD
:
461 count
= pool
->ltp_pending_count
+ pool
->ltp_active_count
;
464 case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX
:
467 case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX
:
470 case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX
:
473 case LDAP_PVT_THREAD_POOL_PARAM_STATE
:
475 pool
->ltp_pause
? "pausing" :
476 !pool
->ltp_finishing
? "running" :
477 pool
->ltp_pending_count
? "finishing" : "stopping";
480 case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN
:
483 ldap_pvt_thread_mutex_unlock( &pool
->ltp_mutex
);
486 *((int *)value
) = count
;
489 return ( count
== -1 ? -1 : 0 );
493 * true if pool is pausing; does not lock any mutex to check.
494 * 0 if not pause, 1 if pause, -1 if error or no pool.
497 ldap_pvt_thread_pool_pausing( ldap_pvt_thread_pool_t
*tpool
)
500 struct ldap_int_thread_pool_s
*pool
;
502 if ( tpool
!= NULL
&& (pool
= *tpool
) != NULL
) {
503 rc
= pool
->ltp_pause
;
510 * wrapper for ldap_pvt_thread_pool_query(), left around
511 * for backwards compatibility
514 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t
*tpool
)
518 rc
= ldap_pvt_thread_pool_query( tpool
,
519 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD
, (void *)&count
);
528 /* Destroy the pool after making its threads finish */
530 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t
*tpool
, int run_pending
)
532 struct ldap_int_thread_pool_s
*pool
, *pptr
;
533 ldap_int_thread_task_t
*task
;
540 if (pool
== NULL
) return(-1);
542 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex
);
543 LDAP_STAILQ_FOREACH(pptr
, &ldap_int_thread_pool_list
, ltp_next
)
544 if (pptr
== pool
) break;
546 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list
, pool
,
547 ldap_int_thread_pool_s
, ltp_next
);
548 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex
);
550 if (pool
!= pptr
) return(-1);
552 ldap_pvt_thread_mutex_lock(&pool
->ltp_mutex
);
554 pool
->ltp_finishing
= 1;
555 SET_VARY_OPEN_COUNT(pool
);
556 if (pool
->ltp_max_pending
> 0)
557 pool
->ltp_max_pending
= -pool
->ltp_max_pending
;
560 while ((task
= LDAP_STAILQ_FIRST(&pool
->ltp_pending_list
)) != NULL
) {
561 LDAP_STAILQ_REMOVE_HEAD(&pool
->ltp_pending_list
, ltt_next
.q
);
564 pool
->ltp_pending_count
= 0;
567 while (pool
->ltp_open_count
) {
568 if (!pool
->ltp_pause
)
569 ldap_pvt_thread_cond_broadcast(&pool
->ltp_cond
);
570 ldap_pvt_thread_cond_wait(&pool
->ltp_cond
, &pool
->ltp_mutex
);
573 while ((task
= LDAP_SLIST_FIRST(&pool
->ltp_free_list
)) != NULL
)
575 LDAP_SLIST_REMOVE_HEAD(&pool
->ltp_free_list
, ltt_next
.l
);
579 ldap_pvt_thread_mutex_unlock(&pool
->ltp_mutex
);
580 ldap_pvt_thread_cond_destroy(&pool
->ltp_pcond
);
581 ldap_pvt_thread_cond_destroy(&pool
->ltp_cond
);
582 ldap_pvt_thread_mutex_destroy(&pool
->ltp_mutex
);
585 ldap_int_has_thread_pool
= 0;
589 /* Thread loop. Accept and handle submitted tasks. */
591 ldap_int_thread_pool_wrapper (
594 struct ldap_int_thread_pool_s
*pool
= xpool
;
595 ldap_int_thread_task_t
*task
;
596 ldap_int_tpool_plist_t
*work_list
;
597 ldap_int_thread_userctx_t ctx
, *kctx
;
598 unsigned i
, keyslot
, hash
;
600 assert(pool
!= NULL
);
602 for ( i
=0; i
<MAXKEYS
; i
++ ) {
603 ctx
.ltu_key
[i
].ltk_key
= NULL
;
606 ctx
.ltu_id
= ldap_pvt_thread_self();
607 TID_HASH(ctx
.ltu_id
, hash
);
609 ldap_pvt_thread_key_setdata( ldap_tpool_key
, &ctx
);
611 ldap_pvt_thread_mutex_lock(&pool
->ltp_mutex
);
613 /* thread_keys[] is read-only when paused */
614 while (pool
->ltp_pause
)
615 ldap_pvt_thread_cond_wait(&pool
->ltp_cond
, &pool
->ltp_mutex
);
617 /* find a key slot to give this thread ID and store a
618 * pointer to our keys there; start at the thread ID
619 * itself (mod LDAP_MAXTHR) and look for an empty slot.
621 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex
);
622 for (keyslot
= hash
& (LDAP_MAXTHR
-1);
623 (kctx
= thread_keys
[keyslot
].ctx
) && kctx
!= DELETED_THREAD_CTX
;
624 keyslot
= (keyslot
+1) & (LDAP_MAXTHR
-1));
625 thread_keys
[keyslot
].ctx
= &ctx
;
626 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex
);
628 pool
->ltp_starting
--;
631 work_list
= pool
->ltp_work_list
; /* help the compiler a bit */
632 task
= LDAP_STAILQ_FIRST(work_list
);
633 if (task
== NULL
) { /* paused or no pending tasks */
634 if (pool
->ltp_vary_open_count
< 0) {
635 /* not paused, and either finishing or too many
636 * threads running (can happen if ltp_max_count
637 * was reduced) so let this thread die.
642 /* we could check an idle timer here, and let the
643 * thread die if it has been inactive for a while.
644 * only die if there are other open threads (i.e.,
645 * always have at least one thread open). the check
646 * should be like this:
647 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
648 * check timer, wait if ltp_pause, leave thread (break;)
650 * Just use pthread_cond_timedwait if we want to
654 ldap_pvt_thread_cond_wait(&pool
->ltp_cond
, &pool
->ltp_mutex
);
658 LDAP_STAILQ_REMOVE_HEAD(work_list
, ltt_next
.q
);
659 pool
->ltp_pending_count
--;
660 pool
->ltp_active_count
++;
661 ldap_pvt_thread_mutex_unlock(&pool
->ltp_mutex
);
663 task
->ltt_start_routine(&ctx
, task
->ltt_arg
);
665 ldap_pvt_thread_mutex_lock(&pool
->ltp_mutex
);
666 LDAP_SLIST_INSERT_HEAD(&pool
->ltp_free_list
, task
, ltt_next
.l
);
667 pool
->ltp_active_count
--;
668 /* let pool_pause know when it is the sole active thread */
669 if (pool
->ltp_active_count
< 2)
670 ldap_pvt_thread_cond_signal(&pool
->ltp_pcond
);
673 assert(!pool
->ltp_pause
); /* thread_keys writable, ltp_open_count >= 0 */
675 /* The ltp_mutex lock protects ctx->ltu_key from pool_purgekey()
676 * during this call, since it prevents new pauses. */
677 ldap_pvt_thread_pool_context_reset(&ctx
);
679 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex
);
680 thread_keys
[keyslot
].ctx
= DELETED_THREAD_CTX
;
681 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex
);
683 pool
->ltp_open_count
--;
684 SET_VARY_OPEN_COUNT(pool
);
685 /* let pool_destroy know we're all done */
686 if (pool
->ltp_open_count
== 0)
687 ldap_pvt_thread_cond_signal(&pool
->ltp_cond
);
689 ldap_pvt_thread_mutex_unlock(&pool
->ltp_mutex
);
691 ldap_pvt_thread_exit(NULL
);
696 handle_pause( ldap_pvt_thread_pool_t
*tpool
, int do_pause
)
698 struct ldap_int_thread_pool_s
*pool
;
708 if (! (do_pause
|| pool
->ltp_pause
))
711 ldap_pvt_thread_mutex_lock(&pool
->ltp_mutex
);
713 /* If someone else has already requested a pause, we have to wait */
714 if (pool
->ltp_pause
) {
715 pool
->ltp_pending_count
++;
716 pool
->ltp_active_count
--;
717 /* let the other pool_pause() know when it can proceed */
718 if (pool
->ltp_active_count
< 2)
719 ldap_pvt_thread_cond_signal(&pool
->ltp_pcond
);
721 ldap_pvt_thread_cond_wait(&pool
->ltp_cond
, &pool
->ltp_mutex
);
722 } while (pool
->ltp_pause
);
723 pool
->ltp_pending_count
--;
724 pool
->ltp_active_count
++;
728 /* Wait for everyone else to pause or finish */
730 /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test,
731 * and do not finish threads in ldap_pvt_thread_pool_wrapper() */
732 pool
->ltp_open_count
= -pool
->ltp_open_count
;
733 SET_VARY_OPEN_COUNT(pool
);
734 /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
735 pool
->ltp_work_list
= &empty_pending_list
;
737 while (pool
->ltp_active_count
> 1) {
738 ldap_pvt_thread_cond_wait(&pool
->ltp_pcond
, &pool
->ltp_mutex
);
742 ldap_pvt_thread_mutex_unlock(&pool
->ltp_mutex
);
747 * If a pause was requested, wait for it. If several threads
748 * are waiting to pause, let through one or more pauses.
749 * Return 1 if we waited, 0 if not, -1 at parameter error.
752 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t
*tpool
)
754 return handle_pause( tpool
, 0 );
757 /* Pause the pool. Return when all other threads are paused. */
759 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t
*tpool
)
761 return handle_pause( tpool
, 1 );
766 ldap_pvt_thread_pool_resume (
767 ldap_pvt_thread_pool_t
*tpool
)
769 struct ldap_int_thread_pool_s
*pool
;
779 ldap_pvt_thread_mutex_lock(&pool
->ltp_mutex
);
781 assert(pool
->ltp_pause
);
783 if (pool
->ltp_open_count
<= 0) /* true when paused, but be paranoid */
784 pool
->ltp_open_count
= -pool
->ltp_open_count
;
785 SET_VARY_OPEN_COUNT(pool
);
786 pool
->ltp_work_list
= &pool
->ltp_pending_list
;
788 if (!pool
->ltp_finishing
)
789 ldap_pvt_thread_cond_broadcast(&pool
->ltp_cond
);
791 ldap_pvt_thread_mutex_unlock(&pool
->ltp_mutex
);
796 * Get the key's data and optionally free function in the given context.
798 int ldap_pvt_thread_pool_getkey(
802 ldap_pvt_thread_pool_keyfree_t
**kfree
)
804 ldap_int_thread_userctx_t
*ctx
= xctx
;
807 if ( !ctx
|| !key
|| !data
) return EINVAL
;
809 for ( i
=0; i
<MAXKEYS
&& ctx
->ltu_key
[i
].ltk_key
; i
++ ) {
810 if ( ctx
->ltu_key
[i
].ltk_key
== key
) {
811 *data
= ctx
->ltu_key
[i
].ltk_data
;
812 if ( kfree
) *kfree
= ctx
->ltu_key
[i
].ltk_free
;
820 clear_key_idx( ldap_int_thread_userctx_t
*ctx
, int i
)
822 for ( ; i
< MAXKEYS
-1 && ctx
->ltu_key
[i
+1].ltk_key
; i
++ )
823 ctx
->ltu_key
[i
] = ctx
->ltu_key
[i
+1];
824 ctx
->ltu_key
[i
].ltk_key
= NULL
;
828 * Set or remove data for the key in the given context.
829 * key can be any unique pointer.
830 * kfree() is an optional function to free the data (but not the key):
831 * pool_context_reset() and pool_purgekey() call kfree(key, data),
832 * but pool_setkey() does not. For pool_setkey() it is the caller's
833 * responsibility to free any existing data with the same key.
834 * kfree() must not call functions taking a tpool argument.
836 int ldap_pvt_thread_pool_setkey(
840 ldap_pvt_thread_pool_keyfree_t
*kfree
,
842 ldap_pvt_thread_pool_keyfree_t
**oldkfreep
)
844 ldap_int_thread_userctx_t
*ctx
= xctx
;
847 if ( !ctx
|| !key
) return EINVAL
;
849 for ( i
=found
=0; i
<MAXKEYS
; i
++ ) {
850 if ( ctx
->ltu_key
[i
].ltk_key
== key
) {
853 } else if ( !ctx
->ltu_key
[i
].ltk_key
) {
860 *olddatap
= ctx
->ltu_key
[i
].ltk_data
;
868 *oldkfreep
= ctx
->ltu_key
[i
].ltk_free
;
874 if ( data
|| kfree
) {
877 ctx
->ltu_key
[i
].ltk_key
= key
;
878 ctx
->ltu_key
[i
].ltk_data
= data
;
879 ctx
->ltu_key
[i
].ltk_free
= kfree
;
880 } else if ( found
) {
881 clear_key_idx( ctx
, i
);
887 /* Free all elements with this key, no matter which thread they're in.
888 * May only be called while the pool is paused.
890 void ldap_pvt_thread_pool_purgekey( void *key
)
893 ldap_int_thread_userctx_t
*ctx
;
895 assert ( key
!= NULL
);
897 for ( i
=0; i
<LDAP_MAXTHR
; i
++ ) {
898 ctx
= thread_keys
[i
].ctx
;
899 if ( ctx
&& ctx
!= DELETED_THREAD_CTX
) {
900 for ( j
=0; j
<MAXKEYS
&& ctx
->ltu_key
[j
].ltk_key
; j
++ ) {
901 if ( ctx
->ltu_key
[j
].ltk_key
== key
) {
902 if (ctx
->ltu_key
[j
].ltk_free
)
903 ctx
->ltu_key
[j
].ltk_free( ctx
->ltu_key
[j
].ltk_key
,
904 ctx
->ltu_key
[j
].ltk_data
);
905 clear_key_idx( ctx
, j
);
914 * Find the context of the current thread.
915 * This is necessary if the caller does not have access to the
916 * thread context handle (for example, a slapd plugin calling
917 * slapi_search_internal()). No doubt it is more efficient
918 * for the application to keep track of the thread context
921 void *ldap_pvt_thread_pool_context( )
925 ldap_pvt_thread_key_getdata( ldap_tpool_key
, &ctx
);
926 return ctx
? ctx
: (void *) &ldap_int_main_thrctx
;
930 * Free the context's keys.
931 * Must not call functions taking a tpool argument (because this
932 * thread already holds ltp_mutex when called from pool_wrapper()).
934 void ldap_pvt_thread_pool_context_reset( void *vctx
)
936 ldap_int_thread_userctx_t
*ctx
= vctx
;
939 for ( i
=MAXKEYS
-1; i
>=0; i
--) {
940 if ( !ctx
->ltu_key
[i
].ltk_key
)
942 if ( ctx
->ltu_key
[i
].ltk_free
)
943 ctx
->ltu_key
[i
].ltk_free( ctx
->ltu_key
[i
].ltk_key
,
944 ctx
->ltu_key
[i
].ltk_data
);
945 ctx
->ltu_key
[i
].ltk_key
= NULL
;
949 ldap_pvt_thread_t
ldap_pvt_thread_pool_tid( void *vctx
)
951 ldap_int_thread_userctx_t
*ctx
= vctx
;
955 #endif /* LDAP_THREAD_HAVE_TPOOL */