Added a mini http fetcher into webcit
[citadel.git] / citadel / threads.c
1 /*
2  * $Id: sysdep.c 5882 2007-12-13 19:46:05Z davew $
3  *
4  * Citadel "system dependent" stuff.
5  * See copyright.txt for copyright information.
6  *
7  * Here's where we have the Citadel thread implimentation
8  *
9  */
10
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <sys/types.h>
15 #include <errno.h>
16 #include <sys/socket.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <signal.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <libcitadel.h>
33
34 #include "threads.h"
35 #include "ctdl_module.h"
36 #include "modules_init.h"
37 #include "housekeeping.h"
38 #include "config.h"
39 #include "citserver.h"
40 #include "sysdep_decls.h"
41
42 /*
43  * define this to use the new worker_thread method of handling connections
44  */
45 //#define NEW_WORKER
46
47 /*
48  * New thread interface.
49  * To create a thread you must call one of the create thread functions.
50  * You must pass it the address of (a pointer to a CtdlThreadNode initialised to NULL) like this
51  * struct CtdlThreadNode *node = NULL;
52  * pass in &node
53  * If the thread is created *node will point to the thread control structure for the created thread.
54  * If the thread creation fails *node remains NULL
55  * Do not free the memory pointed to by *node, it doesn't belong to you.
56  * This new interface duplicates much of the eCrash stuff. We should go for closer integration since that would
57  * remove the need for the calls to eCrashRegisterThread and friends
58  */
59
60 static int num_threads = 0;                     /* Current number of threads */
61 static int num_workers = 0;                     /* Current number of worker threads */
62
63 CtdlThreadNode *CtdlThreadList = NULL;
64 CtdlThreadNode *CtdlThreadSchedList = NULL;
65
66 static CtdlThreadNode *GC_thread = NULL;
67 static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE];
68 double CtdlThreadLoadAvg = 0;
69 double CtdlThreadWorkerAvg = 0;
70 citthread_key_t ThreadKey;
71
72 citthread_mutex_t Critters[MAX_SEMAPHORES];     /* Things needing locking */
73
74
75
76 void InitialiseSemaphores(void)
77 {
78         int i;
79
80         /* Set up a bunch of semaphores to be used for critical sections */
81         for (i=0; i<MAX_SEMAPHORES; ++i) {
82                 citthread_mutex_init(&Critters[i], NULL);
83         }
84 }
85
86
87
88
89 /*
90  * Obtain a semaphore lock to begin a critical section.
91  * but only if no one else has one
92  */
93 int try_critical_section(int which_one)
94 {
95         /* For all types of critical sections except those listed here,
96          * ensure nobody ever tries to do a critical section within a
97          * transaction; this could lead to deadlock.
98          */
99         if (    (which_one != S_FLOORCACHE)
100 #ifdef DEBUG_MEMORY_LEAKS
101                 && (which_one != S_DEBUGMEMLEAKS)
102 #endif
103                 && (which_one != S_RPLIST)
104         ) {
105                 cdb_check_handles();
106         }
107         return (citthread_mutex_trylock(&Critters[which_one]));
108 }
109
110
111 /*
112  * Obtain a semaphore lock to begin a critical section.
113  */
114 void begin_critical_section(int which_one)
115 {
116         /* CtdlLogPrintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
117
118         /* For all types of critical sections except those listed here,
119          * ensure nobody ever tries to do a critical section within a
120          * transaction; this could lead to deadlock.
121          */
122         if (    (which_one != S_FLOORCACHE)
123 #ifdef DEBUG_MEMORY_LEAKS
124                 && (which_one != S_DEBUGMEMLEAKS)
125 #endif
126                 && (which_one != S_RPLIST)
127         ) {
128                 cdb_check_handles();
129         }
130         citthread_mutex_lock(&Critters[which_one]);
131 }
132
133 /*
134  * Release a semaphore lock to end a critical section.
135  */
136 void end_critical_section(int which_one)
137 {
138         citthread_mutex_unlock(&Critters[which_one]);
139 }
140
141
142 /*
143  * A function to destroy the TSD
144  */
145 static void ctdl_thread_internal_dest_tsd(void *arg)
146 {
147         if (arg != NULL) {
148                 check_handles(arg);
149                 free(arg);
150         }
151 }
152
153
154 /*
155  * A function to initialise the thread TSD
156  */
157 void ctdl_thread_internal_init_tsd(void)
158 {
159         int ret;
160         
161         if ((ret = citthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
162                 CtdlLogPrintf(CTDL_EMERG, "citthread_key_create: %s\n", strerror(ret));
163                 exit(CTDLEXIT_DB);
164         }
165 }
166
167 /*
168  * Ensure that we have a key for thread-specific data. 
169  *
170  * This should be called immediately after startup by any thread 
171  * 
172  */
173 void CtdlThreadAllocTSD(void)
174 {
175         ThreadTSD *tsd;
176
177         if (citthread_getspecific(ThreadKey) != NULL)
178                 return;
179
180         tsd = malloc(sizeof(ThreadTSD));
181
182         tsd->tid = NULL;
183
184         memset(tsd->cursors, 0, sizeof tsd->cursors);
185         tsd->self = NULL;
186         
187         citthread_setspecific(ThreadKey, tsd);
188 }
189
190
191 void ctdl_thread_internal_free_tsd(void)
192 {
193         ctdl_thread_internal_dest_tsd(citthread_getspecific(ThreadKey));
194         citthread_setspecific(ThreadKey, NULL);
195 }
196
197
198 void ctdl_thread_internal_cleanup(void)
199 {
200         int i;
201         CtdlThreadNode *this_thread, *that_thread;
202         
203         for (i=0; i<CTDL_THREAD_LAST_STATE; i++)
204         {
205                 free (CtdlThreadStates[i]);
206         }
207         
208         /* Clean up the scheduled thread list */
209         this_thread = CtdlThreadSchedList;
210         while (this_thread)
211         {
212                 that_thread = this_thread;
213                 this_thread = this_thread->next;
214                 citthread_mutex_destroy(&that_thread->ThreadMutex);
215                 citthread_cond_destroy(&that_thread->ThreadCond);
216                 citthread_mutex_destroy(&that_thread->SleepMutex);
217                 citthread_cond_destroy(&that_thread->SleepCond);
218                 citthread_attr_destroy(&that_thread->attr);
219                 free(that_thread);
220         }
221         ctdl_thread_internal_free_tsd();
222 }
223
224 void ctdl_thread_internal_init(void)
225 {
226         CtdlThreadNode *this_thread;
227         int ret = 0;
228         
229         CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread");
230         CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread");
231         CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created");
232         CtdlThreadStates[CTDL_THREAD_CANCELLED] = strdup("Thread Cancelled");
233         CtdlThreadStates[CTDL_THREAD_EXITED] = strdup("Thread Exited");
234         CtdlThreadStates[CTDL_THREAD_STOPPING] = strdup("Thread Stopping");
235         CtdlThreadStates[CTDL_THREAD_STOP_REQ] = strdup("Thread Stop Requested");
236         CtdlThreadStates[CTDL_THREAD_SLEEPING] = strdup("Thread Sleeping");
237         CtdlThreadStates[CTDL_THREAD_RUNNING] = strdup("Thread Running");
238         CtdlThreadStates[CTDL_THREAD_BLOCKED] = strdup("Thread Blocked");
239         
240         /* Get ourself a thread entry */
241         this_thread = malloc(sizeof(CtdlThreadNode));
242         if (this_thread == NULL) {
243                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
244                 return;
245         }
246         // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
247         memset (this_thread, 0, sizeof(CtdlThreadNode));
248         
249         citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
250         citthread_cond_init (&(this_thread->ThreadCond), NULL);
251         citthread_mutex_init (&(this_thread->SleepMutex), NULL);
252         citthread_cond_init (&(this_thread->SleepCond), NULL);
253         
254         /* We are garbage collector so create us as running */
255         this_thread->state = CTDL_THREAD_RUNNING;
256         
257         if ((ret = citthread_attr_init(&this_thread->attr))) {
258                 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
259                 free(this_thread);
260                 return;
261         }
262
263         this_thread->name = "Garbage Collection Thread";
264         
265         this_thread->tid = citthread_self();
266         GC_thread = this_thread;
267         CT = this_thread;
268         
269         num_threads++;  // Increase the count of threads in the system.
270
271         this_thread->next = CtdlThreadList;
272         CtdlThreadList = this_thread;
273         if (this_thread->next)
274                 this_thread->next->prev = this_thread;
275         /* Set up start times */
276         gettimeofday(&this_thread->start_time, NULL);           /* Time this thread started */
277         memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval));     /* Changed state so mark it. */
278 }
279
280
281 /*
282  * A function to update a threads load averages
283  */
284  void ctdl_thread_internal_update_avgs(CtdlThreadNode *this_thread)
285  {
286         struct timeval now, result;
287         double last_duration;
288
289         gettimeofday(&now, NULL);
290         timersub(&now, &(this_thread->last_state_change), &result);
291         /* I don't think these mutex's are needed here */
292         citthread_mutex_lock(&this_thread->ThreadMutex);
293         // result now has a timeval for the time we spent in the last state since we last updated
294         last_duration = (double)result.tv_sec + ((double)result.tv_usec / (double) 1000000);
295         if (this_thread->state == CTDL_THREAD_SLEEPING)
296                 this_thread->avg_sleeping += last_duration;
297         if (this_thread->state == CTDL_THREAD_RUNNING)
298                 this_thread->avg_running += last_duration;
299         if (this_thread->state == CTDL_THREAD_BLOCKED)
300                 this_thread->avg_blocked += last_duration;
301         memcpy (&this_thread->last_state_change, &now, sizeof (struct timeval));
302         citthread_mutex_unlock(&this_thread->ThreadMutex);
303 }
304
305 /*
306  * A function to chenge the state of a thread
307  */
308 void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlThreadState new_state)
309 {
310         /*
311          * Wether we change state or not we need update the load values
312          */
313         ctdl_thread_internal_update_avgs(this_thread);
314         /* This mutex not needed here? */
315         citthread_mutex_lock(&this_thread->ThreadMutex); /* To prevent race condition of a sleeping thread */
316         if ((new_state == CTDL_THREAD_STOP_REQ) && (this_thread->state > CTDL_THREAD_STOP_REQ))
317                 this_thread->state = new_state;
318         if (((new_state == CTDL_THREAD_SLEEPING) || (new_state == CTDL_THREAD_BLOCKED)) && (this_thread->state == CTDL_THREAD_RUNNING))
319                 this_thread->state = new_state;
320         if ((new_state == CTDL_THREAD_RUNNING) && ((this_thread->state == CTDL_THREAD_SLEEPING) || (this_thread->state == CTDL_THREAD_BLOCKED)))
321                 this_thread->state = new_state;
322         citthread_mutex_unlock(&this_thread->ThreadMutex);
323 }
324
325
326 /*
327  * A function to tell all threads to exit
328  */
329 void CtdlThreadStopAll(void)
330 {
331         //FIXME: The signalling of the condition should not be in the critical_section
332         // We need to build a list of threads we are going to signal and then signal them afterwards
333         
334         CtdlThreadNode *this_thread;
335         
336         begin_critical_section(S_THREAD_LIST);
337         this_thread = CtdlThreadList;
338         // Ask the GC thread to stop first so everything knows we are shutting down.
339         GC_thread->state = CTDL_THREAD_STOP_REQ;
340         while(this_thread)
341         {
342 #ifdef THREADS_USESIGNALS
343                 if (!citthread_equal(this_thread->tid, GC_thread->tid))
344                         citthread_kill(this_thread->tid, SIGHUP);
345 #endif
346                 ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
347                 citthread_cond_signal(&this_thread->ThreadCond);
348                 citthread_cond_signal(&this_thread->SleepCond);
349                 this_thread->stop_ticker = time(NULL);
350                 CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (0x%08lx).\n",
351                         this_thread->name, this_thread->tid);
352                 this_thread = this_thread->next;
353         }
354         end_critical_section(S_THREAD_LIST);
355 }
356
357
358 /*
359  * A function to wake up all sleeping threads
360  */
361 void CtdlThreadWakeAll(void)
362 {
363         CtdlThreadNode *this_thread;
364         
365         CtdlLogPrintf(CTDL_DEBUG, "Thread system waking all threads.\n");
366         
367         begin_critical_section(S_THREAD_LIST);
368         this_thread = CtdlThreadList;
369         while(this_thread)
370         {
371                 if (!this_thread->thread_func)
372                 {
373                         citthread_cond_signal(&this_thread->ThreadCond);
374                         citthread_cond_signal(&this_thread->SleepCond);
375                 }
376                 this_thread = this_thread->next;
377         }
378         end_critical_section(S_THREAD_LIST);
379 }
380
381
382 /*
383  * A function to return the number of threads running in the system
384  */
385 int CtdlThreadGetCount(void)
386 {
387         return  num_threads;
388 }
389
390 int CtdlThreadGetWorkers(void)
391 {
392         return  num_workers;
393 }
394
395 double CtdlThreadGetWorkerAvg(void)
396 {
397         double ret;
398         
399         begin_critical_section(S_THREAD_LIST);
400         ret =  CtdlThreadWorkerAvg;
401         end_critical_section(S_THREAD_LIST);
402         return ret;
403 }
404
405 double CtdlThreadGetLoadAvg(void)
406 {
407         double ret;
408         
409         begin_critical_section(S_THREAD_LIST);
410         ret =  CtdlThreadLoadAvg;
411         end_critical_section(S_THREAD_LIST);
412         return ret;
413 }
414
415
416
417
418 /*
419  * A function to rename a thread
420  * Returns a const char *
421  */
422 const char *CtdlThreadName(const char *name)
423 {
424         const char *old_name;
425         
426         if (!CT)
427         {
428                 CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
429                 return NULL;
430         }
431         old_name = CT->name;
432         if (name)
433                 CT->name = name;
434         return (old_name);
435 }       
436
437
438 /*
439  * A function to force a thread to exit
440  */
441 void CtdlThreadCancel(CtdlThreadNode *thread)
442 {
443         CtdlThreadNode *this_thread;
444         
445         if (!thread)
446                 this_thread = CT;
447         else
448                 this_thread = thread;
449         if (!this_thread)
450         {
451                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
452                 CtdlThreadStopAll();
453                 return;
454         }
455         
456         if (!this_thread->thread_func)
457         {
458                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() the garbage collector.\n");
459                 CtdlThreadStopAll();
460                 return;
461         }
462         
463         ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED);
464         citthread_cancel(this_thread->tid);
465 }
466
467
468 /*
469  * A function for a thread to check if it has been asked to stop
470  */
471 int CtdlThreadCheckStop(void)
472 {
473         int state;
474         
475         if (!CT)
476         {
477                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
478                 CtdlThreadStopAll();
479                 return -1;
480         }
481         
482         state = CT->state;
483
484 #ifdef THREADS_USESIGNALS
485         if (CT->signal)
486         {
487                 CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
488                 CT->signal = 0;
489         }
490 #endif
491         if(state == CTDL_THREAD_STOP_REQ)
492         {
493                 CT->state = CTDL_THREAD_STOPPING;
494                 return -1;
495         }
496         else if((state < CTDL_THREAD_STOP_REQ) && (state > CTDL_THREAD_CREATE))
497         {
498                 return -1;
499         }
500         return 0;
501 }
502
503
504 /*
505  * A function to ask a thread to exit
506  * The thread must call CtdlThreadCheckStop() periodically to determine if it should exit
507  */
508 void CtdlThreadStop(CtdlThreadNode *thread)
509 {
510         CtdlThreadNode *this_thread;
511         
512         if (!thread)
513                 this_thread = CT;
514         else
515                 this_thread = thread;
516         if (!this_thread)
517                 return;
518         if (!(this_thread->thread_func))
519                 return;         // Don't stop garbage collector
520 #ifdef THREADS_USESIGNALS
521         if (!citthread_equal(this_thread->tid, GC_thread->tid))
522                 citthread_kill(this_thread->tid, SIGHUP);
523 #endif
524         ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
525         citthread_cond_signal(&this_thread->ThreadCond);
526         citthread_cond_signal(&this_thread->SleepCond);
527         this_thread->stop_ticker = time(NULL);
528 }
529
530 /*
531  * So we now have a sleep command that works with threads but it is in seconds
532  */
533 void CtdlThreadSleep(int secs)
534 {
535         struct timespec wake_time;
536         struct timeval time_now;
537         
538         
539         if (!CT)
540         {
541                 CtdlLogPrintf(CTDL_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
542                 return;
543         }
544         
545         memset (&wake_time, 0, sizeof(struct timespec));
546         gettimeofday(&time_now, NULL);
547         wake_time.tv_sec = time_now.tv_sec + secs;
548         wake_time.tv_nsec = time_now.tv_usec * 10;
549
550         ctdl_thread_internal_change_state (CT, CTDL_THREAD_SLEEPING);
551         
552         citthread_mutex_lock(&CT->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
553         citthread_cond_timedwait(&CT->SleepCond, &CT->ThreadMutex, &wake_time);
554         citthread_mutex_unlock(&CT->ThreadMutex);
555         
556         ctdl_thread_internal_change_state (CT, CTDL_THREAD_RUNNING);
557 }
558
559
560 /*
561  * Routine to clean up our thread function on exit
562  */
563 static void ctdl_internal_thread_cleanup(void *arg)
564 {
565         /*
566          * In here we were called by the current thread because it is exiting
567          * NB. WE ARE THE CURRENT THREAD
568          */
569         CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", CT->name, CT->tid);
570         
571         #ifdef HAVE_BACKTRACE
572         eCrash_UnregisterThread();
573         #endif
574         
575         citthread_mutex_lock(&CT->ThreadMutex);
576         CT->state = CTDL_THREAD_EXITED; // needs to be last thing else house keeping will unlink us too early
577         citthread_mutex_unlock(&CT->ThreadMutex);
578 }
579
580 /*
581  * A quick function to show the load averages
582  */
583 void ctdl_thread_internal_calc_loadavg(void)
584 {
585         CtdlThreadNode *that_thread;
586         double load_avg, worker_avg;
587         int workers = 0;
588
589         that_thread = CtdlThreadList;
590         load_avg = 0;
591         worker_avg = 0;
592         while(that_thread)
593         {
594                 /* Update load averages */
595                 ctdl_thread_internal_update_avgs(that_thread);
596                 citthread_mutex_lock(&that_thread->ThreadMutex);
597                 that_thread->load_avg = (that_thread->avg_sleeping + that_thread->avg_running) / (that_thread->avg_sleeping + that_thread->avg_running + that_thread->avg_blocked) * 100;
598                 that_thread->avg_sleeping /= 2;
599                 that_thread->avg_running /= 2;
600                 that_thread->avg_blocked /= 2;
601                 load_avg += that_thread->load_avg;
602                 if (that_thread->flags & CTDLTHREAD_WORKER)
603                 {
604                         worker_avg += that_thread->load_avg;
605                         workers++;
606                 }
607 #ifdef WITH_THREADLOG
608                 CtdlLogPrintf(CTDL_DEBUG, "CtdlThread, \"%s\" (%lu) \"%s\" %.2f %.2f %.2f %.2f\n",
609                         that_thread->name,
610                         that_thread->tid,
611                         CtdlThreadStates[that_thread->state],
612                         that_thread->avg_sleeping,
613                         that_thread->avg_running,
614                         that_thread->avg_blocked,
615                         that_thread->load_avg);
616 #endif
617                 citthread_mutex_unlock(&that_thread->ThreadMutex);
618                 that_thread = that_thread->next;
619         }
620         CtdlThreadLoadAvg = load_avg/num_threads;
621         CtdlThreadWorkerAvg = worker_avg/workers;
622 #ifdef WITH_THREADLOG
623         CtdlLogPrintf(CTDL_INFO, "System load average %.2f, workers averag %.2f, threads %d, workers %d, sessions %d\n", CtdlThreadLoadAvg, CtdlThreadWorkerAvg, num_threads, num_workers, num_sessions);
624 #endif
625 }
626
627
628 /*
629  * Garbage collection routine.
630  * Gets called by main() in a loop to clean up the thread list periodically.
631  */
632 void CtdlThreadGC (void)
633 {
634         CtdlThreadNode *this_thread, *that_thread;
635         int workers = 0, sys_workers;
636         int ret=0;
637         
638         begin_critical_section(S_THREAD_LIST);
639         
640         /* Handle exiting of garbage collector thread */
641         if(num_threads == 1)
642                 CtdlThreadList->state = CTDL_THREAD_EXITED;
643         
644 #ifdef WITH_THREADLOG
645         CtdlLogPrintf(CTDL_DEBUG, "Thread system running garbage collection.\n");
646 #endif
647         /*
648          * Woke up to do garbage collection
649          */
650         this_thread = CtdlThreadList;
651         while(this_thread)
652         {
653                 that_thread = this_thread;
654                 this_thread = this_thread->next;
655                 
656                 if ((that_thread->state == CTDL_THREAD_STOP_REQ || that_thread->state == CTDL_THREAD_STOPPING)
657                         && (!citthread_equal(that_thread->tid, citthread_self())))
658                                 CtdlLogPrintf(CTDL_DEBUG, "Waiting for thread %s (0x%08lx) to exit.\n", that_thread->name, that_thread->tid);
659                 else
660                 {
661                         /**
662                          * Catch the situation where a worker was asked to stop but couldn't and we are not
663                          * shutting down.
664                          */
665                         that_thread->stop_ticker = 0;
666                 }
667                 
668                 if (that_thread->stop_ticker + 5 == time(NULL))
669                 {
670                         CtdlLogPrintf(CTDL_DEBUG, "Thread System: The thread \"%s\" (0x%08lx) failed to self terminate within 5 ticks. It would be cancelled now.\n", that_thread->name, that_thread->tid);
671                         if ((that_thread->flags & CTDLTHREAD_WORKER) == 0)
672                                 CtdlLogPrintf(CTDL_INFO, "Thread System: A non worker thread would have been canceled this may cause message loss.\n");
673 //                      that_thread->state = CTDL_THREAD_CANCELLED;
674                         that_thread->stop_ticker++;
675 //                      citthread_cancel(that_thread->tid);
676 //                      continue;
677                 }
678                 
679                 /* Do we need to clean up this thread? */
680                 if ((that_thread->state != CTDL_THREAD_EXITED) && (that_thread->state != CTDL_THREAD_CANCELLED))
681                 {
682                         if(that_thread->flags & CTDLTHREAD_WORKER)
683                                 workers++;      /* Sanity check on number of worker threads */
684                         continue;
685                 }
686                 
687                 if (citthread_equal(that_thread->tid, citthread_self()) && that_thread->thread_func)
688                 {       /* Sanity check */
689                         end_critical_section(S_THREAD_LIST);
690                         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, a thread is trying to clean up after itself.\n");
691                         abort();
692                         return;
693                 }
694                 
695                 if (num_threads <= 0)
696                 {       /* Sanity check */
697                         end_critical_section(S_THREAD_LIST);
698                         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, num_threads <= 0 and trying to do Garbage Collection.\n");
699                         abort();
700                         return;
701                 }
702
703                 if(that_thread->flags & CTDLTHREAD_WORKER)
704                         num_workers--;  /* This is a wroker thread so reduce the count. */
705                 num_threads--;
706                 /* If we are unlinking the list head then the next becomes the list head */
707                 if(that_thread->prev)
708                         that_thread->prev->next = that_thread->next;
709                 else
710                         CtdlThreadList = that_thread->next;
711                 if(that_thread->next)
712                         that_thread->next->prev = that_thread->prev;
713                 
714                 citthread_cond_signal(&that_thread->ThreadCond);
715                 citthread_cond_signal(&that_thread->SleepCond); // Make sure this thread is awake
716                 citthread_mutex_lock(&that_thread->ThreadMutex);        // Make sure it has done what its doing
717                 citthread_mutex_unlock(&that_thread->ThreadMutex);
718                 /*
719                  * Join on the thread to do clean up and prevent memory leaks
720                  * Also makes sure the thread has cleaned up after itself before we remove it from the list
721                  * We can join on the garbage collector thread the join should just return EDEADLCK
722                  */
723                 ret = citthread_join (that_thread->tid, NULL);
724                 if (ret == EDEADLK)
725                         CtdlLogPrintf(CTDL_DEBUG, "Garbage collection on own thread.\n");
726                 else if (ret == EINVAL)
727                         CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, that thread already joined on.\n");
728                 else if (ret == ESRCH)
729                         CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, no thread to join on.\n");
730                 else if (ret != 0)
731                         CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, citthread_join returned an unknown error(%d).\n", ret);
732                 /*
733                  * Now we own that thread entry
734                  */
735                 CtdlLogPrintf(CTDL_INFO, "Garbage Collection for thread \"%s\" (0x%08lx).\n",
736                         that_thread->name, that_thread->tid);
737                 citthread_mutex_destroy(&that_thread->ThreadMutex);
738                 citthread_cond_destroy(&that_thread->ThreadCond);
739                 citthread_mutex_destroy(&that_thread->SleepMutex);
740                 citthread_cond_destroy(&that_thread->SleepCond);
741                 citthread_attr_destroy(&that_thread->attr);
742                 free(that_thread);
743         }
744         sys_workers = num_workers;
745         end_critical_section(S_THREAD_LIST);
746         
747         /* Sanity check number of worker threads */
748         if (workers != sys_workers)
749         {
750                 CtdlLogPrintf(CTDL_EMERG,
751                         "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
752                         workers, sys_workers
753                         );
754                 abort();
755         }
756 }
757
758
759
760  
761 /*
762  * Runtime function for a Citadel Thread.
763  * This initialises the threads environment and then calls the user supplied thread function
764  * Note that this is the REAL thread function and wraps the users thread function.
765  */ 
766 static void *ctdl_internal_thread_func (void *arg)
767 {
768         CtdlThreadNode *this_thread;
769         void *ret = NULL;
770
771         /* lock and unlock the thread list.
772          * This causes this thread to wait until all its creation stuff has finished before it
773          * can continue its execution.
774          */
775         begin_critical_section(S_THREAD_LIST);
776         this_thread = (CtdlThreadNode *) arg;
777         gettimeofday(&this_thread->start_time, NULL);           /* Time this thread started */
778 //      citthread_mutex_lock(&this_thread->ThreadMutex);
779         
780         // Register the cleanup function to take care of when we exit.
781         citthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
782         // Get our thread data structure
783         CtdlThreadAllocTSD();
784         CT = this_thread;
785         this_thread->pid = getpid();
786         memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval));     /* Changed state so mark it. */
787         /* Only change to running state if we weren't asked to stop during the create cycle
788          * Other wise there is a window to allow this threads creation to continue to full grown and
789          * therby prevent a shutdown of the server.
790          */
791 //      citthread_mutex_unlock(&this_thread->ThreadMutex);
792                 
793         if (!CtdlThreadCheckStop())
794         {
795                 citthread_mutex_lock(&this_thread->ThreadMutex);
796                 this_thread->state = CTDL_THREAD_RUNNING;
797                 citthread_mutex_unlock(&this_thread->ThreadMutex);
798         }
799         end_critical_section(S_THREAD_LIST);
800         
801         // Register for tracing
802         #ifdef HAVE_BACKTRACE
803         eCrash_RegisterThread(this_thread->name, 0);
804         #endif
805         
806         // Tell the world we are here
807         CtdlLogPrintf(CTDL_NOTICE, "Created a new thread \"%s\" (0x%08lx).\n",
808                 this_thread->name, this_thread->tid);
809         
810         /*
811          * run the thread to do the work but only if we haven't been asked to stop
812          */
813         if (!CtdlThreadCheckStop())
814                 ret = (this_thread->thread_func)(this_thread->user_args);
815         
816         /*
817          * Our thread is exiting either because it wanted to end or because the server is stopping
818          * We need to clean up
819          */
820         citthread_cleanup_pop(1);       // Execute our cleanup routine and remove it
821         
822         return(ret);
823 }
824
825
826
827
828 /*
829  * Function to initialise an empty thread structure
830  */
831 CtdlThreadNode *ctdl_internal_init_thread_struct(CtdlThreadNode *this_thread, long flags)
832 {
833         int ret = 0;
834         
835         // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
836         memset (this_thread, 0, sizeof(CtdlThreadNode));
837         
838         /* Create the mutex's early so we can use them */
839         citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
840         citthread_cond_init (&(this_thread->ThreadCond), NULL);
841         citthread_mutex_init (&(this_thread->SleepMutex), NULL);
842         citthread_cond_init (&(this_thread->SleepCond), NULL);
843         
844         this_thread->state = CTDL_THREAD_CREATE;
845         
846         if ((ret = citthread_attr_init(&this_thread->attr))) {
847                 citthread_mutex_unlock(&this_thread->ThreadMutex);
848                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
849                 citthread_cond_destroy(&(this_thread->ThreadCond));
850                 citthread_mutex_destroy(&(this_thread->SleepMutex));
851                 citthread_cond_destroy(&(this_thread->SleepCond));
852                 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
853                 free(this_thread);
854                 return NULL;
855         }
856
857         /* Our per-thread stacks need to be bigger than the default size,
858          * otherwise the MIME parser crashes on FreeBSD, and the IMAP service
859          * crashes on 64-bit Linux.
860          */
861         if (flags & CTDLTHREAD_BIGSTACK)
862         {
863 #ifdef WITH_THREADLOG
864                 CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n");
865 #endif
866                 if ((ret = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
867                         citthread_mutex_unlock(&this_thread->ThreadMutex);
868                         citthread_mutex_destroy(&(this_thread->ThreadMutex));
869                         citthread_cond_destroy(&(this_thread->ThreadCond));
870                         citthread_mutex_destroy(&(this_thread->SleepMutex));
871                         citthread_cond_destroy(&(this_thread->SleepCond));
872                         citthread_attr_destroy(&this_thread->attr);
873                         CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_setstacksize: %s\n",
874                                 strerror(ret));
875                         free(this_thread);
876                         return NULL;
877                 }
878         }
879
880         /* Set this new thread with an avg_blocked of 2. We do this so that its creation affects the
881          * load average for the system. If we don't do this then we create a mass of threads at the same time 
882          * because the creation didn't affect the load average.
883          */
884         this_thread->avg_blocked = 2;
885         
886         return (this_thread);
887 }
888
889
890
891  
892 /*
893  * Internal function to create a thread.
894  */ 
895 CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args)
896 {
897         int ret = 0;
898         CtdlThreadNode *this_thread;
899
900         if (num_threads >= 32767)
901         {
902                 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
903                 return NULL;
904         }
905                 
906         this_thread = malloc(sizeof(CtdlThreadNode));
907         if (this_thread == NULL) {
908                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
909                 return NULL;
910         }
911         
912         /* Initialise the thread structure */
913         if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
914         {
915                 free(this_thread);
916                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
917                 return NULL;
918         }
919         /*
920          * If we got here we are going to create the thread so we must initilise the structure
921          * first because most implimentations of threading can't create it in a stopped state
922          * and it might want to do things with its structure that aren't initialised otherwise.
923          */
924         if(name)
925         {
926                 this_thread->name = name;
927         }
928         else
929         {
930                 this_thread->name = "Un-named Thread";
931         }
932         
933         this_thread->flags = flags;
934         this_thread->thread_func = thread_func;
935         this_thread->user_args = args;
936         
937 //      citthread_mutex_lock(&this_thread->ThreadMutex);
938         
939         begin_critical_section(S_THREAD_LIST);
940         /*
941          * We pass this_thread into the thread as its args so that it can find out information
942          * about itself and it has a bit of storage space for itself, not to mention that the REAL
943          * thread function needs to finish off the setup of the structure
944          */
945         if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
946         {
947                 end_critical_section(S_THREAD_LIST);
948                 CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
949                         strerror(ret));
950                 citthread_mutex_unlock(&this_thread->ThreadMutex);
951                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
952                 citthread_cond_destroy(&(this_thread->ThreadCond));
953                 citthread_mutex_destroy(&(this_thread->SleepMutex));
954                 citthread_cond_destroy(&(this_thread->SleepCond));
955                 citthread_attr_destroy(&this_thread->attr);
956                 free(this_thread);
957                 return NULL;
958         }
959         
960         num_threads++;  // Increase the count of threads in the system.
961         if(this_thread->flags & CTDLTHREAD_WORKER)
962                 num_workers++;
963
964         this_thread->next = CtdlThreadList;
965         CtdlThreadList = this_thread;
966         if (this_thread->next)
967                 this_thread->next->prev = this_thread;
968         ctdl_thread_internal_calc_loadavg();
969         
970 //      citthread_mutex_unlock(&this_thread->ThreadMutex);
971         end_critical_section(S_THREAD_LIST);
972         
973         return this_thread;
974 }
975
976 /*
977  * Wrapper function to create a thread
978  * ensures the critical section and other protections are in place.
979  * char *name = name to give to thread, if NULL, use generic name
980  * int flags = flags to determine type of thread and standard facilities
981  */
982 CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args)
983 {
984         CtdlThreadNode *ret = NULL;
985         
986         ret = ctdl_internal_create_thread(name, flags, thread_func, args);
987         return ret;
988 }
989
990
991
992 /*
993  * Internal function to schedule a thread.
994  * Must be called from within a S_THREAD_LIST critical section
995  */ 
996 CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) (void *arg), void *args, time_t when)
997 {
998         CtdlThreadNode *this_thread;
999
1000         if (num_threads >= 32767)
1001         {
1002                 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
1003                 return NULL;
1004         }
1005                 
1006         this_thread = malloc(sizeof(CtdlThreadNode));
1007         if (this_thread == NULL) {
1008                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
1009                 return NULL;
1010         }
1011         /* Initialise the thread structure */
1012         if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
1013         {
1014                 free(this_thread);
1015                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
1016                 return NULL;
1017         }
1018
1019         /*
1020          * If we got here we are going to create the thread so we must initilise the structure
1021          * first because most implimentations of threading can't create it in a stopped state
1022          * and it might want to do things with its structure that aren't initialised otherwise.
1023          */
1024         if(name)
1025         {
1026                 this_thread->name = name;
1027         }
1028         else
1029         {
1030                 this_thread->name = "Un-named Thread";
1031         }
1032         
1033         this_thread->flags = flags;
1034         this_thread->thread_func = thread_func;
1035         this_thread->user_args = args;
1036         
1037         /*
1038          * When to start this thread
1039          */
1040         this_thread->when = when;
1041
1042         begin_critical_section(S_SCHEDULE_LIST);
1043         this_thread->next = CtdlThreadSchedList;
1044         CtdlThreadSchedList = this_thread;
1045         if (this_thread->next)
1046                 this_thread->next->prev = this_thread;
1047         end_critical_section(S_SCHEDULE_LIST);
1048         
1049         return this_thread;
1050 }
1051
1052
1053
1054 CtdlThreadNode *ctdl_thread_internal_start_scheduled (CtdlThreadNode *this_thread)
1055 {
1056         int ret = 0;
1057         
1058 //      citthread_mutex_lock(&that_thread->ThreadMutex);
1059         begin_critical_section(S_THREAD_LIST);
1060         /*
1061          * We pass this_thread into the thread as its args so that it can find out information
1062          * about itself and it has a bit of storage space for itself, not to mention that the REAL
1063          * thread function needs to finish off the setup of the structure
1064          */
1065         if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
1066         {
1067                 end_critical_section(S_THREAD_LIST);
1068                 CtdlLogPrintf(CTDL_DEBUG, "Failed to start scheduled thread \"%s\": %s\n", this_thread->name, strerror(ret));
1069 //              citthread_mutex_unlock(&this_thread->ThreadMutex);
1070                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
1071                 citthread_cond_destroy(&(this_thread->ThreadCond));
1072                 citthread_mutex_destroy(&(this_thread->SleepMutex));
1073                 citthread_cond_destroy(&(this_thread->SleepCond));
1074                 citthread_attr_destroy(&this_thread->attr);
1075                 free(this_thread);
1076                 return NULL;
1077         }
1078         
1079         
1080         num_threads++;  // Increase the count of threads in the system.
1081         if(this_thread->flags & CTDLTHREAD_WORKER)
1082                 num_workers++;
1083
1084         this_thread->next = CtdlThreadList;
1085         CtdlThreadList = this_thread;
1086         if (this_thread->next)
1087                 this_thread->next->prev = this_thread;
1088 //      citthread_mutex_unlock(&that_thread->ThreadMutex);
1089         
1090         ctdl_thread_internal_calc_loadavg();
1091         end_critical_section(S_THREAD_LIST);
1092         
1093         
1094         return this_thread;
1095 }
1096
1097
1098
1099 void ctdl_thread_internal_check_scheduled(void)
1100 {
1101         CtdlThreadNode *this_thread, *that_thread;
1102         time_t now;
1103         
1104         if (try_critical_section(S_SCHEDULE_LIST))
1105                 return; /* If this list is locked we wait till the next chance */
1106         
1107         now = time(NULL);
1108         
1109 #ifdef WITH_THREADLOG
1110         CtdlLogPrintf(CTDL_DEBUG, "Checking for scheduled threads to start.\n");
1111 #endif
1112
1113         this_thread = CtdlThreadSchedList;
1114         while(this_thread)
1115         {
1116                 that_thread = this_thread;
1117                 this_thread = this_thread->next;
1118                 
1119                 if (now > that_thread->when)
1120                 {
1121                         /* Unlink from schedule list */
1122                         if (that_thread->prev)
1123                                 that_thread->prev->next = that_thread->next;
1124                         else
1125                                 CtdlThreadSchedList = that_thread->next;
1126                         if (that_thread->next)
1127                                 that_thread->next->prev = that_thread->prev;
1128                                 
1129                         that_thread->next = that_thread->prev = NULL;
1130 #ifdef WITH_THREADLOG
1131                         CtdlLogPrintf(CTDL_DEBUG, "About to start scheduled thread \"%s\".\n", that_thread->name);
1132 #endif
1133                         if (CT->state > CTDL_THREAD_STOP_REQ)
1134                         {       /* Only start it if the system is not stopping */
1135                                 if (ctdl_thread_internal_start_scheduled (that_thread))
1136                                 {
1137 #ifdef WITH_THREADLOG
1138                                         CtdlLogPrintf(CTDL_INFO, "Thread system, Started a scheduled thread \"%s\" (0x%08lx).\n",
1139                                                 that_thread->name, that_thread->tid);
1140 #endif
1141                                 }
1142                         }
1143                 }
1144 #ifdef WITH_THREADLOG
1145                 else
1146                 {
1147                         CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" will start in %ld seconds.\n",
1148                                 that_thread->name, that_thread->when - time(NULL));
1149                 }
1150 #endif
1151         }
1152         end_critical_section(S_SCHEDULE_LIST);
1153 }
1154
1155
1156 /*
1157  * A warapper function for select so we can show a thread as blocked
1158  */
1159 int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
1160 {
1161         int ret = 0;
1162         
1163         ctdl_thread_internal_change_state(CT, CTDL_THREAD_BLOCKED);
1164         if (!CtdlThreadCheckStop())
1165                 ret = select(n, readfds, writefds, exceptfds, timeout);
1166         /**
1167          * If the select returned <= 0 then it failed due to an error
1168          * or timeout so this thread could stop if asked to do so.
1169          * Anything else means it needs to continue unless the system is shutting down
1170          */
1171         if (ret <= 0)
1172         {
1173                 /**
1174                  * select says nothing to do so we can change to running if we haven't been asked to stop.
1175                  */
1176                 ctdl_thread_internal_change_state(CT, CTDL_THREAD_RUNNING);
1177         }
1178         else
1179         {
1180                 /**
1181                  * The select says this thread needs to do something useful.
1182                  * This thread was in an idle state so it may have been asked to stop
1183                  * but if the system isn't shutting down this thread is no longer
1184                  * idle and select has given it a task to do so it must not stop
1185                  * In this condition we need to force it into the running state.
1186                  * CtdlThreadGC will clear its ticker for us.
1187                  *
1188                  * FIXME: there is still a small hole here. It is possible for the sequence of locking
1189                  * to allow the state to get changed to STOP_REQ just after this code if the other thread
1190                  * has decided to change the state before this lock, it there fore has to wait till the lock
1191                  * completes but it will continue to change the state. We need something a bit better here.
1192                  */
1193                 citthread_mutex_lock(&CT->ThreadMutex); /* To prevent race condition of a sleeping thread */
1194                 if (GC_thread->state > CTDL_THREAD_STOP_REQ && CT->state <= CTDL_THREAD_STOP_REQ)
1195                 {
1196                         CtdlLogPrintf(CTDL_DEBUG, "Thread %s (0x%08lx) refused stop request.\n", CT->name, CT->tid);
1197                         CT->state = CTDL_THREAD_RUNNING;
1198                 }
1199                 citthread_mutex_unlock(&CT->ThreadMutex);
1200         }
1201
1202         return ret;
1203 }
1204
1205
1206
1207 void *new_worker_thread(void *arg);
1208 extern void close_masters (void);
1209
1210
1211
1212 void go_threading(void)
1213 {
1214         int i;
1215         CtdlThreadNode *last_worker;
1216         
1217         /*
1218          * Initialise the thread system
1219          */
1220         ctdl_thread_internal_init();
1221
1222         /* Second call to module init functions now that threading is up */
1223         initialise_modules(1);
1224
1225         /*
1226          * This thread is now used for garbage collection of other threads in the thread list
1227          */
1228         CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", citthread_self());
1229
1230         /*
1231          * We do a lot of locking and unlocking of the thread list in here.
1232          * We do this so that we can repeatedly release time for other threads
1233          * that may be waiting on the thread list.
1234          * We are a low priority thread so we can afford to do this
1235          */
1236         
1237         while (CtdlThreadGetCount())
1238         {
1239                 if (CT->signal)
1240                         exit_signal = CT->signal;
1241                 if (exit_signal)
1242                 {
1243                         CtdlThreadStopAll();
1244 //                      close_masters();
1245                 }
1246                 check_sched_shutdown();
1247                 if (CT->state > CTDL_THREAD_STOP_REQ)
1248                 {
1249                         begin_critical_section(S_THREAD_LIST);
1250                         ctdl_thread_internal_calc_loadavg();
1251                         end_critical_section(S_THREAD_LIST);
1252                         
1253                         ctdl_thread_internal_check_scheduled(); /* start scheduled threads */
1254                 }
1255                 
1256                 /* Reduce the size of the worker thread pool if necessary. */
1257                 if ((CtdlThreadGetWorkers() > config.c_min_workers + 1) && (CtdlThreadWorkerAvg < 20) && (CT->state > CTDL_THREAD_STOP_REQ))
1258                 {
1259                         /* Ask a worker thread to stop as we no longer need it */
1260                         begin_critical_section(S_THREAD_LIST);
1261                         last_worker = CtdlThreadList;
1262                         while (last_worker)
1263                         {
1264                                 citthread_mutex_lock(&last_worker->ThreadMutex);
1265                                 if (last_worker->flags & CTDLTHREAD_WORKER && (last_worker->state > CTDL_THREAD_STOPPING) && (last_worker->Context == NULL))
1266                                 {
1267                                         citthread_mutex_unlock(&last_worker->ThreadMutex);
1268                                         break;
1269                                 }
1270                                 citthread_mutex_unlock(&last_worker->ThreadMutex);
1271                                 last_worker = last_worker->next;
1272                         }
1273                         end_critical_section(S_THREAD_LIST);
1274                         if (last_worker)
1275                         {
1276 #ifdef WITH_THREADLOG
1277                                 CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (0x%08lx).\n",
1278                                         last_worker->name,
1279                                         last_worker->tid
1280                                         );
1281 #endif
1282                                 CtdlThreadStop(last_worker);
1283                         }
1284                 }
1285         
1286                 /*
1287                  * If all our workers are working hard, start some more to help out
1288                  * with things
1289                  */
1290                 /* FIXME: come up with a better way to dynamically alter the number of threads
1291                  * based on the system load
1292                  */
1293 #ifdef NEW_WORKER
1294                 if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() <= num_sessions) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
1295 #else
1296                 if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
1297 #endif /* NEW_WORKER */
1298                 {
1299                         for (i=0; i<5 ; i++)
1300                         {
1301 #ifdef NEW_WORKER
1302                                 CtdlThreadCreate("Worker Thread (new)",
1303                                         CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1304                                         new_worker_thread,
1305                                         NULL
1306                                         );
1307 #else
1308                                 CtdlThreadCreate("Worker Thread",
1309                                         CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1310                                         worker_thread,
1311                                         NULL
1312                                         );
1313 #endif /* NEW_WORKER */
1314                         }
1315                 }
1316                 
1317                 CtdlThreadGC();
1318                 
1319                 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
1320                 {
1321                         CtdlThreadGC();
1322                 }
1323                 
1324 #ifdef THREADS_USESIGNALS
1325                 if (CtdlThreadGetCount() && CT->state > CTDL_THREAD_STOP_REQ)
1326 #else
1327                 if (CtdlThreadGetCount())
1328 #endif
1329                         CtdlThreadSleep(1);
1330         }
1331         /*
1332          * If the above loop exits we must be shutting down since we obviously have no threads
1333          */
1334         ctdl_thread_internal_cleanup();
1335 }
1336
1337
1338
1339
1340 /*
1341  * Starting a new implimentation of a worker thread.
1342  * This new implimentation will be faster and do more work per thread.
1343  */
1344  
1345 /*
1346  * Select on master socket.
1347  * First worker thread in here acquires the lock and builds an FDSET of master sockets.
1348  * then it goes into a loop selecting on the master sockets timing out every few milliseconds.
1349  * If it times out it rebiulds its list and loops.
1350  * If the select succeeds it creates a new context and returns.
1351  * During this time the other workers are selecting on existing contexts or sleeping.
1352  */
1353 void select_on_master(void)
1354 {
1355         fd_set readfds;
1356         struct ServiceFunctionHook *serviceptr;
1357         int ssock;                      /* Descriptor for client socket */
1358         int highest;
1359         int m, i;
1360         int retval = 0;
1361         struct timeval tv;
1362         struct CitContext *con;
1363         const char *old_name;
1364
1365
1366
1367         old_name = CtdlThreadName("select_on_master");
1368
1369         /* Initialize the fdset. */
1370         FD_ZERO(&readfds);
1371         highest = 0;
1372
1373         /* First, add the various master sockets to the fdset. */
1374         for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1375                 m = serviceptr->msock;
1376                 FD_SET(m, &readfds);
1377                 if (m > highest) {
1378                         highest = m;
1379                 }
1380         }
1381
1382         tv.tv_sec = 1;          /* wake up every 1 sec if no input */
1383         tv.tv_usec = 0;
1384         retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1385
1386         /* Select got an error or we are shutting down so get out */
1387         if (retval == 0 || CtdlThreadCheckStop()) {
1388                 CtdlThreadName(old_name);
1389                 return;
1390         }
1391
1392         /* Select says something happened on one of our master sockets so now we handle it */
1393         for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1394                 if (FD_ISSET(serviceptr->msock, &readfds)) {
1395                         ssock = accept(serviceptr->msock, NULL, 0);
1396                         if (ssock >= 0) {
1397                                 CtdlLogPrintf(CTDL_DEBUG, "New client socket %d\n", ssock);
1398                                 /* The master socket is non-blocking but the client
1399                                  * sockets need to be blocking, otherwise certain
1400                                  * operations barf on FreeBSD.  Not a fatal error.
1401                                  */
1402                                 if (fcntl(ssock, F_SETFL, 0) < 0) {
1403                                         CtdlLogPrintf(CTDL_EMERG,
1404                                                       "citserver: Can't set socket to blocking: %s\n",
1405                                                       strerror(errno));
1406                                 }
1407
1408                                 /* New context will be created already
1409                                  * set up in the CON_EXECUTING state.
1410                                  */
1411                                 con = CreateNewContext();
1412                                 CT->Context = con;
1413
1414                                 /* Assign our new socket number to it. */
1415                                 con->client_socket = ssock;
1416                                 con->h_command_function = serviceptr->h_command_function;
1417                                 con->h_async_function = serviceptr->h_async_function;
1418                                 con->ServiceName = serviceptr->ServiceName;
1419                                 /* Determine whether it's a local socket */
1420                                 if (serviceptr->sockpath != NULL)
1421                                         con->is_local_socket = 1;
1422
1423                                 /* Set the SO_REUSEADDR socket option */
1424                                 i = 1;
1425                                 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
1426
1427                                 become_session(con);
1428                                 begin_session(con);
1429                                 serviceptr->h_greeting_function();
1430                                 become_session(NULL);
1431                                 con->state = CON_IDLE;
1432                                 break;
1433                         }
1434                 }
1435         }
1436
1437         CtdlThreadName(old_name);
1438 }
1439
1440 /*
1441  * Select on client socket.
1442  * First worker thread in here acquires the lock and builds an FDSET of client sockets.
1443  * then it selects on the client sockets timing out after 1 second.
1444  * If it times out the thread goes off to check on housekeeping etc.
1445  * If the select succeeds the thread goes off to handle the client request.
1446  * If the list of client connections is empty the threads all sleep for one second
1447  */
1448 struct CitContext *select_on_client(void)
1449 {
1450         fd_set readfds;
1451         struct timeval tv;
1452         int retval = 0;
1453         int highest=0;
1454         const char *old_name;
1455         
1456         
1457         old_name = CtdlThreadName("select_on_client");
1458         
1459         /* Initialise the fdset */
1460         FD_ZERO(&readfds);
1461         FD_SET(CT->Context->client_socket, &readfds);
1462         highest = CT->Context->client_socket;   
1463         /* Now we can select on any connections that are waiting */
1464         
1465         if (!CtdlThreadCheckStop())
1466         {
1467                 tv.tv_sec = config.c_sleeping;          /* wake up every second if no input */
1468                 tv.tv_usec = 0;
1469                 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
1470         }
1471         else    /* Shutting down? */
1472         {
1473                 CtdlThreadName(old_name);
1474                 return(NULL);
1475         }
1476                 
1477
1478         /* Now figure out who made this select() unblock.
1479          * First, check for an error or exit condition.
1480          */
1481         if (retval < 0) {
1482                 if (errno == EBADF) {
1483                         CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1484                                 strerror(errno));
1485                 }
1486                 if (errno != EINTR) {
1487                         CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1488                         CtdlThreadStopAll();
1489                 } else if (!CtdlThreadCheckStop()) {
1490                         CtdlLogPrintf(CTDL_DEBUG, "Un handled select failure.\n");
1491                 }
1492                 CtdlThreadName(old_name);
1493                 return NULL;
1494         }
1495         else if(retval == 0)
1496         {
1497                 CtdlThreadName(old_name);
1498                 CT->Context->kill_me = 1;
1499                 CT->Context = NULL;
1500                 return CT->Context;
1501         }
1502         
1503         CT->Context->state = CON_EXECUTING;
1504         CT->Context->input_waiting = 1;
1505         
1506         CtdlThreadName(old_name);
1507         return (CT->Context);
1508 }
1509
1510
1511
1512 /*
1513  * Do the worker threads work when needed
1514  */
1515 int execute_session(struct CitContext *bind_me)
1516 {
1517         int force_purge;
1518         
1519         become_session(bind_me);
1520
1521         /* If the client has sent a command, execute it. */
1522         if (CC->input_waiting) {
1523                 CC->h_command_function();
1524                 CC->input_waiting = 0;
1525         }
1526
1527         /* If there are asynchronous messages waiting and the
1528          * client supports it, do those now */
1529         if ((CC->is_async) && (CC->async_waiting)
1530            && (CC->h_async_function != NULL)) {
1531                 CC->h_async_function();
1532                 CC->async_waiting = 0;
1533         }
1534                 
1535         force_purge = CC->kill_me;
1536         if (force_purge)
1537                 CT->Context = NULL;
1538         become_session(NULL);
1539         bind_me->state = CON_IDLE;
1540         return force_purge;
1541 }
1542
1543
1544
1545 extern void dead_session_purge(int force);
1546
1547 /*
1548  * A new worker_thread loop.
1549  */
1550  
1551 void *new_worker_thread(void *arg)
1552 {
1553         struct CitContext *bind_me;
1554         int force_purge;
1555         
1556         while (!CtdlThreadCheckStop()) {
1557
1558                 /* make doubly sure we're not holding any stale db handles
1559                  * which might cause a deadlock.
1560                  */
1561                 cdb_check_handles();
1562                 force_purge = 0;
1563                 bind_me = NULL;         /* Which session shall we handle? */
1564                         
1565                 if (CT->Context == NULL)
1566                         select_on_master();
1567                 if (CtdlThreadCheckStop())
1568                         break;
1569                         
1570                 if (CT->Context)
1571                         bind_me = select_on_client();
1572                 if (CtdlThreadCheckStop())
1573                         break;
1574                         
1575                 if (bind_me)
1576                         force_purge = execute_session(bind_me);
1577                         
1578                 dead_session_purge(force_purge);
1579                 if (CtdlThreadCheckStop())
1580                         break;
1581                         
1582                 do_housekeeping();
1583         }
1584         return NULL;
1585 }