/*
- * $Id$
+ * Thread handling stuff for Citadel server
*
- * Citadel "system dependent" stuff.
- * See COPYING 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 <fcntl.h>
#include <signal.h>
+#include <syslog.h>
#include "sysdep.h"
#if TIME_WITH_SYS_TIME
#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.
* To create a thread you must call one of the create thread functions.
* You must pass it the address of (a pointer to a CtdlThreadNode initialised to NULL) like this
* struct CtdlThreadNode *node = NULL;
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;
CtdlThreadNode *CtdlThreadList = NULL;
CtdlThreadNode *CtdlThreadSchedList = NULL;
* transaction; this could lead to deadlock.
*/
if ( (which_one != S_FLOORCACHE)
-#ifdef DEBUG_MEMORY_LEAKS
- && (which_one != S_DEBUGMEMLEAKS)
-#endif
&& (which_one != S_RPLIST)
) {
cdb_check_handles();
*/
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
* transaction; this could lead to deadlock.
*/
if ( (which_one != S_FLOORCACHE)
-#ifdef DEBUG_MEMORY_LEAKS
- && (which_one != S_DEBUGMEMLEAKS)
-#endif
&& (which_one != S_RPLIST)
) {
cdb_check_handles();
int ret;
if ((ret = citthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
- CtdlLogPrintf(CTDL_EMERG, "citthread_key_create: %s\n", strerror(ret));
+ syslog(LOG_EMERG, "citthread_key_create: %s\n", strerror(ret));
exit(CTDLEXIT_DB);
}
}
/* Get ourself a thread entry */
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.
this_thread->state = CTDL_THREAD_RUNNING;
if ((ret = citthread_attr_init(&this_thread->attr))) {
- CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
+ syslog(LOG_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
free(this_thread);
return;
}
}
-/*
- * A function to update a threads load averages
- */
- 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);
- /* 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)
- 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));
- citthread_mutex_unlock(&this_thread->ThreadMutex);
-}
-
/*
* A function to chenge the state of a thread
*/
/*
* Wether we change state or not we need update the load values
*/
- ctdl_thread_internal_update_avgs(this_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))
{
/* 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
+ ShutDownEventQueue();
+
CtdlThreadNode *this_thread;
begin_critical_section(S_THREAD_LIST);
citthread_cond_signal(&this_thread->ThreadCond);
citthread_cond_signal(&this_thread->SleepCond);
this_thread->stop_ticker = time(NULL);
- CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (0x%08lx).\n",
+ syslog(LOG_DEBUG, "Thread system stopping thread \"%s\" (0x%08lx).\n",
this_thread->name, this_thread->tid);
this_thread = this_thread->next;
}
{
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;
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;
}
old_name = CT->name;
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;
}
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;
}
if (CT->signal)
{
- CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
+ 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 (!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;
}
const char *name = CT->name;
const pid_t tid = CT->tid;
- CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", name, tid);
+ syslog(LOG_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", name, (unsigned long) tid);
}
else
{
- CtdlLogPrintf(CTDL_NOTICE, "some ((unknown ? ? ?) Thread exited.\n");
+ syslog(LOG_NOTICE, "some ((unknown ? ? ?) Thread exited.\n");
}
#ifdef HAVE_BACKTRACE
citthread_mutex_unlock(&CT->ThreadMutex);
}
-/*
- * A quick function to show the load averages
- */
-void ctdl_thread_internal_calc_loadavg(void)
-{
- 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);
- 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;
- 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\" (%lu) \"%s\" %.2f %.2f %.2f %.2f\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
- 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 %.2f, workers averag %.2f, threads %d, workers %d, sessions %d\n", CtdlThreadGetLoadAvg(), 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)
{
+
+
+ return;
+ /* FIXME this is a big deal, but I think it's causing corruption */
+
+
CtdlThreadNode *this_thread, *that_thread;
int workers = 0, sys_workers;
int ret=0;
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
if ((that_thread->state == CTDL_THREAD_STOP_REQ || that_thread->state == CTDL_THREAD_STOPPING)
&& (!citthread_equal(that_thread->tid, citthread_self())))
- CtdlLogPrintf(CTDL_DEBUG, "Waiting for thread %s (0x%08lx) to exit.\n", that_thread->name, that_thread->tid);
+ {
+ syslog(LOG_DEBUG, "Waiting for thread %s (0x%08lx) to exit.\n", that_thread->name, that_thread->tid);
+ terminate_stuck_sessions();
+ }
else
{
/**
if (that_thread->stop_ticker + 5 == time(NULL))
{
- 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);
+ 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)
- CtdlLogPrintf(CTDL_INFO, "Thread System: A non worker thread would have been canceled this may cause message loss.\n");
+ 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);
if (citthread_equal(that_thread->tid, citthread_self()) && that_thread->thread_func)
{ /* Sanity check */
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 */
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;
}
*/
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, citthread_join returned an unknown error(%d).\n", ret);
+ 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\" (0x%08lx).\n",
+ 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);
/* Sanity check number of worker threads */
if (workers != sys_workers)
{
- CtdlLogPrintf(CTDL_EMERG,
+ syslog(LOG_EMERG,
"Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
workers, sys_workers
);
#if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
this_thread->reltid = syscall(SYS_gettid);
#endif
- CtdlLogPrintf(CTDL_NOTICE, "Created a new thread \"%s\" (0x%08lx).\n",
+ syslog(LOG_NOTICE, "Created a new thread \"%s\" (0x%08lx).\n",
this_thread->name, this_thread->tid);
/*
citthread_cond_destroy(&(this_thread->ThreadCond));
citthread_mutex_destroy(&(this_thread->SleepMutex));
citthread_cond_destroy(&(this_thread->SleepCond));
- CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
+ syslog(LOG_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
free(this_thread);
return NULL;
}
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 = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
citthread_mutex_unlock(&this_thread->ThreadMutex);
citthread_mutex_destroy(&(this_thread->SleepMutex));
citthread_cond_destroy(&(this_thread->SleepCond));
citthread_attr_destroy(&this_thread->attr);
- CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_setstacksize: %s\n",
+ syslog(LOG_EMERG, "Thread system, citthread_attr_setstacksize: %s\n",
strerror(ret));
free(this_thread);
return NULL;
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(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;
}
if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
{
free(this_thread);
- CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
+ syslog(LOG_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
return NULL;
}
/*
if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
{
end_critical_section(S_THREAD_LIST);
- CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
+ 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));
CtdlThreadList = this_thread;
if (this_thread->next)
this_thread->next->prev = this_thread;
- ctdl_thread_internal_calc_loadavg();
end_critical_section(S_THREAD_LIST);
-/*
- * Internal function to schedule a thread.
- * Must be called from within a S_THREAD_LIST critical section
- */
-CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) (void *arg), void *args, time_t when)
-{
- CtdlThreadNode *this_thread;
-
- if (num_threads >= 32767)
- {
- CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
- return NULL;
- }
-
- this_thread = malloc(sizeof(CtdlThreadNode));
- if (this_thread == NULL) {
- CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
- return NULL;
- }
- /* Initialise the thread structure */
- if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
- {
- free(this_thread);
- CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
- 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;
-
- /*
- * When to start this thread
- */
- this_thread->when = when;
-
- begin_critical_section(S_SCHEDULE_LIST);
- this_thread->next = CtdlThreadSchedList;
- CtdlThreadSchedList = this_thread;
- if (this_thread->next)
- this_thread->next->prev = this_thread;
- end_critical_section(S_SCHEDULE_LIST);
-
- return this_thread;
-}
-
-
-
CtdlThreadNode *ctdl_thread_internal_start_scheduled (CtdlThreadNode *this_thread)
{
int ret = 0;
if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
{
end_critical_section(S_THREAD_LIST);
- CtdlLogPrintf(CTDL_DEBUG, "Failed to start scheduled thread \"%s\": %s\n", this_thread->name, strerror(ret));
+ 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));
if (this_thread->next)
this_thread->next->prev = this_thread;
- ctdl_thread_internal_calc_loadavg();
end_critical_section(S_THREAD_LIST);
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;
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
if (CT->state > CTDL_THREAD_STOP_REQ)
{ /* Only start it if the system is not stopping */
if (ctdl_thread_internal_start_scheduled (that_thread))
{
#ifdef WITH_THREADLOG
- CtdlLogPrintf(CTDL_INFO, "Thread system, Started a scheduled thread \"%s\" (0x%08lx).\n",
+ syslog(LOG_INFO, "Thread system, Started a scheduled thread \"%s\" (0x%08lx).\n",
that_thread->name, that_thread->tid);
#endif
}
#ifdef WITH_THREADLOG
else
{
- CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" will start in %ld seconds.\n",
+ syslog(LOG_DEBUG, "Thread \"%s\" will start in %ld seconds.\n",
that_thread->name, that_thread->when - time(NULL));
}
#endif
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)
{
- CtdlLogPrintf(CTDL_DEBUG, "Thread %s (0x%08lx) refused stop request.\n", CT->name, CT->tid);
+ 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);
extern void close_masters (void);
-void *simulation_worker (void*arg) {
- struct CitContext *this;
-
- this = CreateNewContext();
- CtdlThreadSleep(1);
- this->kill_me = 1;
- 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;
CtdlThreadNode *last_worker;
- struct timeval start, now, result;
- double last_duration;
/*
* Initialise the thread system
ctdl_thread_internal_init();
/* Second call to module init functions now that threading is up */
- if (!statcount) {
- initialise_modules(1);
- CtdlThreadCreate("select_on_master", CTDLTHREAD_BIGSTACK, select_on_master, NULL);
- }
- else {
- CtdlLogPrintf(CTDL_EMERG, "Running connection simulation stats\n");
- gettimeofday(&start, NULL);
- CtdlThreadCreate("Connection simulation master", CTDLTHREAD_BIGSTACK, simulation_thread, NULL);
- }
-
+ initialise_modules(1);
+ CtdlThreadCreate("select_on_master", CTDLTHREAD_BIGSTACK, select_on_master, 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", citthread_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.
if (CT->state > CTDL_THREAD_STOP_REQ)
{
begin_critical_section(S_THREAD_LIST);
- ctdl_thread_internal_calc_loadavg();
end_critical_section(S_THREAD_LIST);
ctdl_thread_internal_check_scheduled(); /* start scheduled threads */
if (last_worker)
{
#ifdef WITH_THREADLOG
- CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (0x%08lx).\n",
+ syslog(LOG_DEBUG, "Thread system, stopping excess worker thread \"%s\" (0x%08lx).\n",
last_worker->name,
last_worker->tid
);
/* FIXME: come up with a better way to dynamically alter the number of threads
* based on the system load
*/
- if (!statcount) {
- if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60)) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
+ if ( (((CtdlThreadGetWorkers() < config.c_max_workers)
+ && (CtdlThreadGetWorkerAvg() > 60))
+ || CtdlThreadGetWorkers() < config.c_min_workers)
+ && (CT->state > CTDL_THREAD_STOP_REQ)
+ )
{
/* 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
}
}
else
- CtdlLogPrintf (CTDL_WARNING, "Server strangled due to machine load average too high.\n");
- }
+ syslog(LOG_WARNING, "Server strangled due to machine load average too high.\n");
}
CtdlThreadGC();
* 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);
- CtdlLogPrintf(CTDL_EMERG, "Simulated %ld connections in %f seconds\n", stats_done, last_duration);
- }
}