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