- * A function to destroy the TSD
- */
-static void ctdl_thread_internal_dest_tsd(void *arg)
-{
- if (arg != NULL) {
- check_handles(arg);
- free(arg);
- }
-}
-
-
-/*
- * A function to initialise the thread TSD
- */
-void ctdl_thread_internal_init_tsd(void)
-{
- int ret;
-
- if ((ret = pthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
- lprintf(CTDL_EMERG, "pthread_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 (pthread_getspecific(ThreadKey) != NULL)
- return;
-
- tsd = malloc(sizeof(ThreadTSD));
-
- tsd->tid = NULL;
-
- memset(tsd->cursors, 0, sizeof tsd->cursors);
- tsd->self = NULL;
-
- pthread_setspecific(ThreadKey, tsd);
-}
-
-
-void ctdl_thread_internal_free_tsd(void)
-{
- ctdl_thread_internal_dest_tsd(pthread_getspecific(ThreadKey));
- pthread_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;
- 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);
- }
- ctdl_thread_internal_free_tsd();
-}
-
-void ctdl_thread_internal_init(void)
-{
- CtdlThreadNode *this_thread;
- int ret = 0;
-
- GC_thread = pthread_self();
- 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) {
- CtdlLogPrintf(CTDL_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));
-
- 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);
-
- /* We are garbage collector so create us as running */
- this_thread->state = CTDL_THREAD_RUNNING;
-
- if ((ret = pthread_attr_init(&this_thread->attr))) {
- CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret));
- free(this_thread);
- return;
- }
-
- 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(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 */
- 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 (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);
- /* This mutex not needed here? */
- 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
-
- 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)
-{
- 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;
- }
- 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);
- 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
- 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
- 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)
-{
- 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_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
- pthread_mutex_unlock(&that_thread->ThreadMutex);
- that_thread = that_thread->next;