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_concurrency;
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_backload ( ldap_pvt_thread_pool_t *tpool )
241 struct ldap_int_thread_pool_s *pool;
252 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
253 count = pool->ltp_pending_count + pool->ltp_active_count;
254 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
259 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
261 struct ldap_int_thread_pool_s *pool;
263 ldap_int_thread_ctx_t *ctx;
270 if (pool == NULL) return(-1);
272 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
273 pool = ldap_int_thread_delist(&ldap_int_thread_pool_list, pool);
274 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
276 if (pool == NULL) return(-1);
278 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
279 pool->ltp_state = run_pending
280 ? LDAP_INT_THREAD_POOL_FINISHING
281 : LDAP_INT_THREAD_POOL_STOPPING;
282 waiting = pool->ltp_open_count;
284 /* broadcast could be used here, but only after
285 * it is fixed in the NT thread implementation
287 while (--waiting >= 0) {
288 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
290 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
293 ldap_pvt_thread_yield();
294 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
295 waiting = pool->ltp_open_count;
296 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
297 } while (waiting > 0);
299 while (ctx = (ldap_int_thread_ctx_t *)ldap_int_thread_delist(
300 &pool->ltp_pending_list, NULL))
305 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
306 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
312 ldap_int_thread_pool_wrapper (
313 struct ldap_int_thread_pool_s *pool )
315 ldap_int_thread_ctx_t *ctx;
320 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
322 while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
324 ctx = ldap_int_thread_delist(&pool->ltp_pending_list, NULL);
326 if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
328 /* we could check an idle timer here, and let the
329 * thread die if it has been inactive for a while.
330 * only die if there are other open threads (i.e.,
331 * always have at least one thread open). the check
332 * should be like this:
333 * if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
334 * check timer, leave thread (break;)
337 if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
338 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
343 pool->ltp_pending_count--;
344 pool->ltp_active_count++;
345 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
347 (ctx->ltc_start_routine)(ctx->ltc_arg);
349 ldap_pvt_thread_yield();
351 /* if we use an idle timer, here's
352 * a good place to update it
355 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
356 pool->ltp_active_count--;
359 pool->ltp_open_count--;
360 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
362 ldap_pvt_thread_exit(NULL);
367 ldap_int_thread_enlist( ldap_int_thread_list_t *list, void *elem )
369 ldap_int_thread_list_element_t *prev;
371 if (elem == NULL) return(NULL);
373 ((ldap_int_thread_list_element_t *)elem)->next = NULL;
379 for (prev = *list ; prev->next != NULL; prev = prev->next) ;
385 ldap_int_thread_delist( ldap_int_thread_list_t *list, void *elem )
387 ldap_int_thread_list_element_t *prev;
389 if (*list == NULL) return(NULL);
391 if (elem == NULL) elem = *list;
394 *list = ((ldap_int_thread_list_element_t *)elem)->next;
398 for (prev = *list ; prev->next != NULL; prev = prev->next) {
399 if (prev->next == elem) {
400 prev->next = ((ldap_int_thread_list_element_t *)elem)->next;
408 ldap_int_thread_onlist( ldap_int_thread_list_t *list, void *elem )
410 ldap_int_thread_list_element_t *prev;
412 if (elem == NULL || *list == NULL) return(NULL);
414 for (prev = *list ; prev != NULL; prev = prev->next) {
422 #endif /* LDAP_HAVE_THREAD_POOL */