]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/tpool.c
dc1d683bc087966fa565cb06d41722a4ee800a9f
[openldap] / libraries / libldap_r / tpool.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdarg.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24 #include <ac/errno.h>
25
26 #include "ldap-int.h"
27 #include "ldap_pvt_thread.h" /* Get the thread interface */
28 #include "ldap_queue.h"
29 #define LDAP_THREAD_POOL_IMPLEMENTATION
30 #include "ldap_thr_debug.h"  /* May rename symbols defined below */
31
32 #ifdef LDAP_DEVEL
33 /* #define SLAP_SEM_LOAD_CONTROL /* must also be defined in slapd.h */
34 #endif
35
36 #ifndef LDAP_THREAD_HAVE_TPOOL
37
38 typedef enum ldap_int_thread_pool_state_e {
39         LDAP_INT_THREAD_POOL_RUNNING,
40         LDAP_INT_THREAD_POOL_FINISHING,
41         LDAP_INT_THREAD_POOL_STOPPING,
42         LDAP_INT_THREAD_POOL_PAUSING
43 } ldap_int_thread_pool_state_t;
44
45 typedef struct ldap_int_thread_key_s {
46         void *ltk_key;
47         void *ltk_data;
48         ldap_pvt_thread_pool_keyfree_t *ltk_free;
49 } ldap_int_thread_key_t;
50
51 /* Max number of thread-specific keys we store per thread.
52  * We don't expect to use many...
53  */
54 #define MAXKEYS 32
55 #define LDAP_MAXTHR     1024    /* must be a power of 2 */
56
57 static ldap_pvt_thread_t tid_zero;
58
59 static struct {
60         ldap_pvt_thread_t id;
61         ldap_int_thread_key_t *ctx;
62 } thread_keys[LDAP_MAXTHR];
63         
64
65 typedef struct ldap_int_thread_ctx_s {
66         union {
67         LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
68         LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
69         LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
70         } ltc_next;
71         ldap_pvt_thread_start_t *ltc_start_routine;
72         void *ltc_arg;
73 } ldap_int_thread_ctx_t;
74
75 struct ldap_int_thread_pool_s {
76         LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
77         ldap_pvt_thread_mutex_t ltp_mutex;
78         ldap_pvt_thread_cond_t ltp_cond;
79         ldap_pvt_thread_cond_t ltp_pcond;
80         LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
81         LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
82         LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
83         ldap_int_thread_pool_state_t ltp_state;
84         long ltp_max_count;
85         long ltp_max_pending;
86         long ltp_pending_count;
87         long ltp_active_count;
88         long ltp_open_count;
89         long ltp_starting;
90 };
91
92 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
93         ldap_int_thread_pool_list =
94         LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
95
96 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
97
98 static void *ldap_int_thread_pool_wrapper( void *pool );
99
100 static ldap_pvt_thread_t ldap_int_main_tid;
101
102 static ldap_int_thread_key_t ldap_int_main_thrctx[LDAP_MAXTHR];
103
104 int
105 ldap_int_thread_pool_startup ( void )
106 {
107         ldap_int_main_tid = ldap_pvt_thread_self();
108
109         return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
110 }
111
112 int
113 ldap_int_thread_pool_shutdown ( void )
114 {
115         struct ldap_int_thread_pool_s *pool;
116
117         while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
118                 LDAP_STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
119                 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
120         }
121         ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
122         return(0);
123 }
124
125 typedef struct ldap_lazy_sem_t {
126         ldap_pvt_thread_mutex_t ls_mutex;
127         ldap_pvt_thread_cond_t  ls_cond;
128         int ls_sem_value;
129         /*
130          * when more than ls_lazy_count number of resources
131          * becmoes available, the thread wating for the resources will
132          * be waken up in order to prevent frequent blocking/waking-up
133          */
134         unsigned int ls_lazy_count;
135         /*
136          * only one thread(listener) will wait on this semaphore
137          * using a flag instead of a list
138          */
139         int ls_wait;
140 } ldap_lazy_sem_t;
141
142 ldap_lazy_sem_t* thread_pool_sem = NULL;
143
144 int
145 ldap_lazy_sem_init( unsigned int value, unsigned int lazyness ) {
146         thread_pool_sem = (ldap_lazy_sem_t*) LDAP_CALLOC(1,
147                 sizeof( ldap_lazy_sem_t ));
148
149         if( thread_pool_sem == NULL ) return -1;
150
151         ldap_pvt_thread_mutex_init( &thread_pool_sem->ls_mutex );
152         ldap_pvt_thread_cond_init( &thread_pool_sem->ls_cond );
153         thread_pool_sem->ls_sem_value = value;
154         thread_pool_sem->ls_lazy_count = lazyness;
155         thread_pool_sem->ls_wait = 0;
156
157         return 0;
158 }
159
160 /*
161  * ldap_lazy_sem_wait is used if a caller is blockable(listener).
162  * Otherwise use ldap_lazy_sem_dec (worker)
163  */
164 int
165 ldap_lazy_sem_op_submit( ldap_lazy_sem_t* ls ) {
166         if ( ls == NULL ) return -1;
167
168         /* only worker thread has its thread ctx */
169         if ( ldap_pvt_thread_pool_context() ) {
170                 /* worker thread */
171                 return ldap_lazy_sem_dec( ls );
172         } else {
173                 /* listener */
174                 return ldap_lazy_sem_wait( ls );
175         }
176 }
177
178 /*
179  * test if given semaphore's count is zero.
180  * If 0, the caller is blocked 
181  * If not, the count is decremented.
182  */
183 int
184 ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls ) {
185         ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
186
187 lazy_sem_retry:
188         if ( ls->ls_sem_value <= 0 ) {
189                 /* no more avaliable resources */
190                 ls->ls_wait = 1;
191                 ldap_pvt_thread_cond_wait( &ls->ls_cond, &ls->ls_mutex );
192                 goto lazy_sem_retry;
193         } else {
194                 /* avaliable resources */
195                 ls->ls_sem_value--;
196         }
197
198         ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
199
200         return 0;
201 }
202
203 /*
204  * decrement the count without blocking
205  * even when the count becomes less than or equal to 0
206  */
207 int
208 ldap_lazy_sem_dec ( ldap_lazy_sem_t* ls ) {
209         ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
210
211         ls->ls_sem_value--;
212
213         ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
214
215         return 0;
216 }
217
218 /*
219  * Increment the count by one and test if it is greater or
220  * equal to lazyness. If it is, wake up a blocked thread.
221  */
222 int
223 ldap_lazy_sem_post( ldap_lazy_sem_t* ls ) {
224
225         if( ls == NULL ) return (-1);
226
227         ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
228
229         ls->ls_sem_value++;
230         if ( ls->ls_wait ) {
231                 if ( ls->ls_sem_value >= ls->ls_lazy_count ) {
232                         ls->ls_wait = 0;
233                         ldap_pvt_thread_cond_signal( &ls->ls_cond );
234                 }
235         }
236
237         ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
238
239         return 0;
240 }
241
242 int
243 ldap_pvt_thread_pool_init (
244         ldap_pvt_thread_pool_t *tpool,
245         int max_threads,
246         int max_pending )
247 {
248         ldap_pvt_thread_pool_t pool;
249         int rc;
250
251         *tpool = NULL;
252         pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
253                 sizeof(struct ldap_int_thread_pool_s));
254
255         if (pool == NULL) return(-1);
256
257         rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
258         if (rc != 0)
259                 return(rc);
260         rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
261         if (rc != 0)
262                 return(rc);
263         rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
264         if (rc != 0)
265                 return(rc);
266         pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
267         pool->ltp_max_count = max_threads;
268         pool->ltp_max_pending = max_pending;
269         LDAP_STAILQ_INIT(&pool->ltp_pending_list);
270         LDAP_SLIST_INIT(&pool->ltp_free_list);
271         LDAP_SLIST_INIT(&pool->ltp_active_list);
272         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
273         LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
274         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
275
276 #if 0
277         /* THIS WILL NOT WORK on some systems.  If the process
278          * forks after starting a thread, there is no guarantee
279          * that the thread will survive the fork.  For example,
280          * slapd forks in order to daemonize, and does so after
281          * calling ldap_pvt_thread_pool_init.  On some systems,
282          * this initial thread does not run in the child process,
283          * but ltp_open_count == 1, so two things happen: 
284          * 1) the first client connection fails, and 2) when
285          * slapd is kill'ed, it never terminates since it waits
286          * for all worker threads to exit. */
287
288         /* start up one thread, just so there is one. no need to
289          * lock the mutex right now, since no threads are running.
290          */
291         pool->ltp_open_count++;
292
293         ldap_pvt_thread_t thr;
294         rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
295
296         if( rc != 0) {
297                 /* couldn't start one?  then don't start any */
298                 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
299                 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool, 
300                         ldap_int_thread_pool_s, ltp_next);
301                 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
302                 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
303                 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
304                 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
305                 LDAP_FREE(pool);
306                 return(-1);
307         }
308 #endif
309
310         *tpool = pool;
311         return(0);
312 }
313
314 #define TID_HASH(tid, hash) do { unsigned i; \
315         unsigned char *ptr = (unsigned char *)&(tid); \
316         for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
317
318 int
319 ldap_pvt_thread_pool_submit (
320         ldap_pvt_thread_pool_t *tpool,
321         ldap_pvt_thread_start_t *start_routine, void *arg )
322 {
323         struct ldap_int_thread_pool_s *pool;
324         ldap_int_thread_ctx_t *ctx;
325         int need_thread = 0;
326         ldap_pvt_thread_t thr;
327
328         if (tpool == NULL)
329                 return(-1);
330
331         pool = *tpool;
332
333         if (pool == NULL)
334                 return(-1);
335
336         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
337         if ((pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING &&
338                 pool->ltp_state != LDAP_INT_THREAD_POOL_PAUSING)
339                 || (pool->ltp_max_pending > 0
340                         && pool->ltp_pending_count >= pool->ltp_max_pending))
341         {
342                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
343                 return(-1);
344         }
345         ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
346         if (ctx) {
347                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
348         } else {
349                 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
350                         sizeof(ldap_int_thread_ctx_t));
351                 if (ctx == NULL) {
352                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
353                         return(-1);
354                 }
355         }
356
357         ctx->ltc_start_routine = start_routine;
358         ctx->ltc_arg = arg;
359
360         pool->ltp_pending_count++;
361         LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
362         if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
363                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
364                 return(0);
365         }
366         ldap_pvt_thread_cond_signal(&pool->ltp_cond);
367         if ((pool->ltp_open_count <= 0
368 #if 0
369                         || pool->ltp_pending_count > 1
370 #endif
371                         || pool->ltp_open_count == pool->ltp_active_count)
372                 && (pool->ltp_max_count <= 0
373                         || pool->ltp_open_count < pool->ltp_max_count))
374         {
375                 pool->ltp_open_count++;
376                 pool->ltp_starting++;
377                 need_thread = 1;
378         }
379         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
380
381 #ifdef SLAP_SEM_LOAD_CONTROL
382         ldap_lazy_sem_op_submit( thread_pool_sem );
383 #endif
384
385         if (need_thread) {
386                 int rc;
387
388                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
389
390                 rc = ldap_pvt_thread_create( &thr, 1,
391                         ldap_int_thread_pool_wrapper, pool );
392                 if (rc == 0) {
393                         int hash;
394                         pool->ltp_starting--;
395
396                         /* assign this thread ID to a key slot; start
397                          * at the thread ID itself (mod LDAP_MAXTHR) and
398                          * look for an empty slot.
399                          */
400                         TID_HASH(thr, hash);
401                         for (rc = hash & (LDAP_MAXTHR-1);
402                                 !ldap_pvt_thread_equal(thread_keys[rc].id, tid_zero);
403                                 rc = (rc+1) & (LDAP_MAXTHR-1));
404                         thread_keys[rc].id = thr;
405                 } else {
406                         /* couldn't create thread.  back out of
407                          * ltp_open_count and check for even worse things.
408                          */
409                         pool->ltp_open_count--;
410                         pool->ltp_starting--;
411                         if (pool->ltp_open_count == 0) {
412                                 /* no open threads at all?!?
413                                  */
414                                 ldap_int_thread_ctx_t *ptr;
415                                 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
416                                         if (ptr == ctx) break;
417                                 if (ptr == ctx) {
418                                         /* no open threads, context not handled, so
419                                          * back out of ltp_pending_count, free the context,
420                                          * report the error.
421                                          */
422                                         LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx, 
423                                                 ldap_int_thread_ctx_s, ltc_next.q);
424                                         pool->ltp_pending_count++;
425                                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
426                                         LDAP_FREE(ctx);
427                                         return(-1);
428                                 }
429                         }
430                         /* there is another open thread, so this
431                          * context will be handled eventually.
432                          * continue on and signal that the context
433                          * is waiting.
434                          */
435                 }
436                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
437         }
438
439         return(0);
440 }
441
442 int
443 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
444 {
445         struct ldap_int_thread_pool_s *pool;
446
447         if (tpool == NULL)
448                 return(-1);
449
450         pool = *tpool;
451
452         if (pool == NULL)
453                 return(-1);
454
455         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
456         pool->ltp_max_count = max_threads;
457         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
458         return(0);
459 }
460
461 int
462 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
463 {
464         struct ldap_int_thread_pool_s *pool;
465         int count;
466
467         if (tpool == NULL)
468                 return(-1);
469
470         pool = *tpool;
471
472         if (pool == NULL)
473                 return(0);
474
475         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
476         count = pool->ltp_pending_count + pool->ltp_active_count;
477         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
478         return(count);
479 }
480
481 int
482 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
483 {
484         struct ldap_int_thread_pool_s *pool, *pptr;
485         long waiting;
486         ldap_int_thread_ctx_t *ctx;
487
488         if (tpool == NULL)
489                 return(-1);
490
491         pool = *tpool;
492
493         if (pool == NULL) return(-1);
494
495         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
496         LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
497                 if (pptr == pool) break;
498         if (pptr == pool)
499                 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
500                         ldap_int_thread_pool_s, ltp_next);
501         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
502
503         if (pool != pptr) return(-1);
504
505         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
506         pool->ltp_state = run_pending
507                 ? LDAP_INT_THREAD_POOL_FINISHING
508                 : LDAP_INT_THREAD_POOL_STOPPING;
509
510         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
511         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
512
513         do {
514                 ldap_pvt_thread_yield();
515                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
516                 waiting = pool->ltp_open_count;
517                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
518         } while (waiting > 0);
519
520         while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
521         {
522                 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
523                 LDAP_FREE(ctx);
524         }
525
526         while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
527         {
528                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
529                 LDAP_FREE(ctx);
530         }
531
532         ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
533         ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
534         ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
535         LDAP_FREE(pool);
536 #ifdef SLAP_SEM_LOAD_CONTROL
537         if ( thread_pool_sem ) {
538                 LDAP_FREE( thread_pool_sem );
539         }
540 #endif
541         return(0);
542 }
543
544 static void *
545 ldap_int_thread_pool_wrapper ( 
546         void *xpool )
547 {
548         struct ldap_int_thread_pool_s *pool = xpool;
549         ldap_int_thread_ctx_t *ctx;
550         ldap_int_thread_key_t ltc_key[MAXKEYS];
551         ldap_pvt_thread_t tid;
552         int i, keyslot, hash;
553
554         if (pool == NULL)
555                 return NULL;
556
557         for ( i=0; i<MAXKEYS; i++ ) {
558                 ltc_key[i].ltk_key = NULL;
559         }
560
561         tid = ldap_pvt_thread_self();
562
563         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
564
565         /* store pointer to our keys */
566         TID_HASH(tid, hash);
567         for (i = hash & (LDAP_MAXTHR-1);
568                                 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
569                                 i = (i+1) & (LDAP_MAXTHR-1));
570         thread_keys[i].ctx = ltc_key;
571         keyslot = i;
572
573         while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
574                 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
575                 if (ctx) {
576                         LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
577                 } else {
578                         if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
579                                 break;
580                         if (pool->ltp_max_count > 0
581                                 && pool->ltp_open_count > pool->ltp_max_count)
582                         {
583                                 /* too many threads running (can happen if the
584                                  * maximum threads value is set during ongoing
585                                  * operation using ldap_pvt_thread_pool_maxthreads)
586                                  * so let this thread die.
587                                  */
588                                 break;
589                         }
590
591                         /* we could check an idle timer here, and let the
592                          * thread die if it has been inactive for a while.
593                          * only die if there are other open threads (i.e.,
594                          * always have at least one thread open).  the check
595                          * should be like this:
596                          *   if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
597                          *       check timer, leave thread (break;)
598                          */
599
600                         if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING
601                                 || pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING)
602                         {
603                                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
604                         }
605
606                         continue;
607                 }
608
609                 pool->ltp_pending_count--;
610
611                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
612                 pool->ltp_active_count++;
613                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
614
615                 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
616
617 #ifdef SLAP_SEM_LOAD_CONTROL
618                 ldap_lazy_sem_post( thread_pool_sem );
619 #endif
620                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
621                 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
622                         ldap_int_thread_ctx_s, ltc_next.al);
623                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
624                 pool->ltp_active_count--;
625
626                 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
627                         if (pool->ltp_active_count < 2) {
628                                 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
629                         }
630                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
631                 }
632                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
633
634                 ldap_pvt_thread_yield();
635
636                 /* if we use an idle timer, here's
637                  * a good place to update it
638                  */
639
640                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
641         }
642
643         for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
644                 if (ltc_key[i].ltk_free)
645                         ltc_key[i].ltk_free(
646                                 ltc_key[i].ltk_key,
647                                 ltc_key[i].ltk_data );
648         }
649
650         thread_keys[keyslot].ctx = NULL;
651         thread_keys[keyslot].id = tid_zero;
652
653         pool->ltp_open_count--;
654         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
655
656         ldap_pvt_thread_exit(NULL);
657         return(NULL);
658 }
659
660 int
661 ldap_pvt_thread_pool_pause ( 
662         ldap_pvt_thread_pool_t *tpool )
663 {
664         struct ldap_int_thread_pool_s *pool;
665
666         if (tpool == NULL)
667                 return(-1);
668
669         pool = *tpool;
670
671         if (pool == NULL)
672                 return(0);
673
674         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
675
676         /* If someone else has already requested a pause, we have to wait */
677         while (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
678                 pool->ltp_pending_count++;
679                 pool->ltp_active_count--;
680                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
681                 pool->ltp_pending_count--;
682                 pool->ltp_active_count++;
683         }
684         /* Wait for everyone else to finish */
685         pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
686         while (pool->ltp_active_count > 1) {
687                 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
688         }
689         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
690         return(0);
691 }
692
693 int
694 ldap_pvt_thread_pool_resume ( 
695         ldap_pvt_thread_pool_t *tpool )
696 {
697         struct ldap_int_thread_pool_s *pool;
698
699         if (tpool == NULL)
700                 return(-1);
701
702         pool = *tpool;
703
704         if (pool == NULL)
705                 return(0);
706
707         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
708
709         pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
710         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
711         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
712         return(0);
713 }
714
715 int ldap_pvt_thread_pool_getkey(
716         void *xctx,
717         void *key,
718         void **data,
719         ldap_pvt_thread_pool_keyfree_t **kfree )
720 {
721         ldap_int_thread_key_t *ctx = xctx;
722         int i;
723
724         if ( !ctx || !data ) return EINVAL;
725
726         for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
727                 if ( ctx[i].ltk_key == key ) {
728                         *data = ctx[i].ltk_data;
729                         if ( kfree ) *kfree = ctx[i].ltk_free;
730                         return 0;
731                 }
732         }
733         return ENOENT;
734 }
735
736 int ldap_pvt_thread_pool_setkey(
737         void *xctx,
738         void *key,
739         void *data,
740         ldap_pvt_thread_pool_keyfree_t *kfree )
741 {
742         ldap_int_thread_key_t *ctx = xctx;
743         int i;
744
745         if ( !ctx || !key ) return EINVAL;
746
747         for ( i=0; i<MAXKEYS; i++ ) {
748                 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
749                         ctx[i].ltk_key = key;
750                         ctx[i].ltk_data = data;
751                         ctx[i].ltk_free = kfree;
752                         return 0;
753                 }
754         }
755         return ENOMEM;
756 }
757
758 /* Free all elements with this key, no matter which thread they're in.
759  * May only be called while the pool is paused.
760  */
761 void ldap_pvt_thread_pool_purgekey( void *key )
762 {
763         int i, j;
764         ldap_int_thread_key_t *ctx;
765
766         for ( i=0; i<LDAP_MAXTHR; i++ ) {
767                 if ( thread_keys[i].ctx ) {
768                         ctx = thread_keys[i].ctx;
769                         for ( j=0; j<MAXKEYS; j++ ) {
770                                 if ( ctx[j].ltk_key == key ) {
771                                         if (ctx[j].ltk_free)
772                                                 ctx[j].ltk_free( ctx[j].ltk_key, ctx[j].ltk_data );
773                                         ctx[j].ltk_key = NULL;
774                                         ctx[j].ltk_free = NULL;
775                                         break;
776                                 }
777                         }
778                 }
779         }
780 }
781
782 /*
783  * This is necessary if the caller does not have access to the
784  * thread context handle (for example, a slapd plugin calling
785  * slapi_search_internal()). No doubt it is more efficient to
786  * for the application to keep track of the thread context
787  * handles itself.
788  */
789 void *ldap_pvt_thread_pool_context( )
790 {
791         ldap_pvt_thread_t tid;
792         int i, hash;
793
794         tid = ldap_pvt_thread_self();
795         if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
796                 return ldap_int_main_thrctx;
797
798         TID_HASH( tid, hash );
799         for (i = hash & (LDAP_MAXTHR-1);
800                 !ldap_pvt_thread_equal(thread_keys[i].id, tid_zero) &&
801                 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
802                 i = (i+1) & (LDAP_MAXTHR-1));
803
804         return thread_keys[i].ctx;
805 }
806
807 void ldap_pvt_thread_pool_context_reset( void *vctx )
808 {
809         ldap_int_thread_key_t *ctx = vctx;
810         int i;
811
812         for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++) {
813                 if ( ctx[i].ltk_free )
814                         ctx[i].ltk_free( ctx[i].ltk_key, ctx[i].ltk_data );
815                 ctx[i].ltk_key = NULL;
816         }
817 }
818 #endif /* LDAP_THREAD_HAVE_TPOOL */