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 );
64 static void *ldap_int_thread_onlist( ldap_int_thread_list_t *list, void *elem );
67 ldap_int_thread_pool_startup ( void )
69 return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
73 ldap_int_thread_pool_shutdown ( void )
75 while (ldap_int_thread_pool_list != NULL) {
76 struct ldap_int_thread_pool_s *pool =
77 (struct ldap_int_thread_pool_s *) ldap_int_thread_pool_list;
79 ldap_pvt_thread_pool_destroy( &pool, 0);
81 ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
86 ldap_pvt_thread_pool_init (
87 ldap_pvt_thread_pool_t *tpool,
91 ldap_pvt_thread_pool_t pool;
95 pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
96 sizeof(struct ldap_int_thread_pool_s));
98 if (pool == NULL) return(-1);
100 rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
103 rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
106 pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
107 pool->ltp_max_count = max_threads;
108 pool->ltp_max_pending = max_pending;
109 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
110 ldap_int_thread_enlist(&ldap_int_thread_pool_list, pool);
111 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
114 /* THIS WILL NOT WORK on some systems. If the process
115 * forks after starting a thread, there is no guarantee
116 * that the thread will survive the fork. For example,
117 * slapd forks in order to daemonize, and does so after
118 * calling ldap_pvt_thread_pool_init. On some systems,
119 * this initial thread does not run in the child process,
120 * but ltp_open_count == 1, so two things happen:
121 * 1) the first client connection fails, and 2) when
122 * slapd is kill'ed, it never terminates since it waits
123 * for all worker threads to exit.
125 /* start up one thread, just so there is one. no need to
126 * lock the mutex right now, since no threads are running.
128 pool->ltp_open_count++;
130 ldap_pvt_thread_t thr;
131 rc = ldap_pvt_thread_create( &thr, 1,
132 (void *) 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_int_thread_delist(&ldap_int_thread_pool_list, pool);
138 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
139 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
140 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
151 ldap_pvt_thread_pool_submit (
152 ldap_pvt_thread_pool_t *tpool,
153 void *(*start_routine)( void * ), void *arg )
155 struct ldap_int_thread_pool_s *pool;
156 ldap_int_thread_ctx_t *ctx;
158 ldap_pvt_thread_t thr;
168 ctx = (ldap_int_thread_ctx_t *) LDAP_CALLOC(1,
169 sizeof(ldap_int_thread_ctx_t));
171 if (ctx == NULL) return(-1);
173 ctx->ltc_start_routine = start_routine;
176 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
177 if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
178 || (pool->ltp_max_pending > 0
179 && pool->ltp_pending_count >= pool->ltp_max_pending))
181 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
185 pool->ltp_pending_count++;
186 ldap_int_thread_enlist(&pool->ltp_pending_list, ctx);
187 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
188 if ((pool->ltp_open_count <= 0
189 || pool->ltp_pending_count > 1
190 || pool->ltp_open_count == pool->ltp_active_count)
191 && (pool->ltp_max_count <= 0
192 || pool->ltp_open_count < pool->ltp_max_count))
194 pool->ltp_open_count++;
195 pool->ltp_starting++;
198 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
201 int rc = ldap_pvt_thread_create( &thr, 1,
202 (void *)ldap_int_thread_pool_wrapper, pool );
203 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
205 pool->ltp_starting--;
207 /* couldn't create thread. back out of
208 * ltp_open_count and check for even worse things.
210 pool->ltp_open_count--;
211 pool->ltp_starting--;
212 if (pool->ltp_open_count == 0) {
213 /* no open threads at all?!?
215 if (ldap_int_thread_delist(&pool->ltp_pending_list, ctx)) {
216 /* no open threads, context not handled, so
217 * back out of ltp_pending_count, free the context,
220 pool->ltp_pending_count++;
221 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
226 /* there is another open thread, so this
227 * context will be handled eventually.
228 * continue on and signal that the context
232 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
239 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
241 struct ldap_int_thread_pool_s *pool;
251 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
252 pool->ltp_max_count = max_threads;
253 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
258 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
260 struct ldap_int_thread_pool_s *pool;
271 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
272 count = pool->ltp_pending_count + pool->ltp_active_count;
273 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
278 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
280 struct ldap_int_thread_pool_s *pool;
282 ldap_int_thread_ctx_t *ctx;
289 if (pool == NULL) return(-1);
291 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
292 pool = ldap_int_thread_delist(&ldap_int_thread_pool_list, pool);
293 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
295 if (pool == NULL) return(-1);
297 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
298 pool->ltp_state = run_pending
299 ? LDAP_INT_THREAD_POOL_FINISHING
300 : LDAP_INT_THREAD_POOL_STOPPING;
301 waiting = pool->ltp_open_count;
303 /* broadcast could be used here, but only after
304 * it is fixed in the NT thread implementation
306 while (--waiting >= 0) {
307 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
309 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
312 ldap_pvt_thread_yield();
313 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
314 waiting = pool->ltp_open_count;
315 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
316 } while (waiting > 0);
318 while (ctx = (ldap_int_thread_ctx_t *)ldap_int_thread_delist(
319 &pool->ltp_pending_list, NULL))
324 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
325 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
331 ldap_int_thread_pool_wrapper (
332 struct ldap_int_thread_pool_s *pool )
334 ldap_int_thread_ctx_t *ctx;
339 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
341 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
343 ctx = ldap_int_thread_delist(&pool->ltp_pending_list, NULL);
345 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
347 if (pool->ltp_max_count > 0
348 && pool->ltp_open_count > pool->ltp_max_count)
350 /* too many threads running (can happen if the
351 * maximum threads value is set during ongoing
352 * operation using ldap_pvt_thread_pool_maxthreads)
353 * so let this thread die.
358 /* we could check an idle timer here, and let the
359 * thread die if it has been inactive for a while.
360 * only die if there are other open threads (i.e.,
361 * always have at least one thread open). the check
362 * should be like this:
363 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
364 * check timer, leave thread (break;)
367 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
368 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
373 pool->ltp_pending_count--;
374 pool->ltp_active_count++;
375 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
377 (ctx->ltc_start_routine)(ctx->ltc_arg);
379 ldap_pvt_thread_yield();
381 /* if we use an idle timer, here's
382 * a good place to update it
385 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
386 pool->ltp_active_count--;
389 pool->ltp_open_count--;
390 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
392 ldap_pvt_thread_exit(NULL);
397 ldap_int_thread_enlist( ldap_int_thread_list_t *list, void *elem )
399 ldap_int_thread_list_element_t *prev;
401 if (elem == NULL) return(NULL);
403 ((ldap_int_thread_list_element_t *)elem)->next = NULL;
409 for (prev = *list ; prev->next != NULL; prev = prev->next) ;
415 ldap_int_thread_delist( ldap_int_thread_list_t *list, void *elem )
417 ldap_int_thread_list_element_t *prev;
419 if (*list == NULL) return(NULL);
421 if (elem == NULL) elem = *list;
424 *list = ((ldap_int_thread_list_element_t *)elem)->next;
428 for (prev = *list ; prev->next != NULL; prev = prev->next) {
429 if (prev->next == elem) {
430 prev->next = ((ldap_int_thread_list_element_t *)elem)->next;
438 ldap_int_thread_onlist( ldap_int_thread_list_t *list, void *elem )
440 ldap_int_thread_list_element_t *prev;
442 if (elem == NULL || *list == NULL) return(NULL);
444 for (prev = *list ; prev != NULL; prev = prev->next) {
452 #endif /* LDAP_HAVE_THREAD_POOL */