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