+ this_thread->name = "Garbage Collection Thread";
+
+ this_thread->tid = GC_thread;
+ CT = this_thread;
+
+ num_threads++; // Increase the count of threads in the system.
+
+ this_thread->next = CtdlThreadList;
+ CtdlThreadList = this_thread;
+ if (this_thread->next)
+ this_thread->next->prev = this_thread;
+ /* Set up start times */
+ gettimeofday(&this_thread->start_time, NULL); /* Time this thread started */
+ memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval)); /* Changed state so mark it. */
+}
+
+
+/*
+ * A function to update a threads load averages
+ */
+ void ctdl_thread_internal_update_avgs(struct 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);
+ // 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)
+ this_thread->avg_sleeping += last_duration;
+ if (this_thread->state == CTDL_THREAD_RUNNING)
+ this_thread->avg_running += last_duration;
+ 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);
+}
+
+/*
+ * A function to chenge the state of a thread
+ */
+void ctdl_thread_internal_change_state (struct 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 */
+ 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);
+}
+
+
+/*
+ * A function to tell all threads to exit
+ */
+void CtdlThreadStopAll(void)
+{
+ //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;
+
+ begin_critical_section(S_THREAD_LIST);
+ this_thread = CtdlThreadList;
+ while(this_thread)
+ {
+#ifdef THREADS_USESIGNALS
+ pthread_kill(this_thread->tid, SIGHUP);
+#endif
+ 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);
+ this_thread = this_thread->next;
+ }
+ end_critical_section(S_THREAD_LIST);
+}
+
+
+/*
+ * A function to wake up all sleeping threads
+ */
+void CtdlThreadWakeAll(void)
+{
+ struct CtdlThreadNode *this_thread;
+
+ CtdlLogPrintf(CTDL_DEBUG, "Thread system waking all threads.\n");
+
+ begin_critical_section(S_THREAD_LIST);
+ this_thread = CtdlThreadList;
+ while(this_thread)
+ {
+ if (!this_thread->thread_func)
+ {
+ pthread_cond_signal(&this_thread->ThreadCond);
+ pthread_cond_signal(&this_thread->SleepCond);
+ }
+ this_thread = this_thread->next;
+ }
+ end_critical_section(S_THREAD_LIST);
+}
+
+
+/*
+ * A function to return the number of threads running in the system
+ */
+int CtdlThreadGetCount(void)
+{
+ return num_threads;
+}
+
+int CtdlThreadGetWorkers(void)
+{
+ return num_workers;
+}
+
+double CtdlThreadGetWorkerAvg(void)
+{
+ double ret;
+
+ begin_critical_section(S_THREAD_LIST);
+ ret = CtdlThreadWorkerAvg;
+ end_critical_section(S_THREAD_LIST);
+ return ret;
+}
+
+double CtdlThreadGetLoadAvg(void)
+{
+ double ret;
+
+ begin_critical_section(S_THREAD_LIST);
+ ret = CtdlThreadLoadAvg;
+ end_critical_section(S_THREAD_LIST);
+ return ret;
+}
+
+
+
+
+/*
+ * A function to rename a thread
+ * Returns a const char *
+ */
+const char *CtdlThreadName(const char *name)
+{
+ const char *old_name;
+
+ if (!CT)
+ {
+ CtdlLogPrintf(CTDL_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);
+}
+
+
+/*
+ * A function to force a thread to exit
+ */
+void CtdlThreadCancel(struct CtdlThreadNode *thread)
+{
+ struct CtdlThreadNode *this_thread;
+
+ if (!thread)
+ this_thread = CT;
+ else
+ this_thread = thread;
+ if (!this_thread)
+ {
+ CtdlLogPrintf(CTDL_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");
+ CtdlThreadStopAll();
+ return;
+ }
+
+ ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED);
+ pthread_cancel(this_thread->tid);
+}
+
+
+
+/*
+ * A function for a thread to check if it has been asked to stop
+ */
+int CtdlThreadCheckStop(void)
+{
+ int state;
+
+ if (!CT)
+ {
+ CtdlLogPrintf(CTDL_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);
+ 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;
+}
+
+
+/*
+ * 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)
+{
+ struct CtdlThreadNode *this_thread;
+
+ if (!thread)
+ this_thread = CT;
+ else
+ this_thread = thread;
+ if (!this_thread)
+ return;
+ if (!(this_thread->thread_func))
+ return; // Don't stop garbage collector
+#ifdef THREADS_USESIGNALS
+ pthread_kill(this_thread->tid, SIGHUP);
+#endif
+ ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
+ pthread_cond_signal(&this_thread->ThreadCond);
+ pthread_cond_signal(&this_thread->SleepCond);
+}
+
+/*
+ * So we now have a sleep command that works with threads but it is in seconds
+ */
+void CtdlThreadSleep(int secs)
+{
+ struct timespec wake_time;
+ struct timeval time_now;
+
+
+ if (!CT)
+ {
+ CtdlLogPrintf(CTDL_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
+ return;
+ }
+
+ memset (&wake_time, 0, sizeof(struct timespec));
+ gettimeofday(&time_now, NULL);
+ wake_time.tv_sec = time_now.tv_sec + secs;
+ wake_time.tv_nsec = time_now.tv_usec * 10;
+
+ 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);
+
+ ctdl_thread_internal_change_state (CT, CTDL_THREAD_RUNNING);
+}
+
+
+/*
+ * Routine to clean up our thread function on exit
+ */
+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);
+
+ #ifdef HAVE_BACKTRACE
+ eCrash_UnregisterThread();
+ #endif
+
+ pthread_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);
+}
+
+/*
+ * A quick function to show the load averages
+ */
+void ctdl_thread_internal_calc_loadavg(void)
+{
+ struct CtdlThreadNode *that_thread;
+ double load_avg, worker_avg;
+ int workers = 0;
+
+ that_thread = CtdlThreadList;
+ load_avg = 0;
+ worker_avg = 0;
+ while(that_thread)
+ {
+ /* 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;
+ that_thread->avg_sleeping /= 2;
+ that_thread->avg_running /= 2;
+ that_thread->avg_blocked /= 2;
+ load_avg += that_thread->load_avg;
+ if (that_thread->flags & CTDLTHREAD_WORKER)
+ {
+ worker_avg += that_thread->load_avg;
+ workers++;
+ }
+#ifdef WITH_THREADLOG
+ CtdlLogPrintf(CTDL_DEBUG, "CtdlThread, \"%s\" (%ld) \"%s\" %f %f %f %f.\n",
+ that_thread->name,
+ that_thread->tid,
+ CtdlThreadStates[that_thread->state],
+ that_thread->avg_sleeping,
+ that_thread->avg_running,
+ that_thread->avg_blocked,
+ that_thread->load_avg);
+#endif
+ pthread_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);
+#endif
+}
+
+
+/*
+ * Garbage collection routine.
+ * Gets called by main() in a loop to clean up the thread list periodically.
+ */
+void CtdlThreadGC (void)
+{
+ struct CtdlThreadNode *this_thread, *that_thread;
+ int workers = 0;
+ int ret=0;
+
+ begin_critical_section(S_THREAD_LIST);
+
+ /* Handle exiting of garbage collector thread */
+ if(num_threads == 1)
+ CtdlThreadList->state = CTDL_THREAD_EXITED;
+
+#ifdef WITH_THREADLOG
+ CtdlLogPrintf(CTDL_DEBUG, "Thread system running garbage collection.\n");
+#endif
+ /*
+ * Woke up to do garbage collection
+ */
+ this_thread = CtdlThreadList;
+ while(this_thread)
+ {
+ that_thread = this_thread;
+ this_thread = this_thread->next;
+
+ /* Do we need to clean up this thread? */
+ pthread_mutex_lock(&that_thread->ThreadMutex);
+ if (that_thread->state != CTDL_THREAD_EXITED)
+ {
+ 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)
+ { /* 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");
+ 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");
+ abort();
+ return;
+ }
+
+ if(that_thread->flags & CTDLTHREAD_WORKER)
+ num_workers--; /* This is a wroker thread so reduce the count. */
+ num_threads--;
+ /* If we are unlinking the list head then the next becomes the list head */
+ if(that_thread->prev)
+ that_thread->prev->next = that_thread->next;
+ else
+ CtdlThreadList = that_thread->next;
+ 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);
+ /*
+ * 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);
+ if (ret == EDEADLK)
+ CtdlLogPrintf(CTDL_DEBUG, "Garbage collection on own thread.\n");
+ else if (ret == EINVAL)
+ CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, that thread already joined on.\n");
+ else if (ret == ESRCH)
+ CtdlLogPrintf(CTDL_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");
+ /*
+ * 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);
+ free(that_thread);
+ }
+
+ /* Sanity check number of worker threads */
+ if (workers != num_workers)
+ {
+ end_critical_section(S_THREAD_LIST);
+ CtdlLogPrintf(CTDL_EMERG,
+ "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
+ workers, num_workers
+ );
+ abort();
+ }
+ end_critical_section(S_THREAD_LIST);
+}
+
+
+
+
+/*
+ * Runtime function for a Citadel Thread.
+ * This initialises the threads environment and then calls the user supplied thread function
+ * Note that this is the REAL thread function and wraps the users thread function.
+ */
+static void *ctdl_internal_thread_func (void *arg)
+{
+ struct CtdlThreadNode *this_thread;
+ void *ret = NULL;
+
+ /* lock and unlock the thread list.
+ * This causes this thread to wait until all its creation stuff has finished before it
+ * can continue its execution.
+ */
+ begin_critical_section(S_THREAD_LIST);
+ this_thread = (struct 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);
+ // Get our thread data structure
+ CtdlThreadAllocTSD();
+ CT = this_thread;
+ this_thread->pid = getpid();
+ memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval)); /* Changed state so mark it. */
+ /* Only change to running state if we weren't asked to stop during the create cycle
+ * 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);
+ this_thread->state = CTDL_THREAD_RUNNING;
+ pthread_mutex_unlock(&this_thread->ThreadMutex);
+ }
+ end_critical_section(S_THREAD_LIST);
+
+ // Register for tracing
+ #ifdef HAVE_BACKTRACE
+ 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);
+
+
+
+ /*
+ * run the thread to do the work but only if we haven't been asked to stop
+ */
+ if (!CtdlThreadCheckStop())
+ ret = (this_thread->thread_func)(this_thread->user_args);
+
+ /*
+ * 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
+
+ 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)
+{
+ 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));
+
+ /* 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);
+
+ 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));
+ free(this_thread);
+ return NULL;
+ }
+