- * 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)
- {
- CtdlLogPrintf(CTDL_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)
- {
- 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);
- 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)
- {
- CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
- CtdlThreadStopAll();
- return -1;
- }
-
- state = CT->state;
-
-#ifdef THREADS_USESIGNALS
- if (CT->signal)
- {
- CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
- CT->signal = 0;
- }
-#endif
- 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
-#ifdef THREADS_USESIGNALS
- if (!citthread_equal(this_thread->tid, GC_thread->tid))
- citthread_kill(this_thread->tid, SIGHUP);
-#endif
- 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)
- {
- 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);
-
- 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
- */
- CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", CT->name, CT->tid);
-
- #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);
-}
-
-/*
- * 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)
-{
- 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
- 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;
-
- 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);
- 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))
- {
- 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);
- 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");
-// 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);
- CtdlLogPrintf(CTDL_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");
- 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)
- 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, 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",
- 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)
- {
- CtdlLogPrintf(CTDL_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.