]> 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-2014 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/signal.h>
21 #include <ac/stdarg.h>
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/time.h>
25 #include <ac/errno.h>
26
27 #include "ldap-int.h"
28 #include "ldap_pvt_thread.h" /* Get the thread interface */
29 #include "ldap_queue.h"
30 #define LDAP_THREAD_POOL_IMPLEMENTATION
31 #include "ldap_thr_debug.h"  /* May rename symbols defined below */
32
33 #ifndef LDAP_THREAD_HAVE_TPOOL
34
35 #ifndef CACHELINE
36 #define CACHELINE       64
37 #endif
38
39 /* Thread-specific key with data and optional free function */
40 typedef struct ldap_int_tpool_key_s {
41         void *ltk_key;
42         void *ltk_data;
43         ldap_pvt_thread_pool_keyfree_t *ltk_free;
44 } ldap_int_tpool_key_t;
45
46 /* Max number of thread-specific keys we store per thread.
47  * We don't expect to use many...
48  */
49 #define MAXKEYS 32
50
51 /* Max number of threads */
52 #define LDAP_MAXTHR     1024    /* must be a power of 2 */
53
54 /* (Theoretical) max number of pending requests */
55 #define MAX_PENDING (INT_MAX/2) /* INT_MAX - (room to avoid overflow) */
56
57 /* pool->ltp_pause values */
58 enum { NOT_PAUSED = 0, WANT_PAUSE = 1, PAUSED = 2 };
59
60 /* Context: thread ID and thread-specific key/data pairs */
61 typedef struct ldap_int_thread_userctx_s {
62         struct ldap_int_thread_poolq_s *ltu_pq;
63         ldap_pvt_thread_t ltu_id;
64         ldap_int_tpool_key_t ltu_key[MAXKEYS];
65 } ldap_int_thread_userctx_t;
66
67
68 /* Simple {thread ID -> context} hash table; key=ctx->ltu_id.
69  * Protected by ldap_pvt_thread_pool_mutex except during pauses,
70  * when it is read-only (used by pool_purgekey and pool_context).
71  * Protected by ldap_pvt_thread_pool_mutex.
72  */
73 static struct {
74         ldap_int_thread_userctx_t *ctx;
75         /* ctx is valid when not NULL or DELETED_THREAD_CTX */
76 #       define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
77 } thread_keys[LDAP_MAXTHR];
78
79 #define TID_HASH(tid, hash) do { \
80         unsigned const char *ptr_ = (unsigned const char *)&(tid); \
81         unsigned i_; \
82         for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
83                 (hash) += ((hash) << 5) ^ ptr_[i_]; \
84 } while(0)
85
86
87 /* Task for a thread to perform */
88 typedef struct ldap_int_thread_task_s {
89         union {
90                 LDAP_STAILQ_ENTRY(ldap_int_thread_task_s) q;
91                 LDAP_SLIST_ENTRY(ldap_int_thread_task_s) l;
92         } ltt_next;
93         ldap_pvt_thread_start_t *ltt_start_routine;
94         void *ltt_arg;
95 } ldap_int_thread_task_t;
96
97 typedef LDAP_STAILQ_HEAD(tcq, ldap_int_thread_task_s) ldap_int_tpool_plist_t;
98
99 struct ldap_int_thread_poolq_s {
100         void *ltp_free;
101
102         struct ldap_int_thread_pool_s *ltp_pool;
103
104         /* protect members below */
105         ldap_pvt_thread_mutex_t ltp_mutex;
106
107         /* not paused and something to do for pool_<wrapper/pause/destroy>()
108          * Used for normal pool operation, to synch between submitter and
109          * worker threads. Not used for pauses. In normal operation multiple
110          * queues can rendezvous without acquiring the main pool lock.
111          */
112         ldap_pvt_thread_cond_t ltp_cond;
113
114         /* ltp_pause == 0 ? &ltp_pending_list : &empty_pending_list,
115          * maintaned to reduce work for pool_wrapper()
116          */
117         ldap_int_tpool_plist_t *ltp_work_list;
118
119         /* pending tasks, and unused task objects */
120         ldap_int_tpool_plist_t ltp_pending_list;
121         LDAP_SLIST_HEAD(tcl, ldap_int_thread_task_s) ltp_free_list;
122
123         /* Max number of threads in this queue */
124         int ltp_max_count;
125
126         /* Max pending + paused + idle tasks, negated when ltp_finishing */
127         int ltp_max_pending;
128
129         int ltp_pending_count;          /* Pending + paused + idle tasks */
130         int ltp_active_count;           /* Active, not paused/idle tasks */
131         int ltp_open_count;                     /* Number of threads, negated when ltp_pause */
132         int ltp_starting;                       /* Currently starting threads */
133 };
134
135 struct ldap_int_thread_pool_s {
136         LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
137
138         struct ldap_int_thread_poolq_s **ltp_wqs;
139
140         /* number of poolqs */
141         int ltp_numqs;
142
143         /* protect members below */
144         ldap_pvt_thread_mutex_t ltp_mutex;
145
146         /* paused and waiting for resume
147          * When a pause is in effect all workers switch to waiting on
148          * this cond instead of their per-queue cond.
149          */
150         ldap_pvt_thread_cond_t ltp_cond;
151
152         /* ltp_active_queues < 1 && ltp_pause */
153         ldap_pvt_thread_cond_t ltp_pcond;
154
155         /* number of active queues */
156         int ltp_active_queues;
157
158         /* The pool is finishing, waiting for its threads to close.
159          * They close when ltp_pending_list is done.  pool_submit()
160          * rejects new tasks.  ltp_max_pending = -(its old value).
161          */
162         int ltp_finishing;
163
164         /* Some active task needs to be the sole active task.
165          * Atomic variable so ldap_pvt_thread_pool_pausing() can read it.
166          */
167         volatile sig_atomic_t ltp_pause;
168
169         /* Max number of threads in pool */
170         int ltp_max_count;
171
172         /* Configured max number of threads in pool, 0 for default (LDAP_MAXTHR) */
173         int ltp_conf_max_count;
174
175         /* Max pending + paused + idle tasks, negated when ltp_finishing */
176         int ltp_max_pending;
177 };
178
179 static ldap_int_tpool_plist_t empty_pending_list =
180         LDAP_STAILQ_HEAD_INITIALIZER(empty_pending_list);
181
182 static int ldap_int_has_thread_pool = 0;
183 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
184         ldap_int_thread_pool_list =
185         LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
186
187 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
188
189 static void *ldap_int_thread_pool_wrapper( void *pool );
190
191 static ldap_pvt_thread_key_t    ldap_tpool_key;
192
193 /* Context of the main thread */
194 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
195
196 int
197 ldap_int_thread_pool_startup ( void )
198 {
199         ldap_int_main_thrctx.ltu_id = ldap_pvt_thread_self();
200         ldap_pvt_thread_key_create( &ldap_tpool_key );
201         return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
202 }
203
204 int
205 ldap_int_thread_pool_shutdown ( void )
206 {
207         struct ldap_int_thread_pool_s *pool;
208
209         while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
210                 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
211         }
212         ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
213         ldap_pvt_thread_key_destroy( ldap_tpool_key );
214         return(0);
215 }
216
217
218 /* Create a thread pool */
219 int
220 ldap_pvt_thread_pool_init_q (
221         ldap_pvt_thread_pool_t *tpool,
222         int max_threads,
223         int max_pending,
224         int numqs )
225 {
226         ldap_pvt_thread_pool_t pool;
227         struct ldap_int_thread_poolq_s *pq;
228         int i, rc, rem_thr, rem_pend;
229
230         /* multiple pools are currently not supported (ITS#4943) */
231         assert(!ldap_int_has_thread_pool);
232
233         if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
234                 max_threads = 0;
235         if (! (1 <= max_pending && max_pending <= MAX_PENDING))
236                 max_pending = MAX_PENDING;
237
238         *tpool = NULL;
239         pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
240                 sizeof(struct ldap_int_thread_pool_s));
241
242         if (pool == NULL) return(-1);
243
244         pool->ltp_wqs = LDAP_MALLOC(numqs * sizeof(struct ldap_int_thread_poolq_s *));
245         if (pool->ltp_wqs == NULL) {
246                 LDAP_FREE(pool);
247                 return(-1);
248         }
249
250         for (i=0; i<numqs; i++) {
251                 char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1);
252                 if (ptr == NULL) {
253                         for (--i; i>=0; i--)
254                                 LDAP_FREE(pool->ltp_wqs[i]->ltp_free);
255                         LDAP_FREE(pool->ltp_wqs);
256                         LDAP_FREE(pool);
257                         return(-1);
258                 }
259                 pool->ltp_wqs[i] = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1));
260                 pool->ltp_wqs[i]->ltp_free = ptr;
261         }
262
263         pool->ltp_numqs = numqs;
264         pool->ltp_conf_max_count = max_threads;
265         if ( !max_threads )
266                 max_threads = LDAP_MAXTHR;
267
268         rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
269         if (rc != 0)
270                 return(rc);
271
272         rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
273         if (rc != 0)
274                 return(rc);
275
276         rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
277         if (rc != 0)
278                 return(rc);
279
280         rem_thr = max_threads % numqs;
281         rem_pend = max_pending % numqs;
282         for ( i=0; i<numqs; i++ ) {
283                 pq = pool->ltp_wqs[i];
284                 pq->ltp_pool = pool;
285                 rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
286                 if (rc != 0)
287                         return(rc);
288                 rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
289                 if (rc != 0)
290                         return(rc);
291                 LDAP_STAILQ_INIT(&pq->ltp_pending_list);
292                 pq->ltp_work_list = &pq->ltp_pending_list;
293                 LDAP_SLIST_INIT(&pq->ltp_free_list);
294
295                 pq->ltp_max_count = max_threads / numqs;
296                 if ( rem_thr ) {
297                         pq->ltp_max_count++;
298                         rem_thr--;
299                 }
300                 pq->ltp_max_pending = max_pending / numqs;
301                 if ( rem_pend ) {
302                         pq->ltp_max_pending++;
303                         rem_pend--;
304                 }
305         }
306
307         ldap_int_has_thread_pool = 1;
308
309         pool->ltp_max_count = max_threads;
310         pool->ltp_max_pending = max_pending;
311
312         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
313         LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
314         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
315
316         /* Start no threads just yet.  That can break if the process forks
317          * later, as slapd does in order to daemonize.  On at least POSIX,
318          * only the forking thread would survive in the child.  Yet fork()
319          * can't unlock/clean up other threads' locks and data structures,
320          * unless pthread_atfork() handlers have been set up to do so.
321          */
322
323         *tpool = pool;
324         return(0);
325 }
326
327 int
328 ldap_pvt_thread_pool_init (
329         ldap_pvt_thread_pool_t *tpool,
330         int max_threads,
331         int max_pending )
332 {
333         return ldap_pvt_thread_pool_init_q( tpool, max_threads, max_pending, 1 );
334 }
335
336 static int
337 ldap_int_poolq_hash(
338         struct ldap_int_thread_pool_s *pool,
339         void *arg )
340 {
341         int i = 0, j;
342         unsigned char *ptr = (unsigned char *)&arg;
343         /* dumb hash of arg to choose a queue */
344         for (j=0; j<sizeof(arg); j++)
345                 i += *ptr++;
346         i %= pool->ltp_numqs;
347         return i;
348 }
349
350 /* Submit a task to be performed by the thread pool */
351 int
352 ldap_pvt_thread_pool_submit (
353         ldap_pvt_thread_pool_t *tpool,
354         ldap_pvt_thread_start_t *start_routine, void *arg )
355 {
356         struct ldap_int_thread_pool_s *pool;
357         struct ldap_int_thread_poolq_s *pq;
358         ldap_int_thread_task_t *task;
359         ldap_pvt_thread_t thr;
360         int i, j;
361
362         if (tpool == NULL)
363                 return(-1);
364
365         pool = *tpool;
366
367         if (pool == NULL)
368                 return(-1);
369
370         if ( pool->ltp_numqs > 1 )
371                 i = ldap_int_poolq_hash( pool, arg );
372         else
373                 i = 0;
374
375         j = i;
376         while(1) {
377                 ldap_pvt_thread_mutex_lock(&pool->ltp_wqs[i]->ltp_mutex);
378                 if (pool->ltp_wqs[i]->ltp_pending_count < pool->ltp_wqs[i]->ltp_max_pending) {
379                         break;
380                 }
381                 ldap_pvt_thread_mutex_unlock(&pool->ltp_wqs[i]->ltp_mutex);
382                 i++;
383                 i %= pool->ltp_numqs;
384                 if ( i == j )
385                         return -1;
386         }
387
388         pq = pool->ltp_wqs[i];
389         task = LDAP_SLIST_FIRST(&pq->ltp_free_list);
390         if (task) {
391                 LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
392         } else {
393                 task = (ldap_int_thread_task_t *) LDAP_MALLOC(sizeof(*task));
394                 if (task == NULL)
395                         goto failed;
396         }
397
398         task->ltt_start_routine = start_routine;
399         task->ltt_arg = arg;
400
401         pq->ltp_pending_count++;
402         LDAP_STAILQ_INSERT_TAIL(&pq->ltp_pending_list, task, ltt_next.q);
403
404         if (pool->ltp_pause)
405                 goto done;
406
407         /* should we open (create) a thread? */
408         if (pq->ltp_open_count < pq->ltp_active_count+pq->ltp_pending_count &&
409                 pq->ltp_open_count < pq->ltp_max_count)
410         {
411                 pq->ltp_starting++;
412                 pq->ltp_open_count++;
413
414                 if (0 != ldap_pvt_thread_create(
415                         &thr, 1, ldap_int_thread_pool_wrapper, pq))
416                 {
417                         /* couldn't create thread.  back out of
418                          * ltp_open_count and check for even worse things.
419                          */
420                         pq->ltp_starting--;
421                         pq->ltp_open_count--;
422
423                         if (pq->ltp_open_count == 0) {
424                                 /* no open threads at all?!?
425                                  */
426                                 ldap_int_thread_task_t *ptr;
427
428                                 /* let pool_destroy know there are no more threads */
429                                 ldap_pvt_thread_cond_signal(&pq->ltp_cond);
430
431                                 LDAP_STAILQ_FOREACH(ptr, &pq->ltp_pending_list, ltt_next.q)
432                                         if (ptr == task) break;
433                                 if (ptr == task) {
434                                         /* no open threads, task not handled, so
435                                          * back out of ltp_pending_count, free the task,
436                                          * report the error.
437                                          */
438                                         pq->ltp_pending_count--;
439                                         LDAP_STAILQ_REMOVE(&pq->ltp_pending_list, task,
440                                                 ldap_int_thread_task_s, ltt_next.q);
441                                         LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task,
442                                                 ltt_next.l);
443                                         goto failed;
444                                 }
445                         }
446                         /* there is another open thread, so this
447                          * task will be handled eventually.
448                          */
449                 }
450         }
451         ldap_pvt_thread_cond_signal(&pq->ltp_cond);
452
453  done:
454         ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
455         return(0);
456
457  failed:
458         ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
459         return(-1);
460 }
461
462 static void *
463 no_task( void *ctx, void *arg )
464 {
465         return NULL;
466 }
467
468 /* Cancel a pending task that was previously submitted.
469  * Return 1 if the task was successfully cancelled, 0 if
470  * not found, -1 for invalid parameters
471  */
472 int
473 ldap_pvt_thread_pool_retract (
474         ldap_pvt_thread_pool_t *tpool,
475         ldap_pvt_thread_start_t *start_routine, void *arg )
476 {
477         struct ldap_int_thread_pool_s *pool;
478         struct ldap_int_thread_poolq_s *pq;
479         ldap_int_thread_task_t *task;
480         int i;
481
482         if (tpool == NULL)
483                 return(-1);
484
485         pool = *tpool;
486
487         if (pool == NULL)
488                 return(-1);
489
490         i = ldap_int_poolq_hash( pool, arg );
491         pq = pool->ltp_wqs[i];
492
493         ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
494         LDAP_STAILQ_FOREACH(task, &pq->ltp_pending_list, ltt_next.q)
495                 if (task->ltt_start_routine == start_routine &&
496                         task->ltt_arg == arg) {
497                         /* Could LDAP_STAILQ_REMOVE the task, but that
498                          * walks ltp_pending_list again to find it.
499                          */
500                         task->ltt_start_routine = no_task;
501                         task->ltt_arg = NULL;
502                         break;
503                 }
504         ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
505         return task != NULL;
506 }
507
508 /* Set number of work queues in this pool. Should not be
509  * more than the number of CPUs. */
510 int
511 ldap_pvt_thread_pool_queues(
512         ldap_pvt_thread_pool_t *tpool,
513         int numqs )
514 {
515         struct ldap_int_thread_pool_s *pool;
516         struct ldap_int_thread_poolq_s *pq;
517         int i, rc, rem_thr, rem_pend;
518
519         if (numqs < 1 || tpool == NULL)
520                 return(-1);
521
522         pool = *tpool;
523
524         if (pool == NULL)
525                 return(-1);
526
527         if (numqs < pool->ltp_numqs) {
528                 for (i=numqs; i<pool->ltp_numqs; i++)
529                         pool->ltp_wqs[i]->ltp_max_count = 0;
530         } else if (numqs > pool->ltp_numqs) {
531                 struct ldap_int_thread_poolq_s **wqs;
532                 wqs = LDAP_REALLOC(pool->ltp_wqs, numqs * sizeof(struct ldap_int_thread_poolq_s *));
533                 if (wqs == NULL)
534                         return(-1);
535                 pool->ltp_wqs = wqs;
536                 for (i=pool->ltp_numqs; i<numqs; i++) {
537                         char *ptr = LDAP_CALLOC(1, sizeof(struct ldap_int_thread_poolq_s) + CACHELINE-1);
538                         if (ptr == NULL) {
539                                 for (; i<numqs; i++)
540                                         pool->ltp_wqs[i] = NULL;
541                                 return(-1);
542                         }
543                         pq = (struct ldap_int_thread_poolq_s *)(((size_t)ptr + CACHELINE-1) & ~(CACHELINE-1));
544                         pq->ltp_free = ptr;
545                         pool->ltp_wqs[i] = pq;
546                         pq->ltp_pool = pool;
547                         rc = ldap_pvt_thread_mutex_init(&pq->ltp_mutex);
548                         if (rc != 0)
549                                 return(rc);
550                         rc = ldap_pvt_thread_cond_init(&pq->ltp_cond);
551                         if (rc != 0)
552                                 return(rc);
553                         LDAP_STAILQ_INIT(&pq->ltp_pending_list);
554                         pq->ltp_work_list = &pq->ltp_pending_list;
555                         LDAP_SLIST_INIT(&pq->ltp_free_list);
556                 }
557         }
558         rem_thr = pool->ltp_max_count % numqs;
559         rem_pend = pool->ltp_max_pending % numqs;
560         for ( i=0; i<numqs; i++ ) {
561                 pq = pool->ltp_wqs[i];
562                 pq->ltp_max_count = pool->ltp_max_count / numqs;
563                 if ( rem_thr ) {
564                         pq->ltp_max_count++;
565                         rem_thr--;
566                 }
567                 pq->ltp_max_pending = pool->ltp_max_pending / numqs;
568                 if ( rem_pend ) {
569                         pq->ltp_max_pending++;
570                         rem_pend--;
571                 }
572         }
573         pool->ltp_numqs = numqs;
574         return 0;
575 }
576
577 /* Set max #threads.  value <= 0 means max supported #threads (LDAP_MAXTHR) */
578 int
579 ldap_pvt_thread_pool_maxthreads(
580         ldap_pvt_thread_pool_t *tpool,
581         int max_threads )
582 {
583         struct ldap_int_thread_pool_s *pool;
584         struct ldap_int_thread_poolq_s *pq;
585         int remthr, i;
586
587         if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
588                 max_threads = 0;
589
590         if (tpool == NULL)
591                 return(-1);
592
593         pool = *tpool;
594
595         if (pool == NULL)
596                 return(-1);
597
598         pool->ltp_conf_max_count = max_threads;
599         if ( !max_threads )
600                 max_threads = LDAP_MAXTHR;
601         pool->ltp_max_count = max_threads;
602
603         remthr = max_threads % pool->ltp_numqs;
604         max_threads /= pool->ltp_numqs;
605
606         for (i=0; i<pool->ltp_numqs; i++) {
607                 pq = pool->ltp_wqs[i];
608                 pq->ltp_max_count = max_threads;
609                 if (remthr) {
610                         pq->ltp_max_count++;
611                         remthr--;
612                 }
613         }
614         return(0);
615 }
616
617 /* Inspect the pool */
618 int
619 ldap_pvt_thread_pool_query(
620         ldap_pvt_thread_pool_t *tpool,
621         ldap_pvt_thread_pool_param_t param,
622         void *value )
623 {
624         struct ldap_int_thread_pool_s   *pool;
625         int                             count = -1;
626
627         if ( tpool == NULL || value == NULL ) {
628                 return -1;
629         }
630
631         pool = *tpool;
632
633         if ( pool == NULL ) {
634                 return 0;
635         }
636
637         switch ( param ) {
638         case LDAP_PVT_THREAD_POOL_PARAM_MAX:
639                 count = pool->ltp_conf_max_count;
640                 break;
641
642         case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
643                 count = pool->ltp_max_pending;
644                 if (count < 0)
645                         count = -count;
646                 if (count == MAX_PENDING)
647                         count = 0;
648                 break;
649
650         case LDAP_PVT_THREAD_POOL_PARAM_PAUSING:
651                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
652                 count = (pool->ltp_pause != 0);
653                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
654                 break;
655
656         case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
657         case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
658         case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
659         case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
660         case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
661                 {
662                         int i;
663                         count = 0;
664                         for (i=0; i<pool->ltp_numqs; i++) {
665                                 struct ldap_int_thread_poolq_s *pq = pool->ltp_wqs[i];
666                                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
667                                 switch(param) {
668                                         case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
669                                                 count += pq->ltp_open_count;
670                                                 break;
671                                         case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
672                                                 count += pq->ltp_starting;
673                                                 break;
674                                         case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
675                                                 count += pq->ltp_active_count;
676                                                 break;
677                                         case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
678                                                 count += pq->ltp_pending_count;
679                                                 break;
680                                         case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
681                                                 count += pq->ltp_pending_count + pq->ltp_active_count;
682                                                 break;
683                                 }
684                                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
685                         }
686                         if (count < 0)
687                                 count = -count;
688                 }
689                 break;
690
691         case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
692                 break;
693
694         case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
695                 break;
696
697         case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
698                 break;
699
700         case LDAP_PVT_THREAD_POOL_PARAM_STATE:
701                 if (pool->ltp_pause)
702                         *((char **)value) = "pausing";
703                 else if (!pool->ltp_finishing)
704                         *((char **)value) = "running";
705                 else {
706                         int i;
707                         for (i=0; i<pool->ltp_numqs; i++)
708                                 if (pool->ltp_wqs[i]->ltp_pending_count) break;
709                         if (i<pool->ltp_numqs)
710                                 *((char **)value) = "finishing";
711                         else
712                                 *((char **)value) = "stopping";
713                 }
714                 break;
715
716         case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
717                 break;
718         }
719
720         if ( count > -1 ) {
721                 *((int *)value) = count;
722         }
723
724         return ( count == -1 ? -1 : 0 );
725 }
726
727 /*
728  * true if pool is pausing; does not lock any mutex to check.
729  * 0 if not pause, 1 if pause, -1 if error or no pool.
730  */
731 int
732 ldap_pvt_thread_pool_pausing( ldap_pvt_thread_pool_t *tpool )
733 {
734         int rc = -1;
735         struct ldap_int_thread_pool_s *pool;
736
737         if ( tpool != NULL && (pool = *tpool) != NULL ) {
738                 rc = (pool->ltp_pause != 0);
739         }
740
741         return rc;
742 }
743
744 /*
745  * wrapper for ldap_pvt_thread_pool_query(), left around
746  * for backwards compatibility
747  */
748 int
749 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
750 {
751         int     rc, count;
752
753         rc = ldap_pvt_thread_pool_query( tpool,
754                 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
755
756         if ( rc == 0 ) {
757                 return count;
758         }
759
760         return rc;
761 }
762
763 /* Destroy the pool after making its threads finish */
764 int
765 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
766 {
767         struct ldap_int_thread_pool_s *pool, *pptr;
768         struct ldap_int_thread_poolq_s *pq;
769         ldap_int_thread_task_t *task;
770         int i;
771
772         if (tpool == NULL)
773                 return(-1);
774
775         pool = *tpool;
776
777         if (pool == NULL) return(-1);
778
779         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
780         LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
781                 if (pptr == pool) break;
782         if (pptr == pool)
783                 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
784                         ldap_int_thread_pool_s, ltp_next);
785         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
786
787         if (pool != pptr) return(-1);
788
789         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
790
791         pool->ltp_finishing = 1;
792         if (pool->ltp_max_pending > 0)
793                 pool->ltp_max_pending = -pool->ltp_max_pending;
794
795         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
796         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
797
798         for (i=0; i<pool->ltp_numqs; i++) {
799                 pq = pool->ltp_wqs[i];
800                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
801                 if (pq->ltp_max_pending > 0)
802                         pq->ltp_max_pending = -pq->ltp_max_pending;
803                 if (!run_pending) {
804                         while ((task = LDAP_STAILQ_FIRST(&pq->ltp_pending_list)) != NULL) {
805                                 LDAP_STAILQ_REMOVE_HEAD(&pq->ltp_pending_list, ltt_next.q);
806                                 LDAP_FREE(task);
807                         }
808                         pq->ltp_pending_count = 0;
809                 }
810
811                 while (pq->ltp_open_count) {
812                         ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
813                         ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
814                 }
815
816                 while ((task = LDAP_SLIST_FIRST(&pq->ltp_free_list)) != NULL)
817                 {
818                         LDAP_SLIST_REMOVE_HEAD(&pq->ltp_free_list, ltt_next.l);
819                         LDAP_FREE(task);
820                 }
821                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
822                 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
823                 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
824         }
825
826         ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
827         ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
828         ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
829         LDAP_FREE(pool);
830         *tpool = NULL;
831         ldap_int_has_thread_pool = 0;
832         return(0);
833 }
834
835 /* Thread loop.  Accept and handle submitted tasks. */
836 static void *
837 ldap_int_thread_pool_wrapper ( 
838         void *xpool )
839 {
840         struct ldap_int_thread_poolq_s *pq = xpool;
841         struct ldap_int_thread_pool_s *pool = pq->ltp_pool;
842         ldap_int_thread_task_t *task;
843         ldap_int_tpool_plist_t *work_list;
844         ldap_int_thread_userctx_t ctx, *kctx;
845         unsigned i, keyslot, hash;
846         int pool_lock = 0, freeme = 0;
847
848         assert(pool != NULL);
849
850         for ( i=0; i<MAXKEYS; i++ ) {
851                 ctx.ltu_key[i].ltk_key = NULL;
852         }
853
854         ctx.ltu_pq = pq;
855         ctx.ltu_id = ldap_pvt_thread_self();
856         TID_HASH(ctx.ltu_id, hash);
857
858         ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
859
860         if (pool->ltp_pause) {
861                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
862                 /* thread_keys[] is read-only when paused */
863                 while (pool->ltp_pause)
864                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
865                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
866         }
867
868         /* find a key slot to give this thread ID and store a
869          * pointer to our keys there; start at the thread ID
870          * itself (mod LDAP_MAXTHR) and look for an empty slot.
871          */
872         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
873         for (keyslot = hash & (LDAP_MAXTHR-1);
874                 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
875                 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
876         thread_keys[keyslot].ctx = &ctx;
877         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
878
879         ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
880         pq->ltp_starting--;
881         pq->ltp_active_count++;
882
883         for (;;) {
884                 work_list = pq->ltp_work_list; /* help the compiler a bit */
885                 task = LDAP_STAILQ_FIRST(work_list);
886                 if (task == NULL) {     /* paused or no pending tasks */
887                         if (--(pq->ltp_active_count) < 1) {
888                                 if (pool->ltp_pause) {
889                                         ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
890                                         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
891                                         pool_lock = 1;
892                                         if (--(pool->ltp_active_queues) < 1) {
893                                                 /* Notify pool_pause it is the sole active thread. */
894                                                 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
895                                         }
896                                 }
897                         }
898
899                         do {
900                                 if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) {
901                                         /* Not paused, and either finishing or too many
902                                          * threads running (can happen if ltp_max_count
903                                          * was reduced).  Let this thread die.
904                                          */
905                                         goto done;
906                                 }
907
908                                 /* We could check an idle timer here, and let the
909                                  * thread die if it has been inactive for a while.
910                                  * Only die if there are other open threads (i.e.,
911                                  * always have at least one thread open).
912                                  * The check should be like this:
913                                  *   if (pool->ltp_open_count>1 && pool->ltp_starting==0)
914                                  *       check timer, wait if ltp_pause, leave thread;
915                                  *
916                                  * Just use pthread_cond_timedwait() if we want to
917                                  * check idle time.
918                                  */
919                                 if (pool_lock) {
920                                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
921                                         if (!pool->ltp_pause) {
922                                                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
923                                                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
924                                                 pool_lock = 0;
925                                         }
926                                 } else
927                                         ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
928
929                                 work_list = pq->ltp_work_list;
930                                 task = LDAP_STAILQ_FIRST(work_list);
931                         } while (task == NULL);
932
933                         if (pool_lock) {
934                                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
935                                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
936                                 pool_lock = 0;
937                         }
938                         pq->ltp_active_count++;
939                 }
940
941                 LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q);
942                 pq->ltp_pending_count--;
943                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
944
945                 task->ltt_start_routine(&ctx, task->ltt_arg);
946
947                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
948                 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l);
949         }
950  done:
951
952         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
953
954         /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey()
955          * during this call, since it prevents new pauses. */
956         ldap_pvt_thread_pool_context_reset(&ctx);
957
958         thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
959         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
960
961         pq->ltp_open_count--;
962         if (pq->ltp_open_count == 0) {
963                 if (pool->ltp_finishing)
964                         /* let pool_destroy know we're all done */
965                         ldap_pvt_thread_cond_signal(&pq->ltp_cond);
966                 else
967                         freeme = 1;
968         }
969
970         if (pool_lock)
971                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
972         else
973                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
974
975         if (freeme) {
976                 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
977                 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
978                 LDAP_FREE(pq->ltp_free);
979         }
980         ldap_pvt_thread_exit(NULL);
981         return(NULL);
982 }
983
984 /* Arguments > ltp_pause to handle_pause(,PAUSE_ARG()).  arg=PAUSE_ARG
985  * ensures (arg-ltp_pause) sets GO_* at need and keeps DO_PAUSE/GO_*.
986  */
987 #define GO_IDLE         8
988 #define GO_UNIDLE       16
989 #define CHECK_PAUSE     32      /* if ltp_pause: GO_IDLE; wait; GO_UNIDLE */
990 #define DO_PAUSE        64      /* CHECK_PAUSE; pause the pool */
991 #define PAUSE_ARG(a) \
992                 ((a) | ((a) & (GO_IDLE|GO_UNIDLE) ? GO_IDLE-1 : CHECK_PAUSE))
993
994 static int
995 handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type )
996 {
997         struct ldap_int_thread_pool_s *pool;
998         struct ldap_int_thread_poolq_s *pq;
999         int ret = 0, pause, max_ltp_pause;
1000
1001         if (tpool == NULL)
1002                 return(-1);
1003
1004         pool = *tpool;
1005
1006         if (pool == NULL)
1007                 return(0);
1008
1009         if (pause_type == CHECK_PAUSE && !pool->ltp_pause)
1010                 return(0);
1011
1012         {
1013                 ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context();
1014                 pq = ctx->ltu_pq;
1015         }
1016
1017         /* Let pool_unidle() ignore requests for new pauses */
1018         max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED;
1019
1020         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1021
1022         pause = pool->ltp_pause;        /* NOT_PAUSED, WANT_PAUSE or PAUSED */
1023
1024         /* If ltp_pause and not GO_IDLE|GO_UNIDLE: Set GO_IDLE,GO_UNIDLE */
1025         pause_type -= pause;
1026
1027         if (pause_type & GO_IDLE) {
1028                 int do_pool = 0;
1029                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1030                 pq->ltp_pending_count++;
1031                 pq->ltp_active_count--;
1032                 if (pause && pq->ltp_active_count < 1) {
1033                         do_pool = 1;
1034                 }
1035                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1036                 if (do_pool) {
1037                         pool->ltp_active_queues--;
1038                         if (pool->ltp_active_queues < 1)
1039                         /* Tell the task waiting to DO_PAUSE it can proceed */
1040                                 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
1041                 }
1042         }
1043
1044         if (pause_type & GO_UNIDLE) {
1045                 /* Wait out pause if any, then cancel GO_IDLE */
1046                 if (pause > max_ltp_pause) {
1047                         ret = 1;
1048                         do {
1049                                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
1050                         } while (pool->ltp_pause > max_ltp_pause);
1051                 }
1052                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1053                 pq->ltp_pending_count--;
1054                 pq->ltp_active_count++;
1055                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1056         }
1057
1058         if (pause_type & DO_PAUSE) {
1059                 int i, j;
1060                 /* Tell everyone else to pause or finish, then await that */
1061                 ret = 0;
1062                 assert(!pool->ltp_pause);
1063                 pool->ltp_pause = WANT_PAUSE;
1064                 pool->ltp_active_queues = 0;
1065
1066                 for (i=0; i<pool->ltp_numqs; i++)
1067                         if (pool->ltp_wqs[i] == pq) break;
1068
1069                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1070                 /* temporarily remove ourself from active count */
1071                 pq->ltp_active_count--;
1072
1073                 j=i;
1074                 do {
1075                         pq = pool->ltp_wqs[j];
1076                         if (j != i)
1077                                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1078
1079                         /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test,
1080                          * and do not finish threads in ldap_pvt_thread_pool_wrapper() */
1081                         pq->ltp_open_count = -pq->ltp_open_count;
1082                         /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
1083                         pq->ltp_work_list = &empty_pending_list;
1084
1085                         if (pq->ltp_active_count > 0)
1086                                 pool->ltp_active_queues++;
1087
1088                         ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1089                         if (pool->ltp_numqs > 1) {
1090                                 j++;
1091                                 j %= pool->ltp_numqs;
1092                         }
1093                 } while (j != i);
1094
1095                 /* Wait for this task to become the sole active task */
1096                 while (pool->ltp_active_queues > 0)
1097                         ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
1098
1099                 /* restore us to active count */
1100                 pool->ltp_wqs[i]->ltp_active_count++;
1101
1102                 assert(pool->ltp_pause == WANT_PAUSE);
1103                 pool->ltp_pause = PAUSED;
1104         }
1105         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1106
1107         return(ret);
1108 }
1109
1110 /* Consider this task idle: It will not block pool_pause() in other tasks. */
1111 void
1112 ldap_pvt_thread_pool_idle( ldap_pvt_thread_pool_t *tpool )
1113 {
1114         handle_pause(tpool, PAUSE_ARG(GO_IDLE));
1115 }
1116
1117 /* Cancel pool_idle(). If the pool is paused, wait it out first. */
1118 void
1119 ldap_pvt_thread_pool_unidle( ldap_pvt_thread_pool_t *tpool )
1120 {
1121         handle_pause(tpool, PAUSE_ARG(GO_UNIDLE));
1122 }
1123
1124 /*
1125  * If a pause was requested, wait for it.  If several threads
1126  * are waiting to pause, let through one or more pauses.
1127  * The calling task must be active, not idle.
1128  * Return 1 if we waited, 0 if not, -1 at parameter error.
1129  */
1130 int
1131 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t *tpool )
1132 {
1133         return handle_pause(tpool, PAUSE_ARG(CHECK_PAUSE));
1134 }
1135
1136 /*
1137  * Pause the pool.  The calling task must be active, not idle.
1138  * Return when all other tasks are paused or idle.
1139  */
1140 int
1141 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
1142 {
1143         return handle_pause(tpool, PAUSE_ARG(DO_PAUSE));
1144 }
1145
1146 /* End a pause */
1147 int
1148 ldap_pvt_thread_pool_resume ( 
1149         ldap_pvt_thread_pool_t *tpool )
1150 {
1151         struct ldap_int_thread_pool_s *pool;
1152         struct ldap_int_thread_poolq_s *pq;
1153         int i;
1154
1155         if (tpool == NULL)
1156                 return(-1);
1157
1158         pool = *tpool;
1159
1160         if (pool == NULL)
1161                 return(0);
1162
1163         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1164         assert(pool->ltp_pause == PAUSED);
1165         pool->ltp_pause = 0;
1166         for (i=0; i<pool->ltp_numqs; i++) {
1167                 pq = pool->ltp_wqs[i];
1168                 if (pq->ltp_open_count <= 0) /* true when paused, but be paranoid */
1169                         pq->ltp_open_count = -pq->ltp_open_count;
1170                 pq->ltp_work_list = &pq->ltp_pending_list;
1171                 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
1172         }
1173         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
1174         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1175         return(0);
1176 }
1177
1178 /*
1179  * Get the key's data and optionally free function in the given context.
1180  */
1181 int ldap_pvt_thread_pool_getkey(
1182         void *xctx,
1183         void *key,
1184         void **data,
1185         ldap_pvt_thread_pool_keyfree_t **kfree )
1186 {
1187         ldap_int_thread_userctx_t *ctx = xctx;
1188         int i;
1189
1190         if ( !ctx || !key || !data ) return EINVAL;
1191
1192         for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
1193                 if ( ctx->ltu_key[i].ltk_key == key ) {
1194                         *data = ctx->ltu_key[i].ltk_data;
1195                         if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
1196                         return 0;
1197                 }
1198         }
1199         return ENOENT;
1200 }
1201
1202 static void
1203 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
1204 {
1205         for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
1206                 ctx->ltu_key[i] = ctx->ltu_key[i+1];
1207         ctx->ltu_key[i].ltk_key = NULL;
1208 }
1209
1210 /*
1211  * Set or remove data for the key in the given context.
1212  * key can be any unique pointer.
1213  * kfree() is an optional function to free the data (but not the key):
1214  *   pool_context_reset() and pool_purgekey() call kfree(key, data),
1215  *   but pool_setkey() does not.  For pool_setkey() it is the caller's
1216  *   responsibility to free any existing data with the same key.
1217  *   kfree() must not call functions taking a tpool argument.
1218  */
1219 int ldap_pvt_thread_pool_setkey(
1220         void *xctx,
1221         void *key,
1222         void *data,
1223         ldap_pvt_thread_pool_keyfree_t *kfree,
1224         void **olddatap,
1225         ldap_pvt_thread_pool_keyfree_t **oldkfreep )
1226 {
1227         ldap_int_thread_userctx_t *ctx = xctx;
1228         int i, found;
1229
1230         if ( !ctx || !key ) return EINVAL;
1231
1232         for ( i=found=0; i<MAXKEYS; i++ ) {
1233                 if ( ctx->ltu_key[i].ltk_key == key ) {
1234                         found = 1;
1235                         break;
1236                 } else if ( !ctx->ltu_key[i].ltk_key ) {
1237                         break;
1238                 }
1239         }
1240
1241         if ( olddatap ) {
1242                 if ( found ) {
1243                         *olddatap = ctx->ltu_key[i].ltk_data;
1244                 } else {
1245                         *olddatap = NULL;
1246                 }
1247         }
1248
1249         if ( oldkfreep ) {
1250                 if ( found ) {
1251                         *oldkfreep = ctx->ltu_key[i].ltk_free;
1252                 } else {
1253                         *oldkfreep = 0;
1254                 }
1255         }
1256
1257         if ( data || kfree ) {
1258                 if ( i>=MAXKEYS )
1259                         return ENOMEM;
1260                 ctx->ltu_key[i].ltk_key = key;
1261                 ctx->ltu_key[i].ltk_data = data;
1262                 ctx->ltu_key[i].ltk_free = kfree;
1263         } else if ( found ) {
1264                 clear_key_idx( ctx, i );
1265         }
1266
1267         return 0;
1268 }
1269
1270 /* Free all elements with this key, no matter which thread they're in.
1271  * May only be called while the pool is paused.
1272  */
1273 void ldap_pvt_thread_pool_purgekey( void *key )
1274 {
1275         int i, j;
1276         ldap_int_thread_userctx_t *ctx;
1277
1278         assert ( key != NULL );
1279
1280         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
1281         for ( i=0; i<LDAP_MAXTHR; i++ ) {
1282                 ctx = thread_keys[i].ctx;
1283                 if ( ctx && ctx != DELETED_THREAD_CTX ) {
1284                         for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
1285                                 if ( ctx->ltu_key[j].ltk_key == key ) {
1286                                         if (ctx->ltu_key[j].ltk_free)
1287                                                 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
1288                                                 ctx->ltu_key[j].ltk_data );
1289                                         clear_key_idx( ctx, j );
1290                                         break;
1291                                 }
1292                         }
1293                 }
1294         }
1295         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
1296 }
1297
1298 /*
1299  * Find the context of the current thread.
1300  * This is necessary if the caller does not have access to the
1301  * thread context handle (for example, a slapd plugin calling
1302  * slapi_search_internal()). No doubt it is more efficient
1303  * for the application to keep track of the thread context
1304  * handles itself.
1305  */
1306 void *ldap_pvt_thread_pool_context( )
1307 {
1308         void *ctx = NULL;
1309
1310         ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
1311         return ctx ? ctx : (void *) &ldap_int_main_thrctx;
1312 }
1313
1314 /*
1315  * Free the context's keys.
1316  * Must not call functions taking a tpool argument (because this
1317  * thread already holds ltp_mutex when called from pool_wrapper()).
1318  */
1319 void ldap_pvt_thread_pool_context_reset( void *vctx )
1320 {
1321         ldap_int_thread_userctx_t *ctx = vctx;
1322         int i;
1323
1324         for ( i=MAXKEYS-1; i>=0; i--) {
1325                 if ( !ctx->ltu_key[i].ltk_key )
1326                         continue;
1327                 if ( ctx->ltu_key[i].ltk_free )
1328                         ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
1329                         ctx->ltu_key[i].ltk_data );
1330                 ctx->ltu_key[i].ltk_key = NULL;
1331         }
1332 }
1333
1334 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
1335 {
1336         ldap_int_thread_userctx_t *ctx = vctx;
1337
1338         return ctx->ltu_id;
1339 }
1340 #endif /* LDAP_THREAD_HAVE_TPOOL */