]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/tpool.c
Sync with 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-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                 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
226                         sizeof(ldap_int_thread_ctx_t));
227                 if (ctx == NULL) {
228                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
229                         return(-1);
230                 }
231         }
232
233         ctx->ltc_start_routine = start_routine;
234         ctx->ltc_arg = arg;
235
236         pool->ltp_pending_count++;
237         LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
238         if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
239                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
240                 return(0);
241         }
242         ldap_pvt_thread_cond_signal(&pool->ltp_cond);
243         if ((pool->ltp_open_count <= 0
244 #if 0
245                         || pool->ltp_pending_count > 1
246 #endif
247                         || pool->ltp_open_count == pool->ltp_active_count)
248                 && (pool->ltp_max_count <= 0
249                         || pool->ltp_open_count < pool->ltp_max_count))
250         {
251                 pool->ltp_open_count++;
252                 pool->ltp_starting++;
253                 need_thread = 1;
254         }
255         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
256
257         if (need_thread) {
258                 int rc;
259
260                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
261
262                 rc = ldap_pvt_thread_create( &thr, 1,
263                         ldap_int_thread_pool_wrapper, pool );
264                 if (rc == 0) {
265                         int hash;
266                         pool->ltp_starting--;
267
268                         /* assign this thread ID to a key slot; start
269                          * at the thread ID itself (mod LDAP_MAXTHR) and
270                          * look for an empty slot.
271                          */
272                         TID_HASH(thr, hash);
273                         for (rc = hash & (LDAP_MAXTHR-1);
274                                 !TID_EQ(thread_keys[rc].id, tid_zero);
275                                 rc = (rc+1) & (LDAP_MAXTHR-1));
276                         thread_keys[rc].id = thr;
277                 } else {
278                         /* couldn't create thread.  back out of
279                          * ltp_open_count and check for even worse things.
280                          */
281                         pool->ltp_open_count--;
282                         pool->ltp_starting--;
283                         if (pool->ltp_open_count == 0) {
284                                 /* no open threads at all?!?
285                                  */
286                                 ldap_int_thread_ctx_t *ptr;
287                                 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
288                                         if (ptr == ctx) break;
289                                 if (ptr == ctx) {
290                                         /* no open threads, context not handled, so
291                                          * back out of ltp_pending_count, free the context,
292                                          * report the error.
293                                          */
294                                         LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx, 
295                                                 ldap_int_thread_ctx_s, ltc_next.q);
296                                         pool->ltp_pending_count++;
297                                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
298                                         LDAP_FREE(ctx);
299                                         return(-1);
300                                 }
301                         }
302                         /* there is another open thread, so this
303                          * context will be handled eventually.
304                          * continue on and signal that the context
305                          * is waiting.
306                          */
307                 }
308                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
309         }
310
311         return(0);
312 }
313
314 int
315 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
316 {
317         struct ldap_int_thread_pool_s *pool;
318
319         if (tpool == NULL)
320                 return(-1);
321
322         pool = *tpool;
323
324         if (pool == NULL)
325                 return(-1);
326
327         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
328         pool->ltp_max_count = max_threads;
329         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
330         return(0);
331 }
332
333 int
334 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
335 {
336         struct ldap_int_thread_pool_s *pool;
337         int count;
338
339         if (tpool == NULL)
340                 return(-1);
341
342         pool = *tpool;
343
344         if (pool == NULL)
345                 return(0);
346
347         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
348         count = pool->ltp_pending_count + pool->ltp_active_count;
349         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
350         return(count);
351 }
352
353 int
354 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
355 {
356         struct ldap_int_thread_pool_s *pool, *pptr;
357         long waiting;
358         ldap_int_thread_ctx_t *ctx;
359
360         if (tpool == NULL)
361                 return(-1);
362
363         pool = *tpool;
364
365         if (pool == NULL) return(-1);
366
367         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
368         LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
369                 if (pptr == pool) break;
370         if (pptr == pool)
371                 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
372                         ldap_int_thread_pool_s, ltp_next);
373         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
374
375         if (pool != pptr) return(-1);
376
377         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
378         pool->ltp_state = run_pending
379                 ? LDAP_INT_THREAD_POOL_FINISHING
380                 : LDAP_INT_THREAD_POOL_STOPPING;
381
382         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
383         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
384
385         do {
386                 ldap_pvt_thread_yield();
387                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
388                 waiting = pool->ltp_open_count;
389                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
390         } while (waiting > 0);
391
392         while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
393         {
394                 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
395                 LDAP_FREE(ctx);
396         }
397
398         while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
399         {
400                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
401                 LDAP_FREE(ctx);
402         }
403
404         ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
405         ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
406         ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
407         LDAP_FREE(pool);
408         return(0);
409 }
410
411 static void *
412 ldap_int_thread_pool_wrapper ( 
413         void *xpool )
414 {
415         struct ldap_int_thread_pool_s *pool = xpool;
416         ldap_int_thread_ctx_t *ctx;
417         ldap_int_thread_key_t ltc_key[MAXKEYS];
418         ldap_pvt_thread_t tid;
419         int i, keyslot, hash;
420
421         if (pool == NULL)
422                 return NULL;
423
424         for ( i=0; i<MAXKEYS; i++ ) {
425                 ltc_key[i].ltk_key = NULL;
426         }
427
428         tid = ldap_pvt_thread_self();
429
430         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
431
432         /* store pointer to our keys */
433         TID_HASH(tid, hash);
434         for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid);
435                                 i = (i+1) & (LDAP_MAXTHR-1));
436         thread_keys[i].ctx = ltc_key;
437         keyslot = i;
438
439         while (pool->ltp_state != LDAP_INT_THREAD_POOL_STOPPING) {
440                 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
441                 if (ctx) {
442                         LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
443                 } else {
444                         if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
445                                 break;
446                         if (pool->ltp_max_count > 0
447                                 && pool->ltp_open_count > pool->ltp_max_count)
448                         {
449                                 /* too many threads running (can happen if the
450                                  * maximum threads value is set during ongoing
451                                  * operation using ldap_pvt_thread_pool_maxthreads)
452                                  * so let this thread die.
453                                  */
454                                 break;
455                         }
456
457                         /* we could check an idle timer here, and let the
458                          * thread die if it has been inactive for a while.
459                          * only die if there are other open threads (i.e.,
460                          * always have at least one thread open).  the check
461                          * should be like this:
462                          *   if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
463                          *       check timer, leave thread (break;)
464                          */
465
466                         if (pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING)
467                                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
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                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
493
494                 ldap_pvt_thread_yield();
495
496                 /* if we use an idle timer, here's
497                  * a good place to update it
498                  */
499
500                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
501         }
502
503         for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
504                 if (ltc_key[i].ltk_free)
505                         ltc_key[i].ltk_free(
506                                 ltc_key[i].ltk_key,
507                                 ltc_key[i].ltk_data );
508         }
509
510         thread_keys[keyslot].ctx = NULL;
511         thread_keys[keyslot].id = tid_zero;
512
513         pool->ltp_open_count--;
514         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
515
516         ldap_pvt_thread_exit(NULL);
517         return(NULL);
518 }
519
520 int
521 ldap_pvt_thread_pool_pause ( 
522         ldap_pvt_thread_pool_t *tpool )
523 {
524         struct ldap_int_thread_pool_s *pool;
525
526         if (tpool == NULL)
527                 return(-1);
528
529         pool = *tpool;
530
531         if (pool == NULL)
532                 return(0);
533
534         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
535
536         /* If someone else has already requested a pause, we have to wait */
537         if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
538                 pool->ltp_pending_count++;
539                 pool->ltp_active_count--;
540                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
541                 pool->ltp_pending_count--;
542                 pool->ltp_active_count++;
543         }
544         /* Wait for everyone else to finish */
545         pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
546         while (pool->ltp_active_count > 1) {
547                 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
548         }
549         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
550         return(0);
551 }
552
553 int
554 ldap_pvt_thread_pool_resume ( 
555         ldap_pvt_thread_pool_t *tpool )
556 {
557         struct ldap_int_thread_pool_s *pool;
558
559         if (tpool == NULL)
560                 return(-1);
561
562         pool = *tpool;
563
564         if (pool == NULL)
565                 return(0);
566
567         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
568
569         pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
570         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
571         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
572         return(0);
573 }
574
575 int ldap_pvt_thread_pool_getkey(
576         void *xctx,
577         void *key,
578         void **data,
579         ldap_pvt_thread_pool_keyfree_t **kfree )
580 {
581         ldap_int_thread_key_t *ctx = xctx;
582         int i;
583
584         if ( !ctx || !data ) return EINVAL;
585
586         for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
587                 if ( ctx[i].ltk_key == key ) {
588                         *data = ctx[i].ltk_data;
589                         if ( kfree ) *kfree = ctx[i].ltk_free;
590                         return 0;
591                 }
592         }
593         return ENOENT;
594 }
595
596 int ldap_pvt_thread_pool_setkey(
597         void *xctx,
598         void *key,
599         void *data,
600         ldap_pvt_thread_pool_keyfree_t *kfree )
601 {
602         ldap_int_thread_key_t *ctx = xctx;
603         int i;
604
605         if ( !ctx || !key ) return EINVAL;
606
607         for ( i=0; i<MAXKEYS; i++ ) {
608                 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
609                         ctx[i].ltk_key = key;
610                         ctx[i].ltk_data = data;
611                         ctx[i].ltk_free = kfree;
612                         return 0;
613                 }
614         }
615         return ENOMEM;
616 }
617
618 /* Free all elements with this key, no matter which thread they're in.
619  * May only be called while the pool is paused.
620  */
621 void ldap_pvt_thread_pool_purgekey( void *key )
622 {
623         int i, j;
624         ldap_int_thread_key_t *ctx;
625
626         for ( i=0; i<LDAP_MAXTHR; i++ ) {
627                 if ( thread_keys[i].ctx ) {
628                         ctx = thread_keys[i].ctx;
629                         for ( j=0; j<MAXKEYS; j++ ) {
630                                 if ( ctx[j].ltk_key == key ) {
631                                         if (ctx[j].ltk_free)
632                                                 ctx[j].ltk_free( ctx[j].ltk_key, ctx[j].ltk_data );
633                                         ctx[j].ltk_key = NULL;
634                                         ctx[j].ltk_free = NULL;
635                                         break;
636                                 }
637                         }
638                 }
639         }
640 }
641
642 /*
643  * This is necessary if the caller does not have access to the
644  * thread context handle (for example, a slapd plugin calling
645  * slapi_search_internal()). No doubt it is more efficient to
646  * for the application to keep track of the thread context
647  * handles itself.
648  */
649 void *ldap_pvt_thread_pool_context( )
650 {
651         ldap_pvt_thread_t tid;
652         int i, hash;
653
654         tid = ldap_pvt_thread_self();
655
656         TID_HASH( tid, hash );
657         for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid_zero) &&
658                 !TID_EQ(thread_keys[i].id, tid); i = (i+1) & (LDAP_MAXTHR-1));
659
660         return thread_keys[i].ctx;
661 }
662
663 #endif /* LDAP_THREAD_HAVE_TPOOL */