3 * Copyright 1998-2000 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>
22 #include "ldap_pvt_thread.h"
23 #include "queue-compat.h"
25 #ifndef LDAP_THREAD_HAVE_TPOOL
27 enum ldap_int_thread_pool_state {
28 LDAP_INT_THREAD_POOL_RUNNING,
29 LDAP_INT_THREAD_POOL_FINISHING,
30 LDAP_INT_THREAD_POOL_STOPPING
33 typedef struct ldap_int_thread_ctx_s {
35 SLIST_ENTRY(ldap_int_thread_ctx_s) l;
36 STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
38 void *(*ltc_start_routine)( void *);
40 } ldap_int_thread_ctx_t;
42 struct ldap_int_thread_pool_s {
43 STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
44 ldap_pvt_thread_mutex_t ltp_mutex;
45 ldap_pvt_thread_cond_t ltp_cond;
46 STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
50 long ltp_pending_count;
51 long ltp_active_count;
56 static STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
57 ldap_int_thread_pool_list =
58 STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
60 static SLIST_HEAD(tcl, ldap_int_thread_ctx_s)
61 ldap_int_ctx_free_list =
62 SLIST_HEAD_INITIALIZER(ldap_int_ctx_free_list);
64 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
65 static ldap_pvt_thread_mutex_t ldap_pvt_ctx_free_mutex;
67 static void *ldap_int_thread_pool_wrapper(
68 struct ldap_int_thread_pool_s *pool );
71 ldap_int_thread_pool_startup ( void )
73 int rc = ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
75 rc = ldap_pvt_thread_mutex_init(&ldap_pvt_ctx_free_mutex);
80 ldap_int_thread_pool_shutdown ( void )
82 ldap_int_thread_ctx_t *ctx;
83 struct ldap_int_thread_pool_s *pool;
85 while ((pool = STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
86 STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
87 ldap_pvt_thread_pool_destroy( &pool, 0);
89 while ((ctx = SLIST_FIRST(&ldap_int_ctx_free_list))) {
90 SLIST_REMOVE_HEAD(&ldap_int_ctx_free_list, ltc_next.l);
93 ldap_pvt_thread_mutex_destroy(&ldap_pvt_ctx_free_mutex);
94 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
99 ldap_pvt_thread_pool_init (
100 ldap_pvt_thread_pool_t *tpool,
104 ldap_pvt_thread_pool_t pool;
108 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
109 sizeof(struct ldap_int_thread_pool_s));
111 if (pool == NULL) return(-1);
113 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
116 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
119 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
120 pool->ltp_max_count = max_threads;
121 pool->ltp_max_pending = max_pending;
122 STAILQ_INIT(&pool->ltp_pending_list);
123 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
124 STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
125 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
128 /* THIS WILL NOT WORK on some systems. If the process
129 * forks after starting a thread, there is no guarantee
130 * that the thread will survive the fork. For example,
131 * slapd forks in order to daemonize, and does so after
132 * calling ldap_pvt_thread_pool_init. On some systems,
133 * this initial thread does not run in the child process,
134 * but ltp_open_count == 1, so two things happen:
135 * 1) the first client connection fails, and 2) when
136 * slapd is kill'ed, it never terminates since it waits
137 * for all worker threads to exit. */
139 /* start up one thread, just so there is one. no need to
140 * lock the mutex right now, since no threads are running.
142 pool->ltp_open_count++;
144 ldap_pvt_thread_t thr;
145 rc = ldap_pvt_thread_create( &thr, 1,
146 (void *) ldap_int_thread_pool_wrapper, pool );
149 /* couldn't start one? then don't start any */
150 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
151 STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
152 ldap_int_thread_element_s, ltp_next);
153 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
154 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
155 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
166 ldap_pvt_thread_pool_submit (
167 ldap_pvt_thread_pool_t *tpool,
168 void *(*start_routine)( void * ), void *arg )
170 struct ldap_int_thread_pool_s *pool;
171 ldap_int_thread_ctx_t *ctx;
173 ldap_pvt_thread_t thr;
183 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
184 if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
185 || (pool->ltp_max_pending > 0
186 && pool->ltp_pending_count >= pool->ltp_max_pending))
188 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
191 ldap_pvt_thread_mutex_lock(&ldap_pvt_ctx_free_mutex);
192 ctx = SLIST_FIRST(&ldap_int_ctx_free_list);
194 SLIST_REMOVE_HEAD(&ldap_int_ctx_free_list, ltc_next.l);
195 ldap_pvt_thread_mutex_unlock(&ldap_pvt_ctx_free_mutex);
197 ldap_pvt_thread_mutex_unlock(&ldap_pvt_ctx_free_mutex);
198 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
199 sizeof(ldap_int_thread_ctx_t));
201 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
206 ctx->ltc_start_routine = start_routine;
209 pool->ltp_pending_count++;
210 STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
211 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
212 if ((pool->ltp_open_count <= 0
213 || pool->ltp_pending_count > 1
214 || pool->ltp_open_count == pool->ltp_active_count)
215 && (pool->ltp_max_count <= 0
216 || pool->ltp_open_count < pool->ltp_max_count))
218 pool->ltp_open_count++;
219 pool->ltp_starting++;
222 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
225 int rc = ldap_pvt_thread_create( &thr, 1,
226 (void *)ldap_int_thread_pool_wrapper, pool );
227 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
229 pool->ltp_starting--;
231 /* couldn't create thread. back out of
232 * ltp_open_count and check for even worse things.
234 pool->ltp_open_count--;
235 pool->ltp_starting--;
236 if (pool->ltp_open_count == 0) {
237 /* no open threads at all?!?
239 ldap_int_thread_ctx_t *ptr;
240 STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
241 if (ptr == ctx) break;
243 /* no open threads, context not handled, so
244 * back out of ltp_pending_count, free the context,
247 STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
248 ldap_int_thread_ctx_s, ltc_next.q);
249 pool->ltp_pending_count++;
250 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
255 /* there is another open thread, so this
256 * context will be handled eventually.
257 * continue on and signal that the context
261 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
268 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
270 struct ldap_int_thread_pool_s *pool;
280 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
281 pool->ltp_max_count = max_threads;
282 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
287 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
289 struct ldap_int_thread_pool_s *pool;
300 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
301 count = pool->ltp_pending_count + pool->ltp_active_count;
302 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
307 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
309 struct ldap_int_thread_pool_s *pool, *pptr;
311 ldap_int_thread_ctx_t *ctx;
318 if (pool == NULL) return(-1);
320 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
321 STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
322 if (pptr == pool) break;
324 STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
325 ldap_int_thread_pool_s, ltp_next);
326 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
328 if (pool != pptr) return(-1);
330 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
331 pool->ltp_state = run_pending
332 ? LDAP_INT_THREAD_POOL_FINISHING
333 : LDAP_INT_THREAD_POOL_STOPPING;
334 waiting = pool->ltp_open_count;
336 /* broadcast could be used here, but only after
337 * it is fixed in the NT thread implementation
339 while (--waiting >= 0) {
340 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
342 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
345 ldap_pvt_thread_yield();
346 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
347 waiting = pool->ltp_open_count;
348 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
349 } while (waiting > 0);
351 while ((ctx = STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
353 STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
357 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
358 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
364 ldap_int_thread_pool_wrapper (
365 struct ldap_int_thread_pool_s *pool )
367 ldap_int_thread_ctx_t *ctx;
372 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
374 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
375 ctx = STAILQ_FIRST(&pool->ltp_pending_list);
377 STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
379 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
381 if (pool->ltp_max_count > 0
382 && pool->ltp_open_count > pool->ltp_max_count)
384 /* too many threads running (can happen if the
385 * maximum threads value is set during ongoing
386 * operation using ldap_pvt_thread_pool_maxthreads)
387 * so let this thread die.
392 /* we could check an idle timer here, and let the
393 * thread die if it has been inactive for a while.
394 * only die if there are other open threads (i.e.,
395 * always have at least one thread open). the check
396 * should be like this:
397 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
398 * check timer, leave thread (break;)
401 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
402 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
407 pool->ltp_pending_count--;
408 pool->ltp_active_count++;
409 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
411 (ctx->ltc_start_routine)(ctx->ltc_arg);
412 ldap_pvt_thread_mutex_lock(&ldap_pvt_ctx_free_mutex);
413 SLIST_INSERT_HEAD(&ldap_int_ctx_free_list, ctx, ltc_next.l);
414 ldap_pvt_thread_mutex_unlock(&ldap_pvt_ctx_free_mutex);
415 ldap_pvt_thread_yield();
417 /* if we use an idle timer, here's
418 * a good place to update it
421 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
422 pool->ltp_active_count--;
425 pool->ltp_open_count--;
426 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
428 ldap_pvt_thread_exit(NULL);
431 #endif /* LDAP_HAVE_THREAD_POOL */