3 * Copyright 1998-2003 The OpenLDAP Foundation, Redwood City, California, USA
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
16 #include <ac/stdarg.h>
17 #include <ac/stdlib.h>
18 #include <ac/string.h>
23 #include "ldap_pvt_thread.h"
24 #include "ldap_queue.h"
26 #ifndef LDAP_THREAD_HAVE_TPOOL
28 enum ldap_int_thread_pool_state {
29 LDAP_INT_THREAD_POOL_RUNNING,
30 LDAP_INT_THREAD_POOL_FINISHING,
31 LDAP_INT_THREAD_POOL_STOPPING
34 typedef struct ldap_int_thread_key_s {
37 ldap_pvt_thread_pool_keyfree_t *ltk_free;
38 } ldap_int_thread_key_t;
40 /* Max number of thread-specific keys we store per thread.
41 * We don't expect to use many...
44 #define MAXTHREADS 1024 /* must be a power of 2 */
48 ldap_int_thread_key_t *ctx;
49 } thread_keys[MAXTHREADS];
52 typedef struct ldap_int_thread_ctx_s {
54 LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
55 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
56 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
58 ldap_pvt_thread_start_t *ltc_start_routine;
60 } ldap_int_thread_ctx_t;
62 struct ldap_int_thread_pool_s {
63 LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
64 ldap_pvt_thread_mutex_t ltp_mutex;
65 ldap_pvt_thread_cond_t ltp_cond;
66 LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
67 LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
68 LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
72 long ltp_pending_count;
73 long ltp_active_count;
78 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
79 ldap_int_thread_pool_list =
80 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
82 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
84 static void *ldap_int_thread_pool_wrapper( void *pool );
87 ldap_int_thread_pool_startup ( void )
89 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
93 ldap_int_thread_pool_shutdown ( void )
95 struct ldap_int_thread_pool_s *pool;
97 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
98 LDAP_STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
99 ldap_pvt_thread_pool_destroy( &pool, 0);
101 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
106 ldap_pvt_thread_pool_init (
107 ldap_pvt_thread_pool_t *tpool,
111 ldap_pvt_thread_pool_t pool;
115 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
116 sizeof(struct ldap_int_thread_pool_s));
118 if (pool == NULL) return(-1);
120 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
123 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
126 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
127 pool->ltp_max_count = max_threads;
128 pool->ltp_max_pending = max_pending;
129 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
130 LDAP_SLIST_INIT(&pool->ltp_free_list);
131 LDAP_SLIST_INIT(&pool->ltp_active_list);
132 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
133 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
134 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
137 /* THIS WILL NOT WORK on some systems. If the process
138 * forks after starting a thread, there is no guarantee
139 * that the thread will survive the fork. For example,
140 * slapd forks in order to daemonize, and does so after
141 * calling ldap_pvt_thread_pool_init. On some systems,
142 * this initial thread does not run in the child process,
143 * but ltp_open_count == 1, so two things happen:
144 * 1) the first client connection fails, and 2) when
145 * slapd is kill'ed, it never terminates since it waits
146 * for all worker threads to exit. */
148 /* start up one thread, just so there is one. no need to
149 * lock the mutex right now, since no threads are running.
151 pool->ltp_open_count++;
153 ldap_pvt_thread_t thr;
154 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
157 /* couldn't start one? then don't start any */
158 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
159 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
160 ldap_int_thread_pool_s, ltp_next);
161 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
162 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
163 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
173 #define TID_HASH(tid, hash) do { int i; \
174 unsigned char *ptr = (unsigned char *)&(tid); \
175 for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
178 ldap_pvt_thread_pool_submit (
179 ldap_pvt_thread_pool_t *tpool,
180 ldap_pvt_thread_start_t *start_routine, void *arg )
182 struct ldap_int_thread_pool_s *pool;
183 ldap_int_thread_ctx_t *ctx;
185 ldap_pvt_thread_t thr;
195 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
196 if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
197 || (pool->ltp_max_pending > 0
198 && pool->ltp_pending_count >= pool->ltp_max_pending))
200 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
203 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
205 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
208 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
209 sizeof(ldap_int_thread_ctx_t));
211 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
216 ctx->ltc_start_routine = start_routine;
219 pool->ltp_pending_count++;
220 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
221 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
222 if ((pool->ltp_open_count <= 0
224 || pool->ltp_pending_count > 1
226 || pool->ltp_open_count == pool->ltp_active_count)
227 && (pool->ltp_max_count <= 0
228 || pool->ltp_open_count < pool->ltp_max_count))
230 pool->ltp_open_count++;
231 pool->ltp_starting++;
234 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
239 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
241 rc = ldap_pvt_thread_create( &thr, 1,
242 ldap_int_thread_pool_wrapper, pool );
245 pool->ltp_starting--;
247 /* assign this thread ID to a key slot; start
248 * at the thread ID itself (mod MAXTHREADS) and
249 * look for an empty slot.
252 for (rc = hash & (MAXTHREADS-1); thread_keys[rc].id;
253 rc = (rc+1) & (MAXTHREADS-1));
254 thread_keys[rc].id = thr;
256 /* couldn't create thread. back out of
257 * ltp_open_count and check for even worse things.
259 pool->ltp_open_count--;
260 pool->ltp_starting--;
261 if (pool->ltp_open_count == 0) {
262 /* no open threads at all?!?
264 ldap_int_thread_ctx_t *ptr;
265 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
266 if (ptr == ctx) break;
268 /* no open threads, context not handled, so
269 * back out of ltp_pending_count, free the context,
272 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
273 ldap_int_thread_ctx_s, ltc_next.q);
274 pool->ltp_pending_count++;
275 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
280 /* there is another open thread, so this
281 * context will be handled eventually.
282 * continue on and signal that the context
286 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
293 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
295 struct ldap_int_thread_pool_s *pool;
305 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
306 pool->ltp_max_count = max_threads;
307 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
312 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
314 struct ldap_int_thread_pool_s *pool;
325 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
326 count = pool->ltp_pending_count + pool->ltp_active_count;
327 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
332 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
334 struct ldap_int_thread_pool_s *pool, *pptr;
336 ldap_int_thread_ctx_t *ctx;
343 if (pool == NULL) return(-1);
345 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
346 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
347 if (pptr == pool) break;
349 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
350 ldap_int_thread_pool_s, ltp_next);
351 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
353 if (pool != pptr) return(-1);
355 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
356 pool->ltp_state = run_pending
357 ? LDAP_INT_THREAD_POOL_FINISHING
358 : LDAP_INT_THREAD_POOL_STOPPING;
360 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
361 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
364 ldap_pvt_thread_yield();
365 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
366 waiting = pool->ltp_open_count;
367 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
368 } while (waiting > 0);
370 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
372 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
376 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
378 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
382 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
383 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
389 ldap_int_thread_pool_wrapper (
392 struct ldap_int_thread_pool_s *pool = xpool;
393 ldap_int_thread_ctx_t *ctx;
394 ldap_int_thread_key_t ltc_key[MAXKEYS];
395 ldap_pvt_thread_t tid;
396 int i, keyslot, hash;
401 for ( i=0; i<MAXKEYS; i++ ) {
402 ltc_key[i].ltk_key = NULL;
405 tid = ldap_pvt_thread_self();
407 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
409 /* store pointer to our keys */
411 for (i = hash & (MAXTHREADS-1); thread_keys[i].id != tid;
412 i = (i+1) & (MAXTHREADS-1));
413 thread_keys[i].ctx = ltc_key;
416 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
417 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
419 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
421 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
423 if (pool->ltp_max_count > 0
424 && pool->ltp_open_count > pool->ltp_max_count)
426 /* too many threads running (can happen if the
427 * maximum threads value is set during ongoing
428 * operation using ldap_pvt_thread_pool_maxthreads)
429 * so let this thread die.
434 /* we could check an idle timer here, and let the
435 * thread die if it has been inactive for a while.
436 * only die if there are other open threads (i.e.,
437 * always have at least one thread open). the check
438 * should be like this:
439 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
440 * check timer, leave thread (break;)
443 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
444 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
449 pool->ltp_pending_count--;
451 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
452 pool->ltp_active_count++;
453 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
455 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
457 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
458 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
459 ldap_int_thread_ctx_s, ltc_next.al);
460 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
461 pool->ltp_active_count--;
462 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
464 ldap_pvt_thread_yield();
466 /* if we use an idle timer, here's
467 * a good place to update it
470 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
473 for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
474 if (ltc_key[i].ltk_free)
477 ltc_key[i].ltk_data );
480 thread_keys[keyslot].ctx = NULL;
481 thread_keys[keyslot].id = 0;
483 pool->ltp_open_count--;
484 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
486 ldap_pvt_thread_exit(NULL);
490 int ldap_pvt_thread_pool_getkey(
494 ldap_pvt_thread_pool_keyfree_t **kfree )
496 ldap_int_thread_key_t *ctx = xctx;
499 if ( !ctx || !data ) return EINVAL;
501 for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
502 if ( ctx[i].ltk_key == key ) {
503 *data = ctx[i].ltk_data;
504 if ( kfree ) *kfree = ctx[i].ltk_free;
511 int ldap_pvt_thread_pool_setkey(
515 ldap_pvt_thread_pool_keyfree_t *kfree )
517 ldap_int_thread_key_t *ctx = xctx;
520 if ( !ctx || !key ) return EINVAL;
522 for ( i=0; i<MAXKEYS; i++ ) {
523 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
524 ctx[i].ltk_key = key;
525 ctx[i].ltk_data = data;
526 ctx[i].ltk_free = kfree;
534 * This is necessary if the caller does not have access to the
535 * thread context handle (for example, a slapd plugin calling
536 * slapi_search_internal()). No doubt it is more efficient to
537 * for the application to keep track of the thread context
540 void *ldap_pvt_thread_pool_context( )
542 ldap_pvt_thread_t tid;
545 tid = ldap_pvt_thread_self();
547 TID_HASH( tid, hash );
548 for (i = hash & (MAXTHREADS-1); thread_keys[i].id &&
549 thread_keys[i].id != tid; i = (i+1) & (MAXTHREADS-1));
551 return thread_keys[i].ctx;
554 #endif /* LDAP_HAVE_THREAD_POOL */