- * A function to initialise the thread TSD
- */
-void ctdl_thread_internal_init_tsd(void)
-{
- int 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);
- }
-}
-
-/*
- * Ensure that we have a key for thread-specific data.
- *
- * This should be called immediately after startup by any thread
- *
- */
-void CtdlThreadAllocTSD(void)
-{
- ThreadTSD *tsd;
-
- if (citthread_getspecific(ThreadKey) != NULL)
- return;
-
- tsd = malloc(sizeof(ThreadTSD));
-
- tsd->tid = NULL;
-
- memset(tsd->cursors, 0, sizeof tsd->cursors);
- tsd->self = NULL;
-
- citthread_setspecific(ThreadKey, tsd);
-}
-
-
-void ctdl_thread_internal_free_tsd(void)
-{
- ctdl_thread_internal_dest_tsd(citthread_getspecific(ThreadKey));
- citthread_setspecific(ThreadKey, NULL);
-}
-
-
-void ctdl_thread_internal_cleanup(void)
-{
- int i;
- CtdlThreadNode *this_thread, *that_thread;
-
- for (i=0; i<CTDL_THREAD_LAST_STATE; i++)
- {
- free (CtdlThreadStates[i]);
- }
-
- /* Clean up the scheduled thread list */
- this_thread = CtdlThreadSchedList;
- while (this_thread)
- {
- that_thread = this_thread;
- this_thread = this_thread->next;
- 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();
-}
-
-void ctdl_thread_internal_init(void)
-{
- CtdlThreadNode *this_thread;
- int ret = 0;
-
- CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread");
- CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread");
- CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created");
- CtdlThreadStates[CTDL_THREAD_CANCELLED] = strdup("Thread Cancelled");
- CtdlThreadStates[CTDL_THREAD_EXITED] = strdup("Thread Exited");
- CtdlThreadStates[CTDL_THREAD_STOPPING] = strdup("Thread Stopping");
- CtdlThreadStates[CTDL_THREAD_STOP_REQ] = strdup("Thread Stop Requested");
- CtdlThreadStates[CTDL_THREAD_SLEEPING] = strdup("Thread Sleeping");
- CtdlThreadStates[CTDL_THREAD_RUNNING] = strdup("Thread Running");
- CtdlThreadStates[CTDL_THREAD_BLOCKED] = strdup("Thread Blocked");
-
- /* Get ourself a thread entry */
- this_thread = malloc(sizeof(CtdlThreadNode));
- if (this_thread == NULL) {
- 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(CtdlThreadNode));
-
- 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 = 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 = citthread_self();
- GC_thread = this_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 chenge the state of a thread
- */
-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
- */
- /* 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;
- citthread_mutex_unlock(&this_thread->ThreadMutex);
-}
-
-
-/*
- * A function to tell all threads to exit
- */
-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
-
- 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)
- {
- 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);
- 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);
-}
-
-
-/*
- * A function to wake up all sleeping threads
- */
-void CtdlThreadWakeAll(void)
-{
- CtdlThreadNode *this_thread;
-
- syslog(LOG_DEBUG, "Thread system waking all threads.\n");
-
- begin_critical_section(S_THREAD_LIST);
- this_thread = CtdlThreadList;
- while(this_thread)
- {
- if (!this_thread->thread_func)
- {
- citthread_cond_signal(&this_thread->ThreadCond);
- citthread_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 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;
-*/
-}
-
-
-
-
-/*
- * A function to rename a thread
- * Returns a const char *
- */
-const char *CtdlThreadName(const char *name)
-{
- const char *old_name;
-
- if (!CT)
- {
- syslog(LOG_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
- return NULL;
- }
- old_name = CT->name;
- if (name)
- CT->name = name;
- return (old_name);
-}
-
-
-/*
- * A function to force a thread to exit
- */
-void CtdlThreadCancel(CtdlThreadNode *thread)
-{
- CtdlThreadNode *this_thread;
-
- if (!thread)
- this_thread = CT;
- else
- this_thread = thread;
- if (!this_thread)
- {
- syslog(LOG_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
- CtdlThreadStopAll();
- return;
- }
-
- if (!this_thread->thread_func)
- {
- 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);
- citthread_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)
- {
- syslog(LOG_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
- CtdlThreadStopAll();
- return -1;
- }
-
- state = CT->state;
-
- if (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(state == CTDL_THREAD_STOP_REQ)
- {
- CT->state = CTDL_THREAD_STOPPING;
- return -1;
- }
- else if((state < CTDL_THREAD_STOP_REQ) && (state > CTDL_THREAD_CREATE))
- {
- return -1;
- }
- return 0;
-}
-
-
-/*
- * A function to ask a thread to exit
- * The thread must call CtdlThreadCheckStop() periodically to determine if it should exit
- */
-void CtdlThreadStop(CtdlThreadNode *thread)
-{
- 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
-
- 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);
- citthread_cond_signal(&this_thread->ThreadCond);
- citthread_cond_signal(&this_thread->SleepCond);
- this_thread->stop_ticker = time(NULL);
-}
-
-/*
- * 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)
- {
- syslog(LOG_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);
-
- 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);
-}
-
-
-/*
- * 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
- */
- 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();
- #endif
-
- citthread_mutex_lock(&CT->ThreadMutex);
- CT->state = CTDL_THREAD_EXITED; // needs to be last thing else house keeping will unlink us too early
- citthread_mutex_unlock(&CT->ThreadMutex);
-}
-
-/*
- * 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;
-
- begin_critical_section(S_THREAD_LIST);
-
- /* Handle exiting of garbage collector thread */
- if(num_threads == 1)
- CtdlThreadList->state = CTDL_THREAD_EXITED;
-
-#ifdef WITH_THREADLOG
- syslog(LOG_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;
-
- 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? */
- 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 */
- continue;
- }
-
- if (citthread_equal(that_thread->tid, citthread_self()) && that_thread->thread_func)
- { /* Sanity check */
- end_critical_section(S_THREAD_LIST);
- 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);
- syslog(LOG_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;
-
- 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 = citthread_join (that_thread->tid, NULL);
- if (ret == EDEADLK)
- syslog(LOG_DEBUG, "Garbage collection on own thread.\n");
- else if (ret == EINVAL)
- syslog(LOG_DEBUG, "Garbage collection, that thread already joined on.\n");
- else if (ret == ESRCH)
- syslog(LOG_DEBUG, "Garbage collection, no thread to join on.\n");
- else if (ret != 0)
- syslog(LOG_DEBUG, "Garbage collection, citthread_join returned an unknown error(%d).\n", ret);
- /*
- * Now we own that thread entry
- */
- 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 != sys_workers)
- {
- syslog(LOG_EMERG,
- "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
- workers, sys_workers
- );
- abort();
- }
-}
-
-
-
-
-/*
- * 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.