3 * Copyright 1998-2002 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 "ldap_queue.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 LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
36 LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
38 void *(*ltc_start_routine)( void *);
40 } ldap_int_thread_ctx_t;
42 struct ldap_int_thread_pool_s {
43 LDAP_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 LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
47 LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
51 long ltp_pending_count;
52 long ltp_active_count;
57 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
58 ldap_int_thread_pool_list =
59 LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
61 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
63 static void *ldap_int_thread_pool_wrapper( void *pool );
66 ldap_int_thread_pool_startup ( void )
68 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
72 ldap_int_thread_pool_shutdown ( void )
74 struct ldap_int_thread_pool_s *pool;
76 while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
77 LDAP_STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
78 ldap_pvt_thread_pool_destroy( &pool, 0);
80 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
85 ldap_pvt_thread_pool_init (
86 ldap_pvt_thread_pool_t *tpool,
90 ldap_pvt_thread_pool_t pool;
94 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
95 sizeof(struct ldap_int_thread_pool_s));
97 if (pool == NULL) return(-1);
99 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
102 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
105 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
106 pool->ltp_max_count = max_threads;
107 pool->ltp_max_pending = max_pending;
108 LDAP_STAILQ_INIT(&pool->ltp_pending_list);
109 LDAP_SLIST_INIT(&pool->ltp_free_list);
110 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
111 LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
112 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
115 /* THIS WILL NOT WORK on some systems. If the process
116 * forks after starting a thread, there is no guarantee
117 * that the thread will survive the fork. For example,
118 * slapd forks in order to daemonize, and does so after
119 * calling ldap_pvt_thread_pool_init. On some systems,
120 * this initial thread does not run in the child process,
121 * but ltp_open_count == 1, so two things happen:
122 * 1) the first client connection fails, and 2) when
123 * slapd is kill'ed, it never terminates since it waits
124 * for all worker threads to exit. */
126 /* start up one thread, just so there is one. no need to
127 * lock the mutex right now, since no threads are running.
129 pool->ltp_open_count++;
131 ldap_pvt_thread_t thr;
132 rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
135 /* couldn't start one? then don't start any */
136 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
137 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool,
138 ldap_int_thread_pool_s, ltp_next);
139 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
140 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
141 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
152 ldap_pvt_thread_pool_submit (
153 ldap_pvt_thread_pool_t *tpool,
154 void *(*start_routine)( void * ), void *arg )
156 struct ldap_int_thread_pool_s *pool;
157 ldap_int_thread_ctx_t *ctx;
159 ldap_pvt_thread_t thr;
169 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
170 if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
171 || (pool->ltp_max_pending > 0
172 && pool->ltp_pending_count >= pool->ltp_max_pending))
174 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
177 ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
179 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
181 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
182 sizeof(ldap_int_thread_ctx_t));
184 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
189 ctx->ltc_start_routine = start_routine;
192 pool->ltp_pending_count++;
193 LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
194 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
195 if ((pool->ltp_open_count <= 0
196 || pool->ltp_pending_count > 1
197 || pool->ltp_open_count == pool->ltp_active_count)
198 && (pool->ltp_max_count <= 0
199 || pool->ltp_open_count < pool->ltp_max_count))
201 pool->ltp_open_count++;
202 pool->ltp_starting++;
205 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
208 int rc = ldap_pvt_thread_create( &thr, 1,
209 ldap_int_thread_pool_wrapper, pool );
210 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
212 pool->ltp_starting--;
214 /* couldn't create thread. back out of
215 * ltp_open_count and check for even worse things.
217 pool->ltp_open_count--;
218 pool->ltp_starting--;
219 if (pool->ltp_open_count == 0) {
220 /* no open threads at all?!?
222 ldap_int_thread_ctx_t *ptr;
223 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
224 if (ptr == ctx) break;
226 /* no open threads, context not handled, so
227 * back out of ltp_pending_count, free the context,
230 LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx,
231 ldap_int_thread_ctx_s, ltc_next.q);
232 pool->ltp_pending_count++;
233 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
238 /* there is another open thread, so this
239 * context will be handled eventually.
240 * continue on and signal that the context
244 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
251 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
253 struct ldap_int_thread_pool_s *pool;
263 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
264 pool->ltp_max_count = max_threads;
265 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
270 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
272 struct ldap_int_thread_pool_s *pool;
283 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
284 count = pool->ltp_pending_count + pool->ltp_active_count;
285 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
290 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
292 struct ldap_int_thread_pool_s *pool, *pptr;
294 ldap_int_thread_ctx_t *ctx;
301 if (pool == NULL) return(-1);
303 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
304 LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
305 if (pptr == pool) break;
307 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
308 ldap_int_thread_pool_s, ltp_next);
309 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
311 if (pool != pptr) return(-1);
313 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
314 pool->ltp_state = run_pending
315 ? LDAP_INT_THREAD_POOL_FINISHING
316 : LDAP_INT_THREAD_POOL_STOPPING;
317 waiting = pool->ltp_open_count;
319 /* broadcast could be used here, but only after
320 * it is fixed in the NT thread implementation
322 while (--waiting >= 0) {
323 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
325 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
328 ldap_pvt_thread_yield();
329 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
330 waiting = pool->ltp_open_count;
331 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
332 } while (waiting > 0);
334 while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
336 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
340 while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
342 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
346 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
347 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
353 ldap_int_thread_pool_wrapper (
356 struct ldap_int_thread_pool_s *pool = xpool;
357 ldap_int_thread_ctx_t *ctx;
362 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
364 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
365 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
367 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
369 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
371 if (pool->ltp_max_count > 0
372 && pool->ltp_open_count > pool->ltp_max_count)
374 /* too many threads running (can happen if the
375 * maximum threads value is set during ongoing
376 * operation using ldap_pvt_thread_pool_maxthreads)
377 * so let this thread die.
382 /* we could check an idle timer here, and let the
383 * thread die if it has been inactive for a while.
384 * only die if there are other open threads (i.e.,
385 * always have at least one thread open). the check
386 * should be like this:
387 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
388 * check timer, leave thread (break;)
391 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
392 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
397 pool->ltp_pending_count--;
398 pool->ltp_active_count++;
399 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
401 (ctx->ltc_start_routine)(ctx->ltc_arg);
403 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
404 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
405 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
407 ldap_pvt_thread_yield();
409 /* if we use an idle timer, here's
410 * a good place to update it
413 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
414 pool->ltp_active_count--;
417 pool->ltp_open_count--;
418 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
420 ldap_pvt_thread_exit(NULL);
423 #endif /* LDAP_HAVE_THREAD_POOL */