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.
17 #include <ac/stdlib.h>
18 #include <ac/string.h>
22 #include "ldap_pvt_thread.h"
24 #ifndef LDAP_THREAD_HAVE_TPOOL
26 enum ldap_int_thread_pool_state {
27 LDAP_INT_THREAD_POOL_RUNNING,
28 LDAP_INT_THREAD_POOL_FINISHING,
29 LDAP_INT_THREAD_POOL_STOPPING
32 typedef struct ldap_int_thread_list_element_s {
33 struct ldap_int_thread_list_element_s *next;
34 } ldap_int_thread_list_element_t, *ldap_int_thread_list_t;
36 struct ldap_int_thread_pool_s {
37 struct ldap_int_thread_pool_s *ltp_next;
38 ldap_pvt_thread_mutex_t ltp_mutex;
39 ldap_pvt_thread_cond_t ltp_cond;
40 ldap_int_thread_list_t ltp_pending_list;
44 long ltp_pending_count;
45 long ltp_active_count;
50 typedef struct ldap_int_thread_ctx_s {
51 struct ldap_int_thread_ctx_s *ltc_next;
52 void *(*ltc_start_routine)( void *);
54 } ldap_int_thread_ctx_t;
56 static ldap_int_thread_list_t ldap_int_thread_pool_list = NULL;
57 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
59 static void *ldap_int_thread_pool_wrapper(
60 struct ldap_int_thread_pool_s *pool );
62 static void *ldap_int_thread_enlist( ldap_int_thread_list_t *list, void *elem );
63 static void *ldap_int_thread_delist( ldap_int_thread_list_t *list, void *elem );
65 static void *ldap_int_thread_onlist( ldap_int_thread_list_t *list, void *elem );
69 ldap_int_thread_pool_startup ( void )
71 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
75 ldap_int_thread_pool_shutdown ( void )
77 while (ldap_int_thread_pool_list != NULL) {
78 struct ldap_int_thread_pool_s *pool =
79 (struct ldap_int_thread_pool_s *) ldap_int_thread_pool_list;
81 ldap_pvt_thread_pool_destroy( &pool, 0);
83 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
88 ldap_pvt_thread_pool_init (
89 ldap_pvt_thread_pool_t *tpool,
93 ldap_pvt_thread_pool_t pool;
97 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
98 sizeof(struct ldap_int_thread_pool_s));
100 if (pool == NULL) return(-1);
102 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
105 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
108 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
109 pool->ltp_max_count = max_threads;
110 pool->ltp_max_pending = max_pending;
111 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
112 ldap_int_thread_enlist(&ldap_int_thread_pool_list, pool);
113 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
116 /* THIS WILL NOT WORK on some systems. If the process
117 * forks after starting a thread, there is no guarantee
118 * that the thread will survive the fork. For example,
119 * slapd forks in order to daemonize, and does so after
120 * calling ldap_pvt_thread_pool_init. On some systems,
121 * this initial thread does not run in the child process,
122 * but ltp_open_count == 1, so two things happen:
123 * 1) the first client connection fails, and 2) when
124 * slapd is kill'ed, it never terminates since it waits
125 * for all worker threads to exit. */
127 /* start up one thread, just so there is one. no need to
128 * lock the mutex right now, since no threads are running.
130 pool->ltp_open_count++;
132 ldap_pvt_thread_t thr;
133 rc = ldap_pvt_thread_create( &thr, 1,
134 (void *) ldap_int_thread_pool_wrapper, pool );
137 /* couldn't start one? then don't start any */
138 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
139 ldap_int_thread_delist(&ldap_int_thread_pool_list, pool);
140 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
141 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
142 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
153 ldap_pvt_thread_pool_submit (
154 ldap_pvt_thread_pool_t *tpool,
155 void *(*start_routine)( void * ), void *arg )
157 struct ldap_int_thread_pool_s *pool;
158 ldap_int_thread_ctx_t *ctx;
160 ldap_pvt_thread_t thr;
170 ctx = (ldap_int_thread_ctx_t *) LDAP_CALLOC(1,
171 sizeof(ldap_int_thread_ctx_t));
173 if (ctx == NULL) return(-1);
175 ctx->ltc_start_routine = start_routine;
178 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
179 if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
180 || (pool->ltp_max_pending > 0
181 && pool->ltp_pending_count >= pool->ltp_max_pending))
183 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
187 pool->ltp_pending_count++;
188 ldap_int_thread_enlist(&pool->ltp_pending_list, ctx);
189 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
190 if ((pool->ltp_open_count <= 0
191 || pool->ltp_pending_count > 1
192 || pool->ltp_open_count == pool->ltp_active_count)
193 && (pool->ltp_max_count <= 0
194 || pool->ltp_open_count < pool->ltp_max_count))
196 pool->ltp_open_count++;
197 pool->ltp_starting++;
200 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
203 int rc = ldap_pvt_thread_create( &thr, 1,
204 (void *)ldap_int_thread_pool_wrapper, pool );
205 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
207 pool->ltp_starting--;
209 /* couldn't create thread. back out of
210 * ltp_open_count and check for even worse things.
212 pool->ltp_open_count--;
213 pool->ltp_starting--;
214 if (pool->ltp_open_count == 0) {
215 /* no open threads at all?!?
217 if (ldap_int_thread_delist(&pool->ltp_pending_list, ctx)) {
218 /* no open threads, context not handled, so
219 * back out of ltp_pending_count, free the context,
222 pool->ltp_pending_count++;
223 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
228 /* there is another open thread, so this
229 * context will be handled eventually.
230 * continue on and signal that the context
234 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
241 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
243 struct ldap_int_thread_pool_s *pool;
253 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
254 pool->ltp_max_count = max_threads;
255 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
260 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
262 struct ldap_int_thread_pool_s *pool;
273 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
274 count = pool->ltp_pending_count + pool->ltp_active_count;
275 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
280 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
282 struct ldap_int_thread_pool_s *pool;
284 ldap_int_thread_ctx_t *ctx;
291 if (pool == NULL) return(-1);
293 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
294 pool = ldap_int_thread_delist(&ldap_int_thread_pool_list, pool);
295 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
297 if (pool == NULL) return(-1);
299 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
300 pool->ltp_state = run_pending
301 ? LDAP_INT_THREAD_POOL_FINISHING
302 : LDAP_INT_THREAD_POOL_STOPPING;
303 waiting = pool->ltp_open_count;
305 /* broadcast could be used here, but only after
306 * it is fixed in the NT thread implementation
308 while (--waiting >= 0) {
309 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
311 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
314 ldap_pvt_thread_yield();
315 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
316 waiting = pool->ltp_open_count;
317 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
318 } while (waiting > 0);
320 while ((ctx = (ldap_int_thread_ctx_t *)ldap_int_thread_delist(
321 &pool->ltp_pending_list, NULL)) != NULL)
326 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
327 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
333 ldap_int_thread_pool_wrapper (
334 struct ldap_int_thread_pool_s *pool )
336 ldap_int_thread_ctx_t *ctx;
341 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
343 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
345 ctx = ldap_int_thread_delist(&pool->ltp_pending_list, NULL);
347 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
349 if (pool->ltp_max_count > 0
350 && pool->ltp_open_count > pool->ltp_max_count)
352 /* too many threads running (can happen if the
353 * maximum threads value is set during ongoing
354 * operation using ldap_pvt_thread_pool_maxthreads)
355 * so let this thread die.
360 /* we could check an idle timer here, and let the
361 * thread die if it has been inactive for a while.
362 * only die if there are other open threads (i.e.,
363 * always have at least one thread open). the check
364 * should be like this:
365 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
366 * check timer, leave thread (break;)
369 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
370 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
375 pool->ltp_pending_count--;
376 pool->ltp_active_count++;
377 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
379 (ctx->ltc_start_routine)(ctx->ltc_arg);
381 ldap_pvt_thread_yield();
383 /* if we use an idle timer, here's
384 * a good place to update it
387 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
388 pool->ltp_active_count--;
391 pool->ltp_open_count--;
392 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
394 ldap_pvt_thread_exit(NULL);
399 ldap_int_thread_enlist( ldap_int_thread_list_t *list, void *elem )
401 ldap_int_thread_list_element_t *prev;
403 if (elem == NULL) return(NULL);
405 ((ldap_int_thread_list_element_t *)elem)->next = NULL;
411 for (prev = *list ; prev->next != NULL; prev = prev->next) ;
417 ldap_int_thread_delist( ldap_int_thread_list_t *list, void *elem )
419 ldap_int_thread_list_element_t *prev;
421 if (*list == NULL) return(NULL);
423 if (elem == NULL) elem = *list;
426 *list = ((ldap_int_thread_list_element_t *)elem)->next;
430 for (prev = *list ; prev->next != NULL; prev = prev->next) {
431 if (prev->next == elem) {
432 prev->next = ((ldap_int_thread_list_element_t *)elem)->next;
440 ldap_int_thread_onlist( ldap_int_thread_list_t *list, void *elem )
442 ldap_int_thread_list_element_t *prev;
444 if (elem == NULL || *list == NULL) return(NULL);
446 for (prev = *list ; prev != NULL; prev = prev->next) {
454 #endif /* LDAP_HAVE_THREAD_POOL */