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