2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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);
122 ldap_pvt_thread_pool_init (
123 ldap_pvt_thread_pool_t *tpool,
127 ldap_pvt_thread_pool_t pool;
131 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
132 sizeof(struct ldap_int_thread_pool_s));
134 if (pool == NULL) return(-1);
136 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
139 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
142 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
145 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
146 pool->ltp_max_count = max_threads;
147 pool->ltp_max_pending = max_pending;
148 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
149 LDAP_SLIST_INIT(&pool->ltp_free_list);
150 LDAP_SLIST_INIT(&pool->ltp_active_list);
151 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
152 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
153 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
156 /* THIS WILL NOT WORK on some systems. If the process
157 * forks after starting a thread, there is no guarantee
158 * that the thread will survive the fork. For example,
159 * slapd forks in order to daemonize, and does so after
160 * calling ldap_pvt_thread_pool_init. On some systems,
161 * this initial thread does not run in the child process,
162 * but ltp_open_count == 1, so two things happen:
163 * 1) the first client connection fails, and 2) when
164 * slapd is kill'ed, it never terminates since it waits
165 * for all worker threads to exit. */
167 /* start up one thread, just so there is one. no need to
168 * lock the mutex right now, since no threads are running.
170 pool->ltp_open_count++;
172 ldap_pvt_thread_t thr;
173 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
176 /* couldn't start one? then don't start any */
177 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
178 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
179 ldap_int_thread_pool_s, ltp_next);
180 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
181 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
182 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
183 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
193 #define TID_HASH(tid, hash) do { unsigned i; \
194 unsigned char *ptr = (unsigned char *)&(tid); \
195 for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
198 ldap_pvt_thread_pool_submit (
199 ldap_pvt_thread_pool_t *tpool,
200 ldap_pvt_thread_start_t *start_routine, void *arg )
202 struct ldap_int_thread_pool_s *pool;
203 ldap_int_thread_ctx_t *ctx;
205 ldap_pvt_thread_t thr;
215 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
216 if ((pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING &&
217 pool->ltp_state != LDAP_INT_THREAD_POOL_PAUSING)
218 || (pool->ltp_max_pending > 0
219 && pool->ltp_pending_count >= pool->ltp_max_pending))
221 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
224 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
226 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
228 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
229 sizeof(ldap_int_thread_ctx_t));
231 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
236 ctx->ltc_start_routine = start_routine;
239 pool->ltp_pending_count++;
240 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
241 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
242 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
245 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
246 if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
247 && (pool->ltp_open_count < pool->ltp_max_count ||
248 pool->ltp_max_count <= 0 ))
250 pool->ltp_open_count++;
251 pool->ltp_starting++;
254 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
259 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
261 rc = ldap_pvt_thread_create( &thr, 1,
262 ldap_int_thread_pool_wrapper, pool );
265 pool->ltp_starting--;
267 /* assign this thread ID to a key slot; start
268 * at the thread ID itself (mod LDAP_MAXTHR) and
269 * look for an empty slot.
272 for (rc = hash & (LDAP_MAXTHR-1);
273 !ldap_pvt_thread_equal(thread_keys[rc].id, tid_zero);
274 rc = (rc+1) & (LDAP_MAXTHR-1));
275 thread_keys[rc].id = thr;
277 /* couldn't create thread. back out of
278 * ltp_open_count and check for even worse things.
280 pool->ltp_open_count--;
281 pool->ltp_starting--;
282 if (pool->ltp_open_count == 0) {
283 /* no open threads at all?!?
285 ldap_int_thread_ctx_t *ptr;
286 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
287 if (ptr == ctx) break;
289 /* no open threads, context not handled, so
290 * back out of ltp_pending_count, free the context,
293 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
294 ldap_int_thread_ctx_s, ltc_next.q);
295 pool->ltp_pending_count++;
296 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
301 /* there is another open thread, so this
302 * context will be handled eventually.
303 * continue on and signal that the context
307 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
314 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
316 struct ldap_int_thread_pool_s *pool;
326 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
327 pool->ltp_max_count = max_threads;
328 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
333 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
335 struct ldap_int_thread_pool_s *pool;
346 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
347 count = pool->ltp_pending_count + pool->ltp_active_count;
348 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
353 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
355 struct ldap_int_thread_pool_s *pool, *pptr;
357 ldap_int_thread_ctx_t *ctx;
364 if (pool == NULL) return(-1);
366 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
367 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
368 if (pptr == pool) break;
370 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
371 ldap_int_thread_pool_s, ltp_next);
372 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
374 if (pool != pptr) return(-1);
376 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
377 pool->ltp_state = run_pending
378 ? LDAP_INT_THREAD_POOL_FINISHING
379 : LDAP_INT_THREAD_POOL_STOPPING;
381 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
382 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
384 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
386 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
390 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
392 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
396 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
397 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
398 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
404 ldap_int_thread_pool_wrapper (
407 struct ldap_int_thread_pool_s *pool = xpool;
408 ldap_int_thread_ctx_t *ctx;
409 ldap_int_thread_key_t ltc_key[MAXKEYS];
410 ldap_pvt_thread_t tid;
411 int i, keyslot, hash;
416 for ( i=0; i<MAXKEYS; i++ ) {
417 ltc_key[i].ltk_key = NULL;
420 tid = ldap_pvt_thread_self();
422 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
424 /* store pointer to our keys */
426 for (i = hash & (LDAP_MAXTHR-1);
427 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
428 i = (i+1) & (LDAP_MAXTHR-1));
429 thread_keys[i].ctx = ltc_key;
432 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
433 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
435 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
437 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
439 if (pool->ltp_max_count > 0
440 && pool->ltp_open_count > pool->ltp_max_count)
442 /* too many threads running (can happen if the
443 * maximum threads value is set during ongoing
444 * operation using ldap_pvt_thread_pool_maxthreads)
445 * so let this thread die.
450 /* we could check an idle timer here, and let the
451 * thread die if it has been inactive for a while.
452 * only die if there are other open threads (i.e.,
453 * always have at least one thread open). the check
454 * should be like this:
455 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
456 * check timer, leave thread (break;)
458 * Just use pthread_cond_timedwait if we want to
462 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING
463 || pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING)
465 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
471 pool->ltp_pending_count--;
473 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
474 pool->ltp_active_count++;
475 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
477 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
479 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
480 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
481 ldap_int_thread_ctx_s, ltc_next.al);
482 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
483 pool->ltp_active_count--;
485 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
486 if (pool->ltp_active_count < 2) {
487 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
489 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
493 for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
494 if (ltc_key[i].ltk_free)
497 ltc_key[i].ltk_data );
500 thread_keys[keyslot].ctx = NULL;
501 thread_keys[keyslot].id = tid_zero;
503 pool->ltp_open_count--;
505 /* let pool_destroy know we're all done */
506 if (pool->ltp_open_count < 1)
507 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
509 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
511 ldap_pvt_thread_exit(NULL);
516 ldap_pvt_thread_pool_pause (
517 ldap_pvt_thread_pool_t *tpool )
519 struct ldap_int_thread_pool_s *pool;
529 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
531 /* If someone else has already requested a pause, we have to wait */
532 while (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
533 pool->ltp_pending_count++;
534 pool->ltp_active_count--;
535 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
536 pool->ltp_pending_count--;
537 pool->ltp_active_count++;
539 /* Wait for everyone else to finish */
540 pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
541 while (pool->ltp_active_count > 1) {
542 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
544 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
549 ldap_pvt_thread_pool_resume (
550 ldap_pvt_thread_pool_t *tpool )
552 struct ldap_int_thread_pool_s *pool;
562 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
564 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
565 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
566 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
570 int ldap_pvt_thread_pool_getkey(
574 ldap_pvt_thread_pool_keyfree_t **kfree )
576 ldap_int_thread_key_t *ctx = xctx;
579 if ( !ctx || !data ) return EINVAL;
581 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
582 if ( ctx[i].ltk_key == key ) {
583 *data = ctx[i].ltk_data;
584 if ( kfree ) *kfree = ctx[i].ltk_free;
591 int ldap_pvt_thread_pool_setkey(
595 ldap_pvt_thread_pool_keyfree_t *kfree )
597 ldap_int_thread_key_t *ctx = xctx;
600 if ( !ctx || !key ) return EINVAL;
602 for ( i=0; i<MAXKEYS; i++ ) {
603 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
604 ctx[i].ltk_key = key;
605 ctx[i].ltk_data = data;
606 ctx[i].ltk_free = kfree;
613 /* Free all elements with this key, no matter which thread they're in.
614 * May only be called while the pool is paused.
616 void ldap_pvt_thread_pool_purgekey( void *key )
619 ldap_int_thread_key_t *ctx;
621 for ( i=0; i<LDAP_MAXTHR; i++ ) {
622 if ( thread_keys[i].ctx ) {
623 ctx = thread_keys[i].ctx;
624 for ( j=0; j<MAXKEYS; j++ ) {
625 if ( ctx[j].ltk_key == key ) {
627 ctx[j].ltk_free( ctx[j].ltk_key, ctx[j].ltk_data );
628 ctx[j].ltk_key = NULL;
629 ctx[j].ltk_free = NULL;
638 * This is necessary if the caller does not have access to the
639 * thread context handle (for example, a slapd plugin calling
640 * slapi_search_internal()). No doubt it is more efficient to
641 * for the application to keep track of the thread context
644 void *ldap_pvt_thread_pool_context( )
646 ldap_pvt_thread_t tid;
649 tid = ldap_pvt_thread_self();
650 if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
651 return ldap_int_main_thrctx;
653 TID_HASH( tid, hash );
654 for (i = hash & (LDAP_MAXTHR-1);
655 !ldap_pvt_thread_equal(thread_keys[i].id, tid_zero) &&
656 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
657 i = (i+1) & (LDAP_MAXTHR-1));
659 return thread_keys[i].ctx;
662 void ldap_pvt_thread_pool_context_reset( void *vctx )
664 ldap_int_thread_key_t *ctx = vctx;
667 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++) {
668 if ( ctx[i].ltk_free )
669 ctx[i].ltk_free( ctx[i].ltk_key, ctx[i].ltk_data );
670 ctx[i].ltk_key = NULL;
673 #endif /* LDAP_THREAD_HAVE_TPOOL */