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