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);
225 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
226 sizeof(ldap_int_thread_ctx_t));
228 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
233 ctx->ltc_start_routine = start_routine;
236 pool->ltp_pending_count++;
237 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
238 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
239 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
242 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
243 if ((pool->ltp_open_count <= 0
245 || pool->ltp_pending_count > 1
247 || pool->ltp_open_count == pool->ltp_active_count)
248 && (pool->ltp_max_count <= 0
249 || pool->ltp_open_count < pool->ltp_max_count))
251 pool->ltp_open_count++;
252 pool->ltp_starting++;
255 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
260 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
262 rc = ldap_pvt_thread_create( &thr, 1,
263 ldap_int_thread_pool_wrapper, pool );
266 pool->ltp_starting--;
268 /* assign this thread ID to a key slot; start
269 * at the thread ID itself (mod LDAP_MAXTHR) and
270 * look for an empty slot.
273 for (rc = hash & (LDAP_MAXTHR-1);
274 !TID_EQ(thread_keys[rc].id, tid_zero);
275 rc = (rc+1) & (LDAP_MAXTHR-1));
276 thread_keys[rc].id = thr;
278 /* couldn't create thread. back out of
279 * ltp_open_count and check for even worse things.
281 pool->ltp_open_count--;
282 pool->ltp_starting--;
283 if (pool->ltp_open_count == 0) {
284 /* no open threads at all?!?
286 ldap_int_thread_ctx_t *ptr;
287 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
288 if (ptr == ctx) break;
290 /* no open threads, context not handled, so
291 * back out of ltp_pending_count, free the context,
294 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
295 ldap_int_thread_ctx_s, ltc_next.q);
296 pool->ltp_pending_count++;
297 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
302 /* there is another open thread, so this
303 * context will be handled eventually.
304 * continue on and signal that the context
308 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
315 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
317 struct ldap_int_thread_pool_s *pool;
327 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
328 pool->ltp_max_count = max_threads;
329 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
334 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
336 struct ldap_int_thread_pool_s *pool;
347 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
348 count = pool->ltp_pending_count + pool->ltp_active_count;
349 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
354 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
356 struct ldap_int_thread_pool_s *pool, *pptr;
358 ldap_int_thread_ctx_t *ctx;
365 if (pool == NULL) return(-1);
367 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
368 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
369 if (pptr == pool) break;
371 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
372 ldap_int_thread_pool_s, ltp_next);
373 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
375 if (pool != pptr) return(-1);
377 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
378 pool->ltp_state = run_pending
379 ? LDAP_INT_THREAD_POOL_FINISHING
380 : LDAP_INT_THREAD_POOL_STOPPING;
382 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
383 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
386 ldap_pvt_thread_yield();
387 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
388 waiting = pool->ltp_open_count;
389 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
390 } while (waiting > 0);
392 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
394 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
398 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
400 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
404 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
405 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
406 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
412 ldap_int_thread_pool_wrapper (
415 struct ldap_int_thread_pool_s *pool = xpool;
416 ldap_int_thread_ctx_t *ctx;
417 ldap_int_thread_key_t ltc_key[MAXKEYS];
418 ldap_pvt_thread_t tid;
419 int i, keyslot, hash;
424 for ( i=0; i<MAXKEYS; i++ ) {
425 ltc_key[i].ltk_key = NULL;
428 tid = ldap_pvt_thread_self();
430 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
432 /* store pointer to our keys */
434 for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid);
435 i = (i+1) & (LDAP_MAXTHR-1));
436 thread_keys[i].ctx = ltc_key;
439 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
440 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
442 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
444 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
446 if (pool->ltp_max_count > 0
447 && pool->ltp_open_count > pool->ltp_max_count)
449 /* too many threads running (can happen if the
450 * maximum threads value is set during ongoing
451 * operation using ldap_pvt_thread_pool_maxthreads)
452 * so let this thread die.
457 /* we could check an idle timer here, and let the
458 * thread die if it has been inactive for a while.
459 * only die if there are other open threads (i.e.,
460 * always have at least one thread open). the check
461 * should be like this:
462 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
463 * check timer, leave thread (break;)
466 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
467 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);
492 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
494 ldap_pvt_thread_yield();
496 /* if we use an idle timer, here's
497 * a good place to update it
500 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
503 for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
504 if (ltc_key[i].ltk_free)
507 ltc_key[i].ltk_data );
510 thread_keys[keyslot].ctx = NULL;
511 thread_keys[keyslot].id = tid_zero;
513 pool->ltp_open_count--;
514 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
516 ldap_pvt_thread_exit(NULL);
521 ldap_pvt_thread_pool_pause (
522 ldap_pvt_thread_pool_t *tpool )
524 struct ldap_int_thread_pool_s *pool;
534 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
536 /* If someone else has already requested a pause, we have to wait */
537 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
538 pool->ltp_pending_count++;
539 pool->ltp_active_count--;
540 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
541 pool->ltp_pending_count--;
542 pool->ltp_active_count++;
544 /* Wait for everyone else to finish */
545 pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
546 while (pool->ltp_active_count > 1) {
547 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
549 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
554 ldap_pvt_thread_pool_resume (
555 ldap_pvt_thread_pool_t *tpool )
557 struct ldap_int_thread_pool_s *pool;
567 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
569 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
570 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
571 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
575 int ldap_pvt_thread_pool_getkey(
579 ldap_pvt_thread_pool_keyfree_t **kfree )
581 ldap_int_thread_key_t *ctx = xctx;
584 if ( !ctx || !data ) return EINVAL;
586 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
587 if ( ctx[i].ltk_key == key ) {
588 *data = ctx[i].ltk_data;
589 if ( kfree ) *kfree = ctx[i].ltk_free;
596 int ldap_pvt_thread_pool_setkey(
600 ldap_pvt_thread_pool_keyfree_t *kfree )
602 ldap_int_thread_key_t *ctx = xctx;
605 if ( !ctx || !key ) return EINVAL;
607 for ( i=0; i<MAXKEYS; i++ ) {
608 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
609 ctx[i].ltk_key = key;
610 ctx[i].ltk_data = data;
611 ctx[i].ltk_free = kfree;
618 /* Free all elements with this key, no matter which thread they're in.
619 * May only be called while the pool is paused.
621 void ldap_pvt_thread_pool_purgekey( void *key )
624 ldap_int_thread_key_t *ctx;
626 for ( i=0; i<LDAP_MAXTHR; i++ ) {
627 if ( thread_keys[i].ctx ) {
628 ctx = thread_keys[i].ctx;
629 for ( j=0; j<MAXKEYS; j++ ) {
630 if ( ctx[j].ltk_key == key ) {
632 ctx[j].ltk_free( ctx[j].ltk_key, ctx[j].ltk_data );
633 ctx[j].ltk_key = NULL;
634 ctx[j].ltk_free = NULL;
643 * This is necessary if the caller does not have access to the
644 * thread context handle (for example, a slapd plugin calling
645 * slapi_search_internal()). No doubt it is more efficient to
646 * for the application to keep track of the thread context
649 void *ldap_pvt_thread_pool_context( )
651 ldap_pvt_thread_t tid;
654 tid = ldap_pvt_thread_self();
656 TID_HASH( tid, hash );
657 for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid_zero) &&
658 !TID_EQ(thread_keys[i].id, tid); i = (i+1) & (LDAP_MAXTHR-1));
660 return thread_keys[i].ctx;
663 #endif /* LDAP_THREAD_HAVE_TPOOL */