-
-
-/*
- * 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;
-
- if (CT->signal)
- {
- CtdlLogPrintf(CTDL_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)
- {
- 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
- */
- if (CT)
- {
- const char *name = CT->name;
- const pid_t tid = CT->tid;
-
- CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", name, tid);
- }
- else
- {
- CtdlLogPrintf(CTDL_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);
-}
-
-/*
- * 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