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