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