]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/tpool.c
Happy new year! (belated)
[openldap] / libraries / libldap_r / tpool.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2008 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_state_t;
39
40 /* Thread-specific key with data and optional free function */
41 typedef struct ldap_int_tpool_key_s {
42         void *ltk_key;
43         void *ltk_data;
44         ldap_pvt_thread_pool_keyfree_t *ltk_free;
45 } ldap_int_tpool_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
52 /* Max number of threads */
53 #define LDAP_MAXTHR     1024    /* must be a power of 2 */
54
55 /* Context: thread ID and thread-specific key/data pairs */
56 typedef struct ldap_int_thread_userctx_s {
57         ldap_pvt_thread_t ltu_id;
58         ldap_int_tpool_key_t ltu_key[MAXKEYS];
59 } ldap_int_thread_userctx_t;
60
61
62 /* Simple {thread ID -> context} hash table; key=ctx->ltu_id.
63  * Protected by ldap_pvt_thread_pool_mutex except during pauses,
64  * when it is read-only (used by pool_purgekey and pool_context).
65  * Protected by tpool->ltp_mutex during pauses.
66  */
67 static struct {
68         ldap_int_thread_userctx_t *ctx;
69         /* ctx is valid when not NULL or DELETED_THREAD_CTX */
70 #       define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
71 } thread_keys[LDAP_MAXTHR];
72
73 #define TID_HASH(tid, hash) do { \
74         unsigned const char *ptr_ = (unsigned const char *)&(tid); \
75         unsigned i_; \
76         for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
77                 (hash) += ((hash) << 5) ^ ptr_[i_]; \
78 } while(0)
79
80
81 /* Task for a thread to perform */
82 typedef struct ldap_int_thread_task_s {
83         union {
84                 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s) q;
85                 LDAP_SLIST_ENTRY(ldap_int_thread_task_s) l;
86         } ltt_next;
87         ldap_pvt_thread_start_t *ltt_start_routine;
88         void *ltt_arg;
89 } ldap_int_thread_task_t;
90
91 struct ldap_int_thread_pool_s {
92         LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
93
94         /* protect members below, and protect thread_keys[] during pauses */
95         ldap_pvt_thread_mutex_t ltp_mutex;
96
97         /* not paused and something to do for pool_<wrapper/pause/destroy>() */
98         ldap_pvt_thread_cond_t ltp_cond;
99
100         /* ltp_active_count <= 1 && ltp_pause */
101         ldap_pvt_thread_cond_t ltp_pcond;
102
103         /* pending tasks, and unused task objects */
104         LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ltp_pending_list;
105         LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list;
106
107         ldap_int_thread_pool_state_t ltp_state;
108
109         /* some active request needs to be the sole active request */
110         int ltp_pause;
111
112         long ltp_max_count;                     /* max number of threads in pool, or 0 */
113         long ltp_max_pending;           /* max pending or paused requests, or 0 */
114         long ltp_pending_count;         /* pending or paused requests */
115         long ltp_active_count;          /* active, not paused requests */
116         long ltp_open_count;            /* number of threads */
117         long ltp_starting;                      /* currenlty starting threads */
118 };
119
120 static int ldap_int_has_thread_pool = 0;
121 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
122         ldap_int_thread_pool_list =
123         LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
124
125 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
126
127 static void *ldap_int_thread_pool_wrapper( void *pool );
128
129 static ldap_pvt_thread_key_t    ldap_tpool_key;
130
131 /* Context of the main thread */
132 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
133
134 int
135 ldap_int_thread_pool_startup ( void )
136 {
137         ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
138         ldap_pvt_thread_key_create( &ldap_tpool_key );
139         return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
140 }
141
142 int
143 ldap_int_thread_pool_shutdown ( void )
144 {
145         struct ldap_int_thread_pool_s *pool;
146
147         while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
148                 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
149         }
150         ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
151         ldap_pvt_thread_key_destroy( ldap_tpool_key );
152         return(0);
153 }
154
155
156 /* Create a thread pool */
157 int
158 ldap_pvt_thread_pool_init (
159         ldap_pvt_thread_pool_t *tpool,
160         int max_threads,
161         int max_pending )
162 {
163         ldap_pvt_thread_pool_t pool;
164         int rc;
165
166         /* multiple pools are currently not supported (ITS#4943) */
167         assert(!ldap_int_has_thread_pool);
168
169         if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
170                 max_threads = 0;
171         if (max_pending < 0)
172                 max_pending = 0;
173
174         *tpool = NULL;
175         pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
176                 sizeof(struct ldap_int_thread_pool_s));
177
178         if (pool == NULL) return(-1);
179
180         rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
181         if (rc != 0)
182                 return(rc);
183         rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
184         if (rc != 0)
185                 return(rc);
186         rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
187         if (rc != 0)
188                 return(rc);
189
190         ldap_int_has_thread_pool = 1;
191         pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
192         pool->ltp_max_count = max_threads;
193         pool->ltp_max_pending = max_pending;
194         LDAP_STAILQ_INIT(&pool->ltp_pending_list);
195         LDAP_SLIST_INIT(&pool->ltp_free_list);
196         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
197         LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
198         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
199
200 #if 0
201         /* THIS WILL NOT WORK on some systems.  If the process
202          * forks after starting a thread, there is no guarantee
203          * that the thread will survive the fork.  For example,
204          * slapd forks in order to daemonize, and does so after
205          * calling ldap_pvt_thread_pool_init.  On some systems,
206          * this initial thread does not run in the child process,
207          * but ltp_open_count == 1, so two things happen: 
208          * 1) the first client connection fails, and 2) when
209          * slapd is kill'ed, it never terminates since it waits
210          * for all worker threads to exit. */
211
212         /* start up one thread, just so there is one. no need to
213          * lock the mutex right now, since no threads are running.
214          */
215         pool->ltp_open_count++;
216
217         ldap_pvt_thread_t thr;
218         rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
219
220         if( rc != 0) {
221                 /* couldn't start one?  then don't start any */
222                 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
223                 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool, 
224                         ldap_int_thread_pool_s, ltp_next);
225                 ldap_int_has_thread_pool = 0;
226                 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
227                 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
228                 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
229                 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
230                 LDAP_FREE(pool);
231                 return(-1);
232         }
233 #endif
234
235         *tpool = pool;
236         return(0);
237 }
238
239
240 /* Submit a task to be performed by the thread pool */
241 int
242 ldap_pvt_thread_pool_submit (
243         ldap_pvt_thread_pool_t *tpool,
244         ldap_pvt_thread_start_t *start_routine, void *arg )
245 {
246         struct ldap_int_thread_pool_s *pool;
247         ldap_int_thread_task_t *task;
248         ldap_pvt_thread_t thr;
249
250         if (tpool == NULL)
251                 return(-1);
252
253         pool = *tpool;
254
255         if (pool == NULL)
256                 return(-1);
257
258         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
259         if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
260                 || (pool->ltp_max_pending
261                         && pool->ltp_pending_count >= pool->ltp_max_pending))
262         {
263                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
264                 return(-1);
265         }
266
267         task = LDAP_SLIST_FIRST(&pool->ltp_free_list);
268         if (task) {
269                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l);
270         } else {
271                 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
272                 if (task == NULL) {
273                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
274                         return(-1);
275                 }
276         }
277
278         task->ltt_start_routine = start_routine;
279         task->ltt_arg = arg;
280
281         pool->ltp_pending_count++;
282         LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, task, ltt_next.q);
283         if (pool->ltp_pause) {
284                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
285                 return(0);
286         }
287         ldap_pvt_thread_cond_signal(&pool->ltp_cond);
288         if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
289                 && (pool->ltp_open_count <
290                         (pool->ltp_max_count ? pool->ltp_max_count : LDAP_MAXTHR)))
291         {
292                 pool->ltp_open_count++;
293                 pool->ltp_starting++;
294                 if (0 != ldap_pvt_thread_create(
295                         &thr, 1, ldap_int_thread_pool_wrapper, pool))
296                 {
297                         /* couldn't create thread.  back out of
298                          * ltp_open_count and check for even worse things.
299                          */
300                         pool->ltp_starting--;
301                         pool->ltp_open_count--;
302                         if (pool->ltp_open_count == 0) {
303                                 /* no open threads at all?!?
304                                  */
305                                 ldap_int_thread_task_t *ptr;
306
307                                 /* let pool_destroy know there are no more threads */
308                                 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
309
310                                 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltt_next.q)
311                                         if (ptr == task) break;
312                                 if (ptr == task) {
313                                         /* no open threads, task not handled, so
314                                          * back out of ltp_pending_count, free the task,
315                                          * report the error.
316                                          */
317                                         LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, task,
318                                                 ldap_int_thread_task_s, ltt_next.q);
319                                         pool->ltp_pending_count--;
320                                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
321                                         LDAP_FREE(task);
322                                         return(-1);
323                                 }
324                         }
325                         /* there is another open thread, so this
326                          * task will be handled eventually.
327                          * continue on, we have signalled that
328                          * the task is waiting.
329                          */
330                 }
331         }
332
333         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
334         return(0);
335 }
336
337 /* Set max #threads.  value <= 0 means max supported #threads (LDAP_MAXTHR) */
338 int
339 ldap_pvt_thread_pool_maxthreads(
340         ldap_pvt_thread_pool_t *tpool,
341         int max_threads )
342 {
343         struct ldap_int_thread_pool_s *pool;
344
345         if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
346                 max_threads = 0;
347
348         if (tpool == NULL)
349                 return(-1);
350
351         pool = *tpool;
352
353         if (pool == NULL)
354                 return(-1);
355
356         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
357         pool->ltp_max_count = max_threads;
358         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
359         return(0);
360 }
361
362 /* Inspect the pool */
363 int
364 ldap_pvt_thread_pool_query(
365         ldap_pvt_thread_pool_t *tpool,
366         ldap_pvt_thread_pool_param_t param,
367         void *value )
368 {
369         struct ldap_int_thread_pool_s   *pool;
370         int                             count = -1;
371
372         if ( tpool == NULL || value == NULL ) {
373                 return -1;
374         }
375
376         pool = *tpool;
377
378         if ( pool == NULL ) {
379                 return 0;
380         }
381
382         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
383         switch ( param ) {
384         case LDAP_PVT_THREAD_POOL_PARAM_MAX:
385                 count = pool->ltp_max_count;
386                 break;
387
388         case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
389                 count = pool->ltp_max_pending;
390                 break;
391
392         case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
393                 count = pool->ltp_open_count;
394                 break;
395
396         case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
397                 count = pool->ltp_starting;
398                 break;
399
400         case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
401                 count = pool->ltp_active_count;
402                 break;
403
404         case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
405                 count = pool->ltp_pause;
406                 break;
407
408         case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
409                 count = pool->ltp_pending_count;
410                 break;
411
412         case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
413                 count = pool->ltp_pending_count + pool->ltp_active_count;
414                 break;
415
416         case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
417                 break;
418
419         case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
420                 break;
421
422         case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
423                 break;
424
425         case LDAP_PVT_THREAD_POOL_PARAM_STATE: {
426                 static struct {
427                         char                            *name;
428                         ldap_int_thread_pool_state_t    state;
429                 }               str2state[] = {
430                         { "running",    LDAP_INT_THREAD_POOL_RUNNING },
431                         { "finishing",  LDAP_INT_THREAD_POOL_FINISHING },
432                         { "stopping",   LDAP_INT_THREAD_POOL_STOPPING },
433                         { NULL }
434                 };
435                 int             i;
436
437                 if ( pool->ltp_pause ) {
438                         *((char **)value) = "pausing";
439                 } else {
440                         for ( i = 0; str2state[ i ].name != NULL; i++ ) {
441                                 if ( str2state[ i ].state == pool->ltp_state ) {
442                                         break;
443                                 }
444                         }
445                         *((char **)value) = str2state[ i ].name;
446                 }
447                 if ( *((char **)value) != NULL ) {
448                         count = -2;
449                 }
450                 } break;
451
452         case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
453                 break;
454         }
455         ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex );
456
457         if ( count > -1 ) {
458                 *((int *)value) = count;
459         }
460
461         return ( count == -1 ? -1 : 0 );
462 }
463
464 /*
465  * wrapper for ldap_pvt_thread_pool_query(), left around
466  * for backwards compatibility
467  */
468 int
469 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
470 {
471         int     rc, count;
472
473         rc = ldap_pvt_thread_pool_query( tpool,
474                 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
475
476         if ( rc == 0 ) {
477                 return count;
478         }
479
480         return rc;
481 }
482
483 /* Destroy the pool after making its threads finish */
484 int
485 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
486 {
487         struct ldap_int_thread_pool_s *pool, *pptr;
488         ldap_int_thread_task_t *task;
489
490         if (tpool == NULL)
491                 return(-1);
492
493         pool = *tpool;
494
495         if (pool == NULL) return(-1);
496
497         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
498         LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
499                 if (pptr == pool) break;
500         if (pptr == pool)
501                 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
502                         ldap_int_thread_pool_s, ltp_next);
503         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
504
505         if (pool != pptr) return(-1);
506
507         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
508         pool->ltp_state = run_pending
509                 ? LDAP_INT_THREAD_POOL_FINISHING
510                 : LDAP_INT_THREAD_POOL_STOPPING;
511
512         while (pool->ltp_open_count) {
513                 if (!pool->ltp_pause)
514                         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
515                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
516         }
517         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
518
519         while ((task = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
520         {
521                 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q);
522                 LDAP_FREE(task);
523         }
524
525         while ((task = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
526         {
527                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltt_next.l);
528                 LDAP_FREE(task);
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         *tpool = NULL;
536         ldap_int_has_thread_pool = 0;
537         return(0);
538 }
539
540 /* Thread loop.  Accept and handle submitted tasks. */
541 static void *
542 ldap_int_thread_pool_wrapper ( 
543         void *xpool )
544 {
545         struct ldap_int_thread_pool_s *pool = xpool;
546         ldap_int_thread_task_t *task;
547         ldap_int_thread_userctx_t ctx, *kctx;
548         unsigned i, keyslot, hash;
549
550         assert(pool != NULL);
551
552         for ( i=0; i<MAXKEYS; i++ ) {
553                 ctx.ltu_key[i].ltk_key = NULL;
554         }
555
556         ctx.ltu_id = ldap_pvt_thread_self();
557         TID_HASH(ctx.ltu_id, hash);
558
559         ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
560
561         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
562
563         /* thread_keys[] is read-only when paused */
564         while (pool->ltp_pause)
565                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
566
567         /* find a key slot to give this thread ID and store a
568          * pointer to our keys there; start at the thread ID
569          * itself (mod LDAP_MAXTHR) and look for an empty slot.
570          */
571         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
572         for (keyslot = hash & (LDAP_MAXTHR-1);
573                 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
574                 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
575         thread_keys[keyslot].ctx = &ctx;
576         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
577
578         pool->ltp_starting--;
579
580         for (;;) {
581                 while (pool->ltp_pause)
582                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
583
584                 if (pool->ltp_state == LDAP_INT_THREAD_POOL_STOPPING)
585                         break;
586
587                 task = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
588                 if (task == NULL) {
589                         if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
590                                 break;
591
592                         if (pool->ltp_open_count >
593                                 (pool->ltp_max_count ? pool->ltp_max_count : LDAP_MAXTHR))
594                         {
595                                 /* too many threads running (can happen if the
596                                  * maximum threads value is set during ongoing
597                                  * operation using ldap_pvt_thread_pool_maxthreads)
598                                  * so let this thread die.
599                                  */
600                                 break;
601                         }
602
603                         /* we could check an idle timer here, and let the
604                          * thread die if it has been inactive for a while.
605                          * only die if there are other open threads (i.e.,
606                          * always have at least one thread open).  the check
607                          * should be like this:
608                          *   if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
609                          *       check timer, wait if ltp_pause, leave thread (break;)
610                          *
611                          * Just use pthread_cond_timedwait if we want to
612                          * check idle time.
613                          */
614
615                         assert(pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING);
616                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
617                         continue;
618                 }
619
620                 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltt_next.q);
621                 pool->ltp_pending_count--;
622                 pool->ltp_active_count++;
623                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
624
625                 task->ltt_start_routine(&ctx, task->ltt_arg);
626
627                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
628                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, task, ltt_next.l);
629                 pool->ltp_active_count--;
630                 /* let pool_pause know when it is the sole active thread */
631                 if (pool->ltp_active_count < 2)
632                         ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
633         }
634
635         /* The ltp_mutex lock protects ctx->ltu_key from pool_purgekey()
636          * during this call, since it prevents new pauses. */
637         ldap_pvt_thread_pool_context_reset(&ctx);
638
639         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
640         thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
641         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
642
643         pool->ltp_open_count--;
644         /* let pool_destroy know we're all done */
645         if (pool->ltp_open_count < 1)
646                 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
647
648         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
649
650         ldap_pvt_thread_exit(NULL);
651         return(NULL);
652 }
653
654 /* Pause the pool.  Return when all other threads are paused. */
655 int
656 ldap_pvt_thread_pool_pause ( 
657         ldap_pvt_thread_pool_t *tpool )
658 {
659         struct ldap_int_thread_pool_s *pool;
660
661         if (tpool == NULL)
662                 return(-1);
663
664         pool = *tpool;
665
666         if (pool == NULL)
667                 return(0);
668
669         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
670
671         /* If someone else has already requested a pause, we have to wait */
672         if (pool->ltp_pause) {
673                 pool->ltp_pending_count++;
674                 pool->ltp_active_count--;
675                 /* let the other pool_pause() know when it can proceed */
676                 if (pool->ltp_active_count < 2)
677                         ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
678                 do {
679                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
680                 } while (pool->ltp_pause);
681                 pool->ltp_pending_count--;
682                 pool->ltp_active_count++;
683         }
684
685         /* Wait for everyone else to pause or finish */
686         pool->ltp_pause = 1;
687         while (pool->ltp_active_count > 1) {
688                 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
689         }
690
691         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
692         return(0);
693 }
694
695 /* End a pause */
696 int
697 ldap_pvt_thread_pool_resume ( 
698         ldap_pvt_thread_pool_t *tpool )
699 {
700         struct ldap_int_thread_pool_s *pool;
701
702         if (tpool == NULL)
703                 return(-1);
704
705         pool = *tpool;
706
707         if (pool == NULL)
708                 return(0);
709
710         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
711         pool->ltp_pause = 0;
712         if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
713                 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
714         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
715         return(0);
716 }
717
718 /*
719  * Get the key's data and optionally free function in the given context.
720  */
721 int ldap_pvt_thread_pool_getkey(
722         void *xctx,
723         void *key,
724         void **data,
725         ldap_pvt_thread_pool_keyfree_t **kfree )
726 {
727         ldap_int_thread_userctx_t *ctx = xctx;
728         int i;
729
730         if ( !ctx || !key || !data ) return EINVAL;
731
732         for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
733                 if ( ctx->ltu_key[i].ltk_key == key ) {
734                         *data = ctx->ltu_key[i].ltk_data;
735                         if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
736                         return 0;
737                 }
738         }
739         return ENOENT;
740 }
741
742 static void
743 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
744 {
745         for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
746                 ctx->ltu_key[i] = ctx->ltu_key[i+1];
747         ctx->ltu_key[i].ltk_key = NULL;
748 }
749
750 /*
751  * Set or remove data for the key in the given context.
752  * key can be any unique pointer.
753  * kfree() is an optional function to free the data (but not the key):
754  *   pool_context_reset() and pool_purgekey() call kfree(key, data),
755  *   but pool_setkey() does not.  For pool_setkey() it is the caller's
756  *   responsibility to free any existing data with the same key.
757  *   kfree() must not call functions taking a tpool argument.
758  */
759 int ldap_pvt_thread_pool_setkey(
760         void *xctx,
761         void *key,
762         void *data,
763         ldap_pvt_thread_pool_keyfree_t *kfree,
764         void **olddatap,
765         ldap_pvt_thread_pool_keyfree_t **oldkfreep )
766 {
767         ldap_int_thread_userctx_t *ctx = xctx;
768         int i, found;
769
770         if ( !ctx || !key ) return EINVAL;
771
772         for ( i=found=0; i<MAXKEYS; i++ ) {
773                 if ( ctx->ltu_key[i].ltk_key == key ) {
774                         found = 1;
775                         break;
776                 } else if ( !ctx->ltu_key[i].ltk_key ) {
777                         break;
778                 }
779         }
780
781         if ( olddatap ) {
782                 if ( found ) {
783                         *olddatap = ctx->ltu_key[i].ltk_data;
784                 } else {
785                         *olddatap = NULL;
786                 }
787         }
788
789         if ( oldkfreep ) {
790                 if ( found ) {
791                         *oldkfreep = ctx->ltu_key[i].ltk_free;
792                 } else {
793                         *oldkfreep = 0;
794                 }
795         }
796
797         if ( data || kfree ) {
798                 if ( i>=MAXKEYS )
799                         return ENOMEM;
800                 ctx->ltu_key[i].ltk_key = key;
801                 ctx->ltu_key[i].ltk_data = data;
802                 ctx->ltu_key[i].ltk_free = kfree;
803         } else if ( found ) {
804                 clear_key_idx( ctx, i );
805         }
806
807         return 0;
808 }
809
810 /* Free all elements with this key, no matter which thread they're in.
811  * May only be called while the pool is paused.
812  */
813 void ldap_pvt_thread_pool_purgekey( void *key )
814 {
815         int i, j;
816         ldap_int_thread_userctx_t *ctx;
817
818         assert ( key != NULL );
819
820         for ( i=0; i<LDAP_MAXTHR; i++ ) {
821                 ctx = thread_keys[i].ctx;
822                 if ( ctx && ctx != DELETED_THREAD_CTX ) {
823                         for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
824                                 if ( ctx->ltu_key[j].ltk_key == key ) {
825                                         if (ctx->ltu_key[j].ltk_free)
826                                                 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
827                                                 ctx->ltu_key[j].ltk_data );
828                                         clear_key_idx( ctx, j );
829                                         break;
830                                 }
831                         }
832                 }
833         }
834 }
835
836 /*
837  * Find the context of the current thread.
838  * This is necessary if the caller does not have access to the
839  * thread context handle (for example, a slapd plugin calling
840  * slapi_search_internal()). No doubt it is more efficient
841  * for the application to keep track of the thread context
842  * handles itself.
843  */
844 void *ldap_pvt_thread_pool_context( )
845 {
846         void *ctx = NULL;
847
848         ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
849         return ctx ? ctx : &ldap_int_main_thrctx;
850 }
851
852 /*
853  * Free the context's keys.
854  * Must not call functions taking a tpool argument (because this
855  * thread already holds ltp_mutex when called from pool_wrapper()).
856  */
857 void ldap_pvt_thread_pool_context_reset( void *vctx )
858 {
859         ldap_int_thread_userctx_t *ctx = vctx;
860         int i;
861
862         for ( i=MAXKEYS-1; i>=0; i--) {
863                 if ( !ctx->ltu_key[i].ltk_key )
864                         continue;
865                 if ( ctx->ltu_key[i].ltk_free )
866                         ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
867                         ctx->ltu_key[i].ltk_data );
868                 ctx->ltu_key[i].ltk_key = NULL;
869         }
870 }
871
872 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
873 {
874         ldap_int_thread_userctx_t *ctx = vctx;
875
876         return ctx->ltu_id;
877 }
878 #endif /* LDAP_THREAD_HAVE_TPOOL */