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