]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/tpool.c
Move uidNumber and gidNumber into slapd(8)
[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 typedef enum ldap_int_thread_pool_state_e {
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 } ldap_int_thread_pool_state_t;
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         ldap_int_thread_pool_state_t 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 { unsigned 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                                 || pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING)
474                         {
475                                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
476                         }
477
478                         continue;
479                 }
480
481                 pool->ltp_pending_count--;
482
483                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
484                 pool->ltp_active_count++;
485                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
486
487                 ctx->ltc_start_routine(ltc_key, ctx->ltc_arg);
488
489                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
490                 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
491                         ldap_int_thread_ctx_s, ltc_next.al);
492                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
493                 pool->ltp_active_count--;
494
495                 if (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
496                         if (pool->ltp_active_count < 2) {
497                                 ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
498                         }
499                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
500                 }
501                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
502
503                 ldap_pvt_thread_yield();
504
505                 /* if we use an idle timer, here's
506                  * a good place to update it
507                  */
508
509                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
510         }
511
512         for ( i=0; i<MAXKEYS && ltc_key[i].ltk_key; i++ ) {
513                 if (ltc_key[i].ltk_free)
514                         ltc_key[i].ltk_free(
515                                 ltc_key[i].ltk_key,
516                                 ltc_key[i].ltk_data );
517         }
518
519         thread_keys[keyslot].ctx = NULL;
520         thread_keys[keyslot].id = tid_zero;
521
522         pool->ltp_open_count--;
523         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
524
525         ldap_pvt_thread_exit(NULL);
526         return(NULL);
527 }
528
529 int
530 ldap_pvt_thread_pool_pause ( 
531         ldap_pvt_thread_pool_t *tpool )
532 {
533         struct ldap_int_thread_pool_s *pool;
534
535         if (tpool == NULL)
536                 return(-1);
537
538         pool = *tpool;
539
540         if (pool == NULL)
541                 return(0);
542
543         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
544
545         /* If someone else has already requested a pause, we have to wait */
546         while (pool->ltp_state == LDAP_INT_THREAD_POOL_PAUSING) {
547                 pool->ltp_pending_count++;
548                 pool->ltp_active_count--;
549                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
550                 pool->ltp_pending_count--;
551                 pool->ltp_active_count++;
552         }
553         /* Wait for everyone else to finish */
554         pool->ltp_state = LDAP_INT_THREAD_POOL_PAUSING;
555         while (pool->ltp_active_count > 1) {
556                 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
557         }
558         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
559         return(0);
560 }
561
562 int
563 ldap_pvt_thread_pool_resume ( 
564         ldap_pvt_thread_pool_t *tpool )
565 {
566         struct ldap_int_thread_pool_s *pool;
567
568         if (tpool == NULL)
569                 return(-1);
570
571         pool = *tpool;
572
573         if (pool == NULL)
574                 return(0);
575
576         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
577
578         pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
579         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
580         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
581         return(0);
582 }
583
584 int ldap_pvt_thread_pool_getkey(
585         void *xctx,
586         void *key,
587         void **data,
588         ldap_pvt_thread_pool_keyfree_t **kfree )
589 {
590         ldap_int_thread_key_t *ctx = xctx;
591         int i;
592
593         if ( !ctx || !data ) return EINVAL;
594
595         for ( i=0; i<MAXKEYS && ctx[i].ltk_key; i++ ) {
596                 if ( ctx[i].ltk_key == key ) {
597                         *data = ctx[i].ltk_data;
598                         if ( kfree ) *kfree = ctx[i].ltk_free;
599                         return 0;
600                 }
601         }
602         return ENOENT;
603 }
604
605 int ldap_pvt_thread_pool_setkey(
606         void *xctx,
607         void *key,
608         void *data,
609         ldap_pvt_thread_pool_keyfree_t *kfree )
610 {
611         ldap_int_thread_key_t *ctx = xctx;
612         int i;
613
614         if ( !ctx || !key ) return EINVAL;
615
616         for ( i=0; i<MAXKEYS; i++ ) {
617                 if ( !ctx[i].ltk_key || ctx[i].ltk_key == key ) {
618                         ctx[i].ltk_key = key;
619                         ctx[i].ltk_data = data;
620                         ctx[i].ltk_free = kfree;
621                         return 0;
622                 }
623         }
624         return ENOMEM;
625 }
626
627 /* Free all elements with this key, no matter which thread they're in.
628  * May only be called while the pool is paused.
629  */
630 void ldap_pvt_thread_pool_purgekey( void *key )
631 {
632         int i, j;
633         ldap_int_thread_key_t *ctx;
634
635         for ( i=0; i<LDAP_MAXTHR; i++ ) {
636                 if ( thread_keys[i].ctx ) {
637                         ctx = thread_keys[i].ctx;
638                         for ( j=0; j<MAXKEYS; j++ ) {
639                                 if ( ctx[j].ltk_key == key ) {
640                                         if (ctx[j].ltk_free)
641                                                 ctx[j].ltk_free( ctx[j].ltk_key, ctx[j].ltk_data );
642                                         ctx[j].ltk_key = NULL;
643                                         ctx[j].ltk_free = NULL;
644                                         break;
645                                 }
646                         }
647                 }
648         }
649 }
650
651 /*
652  * This is necessary if the caller does not have access to the
653  * thread context handle (for example, a slapd plugin calling
654  * slapi_search_internal()). No doubt it is more efficient to
655  * for the application to keep track of the thread context
656  * handles itself.
657  */
658 void *ldap_pvt_thread_pool_context( )
659 {
660         ldap_pvt_thread_t tid;
661         int i, hash;
662
663         tid = ldap_pvt_thread_self();
664         if ( TID_EQ( tid, ldap_int_main_tid ))
665                 return ldap_int_main_thrctx;
666
667         TID_HASH( tid, hash );
668         for (i = hash & (LDAP_MAXTHR-1); !TID_EQ(thread_keys[i].id, tid_zero) &&
669                 !TID_EQ(thread_keys[i].id, tid); 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 */