CC->kill_me now contains an enum indicating the REASON session was killed
[citadel.git] / citadel / threads.c
index 215dc7bb483fafbd17ab44408dfc3b68986fd418..1b605d0577da572882aa3f6fad5e1422a8b4c80f 100644 (file)
@@ -1,20 +1,70 @@
 /*
- * $Id: sysdep.c 5882 2007-12-13 19:46:05Z davew $
+ * Thread handling stuff for Citadel server
  *
- * Citadel "system dependent" stuff.
- * See copyright.txt for copyright information.
+ * Copyright (c) 1987-2011 by the citadel.org team
  *
- * Here's where we have the Citadel thread implimentation
+ * This program is open source software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
  *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/types.h>
 #include <errno.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <syslog.h>
+
+#include "sysdep.h"
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
+#ifdef HAVE_SYSCALL_H
+# include <syscall.h>
+#else 
+# if HAVE_SYS_SYSCALL_H
+#  include <sys/syscall.h>
+# endif
+#endif
+
+#include <libcitadel.h>
+
 #include "threads.h"
 #include "ctdl_module.h"
 #include "modules_init.h"
 #include "housekeeping.h"
 #include "config.h"
+#include "citserver.h"
+#include "sysdep_decls.h"
+#include "context.h"
+#include "event_client.h"
+
+/*
+ * define this to use the new worker_thread method of handling connections
+ */
+//#define NEW_WORKER
 
 /*
  * New thread interface.
 
 static int num_threads = 0;                    /* Current number of threads */
 static int num_workers = 0;                    /* Current number of worker threads */
+long statcount = 0;            /* are we doing a stats check? */
+static long stats_done = 0;
 
-struct CtdlThreadNode *CtdlThreadList = NULL;
-struct CtdlThreadNode *CtdlThreadSchedList = NULL;
+CtdlThreadNode *CtdlThreadList = NULL;
+CtdlThreadNode *CtdlThreadSchedList = NULL;
 
-/*
- * Condition variable and Mutex for thread garbage collection
- */
-/*static pthread_mutex_t thread_gc_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_cond_t thread_gc_cond = PTHREAD_COND_INITIALIZER;
-*/
-static pthread_t GC_thread;
+static CtdlThreadNode *GC_thread = NULL;
 static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE];
 double CtdlThreadLoadAvg = 0;
 double CtdlThreadWorkerAvg = 0;
-pthread_key_t ThreadKey;
+citthread_key_t ThreadKey;
 
-pthread_mutex_t Critters[MAX_SEMAPHORES];      /* Things needing locking */
+citthread_mutex_t Critters[MAX_SEMAPHORES];    /* Things needing locking */
 
 
 
@@ -57,7 +103,7 @@ void InitialiseSemaphores(void)
 
        /* Set up a bunch of semaphores to be used for critical sections */
        for (i=0; i<MAX_SEMAPHORES; ++i) {
-               pthread_mutex_init(&Critters[i], NULL);
+               citthread_mutex_init(&Critters[i], NULL);
        }
 }
 
@@ -82,7 +128,7 @@ int try_critical_section(int which_one)
        ) {
                cdb_check_handles();
        }
-       return (pthread_mutex_trylock(&Critters[which_one]));
+       return (citthread_mutex_trylock(&Critters[which_one]));
 }
 
 
