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"
28 #include "ldap_queue.h"
30 #ifndef LDAP_THREAD_HAVE_TPOOL
32 enum ldap_int_thread_pool_state {
33 LDAP_INT_THREAD_POOL_RUNNING,
34 LDAP_INT_THREAD_POOL_FINISHING,
35 LDAP_INT_THREAD_POOL_STOPPING,
36 LDAP_INT_THREAD_POOL_PAUSING,
39 typedef struct ldap_int_thread_key_s {
42 ldap_pvt_thread_pool_keyfree_t *ltk_free;
43 } ldap_int_thread_key_t;
45 /* Max number of thread-specific keys we store per thread.
46 * We don't expect to use many...
49 #define LDAP_MAXTHR 1024 /* must be a power of 2 */
51 static ldap_pvt_thread_t tid_zero;
54 #define TID_EQ(a,b) pthread_equal((a),(b))
56 #define TID_EQ(a,b) ((a) == (b))
60 ldap_int_thread_key_t *ctx;
61 } thread_keys[LDAP_MAXTHR];
64 typedef struct ldap_int_thread_ctx_s {
66 LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
67 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
68 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
70 ldap_pvt_thread_start_t *ltc_start_routine;
72 } ldap_int_thread_ctx_t;
74 struct ldap_int_thread_pool_s {
75 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
76 ldap_pvt_thread_mutex_t ltp_mutex;
77 ldap_pvt_thread_cond_t ltp_cond;
78 ldap_pvt_thread_cond_t ltp_pcond;
79 LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
80 LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
81 LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
85 long ltp_pending_count;
86 long ltp_active_count;
91 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
92 ldap_int_thread_pool_list =
93 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
95 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
97 static void *ldap_int_thread_pool_wrapper( void *pool );
100 ldap_int_thread_pool_startup ( void )
102 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
106 ldap_int_thread_pool_shutdown ( void )
108 struct ldap_int_thread_pool_s *pool;
110 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
111 LDAP_STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
112 ldap_pvt_thread_pool_destroy( &pool, 0);
114 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
119 ldap_pvt_thread_pool_init (
120 ldap_pvt_thread_pool_t *tpool,
124 ldap_pvt_thread_pool_t pool;
128 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
129 sizeof(struct ldap_int_thread_pool_s));
131 if (pool == NULL) return(-1);
133 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
136 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
139 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
142 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
143 pool->ltp_max_count = max_threads;
144 pool->ltp_max_pending = max_pending;
145 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
146 LDAP_SLIST_INIT(&pool->ltp_free_list);
147 LDAP_SLIST_INIT(&pool->ltp_active_list);
148 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
149 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
150 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
153 /* THIS WILL NOT WORK on some systems. If the process
154 * forks after starting a thread, there is no guarantee
155 * that the thread will survive the fork. For example,
156 * slapd forks in order to daemonize, and does so after
157 * calling ldap_pvt_thread_pool_init. On some systems,
158 * this initial thread does not run in the child process,
159 * but ltp_open_count == 1, so two things happen:
160 * 1) the first client connection fails, and 2) when
161 * slapd is kill'ed, it never terminates since it waits
162 * for all worker threads to exit. */
164 /* start up one thread, just so there is one. no need to
165 * lock the mutex right now, since no threads are running.
167 pool->ltp_open_count++;
169 ldap_pvt_thread_t thr;
170 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
173 /* couldn't start one? then don't start any */
174 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
175 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
176 ldap_int_thread_pool_s, ltp_next);
177 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
178 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
179 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
180 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
190 #define TID_HASH(tid, hash) do { int i; \
191 unsigned char *ptr = (unsigned char *)&(tid); \
192 for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
195 ldap_pvt_thread_pool_submit (
196 ldap_pvt_thread_pool_t *tpool,
197 ldap_pvt_thread_start_t *start_routine, void *arg )
199 struct ldap_int_thread_pool_s *pool;
200 ldap_int_thread_ctx_t *ctx;
202 ldap_pvt_thread_t thr;
212 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
213 if ((pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING &&
214 pool->ltp_state != LDAP_INT_THREAD_POOL_PAUSING)
215 || (pool->ltp_max_pending > 0
216 && pool->ltp_pending_count >= pool->ltp_max_pending))
218 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
221 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
223 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
226 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
227 sizeof(ldap_int_thread_ctx_t));
229 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
234 ctx->ltc_start_routine = start_routine;
237 pool->ltp_pending_count++;
238 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
239 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
240 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
243 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
244 if ((pool->ltp_open_count <= 0
246 || pool->ltp_pending_count > 1
248 || pool->ltp_open_count == pool->ltp_active_count)
249 && (pool->ltp_max_count <= 0
250 || pool->ltp_open_count < pool->ltp_max_count))
252 pool->ltp_open_count++;
253 pool->ltp_starting++;
256 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
261 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
263 rc = ldap_pvt_thread_create( &thr, 1,
264 ldap_int_thread_pool_wrapper, pool );
267 pool->ltp_starting--;
269 /* assign this thread ID to a key slot; start
270 * at the thread ID itself (mod LDAP_MAXTHR) and
271 * look for an empty slot.
274 for (rc = hash & (LDAP_MAXTHR-1);
275 !TID_EQ(thread_keys[rc].id, tid_zero);
276 rc = (rc+1) & (LDAP_MAXTHR-1));
277 thread_keys[rc].id = thr;
279 /* couldn't create thread. back out of
280 * ltp_open_count and check for even worse things.
282 pool->ltp_open_count--;
283 pool->ltp_starting--;
284 if (pool->ltp_open_count == 0) {
285 /* no open threads at all?!?
287 ldap_int_thread_ctx_t *ptr;
288 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
289 if (ptr == ctx) break;
291 /* no open threads, context not handled, so
292 * back out of ltp_pending_count, free the context,
295 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
296 ldap_int_thread_ctx_s, ltc_next.q);
297 pool->ltp_pending_count++;
298 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
303 /* there is another open thread, so this
304 * context will be handled eventually.
305 * continue on and signal that the context
309 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
316 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
318 struct ldap_int_thread_pool_s *pool;
328 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
329 pool->ltp_max_count = max_threads;
330 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
335 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
337 struct ldap_int_thread_pool_s *pool;
348 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
349 count = pool->ltp_pending_count + pool->ltp_active_count;
350 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
355 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
357 struct ldap_int_thread_pool_s *pool, *pptr;
359 ldap_int_thread_ctx_t *ctx;
366 if (pool == NULL) return(-1);
368 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
369 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
370 if (pptr == pool) break;
372 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
373 ldap_int_thread_pool_s, ltp_next);
374 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
376 if (pool != pptr) return(-1);
378 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
379 pool->ltp_state = run_pending
380 ? LDAP_INT_THREAD_POOL_FINISHING
381 : LDAP_INT_THREAD_POOL_STOPPING;
383 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
384 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
387 ldap_pvt_thread_yield();
388 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
389 waiting = pool->ltp_open_count;
390 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
391 } while (waiting > 0);
393 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
395 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
399 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
401 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
405 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
406 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
407 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
413 ldap_int_thread_pool_wrapper (
416 struct ldap_int_thread_pool_s *pool = xpool;
417 ldap_int_thread_ctx_t *ctx;
418 ldap_int_thread_key_t ltc_key[MAXKEYS];
419 ldap_pvt_thread_t tid;
420 int i, keyslot, hash;
425 for ( i=0; i<MAXKEYS; i++ ) {
426 ltc_key[i].ltk_key = NULL;
429 tid = ldap_pvt_thread_self();
431 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
433 /* store pointer to our keys */
435 for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid);
436 i = (i+1) & (LDAP_MAXTHR-1));
437 thread_keys[i].ctx = ltc_key;
440 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
441 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
443 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
445 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
447 if (pool->ltp_max_count > 0
448 && pool->ltp_open_count > pool->ltp_max_count)
450 /* too many threads running (can happen if the
451 * maximum threads value is set during ongoing
452 * operation using ldap_pvt_thread_pool_maxthreads)
453 * so let this thread die.
458 /* we could check an idle timer here, and let the
459 * thread die if it has been inactive for a while.
460 * only die if there are other open threads (i.e.,
461 * always have at least one thread open). the check
462 * should be like this:
463 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
464 * check timer, leave thread (break;)
467 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
468 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
473 pool->ltp_pending_count--;
475 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
476 pool->ltp_active_count++;
477 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
479 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
481 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
482 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
483 ldap_int_thread_ctx_s, ltc_next.al);
484 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
485 pool->ltp_active_count--;
487 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
488 if (pool->ltp_active_count < 2) {
489 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
491 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
493 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
495 ldap_pvt_thread_yield();
497 /* if we use an idle timer, here's
498 * a good place to update it
501 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
504 for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
505 if (ltc_key[i].ltk_free)
508 ltc_key[i].ltk_data );
511 thread_keys[keyslot].ctx = NULL;
512 thread_keys[keyslot].id = tid_zero;
514 pool->ltp_open_count--;
515 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
517 ldap_pvt_thread_exit(NULL);
522 ldap_pvt_thread_pool_pause (
523 ldap_pvt_thread_pool_t *tpool )
525 struct ldap_int_thread_pool_s *pool;
535 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
537 /* If someone else has already requested a pause, we have to wait */
538 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
539 pool->ltp_pending_count++;
540 pool->ltp_active_count--;
541 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
542 pool->ltp_pending_count--;
543 pool->ltp_active_count++;
545 /* Wait for everyone else to finish */
546 pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
547 while (pool->ltp_active_count > 1) {
548 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
550 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
555 ldap_pvt_thread_pool_resume (
556 ldap_pvt_thread_pool_t *tpool )
558 struct ldap_int_thread_pool_s *pool;
568 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
570 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
571 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
572 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
576 int ldap_pvt_thread_pool_getkey(
580 ldap_pvt_thread_pool_keyfree_t **kfree )
582 ldap_int_thread_key_t *ctx = xctx;
585 if ( !ctx || !data ) return EINVAL;
587 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
588 if ( ctx[i].ltk_key == key ) {
589 *data = ctx[i].ltk_data;
590 if ( kfree ) *kfree = ctx[i].ltk_free;
597 int ldap_pvt_thread_pool_setkey(
601 ldap_pvt_thread_pool_keyfree_t *kfree )
603 ldap_int_thread_key_t *ctx = xctx;
606 if ( !ctx || !key ) return EINVAL;
608 for ( i=0; i<MAXKEYS; i++ ) {
609 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
610 ctx[i].ltk_key = key;
611 ctx[i].ltk_data = data;
612 ctx[i].ltk_free = kfree;
620 * This is necessary if the caller does not have access to the
621 * thread context handle (for example, a slapd plugin calling
622 * slapi_search_internal()). No doubt it is more efficient to
623 * for the application to keep track of the thread context
626 void *ldap_pvt_thread_pool_context( )
628 ldap_pvt_thread_t tid;
631 tid = ldap_pvt_thread_self();
633 TID_HASH( tid, hash );
634 for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid_zero) &&
635 !TID_EQ(thread_keys[i].id, tid); i = (i+1) & (LDAP_MAXTHR-1));
637 return thread_keys[i].ctx;
640 #endif /* LDAP_THREAD_HAVE_TPOOL */