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