]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/tpool.c
c3e5686ca4ee206325267986d5a300dee5494331
[openldap] / libraries / libldap_r / tpool.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 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_state_t;
39
40 typedef struct ldap_int_thread_key_s {
41         void *ltk_key;
42         void *ltk_data;
43         ldap_pvt_thread_pool_keyfree_t *ltk_free;
44 } ldap_int_thread_key_t;
45
46 /* Max number of thread-specific keys we store per thread.
47  * We don't expect to use many...
48  */
49 #define MAXKEYS 32
50 #define LDAP_MAXTHR     1024    /* must be a power of 2 */
51
52 typedef struct ldap_int_thread_userctx_s {
53         ldap_pvt_thread_t ltu_id;
54         ldap_int_thread_key_t ltu_key[MAXKEYS];
55 } ldap_int_thread_userctx_t;
56
57 static ldap_pvt_thread_t tid_zero;
58
59 /* Thread ID -> context mapping (poor open-addressed hash table).
60  * Protected by ldap_pvt_thread_pool_mutex except during pauses,
61  * when it is read-only (used by pool_purgekey and pool_context).
62  */
63 static struct {
64         ldap_pvt_thread_t id;
65         ldap_int_thread_userctx_t *ctx;         /* set when id != tid_zero */
66 #       define DELETED_THREAD_CTX (&ldap_int_main_thrctx + 1) /* dummy addr */
67 } thread_keys[LDAP_MAXTHR];
68
69 #define TID_HASH(tid, hash) do { \
70         unsigned const char *ptr_ = (unsigned const char *)&(tid); \
71         unsigned i_; \
72         for (i_ = 0, (hash) = ptr_[0]; ++i_ < sizeof(tid);) \
73                 (hash) += ((hash) << 5) ^ ptr_[i_]; \
74 } while(0)
75
76
77 typedef struct ldap_int_thread_ctx_s {
78         union {
79         LDAP_STAILQ_ENTRY(ldap_int_thread_ctx_s) q;
80         LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) l;
81         LDAP_SLIST_ENTRY(ldap_int_thread_ctx_s) al;
82         } ltc_next;
83         ldap_pvt_thread_start_t *ltc_start_routine;
84         void *ltc_arg;
85 } ldap_int_thread_ctx_t;
86
87 struct ldap_int_thread_pool_s {
88         LDAP_STAILQ_ENTRY(ldap_int_thread_pool_s) ltp_next;
89         ldap_pvt_thread_mutex_t ltp_mutex;
90         ldap_pvt_thread_cond_t ltp_cond;
91         ldap_pvt_thread_cond_t ltp_pcond;
92         LDAP_STAILQ_HEAD(tcq, ldap_int_thread_ctx_s) ltp_pending_list;
93         LDAP_SLIST_HEAD(tcl, ldap_int_thread_ctx_s) ltp_free_list;
94         LDAP_SLIST_HEAD(tclq, ldap_int_thread_ctx_s) ltp_active_list;
95         ldap_int_thread_pool_state_t ltp_state;
96         int ltp_pause;
97         long ltp_max_count;
98         long ltp_max_pending;
99         long ltp_pending_count;
100         long ltp_active_count;
101         long ltp_open_count;
102         long ltp_starting;
103 };
104
105 static LDAP_STAILQ_HEAD(tpq, ldap_int_thread_pool_s)
106         ldap_int_thread_pool_list =
107         LDAP_STAILQ_HEAD_INITIALIZER(ldap_int_thread_pool_list);
108
109 static ldap_pvt_thread_mutex_t ldap_pvt_thread_pool_mutex;
110
111 static void *ldap_int_thread_pool_wrapper( void *pool );
112
113 static ldap_pvt_thread_t ldap_int_main_tid;
114
115 static ldap_int_thread_userctx_t ldap_int_main_thrctx;
116
117 int
118 ldap_int_thread_pool_startup ( void )
119 {
120         ldap_int_main_tid = ldap_pvt_thread_self();
121         ldap_int_main_thrctx.ltu_id = ldap_int_main_tid;
122
123         return ldap_pvt_thread_mutex_init(&ldap_pvt_thread_pool_mutex);
124 }
125
126 int
127 ldap_int_thread_pool_shutdown ( void )
128 {
129         struct ldap_int_thread_pool_s *pool;
130
131         while ((pool = LDAP_STAILQ_FIRST(&ldap_int_thread_pool_list)) != NULL) {
132                 (ldap_pvt_thread_pool_destroy)(&pool, 0); /* ignore thr_debug macro */
133         }
134         ldap_pvt_thread_mutex_destroy(&ldap_pvt_thread_pool_mutex);
135         return(0);
136 }
137
138 typedef struct ldap_lazy_sem_t {
139         ldap_pvt_thread_mutex_t ls_mutex;
140         ldap_pvt_thread_cond_t  ls_cond;
141         int ls_sem_value;
142         /*
143          * when more than ls_lazy_count number of resources
144          * becmoes available, the thread wating for the resources will
145          * be waken up in order to prevent frequent blocking/waking-up
146          */
147         unsigned int ls_lazy_count;
148         /*
149          * only one thread(listener) will wait on this semaphore
150          * using a flag instead of a list
151          */
152         int ls_wait;
153 } ldap_lazy_sem_t;
154
155 ldap_lazy_sem_t* thread_pool_sem = NULL;
156
157 int
158 ldap_lazy_sem_init( unsigned int value, unsigned int lazyness )
159 {
160         thread_pool_sem = (ldap_lazy_sem_t*) LDAP_CALLOC(1,
161                 sizeof( ldap_lazy_sem_t ));
162
163         if( thread_pool_sem == NULL ) return -1;
164
165         ldap_pvt_thread_mutex_init( &thread_pool_sem->ls_mutex );
166         ldap_pvt_thread_cond_init( &thread_pool_sem->ls_cond );
167         thread_pool_sem->ls_sem_value = value;
168         thread_pool_sem->ls_lazy_count = lazyness;
169         thread_pool_sem->ls_wait = 0;
170
171         return 0;
172 }
173
174 /* FIXME: move to some approprite header */
175 int ldap_lazy_sem_dec( ldap_lazy_sem_t* ls );
176 int ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls );
177
178 /*
179  * ldap_lazy_sem_wait is used if a caller is blockable(listener).
180  * Otherwise use ldap_lazy_sem_dec (worker)
181  */
182 int
183 ldap_lazy_sem_op_submit( ldap_lazy_sem_t* ls )
184 {
185         if ( ls == NULL ) return -1;
186
187         /* only worker thread has its thread ctx */
188         if ( ldap_pvt_thread_pool_context() ) {
189                 /* worker thread */
190                 return ldap_lazy_sem_dec( ls );
191         } else {
192                 /* listener */
193                 return ldap_lazy_sem_wait( ls );
194         }
195 }
196
197 /*
198  * test if given semaphore's count is zero.
199  * If 0, the caller is blocked 
200  * If not, the count is decremented.
201  */
202 int
203 ldap_lazy_sem_wait ( ldap_lazy_sem_t* ls )
204 {
205         ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
206
207 lazy_sem_retry:
208         if ( ls->ls_sem_value <= 0 ) {
209                 /* no more avaliable resources */
210                 ls->ls_wait = 1;
211                 ldap_pvt_thread_cond_wait( &ls->ls_cond, &ls->ls_mutex );
212                 goto lazy_sem_retry;
213         } else {
214                 /* avaliable resources */
215                 ls->ls_sem_value--;
216         }
217
218         ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
219
220         return 0;
221 }
222
223 /*
224  * decrement the count without blocking
225  * even when the count becomes less than or equal to 0
226  */
227 int
228 ldap_lazy_sem_dec( ldap_lazy_sem_t* ls )
229 {
230         ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
231
232         ls->ls_sem_value--;
233
234         ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
235
236         return 0;
237 }
238
239 /*
240  * Increment the count by one and test if it is greater or
241  * equal to lazyness. If it is, wake up a blocked thread.
242  */
243 int
244 ldap_lazy_sem_post( ldap_lazy_sem_t* ls )
245 {
246         if( ls == NULL ) return (-1);
247
248         ldap_pvt_thread_mutex_lock( &ls->ls_mutex );
249
250         ls->ls_sem_value++;
251         if ( ls->ls_wait ) {
252                 if ( ls->ls_sem_value >= ls->ls_lazy_count ) {
253                         ls->ls_wait = 0;
254                         ldap_pvt_thread_cond_signal( &ls->ls_cond );
255                 }
256         }
257
258         ldap_pvt_thread_mutex_unlock( &ls->ls_mutex );
259
260         return 0;
261 }
262
263 int
264 ldap_pvt_thread_pool_init (
265         ldap_pvt_thread_pool_t *tpool,
266         int max_threads,
267         int max_pending )
268 {
269         ldap_pvt_thread_pool_t pool;
270         int rc;
271
272         if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
273                 max_threads = 0;
274
275         *tpool = NULL;
276         pool = (ldap_pvt_thread_pool_t) LDAP_CALLOC(1,
277                 sizeof(struct ldap_int_thread_pool_s));
278
279         if (pool == NULL) return(-1);
280
281         rc = ldap_pvt_thread_mutex_init(&pool->ltp_mutex);
282         if (rc != 0)
283                 return(rc);
284         rc = ldap_pvt_thread_cond_init(&pool->ltp_cond);
285         if (rc != 0)
286                 return(rc);
287         rc = ldap_pvt_thread_cond_init(&pool->ltp_pcond);
288         if (rc != 0)
289                 return(rc);
290         pool->ltp_state = LDAP_INT_THREAD_POOL_RUNNING;
291         pool->ltp_max_count = max_threads;
292         pool->ltp_max_pending = max_pending;
293         LDAP_STAILQ_INIT(&pool->ltp_pending_list);
294         LDAP_SLIST_INIT(&pool->ltp_free_list);
295         LDAP_SLIST_INIT(&pool->ltp_active_list);
296         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
297         LDAP_STAILQ_INSERT_TAIL(&ldap_int_thread_pool_list, pool, ltp_next);
298         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
299
300 #if 0
301         /* THIS WILL NOT WORK on some systems.  If the process
302          * forks after starting a thread, there is no guarantee
303          * that the thread will survive the fork.  For example,
304          * slapd forks in order to daemonize, and does so after
305          * calling ldap_pvt_thread_pool_init.  On some systems,
306          * this initial thread does not run in the child process,
307          * but ltp_open_count == 1, so two things happen: 
308          * 1) the first client connection fails, and 2) when
309          * slapd is kill'ed, it never terminates since it waits
310          * for all worker threads to exit. */
311
312         /* start up one thread, just so there is one. no need to
313          * lock the mutex right now, since no threads are running.
314          */
315         pool->ltp_open_count++;
316
317         ldap_pvt_thread_t thr;
318         rc = ldap_pvt_thread_create( &thr, 1, ldap_int_thread_pool_wrapper, pool );
319
320         if( rc != 0) {
321                 /* couldn't start one?  then don't start any */
322                 ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
323                 LDAP_STAILQ_REMOVE(ldap_int_thread_pool_list, pool, 
324                         ldap_int_thread_pool_s, ltp_next);
325                 ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
326                 ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
327                 ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
328                 ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
329                 LDAP_FREE(pool);
330                 return(-1);
331         }
332 #endif
333
334         *tpool = pool;
335         return(0);
336 }
337
338
339 int
340 ldap_pvt_thread_pool_submit (
341         ldap_pvt_thread_pool_t *tpool,
342         ldap_pvt_thread_start_t *start_routine, void *arg )
343 {
344         struct ldap_int_thread_pool_s *pool;
345         ldap_int_thread_ctx_t *ctx;
346         int need_thread = 0;
347         ldap_pvt_thread_t thr;
348
349         if (tpool == NULL)
350                 return(-1);
351
352         pool = *tpool;
353
354         if (pool == NULL)
355                 return(-1);
356
357         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
358         if (pool->ltp_state != LDAP_INT_THREAD_POOL_RUNNING
359                 || (pool->ltp_max_pending > 0
360                         && pool->ltp_pending_count >= pool->ltp_max_pending))
361         {
362                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
363                 return(-1);
364         }
365
366         ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list);
367         if (ctx) {
368                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
369         } else {
370                 ctx = (ldap_int_thread_ctx_t *) LDAP_MALLOC(
371                         sizeof(ldap_int_thread_ctx_t));
372                 if (ctx == NULL) {
373                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
374                         return(-1);
375                 }
376         }
377
378         ctx->ltc_start_routine = start_routine;
379         ctx->ltc_arg = arg;
380
381         pool->ltp_pending_count++;
382         LDAP_STAILQ_INSERT_TAIL(&pool->ltp_pending_list, ctx, ltc_next.q);
383         if (pool->ltp_pause) {
384                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
385                 return(0);
386         }
387         ldap_pvt_thread_cond_signal(&pool->ltp_cond);
388         if (pool->ltp_open_count < pool->ltp_active_count + pool->ltp_pending_count
389                 && (pool->ltp_open_count <
390                         (pool->ltp_max_count ? pool->ltp_max_count : LDAP_MAXTHR)))
391         {
392                 pool->ltp_open_count++;
393                 pool->ltp_starting++;
394                 need_thread = 1;
395         }
396
397 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
398         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
399         ldap_lazy_sem_op_submit( thread_pool_sem );
400         /* FIXME:  Another thread can now handle and release ctx, after
401          * which a newly submitted op can reuse ctx.  Then it is wrong for
402          * the "if (pool->ltp_open_count == 0)" code below to release ctx.
403          */
404 #endif
405
406         if (need_thread) {
407                 if (0 != ldap_pvt_thread_create(
408                         &thr, 1, ldap_int_thread_pool_wrapper, pool))
409                 {
410                         /* couldn't create thread.  back out of
411                          * ltp_open_count and check for even worse things.
412                          */
413 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
414                         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
415 #endif
416                         pool->ltp_starting--;
417                         pool->ltp_open_count--;
418                         if (pool->ltp_open_count == 0) {
419                                 /* no open threads at all?!?
420                                  */
421                                 ldap_int_thread_ctx_t *ptr;
422
423                                 /* let pool_destroy know there are no more threads */
424                                 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
425
426                                 LDAP_STAILQ_FOREACH(ptr, &pool->ltp_pending_list, ltc_next.q)
427                                         if (ptr == ctx) break;
428                                 if (ptr == ctx) {
429                                         /* no open threads, context not handled, so
430                                          * back out of ltp_pending_count, free the context,
431                                          * report the error.
432                                          */
433                                         LDAP_STAILQ_REMOVE(&pool->ltp_pending_list, ctx, 
434                                                 ldap_int_thread_ctx_s, ltc_next.q);
435                                         pool->ltp_pending_count--;
436                                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
437                                         LDAP_FREE(ctx);
438                                         return(-1);
439                                 }
440                         }
441 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
442                         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
443 #endif
444                         /* there is another open thread, so this
445                          * context will be handled eventually.
446                          * continue on, we have signalled that
447                          * the context is waiting.
448                          */
449                 }
450         }
451
452 #ifndef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
453         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
454 #endif
455
456         return(0);
457 }
458
459 int
460 ldap_pvt_thread_pool_maxthreads ( ldap_pvt_thread_pool_t *tpool, int max_threads )
461 {
462         struct ldap_int_thread_pool_s *pool;
463
464         if (! (0 <= max_threads && max_threads <= LDAP_MAXTHR))
465                 max_threads = 0;
466
467         if (tpool == NULL)
468                 return(-1);
469
470         pool = *tpool;
471
472         if (pool == NULL)
473                 return(-1);
474
475         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
476         pool->ltp_max_count = max_threads;
477         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
478         return(0);
479 }
480
481 int
482 ldap_pvt_thread_pool_query ( ldap_pvt_thread_pool_t *tpool, ldap_pvt_thread_pool_param_t param, void *value )
483 {
484         struct ldap_int_thread_pool_s   *pool;
485         int                             count = -1;
486
487         if ( tpool == NULL || value == NULL ) {
488                 return -1;
489         }
490
491         pool = *tpool;
492
493         if ( pool == NULL ) {
494                 return 0;
495         }
496
497         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
498         switch ( param ) {
499         case LDAP_PVT_THREAD_POOL_PARAM_MAX:
500                 count = pool->ltp_max_count;
501                 break;
502
503         case LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING:
504                 count = pool->ltp_max_pending;
505                 break;
506
507         case LDAP_PVT_THREAD_POOL_PARAM_OPEN:
508                 count = pool->ltp_open_count;
509                 break;
510
511         case LDAP_PVT_THREAD_POOL_PARAM_STARTING:
512                 count = pool->ltp_starting;
513                 break;
514
515         case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE:
516                 count = pool->ltp_active_count;
517                 break;
518
519         case LDAP_PVT_THREAD_POOL_PARAM_PENDING:
520                 count = pool->ltp_pending_count;
521                 break;
522
523         case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD:
524                 count = pool->ltp_pending_count + pool->ltp_active_count;
525                 break;
526
527         case LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX:
528                 break;
529
530         case LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX:
531                 break;
532
533         case LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX:
534                 break;
535
536         case LDAP_PVT_THREAD_POOL_PARAM_STATE: {
537                 static struct {
538                         char                            *name;
539                         ldap_int_thread_pool_state_t    state;
540                 }               str2state[] = {
541                         { "running",    LDAP_INT_THREAD_POOL_RUNNING },
542                         { "finishing",  LDAP_INT_THREAD_POOL_FINISHING },
543                         { "stopping",   LDAP_INT_THREAD_POOL_STOPPING },
544                         { NULL }
545                 };
546                 int             i;
547
548                 if ( pool->ltp_pause ) {
549                         *((char **)value) = "pausing";
550                 } else {
551                         for ( i = 0; str2state[ i ].name != NULL; i++ ) {
552                                 if ( str2state[ i ].state == pool->ltp_state ) {
553                                         break;
554                                 }
555                         }
556                         *((char **)value) = str2state[ i ].name;
557                 }
558                 if ( *((char **)value) != NULL ) {
559                         count = -2;
560                 }
561                 } break;
562         }
563         ldap_pvt_thread_mutex_unlock( &pool->ltp_mutex );
564
565         if ( count > -1 ) {
566                 *((int *)value) = count;
567         }
568
569         return ( count == -1 ? -1 : 0 );
570 }
571
572 /*
573  * wrapper for ldap_pvt_thread_pool_query(), left around
574  * for backwards compatibility
575  */
576 int
577 ldap_pvt_thread_pool_backload ( ldap_pvt_thread_pool_t *tpool )
578 {
579         int     rc, count;
580
581         rc = ldap_pvt_thread_pool_query( tpool,
582                 LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, (void *)&count );
583
584         if ( rc == 0 ) {
585                 return count;
586         }
587
588         return rc;
589 }
590
591 int
592 ldap_pvt_thread_pool_destroy ( ldap_pvt_thread_pool_t *tpool, int run_pending )
593 {
594         struct ldap_int_thread_pool_s *pool, *pptr;
595         ldap_int_thread_ctx_t *ctx;
596
597         if (tpool == NULL)
598                 return(-1);
599
600         pool = *tpool;
601
602         if (pool == NULL) return(-1);
603
604         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
605         LDAP_STAILQ_FOREACH(pptr, &ldap_int_thread_pool_list, ltp_next)
606                 if (pptr == pool) break;
607         if (pptr == pool)
608                 LDAP_STAILQ_REMOVE(&ldap_int_thread_pool_list, pool,
609                         ldap_int_thread_pool_s, ltp_next);
610         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
611
612         if (pool != pptr) return(-1);
613
614         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
615         pool->ltp_state = run_pending
616                 ? LDAP_INT_THREAD_POOL_FINISHING
617                 : LDAP_INT_THREAD_POOL_STOPPING;
618
619         while (pool->ltp_open_count) {
620                 if (!pool->ltp_pause)
621                         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
622                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
623         }
624         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
625
626         while ((ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list)) != NULL)
627         {
628                 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
629                 LDAP_FREE(ctx);
630         }
631
632         while ((ctx = LDAP_SLIST_FIRST(&pool->ltp_free_list)) != NULL)
633         {
634                 LDAP_SLIST_REMOVE_HEAD(&pool->ltp_free_list, ltc_next.l);
635                 LDAP_FREE(ctx);
636         }
637
638         ldap_pvt_thread_cond_destroy(&pool->ltp_pcond);
639         ldap_pvt_thread_cond_destroy(&pool->ltp_cond);
640         ldap_pvt_thread_mutex_destroy(&pool->ltp_mutex);
641         LDAP_FREE(pool);
642 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
643         if ( thread_pool_sem ) {
644                 LDAP_FREE( thread_pool_sem );
645         }
646 #endif
647         return(0);
648 }
649
650 static void *
651 ldap_int_thread_pool_wrapper ( 
652         void *xpool )
653 {
654         struct ldap_int_thread_pool_s *pool = xpool;
655         ldap_int_thread_ctx_t *ctx;
656         ldap_int_thread_userctx_t uctx;
657         unsigned i, keyslot, hash;
658
659         if (pool == NULL)
660                 return NULL;
661
662         for ( i=0; i<MAXKEYS; i++ ) {
663                 uctx.ltu_key[i].ltk_key = NULL;
664         }
665
666         uctx.ltu_id = ldap_pvt_thread_self();
667         TID_HASH(uctx.ltu_id, hash);
668
669         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
670
671         /* thread_keys[] is read-only when paused */
672         while (pool->ltp_pause)
673                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
674
675         /* find a key slot to give this thread ID and store a
676          * pointer to our keys there; start at the thread ID
677          * itself (mod LDAP_MAXTHR) and look for an empty slot.
678          */
679         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
680         for (keyslot = hash & (LDAP_MAXTHR-1);
681                 !ldap_pvt_thread_equal(thread_keys[keyslot].id, tid_zero);
682                 keyslot = (keyslot+1) & (LDAP_MAXTHR-1));
683         thread_keys[keyslot].id = uctx.ltu_id;
684         thread_keys[keyslot].ctx = &uctx;
685         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
686
687         pool->ltp_starting--;
688
689         for (;;) {
690                 while (pool->ltp_pause)
691                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
692
693                 if (pool->ltp_state == LDAP_INT_THREAD_POOL_STOPPING)
694                         break;
695
696                 ctx = LDAP_STAILQ_FIRST(&pool->ltp_pending_list);
697                 if (ctx == NULL) {
698                         if (pool->ltp_state == LDAP_INT_THREAD_POOL_FINISHING)
699                                 break;
700
701                         if (pool->ltp_open_count >
702                                 (pool->ltp_max_count ? pool->ltp_max_count : LDAP_MAXTHR))
703                         {
704                                 /* too many threads running (can happen if the
705                                  * maximum threads value is set during ongoing
706                                  * operation using ldap_pvt_thread_pool_maxthreads)
707                                  * so let this thread die.
708                                  */
709                                 break;
710                         }
711
712                         /* we could check an idle timer here, and let the
713                          * thread die if it has been inactive for a while.
714                          * only die if there are other open threads (i.e.,
715                          * always have at least one thread open).  the check
716                          * should be like this:
717                          *   if (pool->ltp_open_count > 1 && pool->ltp_starting == 0)
718                          *       check timer, wait if ltp_pause, leave thread (break;)
719                          *
720                          * Just use pthread_cond_timedwait if we want to
721                          * check idle time.
722                          */
723
724                         assert(pool->ltp_state == LDAP_INT_THREAD_POOL_RUNNING);
725                         ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
726                         continue;
727                 }
728
729                 LDAP_STAILQ_REMOVE_HEAD(&pool->ltp_pending_list, ltc_next.q);
730                 pool->ltp_pending_count--;
731
732                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_active_list, ctx, ltc_next.al);
733                 pool->ltp_active_count++;
734                 ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
735
736                 ctx->ltc_start_routine(&uctx, ctx->ltc_arg);
737
738 #ifdef LDAP_PVT_THREAD_POOL_SEM_LOAD_CONTROL
739                 ldap_lazy_sem_post( thread_pool_sem );
740 #endif
741                 ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
742                 LDAP_SLIST_REMOVE(&pool->ltp_active_list, ctx,
743                         ldap_int_thread_ctx_s, ltc_next.al);
744                 LDAP_SLIST_INSERT_HEAD(&pool->ltp_free_list, ctx, ltc_next.l);
745                 pool->ltp_active_count--;
746
747                 /* let pool_pause know when it is the sole active thread */
748                 if (pool->ltp_active_count < 2)
749                         ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
750         }
751
752         ldap_pvt_thread_pool_context_reset(&uctx);
753
754         /* Needed if context_reset can let another thread request a pause */
755         while (pool->ltp_pause)
756                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
757
758         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
759         thread_keys[keyslot].ctx = DELETED_THREAD_CTX;
760         thread_keys[keyslot].id = tid_zero;
761         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
762
763         pool->ltp_open_count--;
764
765         /* let pool_destroy know we're all done */
766         if (pool->ltp_open_count < 1)
767                 ldap_pvt_thread_cond_signal(&pool->ltp_cond);
768
769         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
770
771         ldap_pvt_thread_exit(NULL);
772         return(NULL);
773 }
774
775 int
776 ldap_pvt_thread_pool_pause ( 
777         ldap_pvt_thread_pool_t *tpool )
778 {
779         struct ldap_int_thread_pool_s *pool;
780
781         if (tpool == NULL)
782                 return(-1);
783
784         pool = *tpool;
785
786         if (pool == NULL)
787                 return(0);
788
789         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
790
791         /* If someone else has already requested a pause, we have to wait */
792         while (pool->ltp_pause) {
793                 pool->ltp_pending_count++;
794                 pool->ltp_active_count--;
795                 /* let the other pool_pause() know when it can proceed */
796                 if (pool->ltp_active_count < 2)
797                         ldap_pvt_thread_cond_signal(&pool->ltp_pcond);
798                 ldap_pvt_thread_cond_wait(&pool->ltp_cond, &pool->ltp_mutex);
799                 pool->ltp_pending_count--;
800                 pool->ltp_active_count++;
801         }
802
803         /* Wait for everyone else to pause or finish */
804         pool->ltp_pause = 1;
805         while (pool->ltp_active_count > 1) {
806                 ldap_pvt_thread_cond_wait(&pool->ltp_pcond, &pool->ltp_mutex);
807         }
808
809         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
810         return(0);
811 }
812
813 int
814 ldap_pvt_thread_pool_resume ( 
815         ldap_pvt_thread_pool_t *tpool )
816 {
817         struct ldap_int_thread_pool_s *pool;
818
819         if (tpool == NULL)
820                 return(-1);
821
822         pool = *tpool;
823
824         if (pool == NULL)
825                 return(0);
826
827         ldap_pvt_thread_mutex_lock(&pool->ltp_mutex);
828         pool->ltp_pause = 0;
829         ldap_pvt_thread_cond_broadcast(&pool->ltp_cond);
830         ldap_pvt_thread_mutex_unlock(&pool->ltp_mutex);
831         return(0);
832 }
833
834 int ldap_pvt_thread_pool_getkey(
835         void *xctx,
836         void *key,
837         void **data,
838         ldap_pvt_thread_pool_keyfree_t **kfree )
839 {
840         ldap_int_thread_userctx_t *ctx = xctx;
841         int i;
842
843         if ( !ctx || !key || !data ) return EINVAL;
844
845         for ( i=0; i<MAXKEYS && ctx->ltu_key[i].ltk_key; i++ ) {
846                 if ( ctx->ltu_key[i].ltk_key == key ) {
847                         *data = ctx->ltu_key[i].ltk_data;
848                         if ( kfree ) *kfree = ctx->ltu_key[i].ltk_free;
849                         return 0;
850                 }
851         }
852         return ENOENT;
853 }
854
855 static void
856 clear_key_idx( ldap_int_thread_userctx_t *ctx, int i )
857 {
858         for ( ; i < MAXKEYS-1 && ctx->ltu_key[i+1].ltk_key; i++ )
859                 ctx->ltu_key[i] = ctx->ltu_key[i+1];
860         ctx->ltu_key[i].ltk_key = NULL;
861 }
862
863 int ldap_pvt_thread_pool_setkey(
864         void *xctx,
865         void *key,
866         void *data,
867         ldap_pvt_thread_pool_keyfree_t *kfree )
868 {
869         ldap_int_thread_userctx_t *ctx = xctx;
870         int i, found;
871
872         if ( !ctx || !key ) return EINVAL;
873
874         for ( i=found=0; i<MAXKEYS; i++ ) {
875                 if ( ctx->ltu_key[i].ltk_key == key ) {
876                         found = 1;
877                         break;
878                 } else if ( !ctx->ltu_key[i].ltk_key ) {
879                         break;
880                 }
881         }
882
883         if ( data || kfree ) {
884                 if ( i>=MAXKEYS )
885                         return ENOMEM;
886                 ctx->ltu_key[i].ltk_key = key;
887                 ctx->ltu_key[i].ltk_data = data;
888                 ctx->ltu_key[i].ltk_free = kfree;
889         } else if ( found ) {
890                 clear_key_idx( ctx, i );
891         }
892
893         return 0;
894 }
895
896 /* Free all elements with this key, no matter which thread they're in.
897  * May only be called while the pool is paused.
898  */
899 void ldap_pvt_thread_pool_purgekey( void *key )
900 {
901         int i, j;
902         ldap_int_thread_userctx_t *ctx;
903
904         assert ( key != NULL );
905
906         for ( i=0; i<LDAP_MAXTHR; i++ ) {
907                 ctx = thread_keys[i].ctx;
908                 if ( ctx && ctx != DELETED_THREAD_CTX ) {
909                         for ( j=0; j<MAXKEYS && ctx->ltu_key[j].ltk_key; j++ ) {
910                                 if ( ctx->ltu_key[j].ltk_key == key ) {
911                                         if (ctx->ltu_key[j].ltk_free)
912                                                 ctx->ltu_key[j].ltk_free( ctx->ltu_key[j].ltk_key,
913                                                 ctx->ltu_key[j].ltk_data );
914                                         clear_key_idx( ctx, j );
915                                         break;
916                                 }
917                         }
918                 }
919         }
920 }
921
922 /*
923  * This is necessary if the caller does not have access to the
924  * thread context handle (for example, a slapd plugin calling
925  * slapi_search_internal()). No doubt it is more efficient
926  * for the application to keep track of the thread context
927  * handles itself.
928  */
929 void *ldap_pvt_thread_pool_context( )
930 {
931         ldap_pvt_thread_t tid;
932         unsigned i, hash;
933         ldap_int_thread_userctx_t *ctx;
934
935         tid = ldap_pvt_thread_self();
936         if ( ldap_pvt_thread_equal( tid, ldap_int_main_tid ))
937                 return &ldap_int_main_thrctx;
938
939         TID_HASH( tid, hash );
940         i = hash &= (LDAP_MAXTHR-1);
941         ldap_pvt_thread_mutex_lock(&ldap_pvt_thread_pool_mutex);
942         do {
943                 ctx = thread_keys[i].ctx;
944                 if ( ctx != DELETED_THREAD_CTX )
945                         if ( ldap_pvt_thread_equal(thread_keys[i].id, tid) || !ctx )
946                                 goto done;
947         } while ( (i = (i+1) & (LDAP_MAXTHR-1)) != hash );
948         ctx = NULL;
949  done:
950         ldap_pvt_thread_mutex_unlock(&ldap_pvt_thread_pool_mutex);
951
952         return ctx;
953 }
954
955 void ldap_pvt_thread_pool_context_reset( void *vctx )
956 {
957         ldap_int_thread_userctx_t *ctx = vctx;
958         int i;
959
960         for ( i=MAXKEYS-1; i>=0; i--) {
961                 if ( !ctx->ltu_key[i].ltk_key )
962                         continue;
963                 if ( ctx->ltu_key[i].ltk_free )
964                         ctx->ltu_key[i].ltk_free( ctx->ltu_key[i].ltk_key,
965                         ctx->ltu_key[i].ltk_data );
966                 ctx->ltu_key[i].ltk_key = NULL;
967         }
968 }
969
970 ldap_pvt_thread_t ldap_pvt_thread_pool_tid( void *vctx )
971 {
972         ldap_int_thread_userctx_t *ctx = vctx;
973
974         return ctx->ltu_id;
975 }
976 #endif /* LDAP_THREAD_HAVE_TPOOL */