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 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_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
116 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
121 ldap_pvt_thread_pool_init (
122 ldap_pvt_thread_pool_t *tpool,
126 ldap_pvt_thread_pool_t pool;
130 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
131 sizeof(struct ldap_int_thread_pool_s));
133 if (pool == NULL) return(-1);
135 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
138 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
141 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
144 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
145 pool->ltp_max_count = max_threads;
146 pool->ltp_max_pending = max_pending;
147 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
148 LDAP_SLIST_INIT(&pool->ltp_free_list);
149 LDAP_SLIST_INIT(&pool->ltp_active_list);
150 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
151 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
152 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
155 /* THIS WILL NOT WORK on some systems. If the process
156 * forks after starting a thread, there is no guarantee
157 * that the thread will survive the fork. For example,
158 * slapd forks in order to daemonize, and does so after
159 * calling ldap_pvt_thread_pool_init. On some systems,
160 * this initial thread does not run in the child process,
161 * but ltp_open_count == 1, so two things happen:
162 * 1) the first client connection fails, and 2) when
163 * slapd is kill'ed, it never terminates since it waits
164 * for all worker threads to exit. */
166 /* start up one thread, just so there is one. no need to
167 * lock the mutex right now, since no threads are running.
169 pool->ltp_open_count++;
171 ldap_pvt_thread_t thr;
172 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
175 /* couldn't start one? then don't start any */
176 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
177 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
178 ldap_int_thread_pool_s, ltp_next);
179 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
180 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
181 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
182 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
192 #define TID_HASH(tid, hash) do { unsigned i; \
193 unsigned char *ptr = (unsigned char *)&(tid); \
194 for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
197 ldap_pvt_thread_pool_submit (
198 ldap_pvt_thread_pool_t *tpool,
199 ldap_pvt_thread_start_t *start_routine, void *arg )
201 struct ldap_int_thread_pool_s *pool;
202 ldap_int_thread_ctx_t *ctx;
204 ldap_pvt_thread_t thr;
214 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
215 if ((pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING &&
216 pool->ltp_state != LDAP_INT_THREAD_POOL_PAUSING)
217 || (pool->ltp_max_pending > 0
218 && pool->ltp_pending_count >= pool->ltp_max_pending))
220 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
223 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
225 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
227 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
228 sizeof(ldap_int_thread_ctx_t));
230 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
235 ctx->ltc_start_routine = start_routine;
238 pool->ltp_pending_count++;
239 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
240 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
241 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
244 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
245 if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
246 && (pool->ltp_open_count < pool->ltp_max_count ||
247 pool->ltp_max_count <= 0 ))
249 pool->ltp_open_count++;
250 pool->ltp_starting++;
253 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
258 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
260 rc = ldap_pvt_thread_create( &thr, 1,
261 ldap_int_thread_pool_wrapper, pool );
264 pool->ltp_starting--;
266 /* assign this thread ID to a key slot; start
267 * at the thread ID itself (mod LDAP_MAXTHR) and
268 * look for an empty slot.
271 for (rc = hash & (LDAP_MAXTHR-1);
272 !ldap_pvt_thread_equal(thread_keys[rc].id, tid_zero);
273 rc = (rc+1) & (LDAP_MAXTHR-1));
274 thread_keys[rc].id = thr;
276 /* couldn't create thread. back out of
277 * ltp_open_count and check for even worse things.
279 pool->ltp_open_count--;
280 pool->ltp_starting--;
281 if (pool->ltp_open_count == 0) {
282 /* no open threads at all?!?
284 ldap_int_thread_ctx_t *ptr;
285 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
286 if (ptr == ctx) break;
288 /* no open threads, context not handled, so
289 * back out of ltp_pending_count, free the context,
292 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
293 ldap_int_thread_ctx_s, ltc_next.q);
294 pool->ltp_pending_count++;
295 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
300 /* there is another open thread, so this
301 * context will be handled eventually.
302 * continue on and signal that the context
306 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
313 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
315 struct ldap_int_thread_pool_s *pool;
325 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
326 pool->ltp_max_count = max_threads;
327 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
332 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
334 struct ldap_int_thread_pool_s *pool;
345 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
346 count = pool->ltp_pending_count + pool->ltp_active_count;
347 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
352 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
354 struct ldap_int_thread_pool_s *pool, *pptr;
355 ldap_int_thread_ctx_t *ctx;
362 if (pool == NULL) return(-1);
364 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
365 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
366 if (pptr == pool) break;
368 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
369 ldap_int_thread_pool_s, ltp_next);
370 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
372 if (pool != pptr) return(-1);
374 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
375 pool->ltp_state = run_pending
376 ? LDAP_INT_THREAD_POOL_FINISHING
377 : LDAP_INT_THREAD_POOL_STOPPING;
379 if ( pool->ltp_open_count ) {
380 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
381 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
383 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
385 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
387 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
391 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
393 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
397 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
398 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
399 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
405 ldap_int_thread_pool_wrapper (
408 struct ldap_int_thread_pool_s *pool = xpool;
409 ldap_int_thread_ctx_t *ctx;
410 ldap_int_thread_key_t ltc_key[MAXKEYS];
411 ldap_pvt_thread_t tid;
412 int i, keyslot, hash;
417 for ( i=0; i<MAXKEYS; i++ ) {
418 ltc_key[i].ltk_key = NULL;
421 tid = ldap_pvt_thread_self();
423 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
425 /* store pointer to our keys */
427 for (i = hash & (LDAP_MAXTHR-1);
428 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
429 i = (i+1) & (LDAP_MAXTHR-1));
430 thread_keys[i].ctx = ltc_key;
433 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
434 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
436 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
438 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
440 if (pool->ltp_max_count > 0
441 && pool->ltp_open_count > pool->ltp_max_count)
443 /* too many threads running (can happen if the
444 * maximum threads value is set during ongoing
445 * operation using ldap_pvt_thread_pool_maxthreads)
446 * so let this thread die.
451 /* we could check an idle timer here, and let the
452 * thread die if it has been inactive for a while.
453 * only die if there are other open threads (i.e.,
454 * always have at least one thread open). the check
455 * should be like this:
456 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
457 * check timer, leave thread (break;)
459 * Just use pthread_cond_timedwait if we want to
463 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING
464 || pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING)
466 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
472 pool->ltp_pending_count--;
474 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
475 pool->ltp_active_count++;
476 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
478 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
480 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
481 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
482 ldap_int_thread_ctx_s, ltc_next.al);
483 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
484 pool->ltp_active_count--;
486 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
487 if (pool->ltp_active_count < 2) {
488 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
490 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
494 ldap_pvt_thread_pool_context_reset( ltc_key );
496 thread_keys[keyslot].ctx = NULL;
497 thread_keys[keyslot].id = tid_zero;
499 pool->ltp_open_count--;
501 /* let pool_destroy know we're all done */
502 if (pool->ltp_open_count < 1)
503 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
505 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
507 ldap_pvt_thread_exit(NULL);
512 ldap_pvt_thread_pool_pause (
513 ldap_pvt_thread_pool_t *tpool )
515 struct ldap_int_thread_pool_s *pool;
525 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
527 /* If someone else has already requested a pause, we have to wait */
528 while (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
529 pool->ltp_pending_count++;
530 pool->ltp_active_count--;
531 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
532 pool->ltp_pending_count--;
533 pool->ltp_active_count++;
535 /* Wait for everyone else to finish */
536 pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
537 while (pool->ltp_active_count > 1) {
538 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
540 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
545 ldap_pvt_thread_pool_resume (
546 ldap_pvt_thread_pool_t *tpool )
548 struct ldap_int_thread_pool_s *pool;
558 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
560 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
561 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
562 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
566 int ldap_pvt_thread_pool_getkey(
570 ldap_pvt_thread_pool_keyfree_t **kfree )
572 ldap_int_thread_key_t *ctx = xctx;
575 if ( !ctx || !data ) return EINVAL;
577 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
578 if ( ctx[i].ltk_key == key ) {
579 *data = ctx[i].ltk_data;
580 if ( kfree ) *kfree = ctx[i].ltk_free;
587 int ldap_pvt_thread_pool_setkey(
591 ldap_pvt_thread_pool_keyfree_t *kfree )
593 ldap_int_thread_key_t *ctx = xctx;
596 if ( !ctx || !key ) return EINVAL;
598 for ( i=0; i<MAXKEYS; i++ ) {
599 if (( data && !ctx[i].ltk_key ) || ctx[i].ltk_key == key ) {
600 if ( data || kfree ) {
601 ctx[i].ltk_key = key;
602 ctx[i].ltk_data = data;
603 ctx[i].ltk_free = kfree;
606 for ( j=i+1; j<MAXKEYS; j++ )
607 if ( !ctx[j].ltk_key ) break;
610 ctx[i].ltk_key = ctx[j].ltk_key;
611 ctx[i].ltk_data = ctx[j].ltk_data;
612 ctx[i].ltk_free = ctx[j].ltk_free;
614 ctx[j].ltk_key = NULL;
615 ctx[j].ltk_data = NULL;
616 ctx[j].ltk_free = NULL;
624 /* Free all elements with this key, no matter which thread they're in.
625 * May only be called while the pool is paused.
627 void ldap_pvt_thread_pool_purgekey( void *key )
630 ldap_int_thread_key_t *ctx;
632 for ( i=0; i<LDAP_MAXTHR; i++ ) {
633 if ( thread_keys[i].ctx ) {
634 ctx = thread_keys[i].ctx;
635 for ( j=0; j<MAXKEYS; j++ ) {
636 if ( ctx[j].ltk_key == key ) {
638 ctx[j].ltk_free( ctx[j].ltk_key, ctx[j].ltk_data );
639 ctx[j].ltk_key = NULL;
640 ctx[j].ltk_free = NULL;
649 * This is necessary if the caller does not have access to the
650 * thread context handle (for example, a slapd plugin calling
651 * slapi_search_internal()). No doubt it is more efficient to
652 * for the application to keep track of the thread context
655 void *ldap_pvt_thread_pool_context( )
657 ldap_pvt_thread_t tid;
660 tid = ldap_pvt_thread_self();
661 if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
662 return ldap_int_main_thrctx;
664 TID_HASH( tid, hash );
665 for (i = hash & (LDAP_MAXTHR-1);
666 !ldap_pvt_thread_equal(thread_keys[i].id, tid_zero) &&
667 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
668 i = (i+1) & (LDAP_MAXTHR-1));
670 return thread_keys[i].ctx;
673 void ldap_pvt_thread_pool_context_reset( void *vctx )
675 ldap_int_thread_key_t *ctx = vctx;
678 for ( i=MAXKEYS-1; i>=0; i--) {
679 if ( ctx[i].ltk_key == NULL )
681 if ( ctx[i].ltk_free )
682 ctx[i].ltk_free( ctx[i].ltk_key, ctx[i].ltk_data );
683 ctx[i].ltk_key = NULL;
686 #endif /* LDAP_THREAD_HAVE_TPOOL */