]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/tpool.c
Merge remote-tracking branch 'origin/mdb.master'
[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         for (i=0; i<pool->ltp_numqs; i++) {
830                 pq = pool->ltp_wqs[i];
831                 if (pq->ltp_free) {
832                         LDAP_FREE(pq->ltp_free);
833                 }
834         }
835         LDAP_FREE(pool->ltp_wqs);
836         LDAP_FREE(pool);
837         *tpool = NULL;
838         ldap_int_has_thread_pool = 0;
839         return(0);
840 }
841
842 /* Thread loop.  Accept and handle submitted tasks. */
843 static void *
844 ldap_int_thread_pool_wrapper ( 
845         void *xpool )
846 {
847         struct ldap_int_thread_poolq_s *pq = xpool;
848         struct ldap_int_thread_pool_s *pool = pq->ltp_pool;
849         ldap_int_thread_task_t *task;
850         ldap_int_tpool_plist_t *work_list;
851         ldap_int_thread_userctx_t ctx, *kctx;
852         unsigned i, keyslot, hash;
853         int pool_lock = 0, freeme = 0;
854
855         assert(pool != NULL);
856
857         for ( i=0; i<MAXKEYS; i++ ) {
858                 ctx.ltu_key[i].ltk_key = NULL;
859         }
860
861         ctx.ltu_pq = pq;
862         ctx.ltu_id = ldap_pvt_thread_self();
863         TID_HASH(ctx.ltu_id, hash);
864
865         ldap_pvt_thread_key_setdata( ldap_tpool_key, &ctx );
866
867         if (pool->ltp_pause) {
868                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
869                 /* thread_keys[] is read-only when paused */
870                 while (pool->ltp_pause)
871                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
872                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
873         }
874
875         /* find a key slot to give this thread ID and store a
876          * pointer to our keys there; start at the thread ID
877          * itself (mod LDAP_MAXTHR) and look for an empty slot.
878          */
879         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
880         for (keyslot = hash & (LDAP_MAXTHR-1);
881                 (kctx = thread_keys[keyslot].ctx) && kctx != DELETED_THREAD_CTX;
882                 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
883         thread_keys[keyslot].ctx = &ctx;
884         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
885
886         ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
887         pq->ltp_starting--;
888         pq->ltp_active_count++;
889
890         for (;;) {
891                 work_list = pq->ltp_work_list; /* help the compiler a bit */
892                 task = LDAP_STAILQ_FIRST(work_list);
893                 if (task == NULL) {     /* paused or no pending tasks */
894                         if (--(pq->ltp_active_count) < 1) {
895                                 if (pool->ltp_pause) {
896                                         ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
897                                         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
898                                         pool_lock = 1;
899                                         if (--(pool->ltp_active_queues) < 1) {
900                                                 /* Notify pool_pause it is the sole active thread. */
901                                                 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
902                                         }
903                                 }
904                         }
905
906                         do {
907                                 if (pool->ltp_finishing || pq->ltp_open_count > pq->ltp_max_count) {
908                                         /* Not paused, and either finishing or too many
909                                          * threads running (can happen if ltp_max_count
910                                          * was reduced).  Let this thread die.
911                                          */
912                                         goto done;
913                                 }
914
915                                 /* We could check an idle timer here, and let the
916                                  * thread die if it has been inactive for a while.
917                                  * Only die if there are other open threads (i.e.,
918                                  * always have at least one thread open).
919                                  * The check should be like this:
920                                  *   if (pool->ltp_open_count>1 && pool->ltp_starting==0)
921                                  *       check timer, wait if ltp_pause, leave thread;
922                                  *
923                                  * Just use pthread_cond_timedwait() if we want to
924                                  * check idle time.
925                                  */
926                                 if (pool_lock) {
927                                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
928                                         if (!pool->ltp_pause) {
929                                                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
930                                                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
931                                                 pool_lock = 0;
932                                         }
933                                 } else
934                                         ldap_pvt_thread_cond_wait(&pq->ltp_cond, &pq->ltp_mutex);
935
936                                 work_list = pq->ltp_work_list;
937                                 task = LDAP_STAILQ_FIRST(work_list);
938                         } while (task == NULL);
939
940                         if (pool_lock) {
941                                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
942                                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
943                                 pool_lock = 0;
944                         }
945                         pq->ltp_active_count++;
946                 }
947
948                 LDAP_STAILQ_REMOVE_HEAD(work_list, ltt_next.q);
949                 pq->ltp_pending_count--;
950                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
951
952                 task->ltt_start_routine(&ctx, task->ltt_arg);
953
954                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
955                 LDAP_SLIST_INSERT_HEAD(&pq->ltp_free_list, task, ltt_next.l);
956         }
957  done:
958
959         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
960
961         /* The pool_mutex lock protects ctx->ltu_key from pool_purgekey()
962          * during this call, since it prevents new pauses. */
963         ldap_pvt_thread_pool_context_reset(&ctx);
964
965         thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
966         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
967
968         pq->ltp_open_count--;
969         if (pq->ltp_open_count == 0) {
970                 if (pool->ltp_finishing)
971                         /* let pool_destroy know we're all done */
972                         ldap_pvt_thread_cond_signal(&pq->ltp_cond);
973                 else
974                         freeme = 1;
975         }
976
977         if (pool_lock)
978                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
979         else
980                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
981
982         if (freeme) {
983                 ldap_pvt_thread_cond_destroy(&pq->ltp_cond);
984                 ldap_pvt_thread_mutex_destroy(&pq->ltp_mutex);
985                 LDAP_FREE(pq->ltp_free);
986                 pq->ltp_free = NULL;
987         }
988         ldap_pvt_thread_exit(NULL);
989         return(NULL);
990 }
991
992 /* Arguments > ltp_pause to handle_pause(,PAUSE_ARG()).  arg=PAUSE_ARG
993  * ensures (arg-ltp_pause) sets GO_* at need and keeps DO_PAUSE/GO_*.
994  */
995 #define GO_IDLE         8
996 #define GO_UNIDLE       16
997 #define CHECK_PAUSE     32      /* if ltp_pause: GO_IDLE; wait; GO_UNIDLE */
998 #define DO_PAUSE        64      /* CHECK_PAUSE; pause the pool */
999 #define PAUSE_ARG(a) \
1000                 ((a) | ((a) & (GO_IDLE|GO_UNIDLE) ? GO_IDLE-1 : CHECK_PAUSE))
1001
1002 static int
1003 handle_pause( ldap_pvt_thread_pool_t *tpool, int pause_type )
1004 {
1005         struct ldap_int_thread_pool_s *pool;
1006         struct ldap_int_thread_poolq_s *pq;
1007         int ret = 0, pause, max_ltp_pause;
1008
1009         if (tpool == NULL)
1010                 return(-1);
1011
1012         pool = *tpool;
1013
1014         if (pool == NULL)
1015                 return(0);
1016
1017         if (pause_type == CHECK_PAUSE && !pool->ltp_pause)
1018                 return(0);
1019
1020         {
1021                 ldap_int_thread_userctx_t *ctx = ldap_pvt_thread_pool_context();
1022                 pq = ctx->ltu_pq;
1023         }
1024
1025         /* Let pool_unidle() ignore requests for new pauses */
1026         max_ltp_pause = pause_type==PAUSE_ARG(GO_UNIDLE) ? WANT_PAUSE : NOT_PAUSED;
1027
1028         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1029
1030         pause = pool->ltp_pause;        /* NOT_PAUSED, WANT_PAUSE or PAUSED */
1031
1032         /* If ltp_pause and not GO_IDLE|GO_UNIDLE: Set GO_IDLE,GO_UNIDLE */
1033         pause_type -= pause;
1034
1035         if (pause_type & GO_IDLE) {
1036                 int do_pool = 0;
1037                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1038                 pq->ltp_pending_count++;
1039                 pq->ltp_active_count--;
1040                 if (pause && pq->ltp_active_count < 1) {
1041                         do_pool = 1;
1042                 }
1043                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1044                 if (do_pool) {
1045                         pool->ltp_active_queues--;
1046                         if (pool->ltp_active_queues < 1)
1047                         /* Tell the task waiting to DO_PAUSE it can proceed */
1048                                 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
1049                 }
1050         }
1051
1052         if (pause_type & GO_UNIDLE) {
1053                 /* Wait out pause if any, then cancel GO_IDLE */
1054                 if (pause > max_ltp_pause) {
1055                         ret = 1;
1056                         do {
1057                                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
1058                         } while (pool->ltp_pause > max_ltp_pause);
1059                 }
1060                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1061                 pq->ltp_pending_count--;
1062                 pq->ltp_active_count++;
1063                 ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1064         }
1065
1066         if (pause_type & DO_PAUSE) {
1067                 int i, j;
1068                 /* Tell everyone else to pause or finish, then await that */
1069                 ret = 0;
1070                 assert(!pool->ltp_pause);
1071                 pool->ltp_pause = WANT_PAUSE;
1072                 pool->ltp_active_queues = 0;
1073
1074                 for (i=0; i<pool->ltp_numqs; i++)
1075                         if (pool->ltp_wqs[i] == pq) break;
1076
1077                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1078                 /* temporarily remove ourself from active count */
1079                 pq->ltp_active_count--;
1080
1081                 j=i;
1082                 do {
1083                         pq = pool->ltp_wqs[j];
1084                         if (j != i)
1085                                 ldap_pvt_thread_mutex_lock(&pq->ltp_mutex);
1086
1087                         /* Let ldap_pvt_thread_pool_submit() through to its ltp_pause test,
1088                          * and do not finish threads in ldap_pvt_thread_pool_wrapper() */
1089                         pq->ltp_open_count = -pq->ltp_open_count;
1090                         /* Hide pending tasks from ldap_pvt_thread_pool_wrapper() */
1091                         pq->ltp_work_list = &empty_pending_list;
1092
1093                         if (pq->ltp_active_count > 0)
1094                                 pool->ltp_active_queues++;
1095
1096                         ldap_pvt_thread_mutex_unlock(&pq->ltp_mutex);
1097                         if (pool->ltp_numqs > 1) {
1098                                 j++;
1099                                 j %= pool->ltp_numqs;
1100                         }
1101                 } while (j != i);
1102
1103                 /* Wait for this task to become the sole active task */
1104                 while (pool->ltp_active_queues > 0)
1105                         ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
1106
1107                 /* restore us to active count */
1108                 pool->ltp_wqs[i]->ltp_active_count++;
1109
1110                 assert(pool->ltp_pause == WANT_PAUSE);
1111                 pool->ltp_pause = PAUSED;
1112         }
1113         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1114
1115         return(ret);
1116 }
1117
1118 /* Consider this task idle: It will not block pool_pause() in other tasks. */
1119 void
1120 ldap_pvt_thread_pool_idle( ldap_pvt_thread_pool_t *tpool )
1121 {
1122         handle_pause(tpool, PAUSE_ARG(GO_IDLE));
1123 }
1124
1125 /* Cancel pool_idle(). If the pool is paused, wait it out first. */
1126 void
1127 ldap_pvt_thread_pool_unidle( ldap_pvt_thread_pool_t *tpool )
1128 {
1129         handle_pause(tpool, PAUSE_ARG(GO_UNIDLE));
1130 }
1131
1132 /*
1133  * If a pause was requested, wait for it.  If several threads
1134  * are waiting to pause, let through one or more pauses.
1135  * The calling task must be active, not idle.
1136  * Return 1 if we waited, 0 if not, -1 at parameter error.
1137  */
1138 int
1139 ldap_pvt_thread_pool_pausecheck( ldap_pvt_thread_pool_t *tpool )
1140 {
1141         return handle_pause(tpool, PAUSE_ARG(CHECK_PAUSE));
1142 }
1143
1144 /*
1145  * Pause the pool.  The calling task must be active, not idle.
1146  * Return when all other tasks are paused or idle.
1147  */
1148 int
1149 ldap_pvt_thread_pool_pause( ldap_pvt_thread_pool_t *tpool )
1150 {
1151         return handle_pause(tpool, PAUSE_ARG(DO_PAUSE));
1152 }
1153
1154 /* End a pause */
1155 int
1156 ldap_pvt_thread_pool_resume ( 
1157         ldap_pvt_thread_pool_t *tpool )
1158 {
1159         struct ldap_int_thread_pool_s *pool;
1160         struct ldap_int_thread_poolq_s *pq;
1161         int i;
1162
1163         if (tpool == NULL)
1164                 return(-1);
1165
1166         pool = *tpool;
1167
1168         if (pool == NULL)
1169                 return(0);
1170
1171         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
1172         assert(pool->ltp_pause == PAUSED);
1173         pool->ltp_pause = 0;
1174         for (i=0; i<pool->ltp_numqs; i++) {
1175                 pq = pool->ltp_wqs[i];
1176                 if (pq->ltp_open_count <= 0) /* true when paused, but be paranoid */
1177                         pq->ltp_open_count = -pq->ltp_open_count;
1178                 pq->ltp_work_list = &pq->ltp_pending_list;
1179                 ldap_pvt_thread_cond_broadcast(&pq->ltp_cond);
1180         }
1181         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
1182         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
1183         return(0);
1184 }
1185
1186 /*
1187  * Get the key's data and optionally free function in the given context.
1188  */
1189 int ldap_pvt_thread_pool_getkey(
1190         void *xctx,
1191         void *key,
1192         void **data,
1193         ldap_pvt_thread_pool_keyfree_t **kfree )
1194 {
1195         ldap_int_thread_userctx_t *ctx = xctx;
1196         int i;
1197
1198         if ( !ctx || !key || !data ) return EINVAL;
1199
1200         for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
1201                 if ( ctx->ltu_key[i].ltk_key == key ) {
1202                         *data = ctx->ltu_key[i].ltk_data;
1203                         if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
1204                         return 0;
1205                 }
1206         }
1207         return ENOENT;
1208 }
1209
1210 static void
1211 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
1212 {
1213         for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
1214                 ctx->ltu_key[i] = ctx->ltu_key[i+1];
1215         ctx->ltu_key[i].ltk_key = NULL;
1216 }
1217
1218 /*
1219  * Set or remove data for the key in the given context.
1220  * key can be any unique pointer.
1221  * kfree() is an optional function to free the data (but not the key):
1222  *   pool_context_reset() and pool_purgekey() call kfree(key, data),
1223  *   but pool_setkey() does not.  For pool_setkey() it is the caller's
1224  *   responsibility to free any existing data with the same key.
1225  *   kfree() must not call functions taking a tpool argument.
1226  */
1227 int ldap_pvt_thread_pool_setkey(
1228         void *xctx,
1229         void *key,
1230         void *data,
1231         ldap_pvt_thread_pool_keyfree_t *kfree,
1232         void **olddatap,
1233         ldap_pvt_thread_pool_keyfree_t **oldkfreep )
1234 {
1235         ldap_int_thread_userctx_t *ctx = xctx;
1236         int i, found;
1237
1238         if ( !ctx || !key ) return EINVAL;
1239
1240         for ( i=found=0; i<MAXKEYS; i++ ) {
1241                 if ( ctx->ltu_key[i].ltk_key == key ) {
1242                         found = 1;
1243                         break;
1244                 } else if ( !ctx->ltu_key[i].ltk_key ) {
1245                         break;
1246                 }
1247         }
1248
1249         if ( olddatap ) {
1250                 if ( found ) {
1251                         *olddatap = ctx->ltu_key[i].ltk_data;
1252                 } else {
1253                         *olddatap = NULL;
1254                 }
1255         }
1256
1257         if ( oldkfreep ) {
1258                 if ( found ) {
1259                         *oldkfreep = ctx->ltu_key[i].ltk_free;
1260                 } else {
1261                         *oldkfreep = 0;
1262                 }
1263         }
1264
1265         if ( data || kfree ) {
1266                 if ( i>=MAXKEYS )
1267                         return ENOMEM;
1268                 ctx->ltu_key[i].ltk_key = key;
1269                 ctx->ltu_key[i].ltk_data = data;
1270                 ctx->ltu_key[i].ltk_free = kfree;
1271         } else if ( found ) {
1272                 clear_key_idx( ctx, i );
1273         }
1274
1275         return 0;
1276 }
1277
1278 /* Free all elements with this key, no matter which thread they're in.
1279  * May only be called while the pool is paused.
1280  */
1281 void ldap_pvt_thread_pool_purgekey( void *key )
1282 {
1283         int i, j;
1284         ldap_int_thread_userctx_t *ctx;
1285
1286         assert ( key != NULL );
1287
1288         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
1289         for ( i=0; i<LDAP_MAXTHR; i++ ) {
1290                 ctx = thread_keys[i].ctx;
1291                 if ( ctx && ctx != DELETED_THREAD_CTX ) {
1292                         for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
1293                                 if ( ctx->ltu_key[j].ltk_key == key ) {
1294                                         if (ctx->ltu_key[j].ltk_free)
1295                                                 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
1296                                                 ctx->ltu_key[j].ltk_data );
1297                                         clear_key_idx( ctx, j );
1298                                         break;
1299                                 }
1300                         }
1301                 }
1302         }
1303         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
1304 }
1305
1306 /*
1307  * Find the context of the current thread.
1308  * This is necessary if the caller does not have access to the
1309  * thread context handle (for example, a slapd plugin calling
1310  * slapi_search_internal()). No doubt it is more efficient
1311  * for the application to keep track of the thread context
1312  * handles itself.
1313  */
1314 void *ldap_pvt_thread_pool_context( )
1315 {
1316         void *ctx = NULL;
1317
1318         ldap_pvt_thread_key_getdata( ldap_tpool_key, &ctx );
1319         return ctx ? ctx : (void *) &ldap_int_main_thrctx;
1320 }
1321
1322 /*
1323  * Free the context's keys.
1324  * Must not call functions taking a tpool argument (because this
1325  * thread already holds ltp_mutex when called from pool_wrapper()).
1326  */
1327 void ldap_pvt_thread_pool_context_reset( void *vctx )
1328 {
1329         ldap_int_thread_userctx_t *ctx = vctx;
1330         int i;
1331
1332         for ( i=MAXKEYS-1; i>=0; i--) {
1333                 if ( !ctx->ltu_key[i].ltk_key )
1334                         continue;
1335                 if ( ctx->ltu_key[i].ltk_free )
1336                         ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
1337                         ctx->ltu_key[i].ltk_data );
1338                 ctx->ltu_key[i].ltk_key = NULL;
1339         }
1340 }
1341
1342 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
1343 {
1344         ldap_int_thread_userctx_t *ctx = vctx;
1345
1346         return ctx->ltu_id;
1347 }
1348 #endif /* LDAP_THREAD_HAVE_TPOOL */