]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/tpool.c
From HEAD:
[openldap] / libraries / libldap_r / tpool.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdarg.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24 #include <ac/errno.h>
25
26 #include "ldap-int.h"
27 #include "ldap_pvt_thread.h" /* Get the thread interface */
28 #include "ldap_queue.h"
29 #define LDAP_THREAD_POOL_IMPLEMENTATION
30 #include "ldap_thr_debug.h"  /* May rename symbols defined below */
31
32 #ifndef LDAP_THREAD_HAVE_TPOOL
33
34 typedef enum ldap_int_thread_pool_state_e {
35         LDAP_INT_THREAD_POOL_RUNNING,
36         LDAP_INT_THREAD_POOL_FINISHING,
37         LDAP_INT_THREAD_POOL_STOPPING,
38         LDAP_INT_THREAD_POOL_PAUSING
39 } ldap_int_thread_pool_state_t;
40
41 typedef struct ldap_int_thread_key_s {
42         void *ltk_key;
43         void *ltk_data;
44         ldap_pvt_thread_pool_keyfree_t *ltk_free;
45 } ldap_int_thread_key_t;
46
47 /* Max number of thread-specific keys we store per thread.
48  * We don't expect to use many...
49  */
50 #define MAXKEYS 32
51 #define LDAP_MAXTHR     1024    /* must be a power of 2 */
52
53 static ldap_pvt_thread_t tid_zero;
54
55 static struct {
56         ldap_pvt_thread_t id;
57         ldap_int_thread_key_t *ctx;
58 } thread_keys[LDAP_MAXTHR];
59         
60
61 typedef struct ldap_int_thread_ctx_s {
62         union {
63         LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
64         LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
65         LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
66         } ltc_next;
67         ldap_pvt_thread_start_t *ltc_start_routine;
68         void *ltc_arg;
69 } ldap_int_thread_ctx_t;
70
71 struct ldap_int_thread_pool_s {
72         LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
73         ldap_pvt_thread_mutex_t ltp_mutex;
74         ldap_pvt_thread_cond_t ltp_cond;
75         ldap_pvt_thread_cond_t ltp_pcond;
76         LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
77         LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
78         LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
79         ldap_int_thread_pool_state_t ltp_state;
80         long ltp_max_count;
81         long ltp_max_pending;
82         long ltp_pending_count;
83         long ltp_active_count;
84         long ltp_open_count;
85         long ltp_starting;
86 };
87
88 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
89         ldap_int_thread_pool_list =
90         LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
91
92 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
93
94 static void *ldap_int_thread_pool_wrapper( void *pool );
95
96 static ldap_pvt_thread_t ldap_int_main_tid;
97
98 static ldap_int_thread_key_t ldap_int_main_thrctx[LDAP_MAXTHR];
99
100 int
101 ldap_int_thread_pool_startup ( void )
102 {
103         ldap_int_main_tid = ldap_pvt_thread_self();
104
105         return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
106 }
107
108 int
109 ldap_int_thread_pool_shutdown ( void )
110 {
111         struct ldap_int_thread_pool_s *pool;
112
113         while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
114                 LDAP_STAILQ_REMOVE_HEAD(&ldap_int_thread_pool_list, ltp_next);
115                 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
116         }
117         ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
118         return(0);
119 }
120
121 int
122 ldap_pvt_thread_pool_init (
123         ldap_pvt_thread_pool_t *tpool,
124         int max_threads,
125         int max_pending )
126 {
127         ldap_pvt_thread_pool_t pool;
128         int rc;
129
130         *tpool = NULL;
131         pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
132                 sizeof(struct ldap_int_thread_pool_s));
133
134         if (pool == NULL) return(-1);
135
136         rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
137         if (rc != 0)
138                 return(rc);
139         rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
140         if (rc != 0)
141                 return(rc);
142         rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
143         if (rc != 0)
144                 return(rc);
145         pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
146         pool->ltp_max_count = max_threads;
147         pool->ltp_max_pending = max_pending;
148         LDAP_STAILQ_INIT(&pool->ltp_pending_list);
149         LDAP_SLIST_INIT(&pool->ltp_free_list);
150         LDAP_SLIST_INIT(&pool->ltp_active_list);
151         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
152         LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
153         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
154
155 #if 0
156         /* THIS WILL NOT WORK on some systems.  If the process
157          * forks after starting a thread, there is no guarantee
158          * that the thread will survive the fork.  For example,
159          * slapd forks in order to daemonize, and does so after
160          * calling ldap_pvt_thread_pool_init.  On some systems,
161          * this initial thread does not run in the child process,
162          * but ltp_open_count == 1, so two things happen: 
163          * 1) the first client connection fails, and 2) when
164          * slapd is kill'ed, it never terminates since it waits
165          * for all worker threads to exit. */
166
167         /* start up one thread, just so there is one. no need to
168          * lock the mutex right now, since no threads are running.
169          */
170         pool->ltp_open_count++;
171
172         ldap_pvt_thread_t thr;
173         rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
174
175         if( rc != 0) {
176                 /* couldn't start one?  then don't start any */
177                 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
178                 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool, 
179                         ldap_int_thread_pool_s, ltp_next);
180                 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
181                 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
182                 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
183                 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
184                 LDAP_FREE(pool);
185                 return(-1);
186         }
187 #endif
188
189         *tpool = pool;
190         return(0);
191 }
192
193 #define TID_HASH(tid, hash) do { unsigned i; \
194         unsigned char *ptr = (unsigned char *)&(tid); \
195         for (i=0, hash=0; i<sizeof(tid); i++) hash += ptr[i]; } while(0)
196
197 int
198 ldap_pvt_thread_pool_submit (
199         ldap_pvt_thread_pool_t *tpool,
200         ldap_pvt_thread_start_t *start_routine, void *arg )
201 {
202         struct ldap_int_thread_pool_s *pool;
203         ldap_int_thread_ctx_t *ctx;
204         int need_thread = 0;
205         ldap_pvt_thread_t thr;
206
207         if (tpool == NULL)
208                 return(-1);
209
210         pool = *tpool;
211
212         if (pool == NULL)
213                 return(-1);
214
215         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
216         if ((pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING &&
217                 pool->ltp_state != LDAP_INT_THREAD_POOL_PAUSING)
218                 || (pool->ltp_max_pending > 0
219                         && pool->ltp_pending_count >= pool->ltp_max_pending))
220         {
221                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
222                 return(-1);
223         }
224         ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
225         if (ctx) {
226                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
227         } else {
228                 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
229                         sizeof(ldap_int_thread_ctx_t));
230                 if (ctx == NULL) {
231                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
232                         return(-1);
233                 }
234         }
235
236         ctx->ltc_start_routine = start_routine;
237         ctx->ltc_arg = arg;
238
239         pool->ltp_pending_count++;
240         LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
241         if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
242                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
243                 return(0);
244         }
245         ldap_pvt_thread_cond_signal(&pool->ltp_cond);
246         if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
247                 && (pool->ltp_open_count < pool->ltp_max_count ||
248                         pool->ltp_max_count <= 0 ))
249         {
250                 pool->ltp_open_count++;
251                 pool->ltp_starting++;
252                 need_thread = 1;
253         }
254         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
255
256         if (need_thread) {
257                 int rc;
258
259                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
260
261                 rc = ldap_pvt_thread_create( &thr, 1,
262                         ldap_int_thread_pool_wrapper, pool );
263                 if (rc == 0) {
264                         int hash;
265                         pool->ltp_starting--;
266
267                         /* assign this thread ID to a key slot; start
268                          * at the thread ID itself (mod LDAP_MAXTHR) and
269                          * look for an empty slot.
270                          */
271                         TID_HASH(thr, hash);
272                         for (rc = hash & (LDAP_MAXTHR-1);
273                                 !ldap_pvt_thread_equal(thread_keys[rc].id, tid_zero);
274                                 rc = (rc+1) & (LDAP_MAXTHR-1));
275                         thread_keys[rc].id = thr;
276                 } else {
277                         /* couldn't create thread.  back out of
278                          * ltp_open_count and check for even worse things.
279                          */
280                         pool->ltp_open_count--;
281                         pool->ltp_starting--;
282                         if (pool->ltp_open_count == 0) {
283                                 /* no open threads at all?!?
284                                  */
285                                 ldap_int_thread_ctx_t *ptr;
286                                 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
287                                         if (ptr == ctx) break;
288                                 if (ptr == ctx) {
289                                         /* no open threads, context not handled, so
290                                          * back out of ltp_pending_count, free the context,
291                                          * report the error.
292                                          */
293                                         LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx, 
294                                                 ldap_int_thread_ctx_s, ltc_next.q);
295                                         pool->ltp_pending_count++;
296                                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
297                                         LDAP_FREE(ctx);
298                                         return(-1);
299                                 }
300                         }
301                         /* there is another open thread, so this
302                          * context will be handled eventually.
303                          * continue on and signal that the context
304                          * is waiting.
305                          */
306                 }
307                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
308         }
309
310         return(0);
311 }
312
313 int
314 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
315 {
316         struct ldap_int_thread_pool_s *pool;
317
318         if (tpool == NULL)
319                 return(-1);
320
321         pool = *tpool;
322
323         if (pool == NULL)
324                 return(-1);
325
326         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
327         pool->ltp_max_count = max_threads;
328         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
329         return(0);
330 }
331
332 int
333 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
334 {
335         struct ldap_int_thread_pool_s *pool;
336         int count;
337
338         if (tpool == NULL)
339                 return(-1);
340
341         pool = *tpool;
342
343         if (pool == NULL)
344                 return(0);
345
346         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
347         count = pool->ltp_pending_count + pool->ltp_active_count;
348         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
349         return(count);
350 }
351
352 int
353 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
354 {
355         struct ldap_int_thread_pool_s *pool, *pptr;
356         long waiting;
357         ldap_int_thread_ctx_t *ctx;
358
359         if (tpool == NULL)
360                 return(-1);
361
362         pool = *tpool;
363
364         if (pool == NULL) return(-1);
365
366         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
367         LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
368                 if (pptr == pool) break;
369         if (pptr == pool)
370                 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
371                         ldap_int_thread_pool_s, ltp_next);
372         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
373
374         if (pool != pptr) return(-1);
375
376         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
377         pool->ltp_state = run_pending
378                 ? LDAP_INT_THREAD_POOL_FINISHING
379                 : LDAP_INT_THREAD_POOL_STOPPING;
380
381         if ( pool->ltp_open_count ) {
382                 ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
383                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
384         }
385
386         while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
387         {
388                 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
389                 LDAP_FREE(ctx);
390         }
391
392         while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
393         {
394                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
395                 LDAP_FREE(ctx);
396         }
397
398         ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
399         ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
400         ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
401         LDAP_FREE(pool);
402         return(0);
403 }
404
405 static void *
406 ldap_int_thread_pool_wrapper ( 
407         void *xpool )
408 {
409         struct ldap_int_thread_pool_s *pool = xpool;
410         ldap_int_thread_ctx_t *ctx;
411         ldap_int_thread_key_t ltc_key[MAXKEYS];
412         ldap_pvt_thread_t tid;
413         int i, keyslot, hash;
414
415         if (pool == NULL)
416                 return NULL;
417
418         for ( i=0; i<MAXKEYS; i++ ) {
419                 ltc_key[i].ltk_key = NULL;
420         }
421
422         tid = ldap_pvt_thread_self();
423
424         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
425
426         /* store pointer to our keys */
427         TID_HASH(tid, hash);
428         for (i = hash & (LDAP_MAXTHR-1);
429                                 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
430                                 i = (i+1) & (LDAP_MAXTHR-1));
431         thread_keys[i].ctx = ltc_key;
432         keyslot = i;
433
434         while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
435                 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
436                 if (ctx) {
437                         LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
438                 } else {
439                         if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
440                                 break;
441                         if (pool->ltp_max_count > 0
442                                 && pool->ltp_open_count > pool->ltp_max_count)
443                         {
444                                 /* too many threads running (can happen if the
445                                  * maximum threads value is set during ongoing
446                                  * operation using ldap_pvt_thread_pool_maxthreads)
447                                  * so let this thread die.
448                                  */
449                                 break;
450                         }
451
452                         /* we could check an idle timer here, and let the
453                          * thread die if it has been inactive for a while.
454                          * only die if there are other open threads (i.e.,
455                          * always have at least one thread open).  the check
456                          * should be like this:
457                          *   if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
458                          *       check timer, leave thread (break;)
459                          *
460                          * Just use pthread_cond_timedwait if we want to
461                          * check idle time.
462                          */
463
464                         if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING
465                                 || pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING)
466                         {
467                                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
468                         }
469
470                         continue;
471                 }
472
473                 pool->ltp_pending_count--;
474
475                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
476                 pool->ltp_active_count++;
477                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
478
479                 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
480
481                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
482                 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
483                         ldap_int_thread_ctx_s, ltc_next.al);
484                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
485                 pool->ltp_active_count--;
486
487                 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
488                         if (pool->ltp_active_count < 2) {
489                                 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
490                         }
491                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
492                 }
493         }
494
495         for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
496                 if (ltc_key[i].ltk_free)
497                         ltc_key[i].ltk_free(
498                                 ltc_key[i].ltk_key,
499                                 ltc_key[i].ltk_data );
500         }
501
502         thread_keys[keyslot].ctx = NULL;
503         thread_keys[keyslot].id = tid_zero;
504
505         pool->ltp_open_count--;
506
507         /* let pool_destroy know we're all done */
508         if (pool->ltp_open_count < 1)
509                 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
510
511         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
512
513         ldap_pvt_thread_exit(NULL);
514         return(NULL);
515 }
516
517 int
518 ldap_pvt_thread_pool_pause ( 
519         ldap_pvt_thread_pool_t *tpool )
520 {
521         struct ldap_int_thread_pool_s *pool;
522
523         if (tpool == NULL)
524                 return(-1);
525
526         pool = *tpool;
527
528         if (pool == NULL)
529                 return(0);
530
531         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
532
533         /* If someone else has already requested a pause, we have to wait */
534         while (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
535                 pool->ltp_pending_count++;
536                 pool->ltp_active_count--;
537                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
538                 pool->ltp_pending_count--;
539                 pool->ltp_active_count++;
540         }
541         /* Wait for everyone else to finish */
542         pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
543         while (pool->ltp_active_count > 1) {
544                 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
545         }
546         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
547         return(0);
548 }
549
550 int
551 ldap_pvt_thread_pool_resume ( 
552         ldap_pvt_thread_pool_t *tpool )
553 {
554         struct ldap_int_thread_pool_s *pool;
555
556         if (tpool == NULL)
557                 return(-1);
558
559         pool = *tpool;
560
561         if (pool == NULL)
562                 return(0);
563
564         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
565
566         pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
567         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
568         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
569         return(0);
570 }
571
572 int ldap_pvt_thread_pool_getkey(
573         void *xctx,
574         void *key,
575         void **data,
576         ldap_pvt_thread_pool_keyfree_t **kfree )
577 {
578         ldap_int_thread_key_t *ctx = xctx;
579         int i;
580
581         if ( !ctx || !data ) return EINVAL;
582
583         for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
584                 if ( ctx[i].ltk_key == key ) {
585                         *data = ctx[i].ltk_data;
586                         if ( kfree ) *kfree = ctx[i].ltk_free;
587                         return 0;
588                 }
589         }
590         return ENOENT;
591 }
592
593 int ldap_pvt_thread_pool_setkey(
594         void *xctx,
595         void *key,
596         void *data,
597         ldap_pvt_thread_pool_keyfree_t *kfree )
598 {
599         ldap_int_thread_key_t *ctx = xctx;
600         int i;
601
602         if ( !ctx || !key ) return EINVAL;
603
604         for ( i=0; i<MAXKEYS; i++ ) {
605                 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
606                         ctx[i].ltk_key = key;
607                         ctx[i].ltk_data = data;
608                         ctx[i].ltk_free = kfree;
609                         return 0;
610                 }
611         }
612         return ENOMEM;
613 }
614
615 /* Free all elements with this key, no matter which thread they're in.
616  * May only be called while the pool is paused.
617  */
618 void ldap_pvt_thread_pool_purgekey( void *key )
619 {
620         int i, j;
621         ldap_int_thread_key_t *ctx;
622
623         for ( i=0; i<LDAP_MAXTHR; i++ ) {
624                 if ( thread_keys[i].ctx ) {
625                         ctx = thread_keys[i].ctx;
626                         for ( j=0; j<MAXKEYS; j++ ) {
627                                 if ( ctx[j].ltk_key == key ) {
628                                         if (ctx[j].ltk_free)
629                                                 ctx[j].ltk_free( ctx[j].ltk_key, ctx[j].ltk_data );
630                                         ctx[j].ltk_key = NULL;
631                                         ctx[j].ltk_free = NULL;
632                                         break;
633                                 }
634                         }
635                 }
636         }
637 }
638
639 /*
640  * This is necessary if the caller does not have access to the
641  * thread context handle (for example, a slapd plugin calling
642  * slapi_search_internal()). No doubt it is more efficient to
643  * for the application to keep track of the thread context
644  * handles itself.
645  */
646 void *ldap_pvt_thread_pool_context( )
647 {
648         ldap_pvt_thread_t tid;
649         int i, hash;
650
651         tid = ldap_pvt_thread_self();
652         if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
653                 return ldap_int_main_thrctx;
654
655         TID_HASH( tid, hash );
656         for (i = hash & (LDAP_MAXTHR-1);
657                 !ldap_pvt_thread_equal(thread_keys[i].id, tid_zero) &&
658                 !ldap_pvt_thread_equal(thread_keys[i].id, tid);
659                 i = (i+1) & (LDAP_MAXTHR-1));
660
661         return thread_keys[i].ctx;
662 }
663
664 void ldap_pvt_thread_pool_context_reset( void *vctx )
665 {
666         ldap_int_thread_key_t *ctx = vctx;
667         int i;
668
669         for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++) {
670                 if ( ctx[i].ltk_free )
671                         ctx[i].ltk_free( ctx[i].ltk_key, ctx[i].ltk_data );
672                 ctx[i].ltk_key = NULL;
673         }
674 }
675 #endif /* LDAP_THREAD_HAVE_TPOOL */