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