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