@@ -91,7 +137,7 @@ int try_critical_section(int which_one)
  */
 void begin_critical_section(int which_one)
 {
-       /* CtdlLogPrintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
+       /* syslog(LOG_DEBUG, "begin_critical_section(%d)\n", which_one); */
 
        /* For all types of critical sections except those listed here,
         * ensure nobody ever tries to do a critical section within a
@@ -105,7 +151,7 @@ void begin_critical_section(int which_one)
        ) {
                cdb_check_handles();
        }
-       pthread_mutex_lock(&Critters[which_one]);
+       citthread_mutex_lock(&Critters[which_one]);
 }
 
 /*
@@ -113,7 +159,7 @@ void begin_critical_section(int which_one)
  */
 void end_critical_section(int which_one)
 {
-       pthread_mutex_unlock(&Critters[which_one]);
+       citthread_mutex_unlock(&Critters[which_one]);
 }
 
 
@@ -136,9 +182,8 @@ void ctdl_thread_internal_init_tsd(void)
 {
        int ret;
        
-       if ((ret = pthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
-               lprintf(CTDL_EMERG, "pthread_key_create: %s\n",
-                       strerror(ret));
+       if ((ret = citthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
+               syslog(LOG_EMERG, "citthread_key_create: %s\n", strerror(ret));
                exit(CTDLEXIT_DB);
        }
 }
@@ -153,7 +198,7 @@ void CtdlThreadAllocTSD(void)
 {
        ThreadTSD *tsd;
 
-       if (pthread_getspecific(ThreadKey) != NULL)
+       if (citthread_getspecific(ThreadKey) != NULL)
                return;
 
        tsd = malloc(sizeof(ThreadTSD));
@@ -163,21 +208,21 @@ void CtdlThreadAllocTSD(void)
        memset(tsd->cursors, 0, sizeof tsd->cursors);
        tsd->self = NULL;
        
-       pthread_setspecific(ThreadKey, tsd);
+       citthread_setspecific(ThreadKey, tsd);
 }
 
 
 void ctdl_thread_internal_free_tsd(void)
 {
-       ctdl_thread_internal_dest_tsd(pthread_getspecific(ThreadKey));
-       pthread_setspecific(ThreadKey, NULL);
+       ctdl_thread_internal_dest_tsd(citthread_getspecific(ThreadKey));
+       citthread_setspecific(ThreadKey, NULL);
 }
 
 
 void ctdl_thread_internal_cleanup(void)
 {
        int i;
-       struct CtdlThreadNode *this_thread, *that_thread;
+       CtdlThreadNode *this_thread, *that_thread;
        
        for (i=0; i<CTDL_THREAD_LAST_STATE; i++)
        {
@@ -190,11 +235,11 @@ void ctdl_thread_internal_cleanup(void)
        {
                that_thread = this_thread;
                this_thread = this_thread->next;
-               pthread_mutex_destroy(&that_thread->ThreadMutex);
-               pthread_cond_destroy(&that_thread->ThreadCond);
-               pthread_mutex_destroy(&that_thread->SleepMutex);
-               pthread_cond_destroy(&that_thread->SleepCond);
-               pthread_attr_destroy(&that_thread->attr);
+               citthread_mutex_destroy(&that_thread->ThreadMutex);
+               citthread_cond_destroy(&that_thread->ThreadCond);
+               citthread_mutex_destroy(&that_thread->SleepMutex);
+               citthread_cond_destroy(&that_thread->SleepCond);
+               citthread_attr_destroy(&that_thread->attr);
                free(that_thread);
        }
        ctdl_thread_internal_free_tsd();
@@ -202,10 +247,9 @@ void ctdl_thread_internal_cleanup(void)
 
 void ctdl_thread_internal_init(void)
 {
-       struct CtdlThreadNode *this_thread;
+       CtdlThreadNode *this_thread;
        int ret = 0;
        
-       GC_thread = pthread_self();
        CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread");
        CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread");
        CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created");
@@ -218,31 +262,32 @@ void ctdl_thread_internal_init(void)
        CtdlThreadStates[CTDL_THREAD_BLOCKED] = strdup("Thread Blocked");
        
        /* Get ourself a thread entry */
-       this_thread = malloc(sizeof(struct CtdlThreadNode));
+       this_thread = malloc(sizeof(CtdlThreadNode));
        if (this_thread == NULL) {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
+               syslog(LOG_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
                return;
        }
        // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
-       memset (this_thread, 0, sizeof(struct CtdlThreadNode));
+       memset (this_thread, 0, sizeof(CtdlThreadNode));
        
-       pthread_mutex_init (&(this_thread->ThreadMutex), NULL);
-       pthread_cond_init (&(this_thread->ThreadCond), NULL);
-       pthread_mutex_init (&(this_thread->SleepMutex), NULL);
-       pthread_cond_init (&(this_thread->SleepCond), NULL);
+       citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
+       citthread_cond_init (&(this_thread->ThreadCond), NULL);
+       citthread_mutex_init (&(this_thread->SleepMutex), NULL);
+       citthread_cond_init (&(this_thread->SleepCond), NULL);
        
        /* We are garbage collector so create us as running */
        this_thread->state = CTDL_THREAD_RUNNING;
        
-       if ((ret = pthread_attr_init(&this_thread->attr))) {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret));
+       if ((ret = citthread_attr_init(&this_thread->attr))) {
+               syslog(LOG_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
                free(this_thread);
                return;
        }
 
        this_thread->name = "Garbage Collection Thread";
        
-       this_thread->tid = GC_thread;
+       this_thread->tid = citthread_self();
+       GC_thread = this_thread;
        CT = this_thread;
        
        num_threads++;  // Increase the count of threads in the system.
@@ -260,14 +305,15 @@ void ctdl_thread_internal_init(void)
 /*
  * A function to update a threads load averages
  */
- void ctdl_thread_internal_update_avgs(struct CtdlThreadNode *this_thread)
+ void ctdl_thread_internal_update_avgs(CtdlThreadNode *this_thread)
  {
        struct timeval now, result;
        double last_duration;
 
        gettimeofday(&now, NULL);
        timersub(&now, &(this_thread->last_state_change), &result);
-       pthread_mutex_lock(&this_thread->ThreadMutex);
+       /* I don't think these mutex's are needed here */
+       citthread_mutex_lock(&this_thread->ThreadMutex);
        // result now has a timeval for the time we spent in the last state since we last updated
        last_duration = (double)result.tv_sec + ((double)result.tv_usec / (double) 1000000);
        if (this_thread->state == CTDL_THREAD_SLEEPING)
@@ -277,26 +323,27 @@ void ctdl_thread_internal_init(void)
        if (this_thread->state == CTDL_THREAD_BLOCKED)
                this_thread->avg_blocked += last_duration;
        memcpy (&this_thread->last_state_change, &now, sizeof (struct timeval));
-       pthread_mutex_unlock(&this_thread->ThreadMutex);
+       citthread_mutex_unlock(&this_thread->ThreadMutex);
 }
 
 /*
  * A function to chenge the state of a thread
  */
-void ctdl_thread_internal_change_state (struct CtdlThreadNode *this_thread, enum CtdlThreadState new_state)
+void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlThreadState new_state)
 {
        /*
         * Wether we change state or not we need update the load values
         */
        ctdl_thread_internal_update_avgs(this_thread);
-       pthread_mutex_lock(&this_thread->ThreadMutex); /* To prevent race condition of a sleeping thread */
+       /* This mutex not needed here? */
+       citthread_mutex_lock(&this_thread->ThreadMutex); /* To prevent race condition of a sleeping thread */
        if ((new_state == CTDL_THREAD_STOP_REQ) && (this_thread->state > CTDL_THREAD_STOP_REQ))
                this_thread->state = new_state;
        if (((new_state == CTDL_THREAD_SLEEPING) || (new_state == CTDL_THREAD_BLOCKED)) && (this_thread->state == CTDL_THREAD_RUNNING))
                this_thread->state = new_state;
        if ((new_state == CTDL_THREAD_RUNNING) && ((this_thread->state == CTDL_THREAD_SLEEPING) || (this_thread->state == CTDL_THREAD_BLOCKED)))
                this_thread->state = new_state;
-       pthread_mutex_unlock(&this_thread->ThreadMutex);
+       citthread_mutex_unlock(&this_thread->ThreadMutex);
 }
 
 
@@ -305,22 +352,33 @@ void ctdl_thread_internal_change_state (struct CtdlThreadNode *this_thread, enum
  */
 void CtdlThreadStopAll(void)
 {
+       /* First run any registered shutdown hooks.  This probably doesn't belong here. */
+       PerformSessionHooks(EVT_SHUTDOWN);
+       
+       /* then close all tcp ports so nobody else can talk to us anymore. */
+       CtdlShutdownServiceHooks();
        //FIXME: The signalling of the condition should not be in the critical_section
        // We need to build a list of threads we are going to signal and then signal them afterwards
        
-       struct CtdlThreadNode *this_thread;
+       ShutDownEventQueue();
+
+       CtdlThreadNode *this_thread;
        
        begin_critical_section(S_THREAD_LIST);
        this_thread = CtdlThreadList;
+       // Ask the GC thread to stop first so everything knows we are shutting down.
+       GC_thread->state = CTDL_THREAD_STOP_REQ;
        while(this_thread)
        {
-#ifdef THREADS_USESIGNALS
-               pthread_kill(this_thread->tid, SIGHUP);
-#endif
+               if (!citthread_equal(this_thread->tid, GC_thread->tid))
+                       citthread_kill(this_thread->tid, SIGHUP);
+
                ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
-               pthread_cond_signal(&this_thread->ThreadCond);
-               pthread_cond_signal(&this_thread->SleepCond);
-               CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (%ld).\n", this_thread->name, this_thread->tid);
+               citthread_cond_signal(&this_thread->ThreadCond);
+               citthread_cond_signal(&this_thread->SleepCond);
+               this_thread->stop_ticker = time(NULL);
+               syslog(LOG_DEBUG, "Thread system stopping thread \"%s\" (0x%08lx).\n",
+                       this_thread->name, this_thread->tid);
                this_thread = this_thread->next;
        }
        end_critical_section(S_THREAD_LIST);
@@ -332,9 +390,9 @@ void CtdlThreadStopAll(void)
  */
 void CtdlThreadWakeAll(void)
 {
-       struct CtdlThreadNode *this_thread;
+       CtdlThreadNode *this_thread;
        
-       CtdlLogPrintf(CTDL_DEBUG, "Thread system waking all threads.\n");
+       syslog(LOG_DEBUG, "Thread system waking all threads.\n");
        
        begin_critical_section(S_THREAD_LIST);
        this_thread = CtdlThreadList;
@@ -342,8 +400,8 @@ void CtdlThreadWakeAll(void)
        {
                if (!this_thread->thread_func)
                {
-                       pthread_cond_signal(&this_thread->ThreadCond);
-                       pthread_cond_signal(&this_thread->SleepCond);
+                       citthread_cond_signal(&this_thread->ThreadCond);
+                       citthread_cond_signal(&this_thread->SleepCond);
                }
                this_thread = this_thread->next;
        }
@@ -376,12 +434,35 @@ double CtdlThreadGetWorkerAvg(void)
 
 double CtdlThreadGetLoadAvg(void)
 {
-       double ret;
-       
+       double load_avg[3] = {0.0, 0.0, 0.0};
+
+       int ret = 0;
+       int smp_num_cpus;
+
+       /* Borrowed this straight from procps */
+       smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
+       if(smp_num_cpus<1) smp_num_cpus=1; /* SPARC glibc is buggy */
+
+#ifdef HAVE_GETLOADAVG
+       ret = getloadavg(load_avg, 3);
+#endif
+       if (ret < 0)
+               return 0;
+       return load_avg[0] / smp_num_cpus;
+/*
+ * This old chunk of code return a value that indicated the load on citserver
+ * This value could easily reach 100 % even when citserver was doing very little and
+ * hence the machine has much more spare capacity.
+ * Because this value was used to determine if the machine was under heavy load conditions
+ * from other processes in the system then citserver could be strangled un-necesarily
+ * What we are actually trying to achieve is to strangle citserver if the machine is heavily loaded.
+ * So we have changed this.
+
        begin_critical_section(S_THREAD_LIST);
        ret =  CtdlThreadLoadAvg;
        end_critical_section(S_THREAD_LIST);
        return ret;
+*/
 }
 
 
@@ -397,15 +478,12 @@ const char *CtdlThreadName(const char *name)
        
        if (!CT)
        {
-               CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
+               syslog(LOG_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
                return NULL;
        }
-// FIXME: do we need this lock? I think not since the pointer asignmaent should be atomic
-       pthread_mutex_lock(&CT->ThreadMutex);
        old_name = CT->name;
        if (name)
                CT->name = name;
-       pthread_mutex_unlock(&CT->ThreadMutex);
        return (old_name);
 }      
 
@@ -413,9 +491,9 @@ const char *CtdlThreadName(const char *name)
 /*
  * A function to force a thread to exit
  */
-void CtdlThreadCancel(struct CtdlThreadNode *thread)
+void CtdlThreadCancel(CtdlThreadNode *thread)
 {
-       struct CtdlThreadNode *this_thread;
+       CtdlThreadNode *this_thread;
        
        if (!thread)
                this_thread = CT;
@@ -423,24 +501,23 @@ void CtdlThreadCancel(struct CtdlThreadNode *thread)
                this_thread = thread;
        if (!this_thread)
        {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
+               syslog(LOG_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
                CtdlThreadStopAll();
                return;
        }
        
        if (!this_thread->thread_func)
        {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() the garbage collector.\n");
+               syslog(LOG_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() the garbage collector.\n");
                CtdlThreadStopAll();
                return;
        }
        
        ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED);
-       pthread_cancel(this_thread->tid);
+       citthread_cancel(this_thread->tid);
 }
 
 
-
 /*
  * A function for a thread to check if it has been asked to stop
  */
@@ -450,30 +527,29 @@ int CtdlThreadCheckStop(void)
        
        if (!CT)
        {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
+               syslog(LOG_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
                CtdlThreadStopAll();
                return -1;
        }
        
        state = CT->state;
 
-#ifdef THREADS_USERSIGNALS
        if (CT->signal)
-               CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
-#endif
-       pthread_mutex_lock(&CT->ThreadMutex);
+       {
+               syslog(LOG_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
+               if (CT->signal == SIGHUP)
+                       CT->state = CTDL_THREAD_STOP_REQ;
+               CT->signal = 0;
+       }
        if(state == CTDL_THREAD_STOP_REQ)
        {
                CT->state = CTDL_THREAD_STOPPING;
-               pthread_mutex_unlock(&CT->ThreadMutex);
                return -1;
        }
        else if((state < CTDL_THREAD_STOP_REQ) && (state > CTDL_THREAD_CREATE))
        {
-               pthread_mutex_unlock(&CT->ThreadMutex);
                return -1;
        }
-       pthread_mutex_unlock(&CT->ThreadMutex);
        return 0;
 }
 
@@ -482,9 +558,9 @@ int CtdlThreadCheckStop(void)
  * A function to ask a thread to exit
  * The thread must call CtdlThreadCheckStop() periodically to determine if it should exit
  */
-void CtdlThreadStop(struct CtdlThreadNode *thread)
+void CtdlThreadStop(CtdlThreadNode *thread)
 {
-       struct CtdlThreadNode *this_thread;
+       CtdlThreadNode *this_thread;
        
        if (!thread)
                this_thread = CT;
@@ -494,12 +570,14 @@ void CtdlThreadStop(struct CtdlThreadNode *thread)
                return;
        if (!(this_thread->thread_func))
                return;         // Don't stop garbage collector
-#ifdef THREADS_USESIGNALS
-       pthread_kill(this_thread->tid, SIGHUP); 
-#endif
+
+       if (!citthread_equal(this_thread->tid, GC_thread->tid))
+               citthread_kill(this_thread->tid, SIGHUP);
+
        ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
-       pthread_cond_signal(&this_thread->ThreadCond);
-       pthread_cond_signal(&this_thread->SleepCond);
+       citthread_cond_signal(&this_thread->ThreadCond);
+       citthread_cond_signal(&this_thread->SleepCond);
+       this_thread->stop_ticker = time(NULL);
 }
 
 /*
@@ -513,7 +591,7 @@ void CtdlThreadSleep(int secs)
        
        if (!CT)
        {
-               CtdlLogPrintf(CTDL_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
+               syslog(LOG_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
                return;
        }
        
@@ -524,9 +602,9 @@ void CtdlThreadSleep(int secs)
 
        ctdl_thread_internal_change_state (CT, CTDL_THREAD_SLEEPING);
        
-       pthread_mutex_lock(&CT->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
-       pthread_cond_timedwait(&CT->SleepCond, &CT->ThreadMutex, &wake_time);
-       pthread_mutex_unlock(&CT->ThreadMutex);
+       citthread_mutex_lock(&CT->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
+       citthread_cond_timedwait(&CT->SleepCond, &CT->ThreadMutex, &wake_time);
+       citthread_mutex_unlock(&CT->ThreadMutex);
        
        ctdl_thread_internal_change_state (CT, CTDL_THREAD_RUNNING);
 }
@@ -541,15 +619,25 @@ static void ctdl_internal_thread_cleanup(void *arg)
         * In here we were called by the current thread because it is exiting
         * NB. WE ARE THE CURRENT THREAD
         */
-       CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (%ld) exited.\n", CT->name, CT->tid);
+       if (CT)
+       {
+               const char *name = CT->name;
+               const pid_t tid = CT->tid;
+
+               syslog(LOG_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", name, (unsigned long) tid);
+       }
+       else 
+       {
+               syslog(LOG_NOTICE, "some ((unknown ? ? ?) Thread exited.\n");
+       }
        
        #ifdef HAVE_BACKTRACE
-       eCrash_UnregisterThread();
+///    eCrash_UnregisterThread();
        #endif
        
-       pthread_mutex_lock(&CT->ThreadMutex);
+       citthread_mutex_lock(&CT->ThreadMutex);
        CT->state = CTDL_THREAD_EXITED; // needs to be last thing else house keeping will unlink us too early
-       pthread_mutex_unlock(&CT->ThreadMutex);
+       citthread_mutex_unlock(&CT->ThreadMutex);
 }
 
 /*
@@ -557,7 +645,7 @@ static void ctdl_internal_thread_cleanup(void *arg)
  */
 void ctdl_thread_internal_calc_loadavg(void)
 {
-       struct CtdlThreadNode *that_thread;
+       CtdlThreadNode *that_thread;
        double load_avg, worker_avg;
        int workers = 0;
 
@@ -568,9 +656,8 @@ void ctdl_thread_internal_calc_loadavg(void)
        {
                /* Update load averages */
                ctdl_thread_internal_update_avgs(that_thread);
-               pthread_mutex_lock(&that_thread->ThreadMutex);
-               that_thread->load_avg = that_thread->avg_sleeping + that_thread->avg_running + that_thread->avg_blocked;
-               that_thread->load_avg = that_thread->avg_running / that_thread->load_avg * 100;
+               citthread_mutex_lock(&that_thread->ThreadMutex);
+               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;
                that_thread->avg_sleeping /= 2;
                that_thread->avg_running /= 2;
                that_thread->avg_blocked /= 2;
@@ -581,7 +668,7 @@ void ctdl_thread_internal_calc_loadavg(void)
                        workers++;
                }
 #ifdef WITH_THREADLOG
-               CtdlLogPrintf(CTDL_DEBUG, "CtdlThread, \"%s\" (%ld) \"%s\" %f %f %f %f.\n",
+               syslog(LOG_DEBUG, "CtdlThread, \"%s\" (%lu) \"%s\" %.2f %.2f %.2f %.2f\n",
                        that_thread->name,
                        that_thread->tid,
                        CtdlThreadStates[that_thread->state],
@@ -590,13 +677,13 @@ void ctdl_thread_internal_calc_loadavg(void)
                        that_thread->avg_blocked,
                        that_thread->load_avg);
 #endif
-               pthread_mutex_unlock(&that_thread->ThreadMutex);
+               citthread_mutex_unlock(&that_thread->ThreadMutex);
                that_thread = that_thread->next;
        }
        CtdlThreadLoadAvg = load_avg/num_threads;
        CtdlThreadWorkerAvg = worker_avg/workers;
 #ifdef WITH_THREADLOG
-       CtdlLogPrintf(CTDL_INFO, "System load average %f, workers averag %f, threads %d, workers %d, sessions %d\n", CtdlThreadLoadAvg, CtdlThreadWorkerAvg, num_threads, num_workers, num_sessions);
+       syslog(LOG_INFO, "System load average %.2f, workers averag %.2f, threads %d, workers %d, sessions %d\n", CtdlThreadGetLoadAvg(), CtdlThreadWorkerAvg, num_threads, num_workers, num_sessions);
 #endif
 }
 
@@ -607,10 +694,10 @@ void ctdl_thread_internal_calc_loadavg(void)
  */
 void CtdlThreadGC (void)
 {
-       struct CtdlThreadNode *this_thread, *that_thread;
-       int workers = 0;
+       CtdlThreadNode *this_thread, *that_thread;
+       int workers = 0, sys_workers;
        int ret=0;
-       
+
        begin_critical_section(S_THREAD_LIST);
        
        /* Handle exiting of garbage collector thread */
@@ -618,7 +705,7 @@ void CtdlThreadGC (void)
                CtdlThreadList->state = CTDL_THREAD_EXITED;
        
 #ifdef WITH_THREADLOG
-       CtdlLogPrintf(CTDL_DEBUG, "Thread system running garbage collection.\n");
+       syslog(LOG_DEBUG, "Thread system running garbage collection.\n");
 #endif
        /*
         * Woke up to do garbage collection
@@ -629,30 +716,52 @@ void CtdlThreadGC (void)
                that_thread = this_thread;
                this_thread = this_thread->next;
                
+               if ((that_thread->state == CTDL_THREAD_STOP_REQ || that_thread->state == CTDL_THREAD_STOPPING)
+                       && (!citthread_equal(that_thread->tid, citthread_self())))
+               {
+                       syslog(LOG_DEBUG, "Waiting for thread %s (0x%08lx) to exit.\n", that_thread->name, that_thread->tid);
+                       terminate_stuck_sessions();
+               }
+               else
+               {
+                       /**
+                        * Catch the situation where a worker was asked to stop but couldn't and we are not
+                        * shutting down.
+                        */
+                       that_thread->stop_ticker = 0;
+               }
+               
+               if (that_thread->stop_ticker + 5 == time(NULL))
+               {
+                       syslog(LOG_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);
+                       if ((that_thread->flags & CTDLTHREAD_WORKER) == 0)
+                               syslog(LOG_INFO, "Thread System: A non worker thread would have been canceled this may cause message loss.\n");
+//                     that_thread->state = CTDL_THREAD_CANCELLED;
+                       that_thread->stop_ticker++;
+//                     citthread_cancel(that_thread->tid);
+//                     continue;
+               }
+               
                /* Do we need to clean up this thread? */
-               pthread_mutex_lock(&that_thread->ThreadMutex);
-               if (that_thread->state != CTDL_THREAD_EXITED)
+               if ((that_thread->state != CTDL_THREAD_EXITED) && (that_thread->state != CTDL_THREAD_CANCELLED))
                {
                        if(that_thread->flags & CTDLTHREAD_WORKER)
                                workers++;      /* Sanity check on number of worker threads */
-                       pthread_mutex_unlock(&that_thread->ThreadMutex);
                        continue;
                }
                
-               if (pthread_equal(that_thread->tid, pthread_self()) && that_thread->thread_func)
+               if (citthread_equal(that_thread->tid, citthread_self()) && that_thread->thread_func)
                {       /* Sanity check */
-                       pthread_mutex_unlock(&that_thread->ThreadMutex);
                        end_critical_section(S_THREAD_LIST);
-                       CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, a thread is trying to clean up after itself.\n");
+                       syslog(LOG_EMERG, "Thread system PANIC, a thread is trying to clean up after itself.\n");
                        abort();
                        return;
                }
                
                if (num_threads <= 0)
                {       /* Sanity check */
-                       pthread_mutex_unlock(&that_thread->ThreadMutex);
                        end_critical_section(S_THREAD_LIST);
-                       CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, num_threads <= 0 and trying to do Garbage Collection.\n");
+                       syslog(LOG_EMERG, "Thread system PANIC, num_threads <= 0 and trying to do Garbage Collection.\n");
                        abort();
                        return;
                }
@@ -668,48 +777,48 @@ void CtdlThreadGC (void)
                if(that_thread->next)
                        that_thread->next->prev = that_thread->prev;
                
-               pthread_mutex_unlock(&that_thread->ThreadMutex);
-               pthread_cond_signal(&that_thread->ThreadCond);
-               pthread_cond_signal(&that_thread->SleepCond);   // Make sure this thread is awake
-               pthread_mutex_lock(&that_thread->ThreadMutex);  // Make sure it has done what its doing
-               pthread_mutex_unlock(&that_thread->ThreadMutex);
+               citthread_cond_signal(&that_thread->ThreadCond);
+               citthread_cond_signal(&that_thread->SleepCond); // Make sure this thread is awake
+               citthread_mutex_lock(&that_thread->ThreadMutex);        // Make sure it has done what its doing
+               citthread_mutex_unlock(&that_thread->ThreadMutex);
                /*
                 * Join on the thread to do clean up and prevent memory leaks
                 * Also makes sure the thread has cleaned up after itself before we remove it from the list
                 * We can join on the garbage collector thread the join should just return EDEADLCK
                 */
-               ret = pthread_join (that_thread->tid, NULL);
+               ret = citthread_join (that_thread->tid, NULL);
                if (ret == EDEADLK)
-                       CtdlLogPrintf(CTDL_DEBUG, "Garbage collection on own thread.\n");
+                       syslog(LOG_DEBUG, "Garbage collection on own thread.\n");
                else if (ret == EINVAL)
-                       CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, that thread already joined on.\n");
+                       syslog(LOG_DEBUG, "Garbage collection, that thread already joined on.\n");
                else if (ret == ESRCH)
-                       CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, no thread to join on.\n");
+                       syslog(LOG_DEBUG, "Garbage collection, no thread to join on.\n");
                else if (ret != 0)
-                       CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, pthread_join returned an unknown error.\n");
+                       syslog(LOG_DEBUG, "Garbage collection, citthread_join returned an unknown error(%d).\n", ret);
                /*
                 * Now we own that thread entry
                 */
-               CtdlLogPrintf(CTDL_INFO, "Garbage Collection for thread \"%s\" (%ld).\n", that_thread->name, that_thread->tid);
-               pthread_mutex_destroy(&that_thread->ThreadMutex);
-               pthread_cond_destroy(&that_thread->ThreadCond);
-               pthread_mutex_destroy(&that_thread->SleepMutex);
-               pthread_cond_destroy(&that_thread->SleepCond);
-               pthread_attr_destroy(&that_thread->attr);
+               syslog(LOG_INFO, "Garbage Collection for thread \"%s\" (0x%08lx).\n",
+                       that_thread->name, that_thread->tid);
+               citthread_mutex_destroy(&that_thread->ThreadMutex);
+               citthread_cond_destroy(&that_thread->ThreadCond);
+               citthread_mutex_destroy(&that_thread->SleepMutex);
+               citthread_cond_destroy(&that_thread->SleepCond);
+               citthread_attr_destroy(&that_thread->attr);
                free(that_thread);
        }
+       sys_workers = num_workers;
+       end_critical_section(S_THREAD_LIST);
        
        /* Sanity check number of worker threads */
-       if (workers != num_workers)
+       if (workers != sys_workers)
        {
-               end_critical_section(S_THREAD_LIST);
-               CtdlLogPrintf(CTDL_EMERG,
+               syslog(LOG_EMERG,
                        "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
-                       workers, num_workers
+                       workers, sys_workers
                        );
                abort();
        }
-       end_critical_section(S_THREAD_LIST);
 }
 
 
@@ -722,7 +831,7 @@ void CtdlThreadGC (void)
  */ 
 static void *ctdl_internal_thread_func (void *arg)
 {
-       struct CtdlThreadNode *this_thread;
+       CtdlThreadNode *this_thread;
        void *ret = NULL;
 
        /* lock and unlock the thread list.
@@ -730,12 +839,11 @@ static void *ctdl_internal_thread_func (void *arg)
         * can continue its execution.
         */
        begin_critical_section(S_THREAD_LIST);
-       this_thread = (struct CtdlThreadNode *) arg;
+       this_thread = (CtdlThreadNode *) arg;
        gettimeofday(&this_thread->start_time, NULL);           /* Time this thread started */
-       pthread_mutex_lock(&this_thread->ThreadMutex);
        
        // Register the cleanup function to take care of when we exit.
-       pthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
+       citthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
        // Get our thread data structure
        CtdlThreadAllocTSD();
        CT = this_thread;
@@ -745,25 +853,25 @@ static void *ctdl_internal_thread_func (void *arg)
         * Other wise there is a window to allow this threads creation to continue to full grown and
         * therby prevent a shutdown of the server.
         */
-       pthread_mutex_unlock(&this_thread->ThreadMutex);
-               
        if (!CtdlThreadCheckStop())
        {
-               pthread_mutex_lock(&this_thread->ThreadMutex);
+               citthread_mutex_lock(&this_thread->ThreadMutex);
                this_thread->state = CTDL_THREAD_RUNNING;
-               pthread_mutex_unlock(&this_thread->ThreadMutex);
+               citthread_mutex_unlock(&this_thread->ThreadMutex);
        }
        end_critical_section(S_THREAD_LIST);
        
        // Register for tracing
        #ifdef HAVE_BACKTRACE
-       eCrash_RegisterThread(this_thread->name, 0);
+///    eCrash_RegisterThread(this_thread->name, 0);
        #endif
        
        // Tell the world we are here
-       CtdlLogPrintf(CTDL_NOTICE, "Created a new thread \"%s\" (%ld). \n", this_thread->name, this_thread->tid);
-
-       
+#if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
+       this_thread->reltid = syscall(SYS_gettid);
+#endif
+       syslog(LOG_NOTICE, "Created a new thread \"%s\" (0x%08lx).\n",
+               this_thread->name, this_thread->tid);
        
        /*
         * run the thread to do the work but only if we haven't been asked to stop
@@ -775,53 +883,39 @@ static void *ctdl_internal_thread_func (void *arg)
         * Our thread is exiting either because it wanted to end or because the server is stopping
         * We need to clean up
         */
-       pthread_cleanup_pop(1); // Execute our cleanup routine and remove it
+       citthread_cleanup_pop(1);       // Execute our cleanup routine and remove it
        
        return(ret);
 }
 
 
+
+
 /*
- * Internal function to create a thread.
- * Must be called from within a S_THREAD_LIST critical section
- */ 
-struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args)
+ * Function to initialise an empty thread structure
+ */
+CtdlThreadNode *ctdl_internal_init_thread_struct(CtdlThreadNode *this_thread, long flags)
 {
        int ret = 0;
-       struct CtdlThreadNode *this_thread;
-
-       if (num_threads >= 32767)
-       {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
-               return NULL;
-       }
-               
-       this_thread = malloc(sizeof(struct CtdlThreadNode));
-       if (this_thread == NULL) {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
-               return NULL;
-       }
+       
        // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
-       memset (this_thread, 0, sizeof(struct CtdlThreadNode));
+       memset (this_thread, 0, sizeof(CtdlThreadNode));
        
        /* Create the mutex's early so we can use them */
-       pthread_mutex_init (&(this_thread->ThreadMutex), NULL);
-       pthread_cond_init (&(this_thread->ThreadCond), NULL);
-       pthread_mutex_init (&(this_thread->SleepMutex), NULL);
-       pthread_cond_init (&(this_thread->SleepCond), NULL);
-       
-       pthread_mutex_lock(&this_thread->ThreadMutex);
+       citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
+       citthread_cond_init (&(this_thread->ThreadCond), NULL);
+       citthread_mutex_init (&(this_thread->SleepMutex), NULL);
+       citthread_cond_init (&(this_thread->SleepCond), NULL);
        
        this_thread->state = CTDL_THREAD_CREATE;
        
-       if ((ret = pthread_attr_init(&this_thread->attr))) {
-               pthread_mutex_unlock(&this_thread->ThreadMutex);
-               pthread_mutex_destroy(&(this_thread->ThreadMutex));
-               pthread_cond_destroy(&(this_thread->ThreadCond));
-               pthread_mutex_destroy(&(this_thread->SleepMutex));
-               pthread_cond_destroy(&(this_thread->SleepCond));
-               CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret));
+       if ((ret = citthread_attr_init(&this_thread->attr))) {
+               citthread_mutex_unlock(&this_thread->ThreadMutex);
+               citthread_mutex_destroy(&(this_thread->ThreadMutex));
+               citthread_cond_destroy(&(this_thread->ThreadCond));
+               citthread_mutex_destroy(&(this_thread->SleepMutex));
+               citthread_cond_destroy(&(this_thread->SleepCond));
+               syslog(LOG_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
                free(this_thread);
                return NULL;
        }
@@ -833,159 +927,61 @@ struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void
        if (flags & CTDLTHREAD_BIGSTACK)
        {
 #ifdef WITH_THREADLOG
-               CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n");
+               syslog(LOG_INFO, "Thread system. Creating BIG STACK thread.\n");
 #endif
-               if ((ret = pthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
-                       pthread_mutex_unlock(&this_thread->ThreadMutex);
-                       pthread_mutex_destroy(&(this_thread->ThreadMutex));
-                       pthread_cond_destroy(&(this_thread->ThreadCond));
-                       pthread_mutex_destroy(&(this_thread->SleepMutex));
-                       pthread_cond_destroy(&(this_thread->SleepCond));
-                       pthread_attr_destroy(&this_thread->attr);
-                       CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_setstacksize: %s\n",
+               if ((ret = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
+                       citthread_mutex_unlock(&this_thread->ThreadMutex);
+                       citthread_mutex_destroy(&(this_thread->ThreadMutex));
+                       citthread_cond_destroy(&(this_thread->ThreadCond));
+                       citthread_mutex_destroy(&(this_thread->SleepMutex));
+                       citthread_cond_destroy(&(this_thread->SleepCond));
+                       citthread_attr_destroy(&this_thread->attr);
+                       syslog(LOG_EMERG, "Thread system, citthread_attr_setstacksize: %s\n",
                                strerror(ret));
                        free(this_thread);
                        return NULL;
                }
        }
 
-       /*
-        * If we got here we are going to create the thread so we must initilise the structure
-        * first because most implimentations of threading can't create it in a stopped state
-        * and it might want to do things with its structure that aren't initialised otherwise.
-        */
-       if(name)
-       {
-               this_thread->name = name;
-       }
-       else
-       {
-               this_thread->name = "Un-named Thread";
-       }
-       
-       this_thread->flags = flags;
-       this_thread->thread_func = thread_func;
-       this_thread->user_args = args;
        /* Set this new thread with an avg_blocked of 2. We do this so that its creation affects the
         * load average for the system. If we don't do this then we create a mass of threads at the same time 
         * because the creation didn't affect the load average.
         */
        this_thread->avg_blocked = 2;
        
-       /*
-        * We pass this_thread into the thread as its args so that it can find out information
-        * about itself and it has a bit of storage space for itself, not to mention that the REAL
-        * thread function needs to finish off the setup of the structure
-        */
-       if ((ret = pthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
-       {
-
-               CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
-                       strerror(ret));
-               pthread_mutex_unlock(&this_thread->ThreadMutex);
-               pthread_mutex_destroy(&(this_thread->ThreadMutex));
-               pthread_cond_destroy(&(this_thread->ThreadCond));
-               pthread_mutex_destroy(&(this_thread->SleepMutex));
-               pthread_cond_destroy(&(this_thread->SleepCond));
-               pthread_attr_destroy(&this_thread->attr);
-               free(this_thread);
-               return NULL;
-       }
-       
-       num_threads++;  // Increase the count of threads in the system.
-       if(this_thread->flags & CTDLTHREAD_WORKER)
-               num_workers++;
-
-       this_thread->next = CtdlThreadList;
-       CtdlThreadList = this_thread;
-       if (this_thread->next)
-               this_thread->next->prev = this_thread;
-       
-       pthread_mutex_unlock(&this_thread->ThreadMutex);
-       
-       ctdl_thread_internal_calc_loadavg();
-       return this_thread;
-}
-
-/*
- * Wrapper function to create a thread
- * ensures the critical section and other protections are in place.
- * char *name = name to give to thread, if NULL, use generic name
- * int flags = flags to determine type of thread and standard facilities
- */
-struct CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args)
-{
-       struct CtdlThreadNode *ret = NULL;
-       
-       begin_critical_section(S_THREAD_LIST);
-       ret = ctdl_internal_create_thread(name, flags, thread_func, args);
-       end_critical_section(S_THREAD_LIST);
-       return ret;
+       return (this_thread);
 }
 
 
 
 /*
- * Internal function to schedule a thread.
- * Must be called from within a S_THREAD_LIST critical section
+ * Internal function to create a thread.
  */ 
-struct CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) (void *arg), void *args, time_t when)
+CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args)
 {
        int ret = 0;
-       struct CtdlThreadNode *this_thread;
+       CtdlThreadNode *this_thread;
 
        if (num_threads >= 32767)
        {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
+               syslog(LOG_EMERG, "Thread system. Thread list full.\n");
                return NULL;
        }
                
-       this_thread = malloc(sizeof(struct CtdlThreadNode));
+       this_thread = malloc(sizeof(CtdlThreadNode));
        if (this_thread == NULL) {
-               CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
+               syslog(LOG_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
                return NULL;
        }
-       // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
-       memset (this_thread, 0, sizeof(struct CtdlThreadNode));
-       
-       /* Create the mutex's early so we can use them */
-       pthread_mutex_init (&(this_thread->ThreadMutex), NULL);
-       pthread_cond_init (&(this_thread->ThreadCond), NULL);
-       pthread_mutex_init (&(this_thread->SleepMutex), NULL);
-       pthread_cond_init (&(this_thread->SleepCond), NULL);
-       
-       this_thread->state = CTDL_THREAD_CREATE;
        
-       if ((ret = pthread_attr_init(&this_thread->attr))) {
-               pthread_mutex_destroy(&(this_thread->ThreadMutex));
-               pthread_cond_destroy(&(this_thread->ThreadCond));
-               pthread_mutex_destroy(&(this_thread->SleepMutex));
-               pthread_cond_destroy(&(this_thread->SleepCond));
-               CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret));
+       /* Initialise the thread structure */
+       if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
+       {
                free(this_thread);
+               syslog(LOG_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
                return NULL;
        }
-
-       /* Our per-thread stacks need to be bigger than the default size,
-        * otherwise the MIME parser crashes on FreeBSD, and the IMAP service
-        * crashes on 64-bit Linux.
-        */
-       if (flags & CTDLTHREAD_BIGSTACK)
-       {
-               CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n");
-               if ((ret = pthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
-                       pthread_mutex_destroy(&(this_thread->ThreadMutex));
-                       pthread_cond_destroy(&(this_thread->ThreadCond));
-                       pthread_mutex_destroy(&(this_thread->SleepMutex));
-                       pthread_cond_destroy(&(this_thread->SleepCond));
-                       pthread_attr_destroy(&this_thread->attr);
-                       CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_setstacksize: %s\n",
-                               strerror(ret));
-                       free(this_thread);
-                       return NULL;
-               }
-       }
-
        /*
         * If we got here we are going to create the thread so we must initilise the structure
         * first because most implimentations of threading can't create it in a stopped state
@@ -1003,43 +999,78 @@ struct CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread
        this_thread->flags = flags;
        this_thread->thread_func = thread_func;
        this_thread->user_args = args;
-       /* Set this new thread with an avg_blocked of 2. We do this so that its creation affects the
-        * load average for the system. If we don't do this then we create a mass of threads at the same time 
-        * because the creation didn't affect the load average.
-        */
-       this_thread->avg_blocked = 2;
        
+       begin_critical_section(S_THREAD_LIST);
        /*
-        * When to start this thread
+        * We pass this_thread into the thread as its args so that it can find out information
+        * about itself and it has a bit of storage space for itself, not to mention that the REAL
+        * thread function needs to finish off the setup of the structure
         */
-       this_thread->when = when;
+       if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
+       {
+               end_critical_section(S_THREAD_LIST);
+               syslog(LOG_ALERT, "Thread system, Can't create thread: %s\n",
+                       strerror(ret));
+               citthread_mutex_unlock(&this_thread->ThreadMutex);
+               citthread_mutex_destroy(&(this_thread->ThreadMutex));
+               citthread_cond_destroy(&(this_thread->ThreadCond));
+               citthread_mutex_destroy(&(this_thread->SleepMutex));
+               citthread_cond_destroy(&(this_thread->SleepCond));
+               citthread_attr_destroy(&this_thread->attr);
+               free(this_thread);
+               return NULL;
+       }
+       num_threads++;  // Increase the count of threads in the system.
+       if(this_thread->flags & CTDLTHREAD_WORKER)
+               num_workers++;
 
-       begin_critical_section(S_SCHEDULE_LIST);
-       this_thread->next = CtdlThreadSchedList;
-       CtdlThreadSchedList = this_thread;
+       this_thread->next = CtdlThreadList;
+       CtdlThreadList = this_thread;
        if (this_thread->next)
                this_thread->next->prev = this_thread;
-       end_critical_section(S_SCHEDULE_LIST);
+       ctdl_thread_internal_calc_loadavg();
+       
+       end_critical_section(S_THREAD_LIST);
        
        return this_thread;
 }
 
+/*
+ * Wrapper function to create a thread
+ * ensures the critical section and other protections are in place.
+ * char *name = name to give to thread, if NULL, use generic name
+ * int flags = flags to determine type of thread and standard facilities
+ */
+CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args)
+{
+       CtdlThreadNode *ret = NULL;
+       
+       ret = ctdl_internal_create_thread(name, flags, thread_func, args);
+       return ret;
+}
+
 
 
-struct CtdlThreadNode *ctdl_thread_internal_start_scheduled (struct CtdlThreadNode *this_thread)
+CtdlThreadNode *ctdl_thread_internal_start_scheduled (CtdlThreadNode *this_thread)
 {
        int ret = 0;
        
+       begin_critical_section(S_THREAD_LIST);
        /*
         * We pass this_thread into the thread as its args so that it can find out information
         * about itself and it has a bit of storage space for itself, not to mention that the REAL
         * thread function needs to finish off the setup of the structure
         */
-       if ((ret = pthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
+       if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
        {
-
-               CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
-                       strerror(ret));
+               end_critical_section(S_THREAD_LIST);
+               syslog(LOG_DEBUG, "Failed to start scheduled thread \"%s\": %s\n", this_thread->name, strerror(ret));
+               citthread_mutex_destroy(&(this_thread->ThreadMutex));
+               citthread_cond_destroy(&(this_thread->ThreadCond));
+               citthread_mutex_destroy(&(this_thread->SleepMutex));
+               citthread_cond_destroy(&(this_thread->SleepCond));
+               citthread_attr_destroy(&this_thread->attr);
+               free(this_thread);
                return NULL;
        }
        
@@ -1053,6 +1084,10 @@ struct CtdlThreadNode *ctdl_thread_internal_start_scheduled (struct CtdlThreadNo
        if (this_thread->next)
                this_thread->next->prev = this_thread;
        
+       ctdl_thread_internal_calc_loadavg();
+       end_critical_section(S_THREAD_LIST);
+       
+       
        return this_thread;
 }
 
@@ -1060,16 +1095,20 @@ struct CtdlThreadNode *ctdl_thread_internal_start_scheduled (struct CtdlThreadNo
 
 void ctdl_thread_internal_check_scheduled(void)
 {
-       struct CtdlThreadNode *this_thread, *that_thread;
+       CtdlThreadNode *this_thread, *that_thread;
        time_t now;
        
+       /* Don't start scheduled threads if the system wants single user mode */
+       if (CtdlWantSingleUser())
+               return;
+       
        if (try_critical_section(S_SCHEDULE_LIST))
                return; /* If this list is locked we wait till the next chance */
        
        now = time(NULL);
        
 #ifdef WITH_THREADLOG
-       CtdlLogPrintf(CTDL_DEBUG, "Checking for scheduled threads to start.\n");
+       syslog(LOG_DEBUG, "Checking for scheduled threads to start.\n");
 #endif
 
        this_thread = CtdlThreadSchedList;
@@ -1090,41 +1129,26 @@ void ctdl_thread_internal_check_scheduled(void)
                                
                        that_thread->next = that_thread->prev = NULL;
 #ifdef WITH_THREADLOG
-                       CtdlLogPrintf(CTDL_DEBUG, "About to start scheduled thread \"%s\".\n", that_thread->name);
+                       syslog(LOG_DEBUG, "About to start scheduled thread \"%s\".\n", that_thread->name);
 #endif
-                       begin_critical_section(S_THREAD_LIST);
                        if (CT->state > CTDL_THREAD_STOP_REQ)
                        {       /* Only start it if the system is not stopping */
-                               pthread_mutex_lock(&that_thread->ThreadMutex);
-                               if (ctdl_thread_internal_start_scheduled (that_thread) == NULL)
+                               if (ctdl_thread_internal_start_scheduled (that_thread))
                                {
 #ifdef WITH_THREADLOG
-                       CtdlLogPrintf(CTDL_DEBUG, "Failed to start scheduled thread \"%s\".\n", that_thread->name);
-#endif
-                                       pthread_mutex_unlock(&that_thread->ThreadMutex);
-                                       pthread_mutex_destroy(&(that_thread->ThreadMutex));
-                                       pthread_cond_destroy(&(that_thread->ThreadCond));
-                                       pthread_mutex_destroy(&(that_thread->SleepMutex));
-                                       pthread_cond_destroy(&(that_thread->SleepCond));
-                                       pthread_attr_destroy(&that_thread->attr);
-                                       free(that_thread);
-                               }
-                               else
-                               {
-                                       CtdlLogPrintf(CTDL_INFO, "Thread system, Started a scheduled thread \"%s\" (%ld).\n",
+                                       syslog(LOG_INFO, "Thread system, Started a scheduled thread \"%s\" (0x%08lx).\n",
                                                that_thread->name, that_thread->tid);
-                                       pthread_mutex_unlock(&that_thread->ThreadMutex);
-                                       ctdl_thread_internal_calc_loadavg();
+#endif
                                }
                        }
-                       end_critical_section(S_THREAD_LIST);
                }
+#ifdef WITH_THREADLOG
                else
                {
-#ifdef WITH_THREADLOG
-                       CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" will start in %ld seconds.\n", that_thread->name, that_thread->when - time(NULL));
-#endif
+                       syslog(LOG_DEBUG, "Thread \"%s\" will start in %ld seconds.\n",
+                               that_thread->name, that_thread->when - time(NULL));
                }
+#endif
        }
        end_critical_section(S_SCHEDULE_LIST);
 }
@@ -1135,47 +1159,106 @@ void ctdl_thread_internal_check_scheduled(void)
  */
 int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
 {
-       int ret;
+       int ret = 0;
        
        ctdl_thread_internal_change_state(CT, CTDL_THREAD_BLOCKED);
-       ret = select(n, readfds, writefds, exceptfds, timeout);
+       if (!CtdlThreadCheckStop())
+               ret = select(n, readfds, writefds, exceptfds, timeout);
+       /**
+        * If the select returned <= 0 then it failed due to an error
+        * or timeout so this thread could stop if asked to do so.
+        * Anything else means it needs to continue unless the system is shutting down
+        */
+       if (ret > 0)
+       {
+               /**
+                * The select says this thread needs to do something useful.
+                * This thread was in an idle state so it may have been asked to stop
+                * but if the system isn't shutting down this thread is no longer
+                * idle and select has given it a task to do so it must not stop
+                * In this condition we need to force it into the running state.
+                * CtdlThreadGC will clear its ticker for us.
+                *
+                * FIXME: there is still a small hole here. It is possible for the sequence of locking
+                * to allow the state to get changed to STOP_REQ just after this code if the other thread
+                * has decided to change the state before this lock, it there fore has to wait till the lock
+                * completes but it will continue to change the state. We need something a bit better here.
+                */
+               citthread_mutex_lock(&CT->ThreadMutex); /* To prevent race condition of a sleeping thread */
+               if (GC_thread->state > CTDL_THREAD_STOP_REQ && CT->state <= CTDL_THREAD_STOP_REQ)
+               {
+                       syslog(LOG_DEBUG, "Thread %s (0x%08lx) refused stop request.\n", CT->name, CT->tid);
+                       CT->state = CTDL_THREAD_RUNNING;
+               }
+               citthread_mutex_unlock(&CT->ThreadMutex);
+       }
+
        ctdl_thread_internal_change_state(CT, CTDL_THREAD_RUNNING);
+
        return ret;
 }
 
 
 
+void *new_worker_thread(void *arg);
+extern void close_masters (void);
+
+
+void *simulation_worker (void*arg) {
+       struct CitContext *this;
+
+       this = CreateNewContext();
+       CtdlThreadSleep(1);
+       this->kill_me = KILLME_SIMULATION_WORKER;
+       this->state = CON_IDLE;
+       dead_session_purge(1);
+       begin_critical_section(S_SESSION_TABLE);
+       stats_done++;
+       end_critical_section(S_SESSION_TABLE);
+       return NULL;
+}
+
 
+void *simulation_thread (void *arg)
+{
+       long stats = statcount;
 
+       while(stats && !CtdlThreadCheckStop()) {
+               CtdlThreadCreate("Connection simulation worker", CTDLTHREAD_BIGSTACK, simulation_worker, NULL);
+               stats--;
+       }
+       CtdlThreadStopAll();
+       return NULL;
+}
 
 void go_threading(void)
 {
        int i;
-       struct CtdlThreadNode *last_worker;
-       
+       CtdlThreadNode *last_worker;
+       struct timeval start, now, result;
+       double last_duration;
+
        /*
         * Initialise the thread system
         */
        ctdl_thread_internal_init();
-       /*
-        * Now create a bunch of worker threads.
-        */
-//     CtdlLogPrintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers);
-//     begin_critical_section(S_THREAD_LIST);
-//     i=0;    /* Always start at least 1 worker thread */
-//     do
-//     {
-//             ctdl_internal_create_thread("Worker Thread", CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER, worker_thread, NULL);
-//     } while (++i < config.c_min_workers);
-//     end_critical_section(S_THREAD_LIST);
 
        /* Second call to module init functions now that threading is up */
-       initialise_modules(1);
+       if (!statcount) {
+               initialise_modules(1);
+               CtdlThreadCreate("select_on_master", CTDLTHREAD_BIGSTACK, select_on_master, NULL);
+       }
+       else {
+               syslog(LOG_EMERG, "Running connection simulation stats\n");
+               gettimeofday(&start, NULL);
+               CtdlThreadCreate("Connection simulation master", CTDLTHREAD_BIGSTACK, simulation_thread, NULL);
+       }
+
 
        /*
         * This thread is now used for garbage collection of other threads in the thread list
         */
-       CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", pthread_self());
+       syslog(LOG_INFO, "Startup thread %ld becoming garbage collector,\n", (long) citthread_self());
 
        /*
         * We do a lot of locking and unlocking of the thread list in here.
@@ -1189,7 +1272,9 @@ void go_threading(void)
                if (CT->signal)
                        exit_signal = CT->signal;
                if (exit_signal)
+               {
                        CtdlThreadStopAll();
+               }
                check_sched_shutdown();
                if (CT->state > CTDL_THREAD_STOP_REQ)
                {
@@ -1201,27 +1286,27 @@ void go_threading(void)
                }
                
                /* Reduce the size of the worker thread pool if necessary. */
-               if ((CtdlThreadGetWorkers() > config.c_min_workers) && (CtdlThreadWorkerAvg < 20) && (CT->state > CTDL_THREAD_STOP_REQ))
+               if ((CtdlThreadGetWorkers() > config.c_min_workers + 1) && (CtdlThreadWorkerAvg < 20) && (CT->state > CTDL_THREAD_STOP_REQ))
                {
                        /* Ask a worker thread to stop as we no longer need it */
                        begin_critical_section(S_THREAD_LIST);
                        last_worker = CtdlThreadList;
                        while (last_worker)
                        {
-                               pthread_mutex_lock(&last_worker->ThreadMutex);
-                               if (last_worker->flags & CTDLTHREAD_WORKER && last_worker->state > CTDL_THREAD_STOPPING)
+                               citthread_mutex_lock(&last_worker->ThreadMutex);
+                               if (last_worker->flags & CTDLTHREAD_WORKER && (last_worker->state > CTDL_THREAD_STOPPING) && (last_worker->Context == NULL))
                                {
-                                       pthread_mutex_unlock(&last_worker->ThreadMutex);
+                                       citthread_mutex_unlock(&last_worker->ThreadMutex);
                                        break;
                                }
-                               pthread_mutex_unlock(&last_worker->ThreadMutex);
+                               citthread_mutex_unlock(&last_worker->ThreadMutex);
                                last_worker = last_worker->next;
                        }
                        end_critical_section(S_THREAD_LIST);
                        if (last_worker)
                        {
 #ifdef WITH_THREADLOG
-                               CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (%ld).\n",
+                               syslog(LOG_DEBUG, "Thread system, stopping excess worker thread \"%s\" (0x%08lx).\n",
                                        last_worker->name,
                                        last_worker->tid
                                        );
@@ -1237,36 +1322,53 @@ void go_threading(void)
                /* FIXME: come up with a better way to dynamically alter the number of threads
                 * based on the system load
                 */
-//             if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() < num_sessions))
-               // && (CtdlThreadLoadAvg < 90) )
-               if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
+               if (!statcount) {
+               if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60)) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
                {
-                       for (i=0; i<5 ; i++)
-//                     for (i=0; i< (num_sessions - CtdlThreadGetWorkers()) ; i++)
-//                     for (i=0; i< (10 - (55 - CtdlThreadWorkerAvg) / CtdlThreadWorkerAvg / CtdlThreadGetWorkers()) ; i++)
-                       {
-//                             begin_critical_section(S_THREAD_LIST);
-                               CtdlThreadCreate("Worker Thread",
-                                       CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
-                                       worker_thread,
-                                       NULL
-                                       );
-//                             end_critical_section(S_THREAD_LIST);
+                       /* Only start new threads if we are not going to overload the machine */
+                       /* Temporarily set to 10 should be enough to make sure we don't stranglew the server
+                        * at least until we make this a config option */
+                       if (CtdlThreadGetLoadAvg() < ((double)10.00)) {
+                               for (i=0; i<5 ; i++) {
+                                       CtdlThreadCreate("Worker Thread",
+                                               CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
+                                               worker_thread,
+                                               NULL
+                                               );
+                               }
                        }
+                       else
+                               syslog(LOG_WARNING, "Server strangled due to machine load average too high.\n");
                }
-               
+               }
+
                CtdlThreadGC();
-               
+
                if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
                {
                        CtdlThreadGC();
                }
                
+#ifdef THREADS_USESIGNALS
+               if (CtdlThreadGetCount() && CT->state > CTDL_THREAD_STOP_REQ)
+#else
                if (CtdlThreadGetCount())
+#endif
                        CtdlThreadSleep(1);
        }
        /*
         * If the above loop exits we must be shutting down since we obviously have no threads
         */
        ctdl_thread_internal_cleanup();
+
+       if (statcount) {
+               gettimeofday(&now, NULL);
+               timersub(&now, &start, &result);
+               last_duration = (double)result.tv_sec + ((double)result.tv_usec / (double) 1000000);
+               syslog(LOG_EMERG, "Simulated %ld connections in %f seconds\n", stats_done, last_duration);
+       }
 }
+
+
+
+