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 );
99 static ldap_pvt_thread_t ldap_int_main_tid;
101 static ldap_int_thread_key_t ldap_int_main_thrctx[LDAP_MAXTHR];
104 ldap_int_thread_pool_startup ( void )
106 ldap_int_main_tid = ldap_pvt_thread_self();
108 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
112 ldap_int_thread_pool_shutdown ( void )
114 struct ldap_int_thread_pool_s *pool;
116 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
117 LDAP_STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
118 ldap_pvt_thread_pool_destroy( &pool, 0);
120 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
125 ldap_pvt_thread_pool_init (
126 ldap_pvt_thread_pool_t *tpool,
130 ldap_pvt_thread_pool_t pool;
134 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
135 sizeof(struct ldap_int_thread_pool_s));
137 if (pool == NULL) return(-1);
139 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
142 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
145 rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
148 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
149 pool->ltp_max_count = max_threads;
150 pool->ltp_max_pending = max_pending;
151 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
152 LDAP_SLIST_INIT(&pool->ltp_free_list);
153 LDAP_SLIST_INIT(&pool->ltp_active_list);
154 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
155 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
156 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
159 /* THIS WILL NOT WORK on some systems. If the process
160 * forks after starting a thread, there is no guarantee
161 * that the thread will survive the fork. For example,
162 * slapd forks in order to daemonize, and does so after
163 * calling ldap_pvt_thread_pool_init. On some systems,
164 * this initial thread does not run in the child process,
165 * but ltp_open_count == 1, so two things happen:
166 * 1) the first client connection fails, and 2) when
167 * slapd is kill'ed, it never terminates since it waits
168 * for all worker threads to exit. */
170 /* start up one thread, just so there is one. no need to
171 * lock the mutex right now, since no threads are running.
173 pool->ltp_open_count++;
175 ldap_pvt_thread_t thr;
176 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
179 /* couldn't start one? then don't start any */
180 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
181 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
182 ldap_int_thread_pool_s, ltp_next);
183 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
184 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
185 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
186 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
196 #define TID_HASH(tid, hash) do { int i; \
197 unsigned char *ptr = (unsigned char *)&(tid); \
198 for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
201 ldap_pvt_thread_pool_submit (
202 ldap_pvt_thread_pool_t *tpool,
203 ldap_pvt_thread_start_t *start_routine, void *arg )
205 struct ldap_int_thread_pool_s *pool;
206 ldap_int_thread_ctx_t *ctx;
208 ldap_pvt_thread_t thr;
218 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
219 if ((pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING &&
220 pool->ltp_state != LDAP_INT_THREAD_POOL_PAUSING)
221 || (pool->ltp_max_pending > 0
222 && pool->ltp_pending_count >= pool->ltp_max_pending))
224 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
227 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
229 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
231 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
232 sizeof(ldap_int_thread_ctx_t));
234 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
239 ctx->ltc_start_routine = start_routine;
242 pool->ltp_pending_count++;
243 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
244 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
245 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
248 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
249 if ((pool->ltp_open_count <= 0
251 || pool->ltp_pending_count > 1
253 || pool->ltp_open_count == pool->ltp_active_count)
254 && (pool->ltp_max_count <= 0
255 || pool->ltp_open_count < pool->ltp_max_count))
257 pool->ltp_open_count++;
258 pool->ltp_starting++;
261 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
266 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
268 rc = ldap_pvt_thread_create( &thr, 1,
269 ldap_int_thread_pool_wrapper, pool );
272 pool->ltp_starting--;
274 /* assign this thread ID to a key slot; start
275 * at the thread ID itself (mod LDAP_MAXTHR) and
276 * look for an empty slot.
279 for (rc = hash & (LDAP_MAXTHR-1);
280 !TID_EQ(thread_keys[rc].id, tid_zero);
281 rc = (rc+1) & (LDAP_MAXTHR-1));
282 thread_keys[rc].id = thr;
284 /* couldn't create thread. back out of
285 * ltp_open_count and check for even worse things.
287 pool->ltp_open_count--;
288 pool->ltp_starting--;
289 if (pool->ltp_open_count == 0) {
290 /* no open threads at all?!?
292 ldap_int_thread_ctx_t *ptr;
293 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
294 if (ptr == ctx) break;
296 /* no open threads, context not handled, so
297 * back out of ltp_pending_count, free the context,
300 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
301 ldap_int_thread_ctx_s, ltc_next.q);
302 pool->ltp_pending_count++;
303 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
308 /* there is another open thread, so this
309 * context will be handled eventually.
310 * continue on and signal that the context
314 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
321 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
323 struct ldap_int_thread_pool_s *pool;
333 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
334 pool->ltp_max_count = max_threads;
335 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
340 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
342 struct ldap_int_thread_pool_s *pool;
353 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
354 count = pool->ltp_pending_count + pool->ltp_active_count;
355 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
360 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
362 struct ldap_int_thread_pool_s *pool, *pptr;
364 ldap_int_thread_ctx_t *ctx;
371 if (pool == NULL) return(-1);
373 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
374 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
375 if (pptr == pool) break;
377 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
378 ldap_int_thread_pool_s, ltp_next);
379 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
381 if (pool != pptr) return(-1);
383 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
384 pool->ltp_state = run_pending
385 ? LDAP_INT_THREAD_POOL_FINISHING
386 : LDAP_INT_THREAD_POOL_STOPPING;
388 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
389 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
392 ldap_pvt_thread_yield();
393 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
394 waiting = pool->ltp_open_count;
395 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
396 } while (waiting > 0);
398 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
400 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
404 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
406 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
410 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
411 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
412 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
418 ldap_int_thread_pool_wrapper (
421 struct ldap_int_thread_pool_s *pool = xpool;
422 ldap_int_thread_ctx_t *ctx;
423 ldap_int_thread_key_t ltc_key[MAXKEYS];
424 ldap_pvt_thread_t tid;
425 int i, keyslot, hash;
430 for ( i=0; i<MAXKEYS; i++ ) {
431 ltc_key[i].ltk_key = NULL;
434 tid = ldap_pvt_thread_self();
436 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
438 /* store pointer to our keys */
440 for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid);
441 i = (i+1) & (LDAP_MAXTHR-1));
442 thread_keys[i].ctx = ltc_key;
445 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
446 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
448 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
450 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
452 if (pool->ltp_max_count > 0
453 && pool->ltp_open_count > pool->ltp_max_count)
455 /* too many threads running (can happen if the
456 * maximum threads value is set during ongoing
457 * operation using ldap_pvt_thread_pool_maxthreads)
458 * so let this thread die.
463 /* we could check an idle timer here, and let the
464 * thread die if it has been inactive for a while.
465 * only die if there are other open threads (i.e.,
466 * always have at least one thread open). the check
467 * should be like this:
468 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
469 * check timer, leave thread (break;)
472 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
473 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
478 pool->ltp_pending_count--;
480 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
481 pool->ltp_active_count++;
482 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
484 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
486 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
487 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
488 ldap_int_thread_ctx_s, ltc_next.al);
489 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
490 pool->ltp_active_count--;
492 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
493 if (pool->ltp_active_count < 2) {
494 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
496 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
498 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
500 ldap_pvt_thread_yield();
502 /* if we use an idle timer, here's
503 * a good place to update it
506 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
509 for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
510 if (ltc_key[i].ltk_free)
513 ltc_key[i].ltk_data );
516 thread_keys[keyslot].ctx = NULL;
517 thread_keys[keyslot].id = tid_zero;
519 pool->ltp_open_count--;
520 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
522 ldap_pvt_thread_exit(NULL);
527 ldap_pvt_thread_pool_pause (
528 ldap_pvt_thread_pool_t *tpool )
530 struct ldap_int_thread_pool_s *pool;
540 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
542 /* If someone else has already requested a pause, we have to wait */
543 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
544 pool->ltp_pending_count++;
545 pool->ltp_active_count--;
546 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
547 pool->ltp_pending_count--;
548 pool->ltp_active_count++;
550 /* Wait for everyone else to finish */
551 pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
552 while (pool->ltp_active_count > 1) {
553 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
555 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
560 ldap_pvt_thread_pool_resume (
561 ldap_pvt_thread_pool_t *tpool )
563 struct ldap_int_thread_pool_s *pool;
573 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
575 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
576 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
577 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
581 int ldap_pvt_thread_pool_getkey(
585 ldap_pvt_thread_pool_keyfree_t **kfree )
587 ldap_int_thread_key_t *ctx = xctx;
590 if ( !ctx || !data ) return EINVAL;
592 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
593 if ( ctx[i].ltk_key == key ) {
594 *data = ctx[i].ltk_data;
595 if ( kfree ) *kfree = ctx[i].ltk_free;
602 int ldap_pvt_thread_pool_setkey(
606 ldap_pvt_thread_pool_keyfree_t *kfree )
608 ldap_int_thread_key_t *ctx = xctx;
611 if ( !ctx || !key ) return EINVAL;
613 for ( i=0; i<MAXKEYS; i++ ) {
614 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
615 ctx[i].ltk_key = key;
616 ctx[i].ltk_data = data;
617 ctx[i].ltk_free = kfree;
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 ( TID_EQ( tid, ldap_int_main_tid ))
662 return ldap_int_main_thrctx;
664 TID_HASH( tid, hash );
665 for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid_zero) &&
666 !TID_EQ(thread_keys[i].id, tid); i = (i+1) & (LDAP_MAXTHR-1));
668 return thread_keys[i].ctx;
671 void ldap_pvt_thread_pool_context_reset( void *vctx )
673 ldap_int_thread_key_t *ctx = vctx;
676 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++) {
677 if ( ctx[i].ltk_free )
678 ctx[i].ltk_free( ctx[i].ltk_key, ctx[i].ltk_data );
679 ctx[i].ltk_key = NULL;
682 #endif /* LDAP_THREAD_HAVE_TPOOL */