From cab2dbf7872d4ff2d86b0a0db8d1899825d7482f Mon Sep 17 00:00:00 2001 From: Dave West Date: Sat, 29 Dec 2007 21:53:09 +0000 Subject: [PATCH] Abstracted pthread from threads.* into sysdep_decls.h to ease porting. At the moment its all typdefs ande #defines. --- citadel/include/ctdl_module.h | 2 +- citadel/sysdep.c | 8 +- citadel/sysdep_decls.h | 47 ++++++- citadel/threads.c | 249 +++++++++++++++++----------------- citadel/threads.h | 15 +- 5 files changed, 180 insertions(+), 141 deletions(-) diff --git a/citadel/include/ctdl_module.h b/citadel/include/ctdl_module.h index 6005b1993..e5a544b73 100644 --- a/citadel/include/ctdl_module.h +++ b/citadel/include/ctdl_module.h @@ -117,7 +117,7 @@ struct CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread void CtdlThreadSleep(int secs); void CtdlThreadStop(struct CtdlThreadNode *thread); int CtdlThreadCheckStop(void); -void CtdlThreadCancel(struct CtdlThreadNode *thread); +/* void CtdlThreadCancel2(struct CtdlThreadNode *thread); Leave this out, it should never be needed */ const char *CtdlThreadName(const char *name); struct CtdlThreadNode *CtdlThreadSelf(void); int CtdlThreadGetCount(void); diff --git a/citadel/sysdep.c b/citadel/sysdep.c index 12ef309cc..b89680c9e 100644 --- a/citadel/sysdep.c +++ b/citadel/sysdep.c @@ -84,7 +84,7 @@ struct igheap *igheap = NULL; #endif -pthread_key_t MyConKey; /* TSD key for MyContext() */ +citthread_key_t MyConKey; /* TSD key for MyContext() */ int verbosity = DEFAULT_VERBOSITY; /* Logging level */ @@ -216,7 +216,7 @@ void init_sysdep(void) { * CitContext structure (in the ContextList linked list) of the * session to which the calling thread is currently bound. */ - if (pthread_key_create(&MyConKey, NULL) != 0) { + if (citthread_key_create(&MyConKey, NULL) != 0) { CtdlLogPrintf(CTDL_CRIT, "Can't create TSD key: %s\n", strerror(errno)); } @@ -414,7 +414,7 @@ struct CitContext *MyContext(void) { register struct CitContext *c; - return ((c = (struct CitContext *) pthread_getspecific(MyConKey), + return ((c = (struct CitContext *) citthread_getspecific(MyConKey), c == NULL) ? &masterCC : c ); } @@ -1030,7 +1030,7 @@ void InitializeMasterCC(void) { * Bind a thread to a context. (It's inline merely to speed things up.) */ INLINE void become_session(struct CitContext *which_con) { - pthread_setspecific(MyConKey, (void *)which_con ); + citthread_setspecific(MyConKey, (void *)which_con ); } diff --git a/citadel/sysdep_decls.h b/citadel/sysdep_decls.h index 3bb1af0d4..7d02ba74b 100644 --- a/citadel/sysdep_decls.h +++ b/citadel/sysdep_decls.h @@ -11,10 +11,13 @@ */ -#include #include #include "sysdep.h" +#ifdef HAVE_PTHREAD_H +#include +#endif + #ifdef HAVE_DB_H #include #elif defined(HAVE_DB4_DB_H) @@ -114,6 +117,48 @@ extern int rescan[]; extern int SyslogFacility(char *name); extern int syslog_facility; + +/* + * Typdefs and stuff to abstract pthread for Citadel + */ +#ifdef HAVE_PTHREAD_H + +typedef pthread_t citthread_t; +typedef pthread_key_t citthread_key_t; +typedef pthread_mutex_t citthread_mutex_t; +typedef pthread_cond_t citthread_cond_t; +typedef pthread_attr_t citthread_attr_t; + + +#define citthread_mutex_init pthread_mutex_init +#define citthread_cond_init pthread_cond_init +#define citthread_attr_init pthread_attr_init +#define citthread_mutex_trylock pthread_mutex_trylock +#define citthread_mutex_lock pthread_mutex_lock +#define citthread_mutex_unlock pthread_mutex_unlock +#define citthread_key_create pthread_key_create +#define citthread_getspecific pthread_getspecific +#define citthread_setspecific pthread_setspecific +#define citthread_mutex_destroy pthread_mutex_destroy +#define citthread_cond_destroy pthread_cond_destroy +#define citthread_attr_destroy pthread_attr_destroy + +#define citthread_kill pthread_kill +#define citthread_cond_signal pthread_cond_signal +#define citthread_cancel pthread_cancel +#define citthread_cond_timedwait pthread_cond_timedwait +#define citthread_equal pthread_equal +#define citthread_self pthread_self +#define citthread_create pthread_create +#define citthread_attr_setstacksize pthread_attr_setstacksize +#define citthread_join pthread_join +#define citthread_cleanup_push pthread_cleanup_push +#define citthread_cleanup_pop pthread_cleanup_pop + + +#endif /* HAVE_PTHREAD_H */ + + #ifdef DEBUG_MEMORY_LEAKS #define malloc(x) tracked_malloc(x, __FILE__, __LINE__) #define realloc(x,y) tracked_realloc(x, y, __FILE__, __LINE__) diff --git a/citadel/threads.c b/citadel/threads.c index f09917735..ffcf5f3bd 100644 --- a/citadel/threads.c +++ b/citadel/threads.c @@ -56,19 +56,13 @@ static int num_workers = 0; /* Current number of worker threads */ CtdlThreadNode *CtdlThreadList = NULL; CtdlThreadNode *CtdlThreadSchedList = NULL; -/* - * Condition variable and Mutex for thread garbage collection - */ -/*static pthread_mutex_t thread_gc_mutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t thread_gc_cond = PTHREAD_COND_INITIALIZER; -*/ -static pthread_t GC_thread; +static citthread_t GC_thread; static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE]; double CtdlThreadLoadAvg = 0; double CtdlThreadWorkerAvg = 0; -pthread_key_t ThreadKey; +citthread_key_t ThreadKey; -pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */ +citthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */ @@ -78,7 +72,7 @@ void InitialiseSemaphores(void) /* Set up a bunch of semaphores to be used for critical sections */ for (i=0; icursors, 0, sizeof tsd->cursors); tsd->self = NULL; - pthread_setspecific(ThreadKey, tsd); + citthread_setspecific(ThreadKey, tsd); } void ctdl_thread_internal_free_tsd(void) { - ctdl_thread_internal_dest_tsd(pthread_getspecific(ThreadKey)); - pthread_setspecific(ThreadKey, NULL); + ctdl_thread_internal_dest_tsd(citthread_getspecific(ThreadKey)); + citthread_setspecific(ThreadKey, NULL); } @@ -211,11 +205,11 @@ void ctdl_thread_internal_cleanup(void) { 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); + 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(); @@ -226,7 +220,7 @@ void ctdl_thread_internal_init(void) CtdlThreadNode *this_thread; int ret = 0; - GC_thread = pthread_self(); + GC_thread = citthread_self(); CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread"); CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread"); CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created"); @@ -247,16 +241,16 @@ void ctdl_thread_internal_init(void) // 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); + 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 = pthread_attr_init(&this_thread->attr))) { - CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret)); + if ((ret = citthread_attr_init(&this_thread->attr))) { + CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret)); free(this_thread); return; } @@ -289,7 +283,7 @@ void ctdl_thread_internal_init(void) 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); + 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) @@ -299,7 +293,7 @@ void ctdl_thread_internal_init(void) 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); + citthread_mutex_unlock(&this_thread->ThreadMutex); } /* @@ -312,14 +306,14 @@ void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlTh */ 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 */ + 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; - pthread_mutex_unlock(&this_thread->ThreadMutex); + citthread_mutex_unlock(&this_thread->ThreadMutex); } @@ -338,11 +332,11 @@ void CtdlThreadStopAll(void) while(this_thread) { #ifdef THREADS_USESIGNALS - pthread_kill(this_thread->tid, SIGHUP); + citthread_killl(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); + citthread_cond_signal(&this_thread->ThreadCond); + citthread_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; } @@ -365,8 +359,8 @@ void CtdlThreadWakeAll(void) { if (!this_thread->thread_func) { - pthread_cond_signal(&this_thread->ThreadCond); - pthread_cond_signal(&this_thread->SleepCond); + citthread_cond_signal(&this_thread->ThreadCond); + citthread_cond_signal(&this_thread->SleepCond); } this_thread = this_thread->next; } @@ -456,11 +450,10 @@ void CtdlThreadCancel(CtdlThreadNode *thread) } ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED); - pthread_cancel(this_thread->tid); + citthread_cancel(this_thread->tid); } - /* * A function for a thread to check if it has been asked to stop */ @@ -511,11 +504,11 @@ void CtdlThreadStop(CtdlThreadNode *thread) if (!(this_thread->thread_func)) return; // Don't stop garbage collector #ifdef THREADS_USESIGNALS - pthread_kill(this_thread->tid, SIGHUP); + citthread_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); + citthread_cond_signal(&this_thread->ThreadCond); + citthread_cond_signal(&this_thread->SleepCond); } /* @@ -540,9 +533,9 @@ void CtdlThreadSleep(int secs) 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); + 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); } @@ -563,9 +556,9 @@ static void ctdl_internal_thread_cleanup(void *arg) eCrash_UnregisterThread(); #endif - pthread_mutex_lock(&CT->ThreadMutex); + citthread_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); + citthread_mutex_unlock(&CT->ThreadMutex); } /* @@ -584,7 +577,7 @@ void ctdl_thread_internal_calc_loadavg(void) { /* Update load averages */ ctdl_thread_internal_update_avgs(that_thread); - pthread_mutex_lock(&that_thread->ThreadMutex); + 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; @@ -605,7 +598,7 @@ void ctdl_thread_internal_calc_loadavg(void) that_thread->avg_blocked, that_thread->load_avg); #endif - pthread_mutex_unlock(&that_thread->ThreadMutex); + citthread_mutex_unlock(&that_thread->ThreadMutex); that_thread = that_thread->next; } CtdlThreadLoadAvg = load_avg/num_threads; @@ -652,7 +645,7 @@ void CtdlThreadGC (void) continue; } - if (pthread_equal(that_thread->tid, pthread_self()) && that_thread->thread_func) + 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"); @@ -679,16 +672,16 @@ void CtdlThreadGC (void) if(that_thread->next) that_thread->next->prev = that_thread->prev; - pthread_cond_signal(&that_thread->ThreadCond); - pthread_cond_signal(&that_thread->SleepCond); // Make sure this thread is awake - pthread_mutex_lock(&that_thread->ThreadMutex); // Make sure it has done what its doing - pthread_mutex_unlock(&that_thread->ThreadMutex); + 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 = pthread_join (that_thread->tid, NULL); + ret = citthread_join (that_thread->tid, NULL); if (ret == EDEADLK) CtdlLogPrintf(CTDL_DEBUG, "Garbage collection on own thread.\n"); else if (ret == EINVAL) @@ -696,16 +689,16 @@ void CtdlThreadGC (void) else if (ret == ESRCH) CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, no thread to join on.\n"); else if (ret != 0) - CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, pthread_join returned an unknown error.\n"); + CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, citthread_join returned an unknown error.\n"); /* * Now we own that thread entry */ CtdlLogPrintf(CTDL_INFO, "Garbage Collection for thread \"%s\" (%ld).\n", that_thread->name, that_thread->tid); - 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); + 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; @@ -742,10 +735,10 @@ static void *ctdl_internal_thread_func (void *arg) begin_critical_section(S_THREAD_LIST); this_thread = (CtdlThreadNode *) arg; gettimeofday(&this_thread->start_time, NULL); /* Time this thread started */ - pthread_mutex_lock(&this_thread->ThreadMutex); + citthread_mutex_lock(&this_thread->ThreadMutex); // Register the cleanup function to take care of when we exit. - pthread_cleanup_push(ctdl_internal_thread_cleanup, NULL); + citthread_cleanup_push(ctdl_internal_thread_cleanup, NULL); // Get our thread data structure CtdlThreadAllocTSD(); CT = this_thread; @@ -755,13 +748,13 @@ static void *ctdl_internal_thread_func (void *arg) * Other wise there is a window to allow this threads creation to continue to full grown and * therby prevent a shutdown of the server. */ - pthread_mutex_unlock(&this_thread->ThreadMutex); + citthread_mutex_unlock(&this_thread->ThreadMutex); if (!CtdlThreadCheckStop()) { - pthread_mutex_lock(&this_thread->ThreadMutex); + citthread_mutex_lock(&this_thread->ThreadMutex); this_thread->state = CTDL_THREAD_RUNNING; - pthread_mutex_unlock(&this_thread->ThreadMutex); + citthread_mutex_unlock(&this_thread->ThreadMutex); } end_critical_section(S_THREAD_LIST); @@ -785,7 +778,7 @@ static void *ctdl_internal_thread_func (void *arg) * Our thread is exiting either because it wanted to end or because the server is stopping * We need to clean up */ - pthread_cleanup_pop(1); // Execute our cleanup routine and remove it + citthread_cleanup_pop(1); // Execute our cleanup routine and remove it return(ret); } @@ -816,22 +809,22 @@ CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thre memset (this_thread, 0, sizeof(CtdlThreadNode)); /* Create the mutex's early so we can use them */ - 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); + 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); - pthread_mutex_lock(&this_thread->ThreadMutex); + citthread_mutex_lock(&this_thread->ThreadMutex); this_thread->state = CTDL_THREAD_CREATE; - if ((ret = pthread_attr_init(&this_thread->attr))) { - pthread_mutex_unlock(&this_thread->ThreadMutex); - pthread_mutex_destroy(&(this_thread->ThreadMutex)); - pthread_cond_destroy(&(this_thread->ThreadCond)); - pthread_mutex_destroy(&(this_thread->SleepMutex)); - pthread_cond_destroy(&(this_thread->SleepCond)); - CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret)); + if ((ret = citthread_attr_init(&this_thread->attr))) { + citthread_mutex_unlock(&this_thread->ThreadMutex); + citthread_mutex_destroy(&(this_thread->ThreadMutex)); + 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)); free(this_thread); return NULL; } @@ -845,14 +838,14 @@ CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thre #ifdef WITH_THREADLOG CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n"); #endif - if ((ret = pthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) { - pthread_mutex_unlock(&this_thread->ThreadMutex); - pthread_mutex_destroy(&(this_thread->ThreadMutex)); - pthread_cond_destroy(&(this_thread->ThreadCond)); - pthread_mutex_destroy(&(this_thread->SleepMutex)); - pthread_cond_destroy(&(this_thread->SleepCond)); - pthread_attr_destroy(&this_thread->attr); - CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_setstacksize: %s\n", + if ((ret = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) { + citthread_mutex_unlock(&this_thread->ThreadMutex); + citthread_mutex_destroy(&(this_thread->ThreadMutex)); + citthread_cond_destroy(&(this_thread->ThreadCond)); + 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", strerror(ret)); free(this_thread); return NULL; @@ -887,17 +880,17 @@ CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thre * about itself and it has a bit of storage space for itself, not to mention that the REAL * thread function needs to finish off the setup of the structure */ - if ((ret = pthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0)) + if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0)) { CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n", strerror(ret)); - pthread_mutex_unlock(&this_thread->ThreadMutex); - pthread_mutex_destroy(&(this_thread->ThreadMutex)); - pthread_cond_destroy(&(this_thread->ThreadCond)); - pthread_mutex_destroy(&(this_thread->SleepMutex)); - pthread_cond_destroy(&(this_thread->SleepCond)); - pthread_attr_destroy(&this_thread->attr); + citthread_mutex_unlock(&this_thread->ThreadMutex); + citthread_mutex_destroy(&(this_thread->ThreadMutex)); + citthread_cond_destroy(&(this_thread->ThreadCond)); + citthread_mutex_destroy(&(this_thread->SleepMutex)); + citthread_cond_destroy(&(this_thread->SleepCond)); + citthread_attr_destroy(&this_thread->attr); free(this_thread); return NULL; } @@ -911,7 +904,7 @@ CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thre if (this_thread->next) this_thread->next->prev = this_thread; - pthread_mutex_unlock(&this_thread->ThreadMutex); + citthread_mutex_unlock(&this_thread->ThreadMutex); ctdl_thread_internal_calc_loadavg(); return this_thread; @@ -959,19 +952,19 @@ CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) memset (this_thread, 0, sizeof(CtdlThreadNode)); /* Create the mutex's early so we can use them */ - 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); + 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); this_thread->state = CTDL_THREAD_CREATE; - if ((ret = pthread_attr_init(&this_thread->attr))) { - pthread_mutex_destroy(&(this_thread->ThreadMutex)); - pthread_cond_destroy(&(this_thread->ThreadCond)); - pthread_mutex_destroy(&(this_thread->SleepMutex)); - pthread_cond_destroy(&(this_thread->SleepCond)); - CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_init: %s\n", strerror(ret)); + if ((ret = citthread_attr_init(&this_thread->attr))) { + citthread_mutex_destroy(&(this_thread->ThreadMutex)); + 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)); free(this_thread); return NULL; } @@ -983,13 +976,13 @@ CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) if (flags & CTDLTHREAD_BIGSTACK) { CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n"); - if ((ret = pthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) { - pthread_mutex_destroy(&(this_thread->ThreadMutex)); - pthread_cond_destroy(&(this_thread->ThreadCond)); - pthread_mutex_destroy(&(this_thread->SleepMutex)); - pthread_cond_destroy(&(this_thread->SleepCond)); - pthread_attr_destroy(&this_thread->attr); - CtdlLogPrintf(CTDL_EMERG, "Thread system, pthread_attr_setstacksize: %s\n", + if ((ret = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) { + citthread_mutex_destroy(&(this_thread->ThreadMutex)); + citthread_cond_destroy(&(this_thread->ThreadCond)); + 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", strerror(ret)); free(this_thread); return NULL; @@ -1045,7 +1038,7 @@ CtdlThreadNode *ctdl_thread_internal_start_scheduled (CtdlThreadNode *this_threa * about itself and it has a bit of storage space for itself, not to mention that the REAL * thread function needs to finish off the setup of the structure */ - if ((ret = pthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0)) + if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0)) { CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n", @@ -1105,25 +1098,25 @@ void ctdl_thread_internal_check_scheduled(void) begin_critical_section(S_THREAD_LIST); if (CT->state > CTDL_THREAD_STOP_REQ) { /* Only start it if the system is not stopping */ - pthread_mutex_lock(&that_thread->ThreadMutex); + citthread_mutex_lock(&that_thread->ThreadMutex); if (ctdl_thread_internal_start_scheduled (that_thread) == NULL) { #ifdef WITH_THREADLOG CtdlLogPrintf(CTDL_DEBUG, "Failed to start scheduled thread \"%s\".\n", that_thread->name); #endif - pthread_mutex_unlock(&that_thread->ThreadMutex); - 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); + citthread_mutex_unlock(&that_thread->ThreadMutex); + 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); } else { CtdlLogPrintf(CTDL_INFO, "Thread system, Started a scheduled thread \"%s\" (%ld).\n", that_thread->name, that_thread->tid); - pthread_mutex_unlock(&that_thread->ThreadMutex); + citthread_mutex_unlock(&that_thread->ThreadMutex); ctdl_thread_internal_calc_loadavg(); } } @@ -1176,7 +1169,7 @@ void go_threading(void) /* * 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", pthread_self()); + CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", citthread_self()); /* * We do a lot of locking and unlocking of the thread list in here. @@ -1212,13 +1205,13 @@ void go_threading(void) last_worker = CtdlThreadList; while (last_worker) { - pthread_mutex_lock(&last_worker->ThreadMutex); + citthread_mutex_lock(&last_worker->ThreadMutex); if (last_worker->flags & CTDLTHREAD_WORKER && (last_worker->state > CTDL_THREAD_STOPPING) && (last_worker->Context == NULL)) { - pthread_mutex_unlock(&last_worker->ThreadMutex); + citthread_mutex_unlock(&last_worker->ThreadMutex); break; } - pthread_mutex_unlock(&last_worker->ThreadMutex); + citthread_mutex_unlock(&last_worker->ThreadMutex); last_worker = last_worker->next; } end_critical_section(S_THREAD_LIST); diff --git a/citadel/threads.h b/citadel/threads.h index cbffc273e..5a3831618 100644 --- a/citadel/threads.h +++ b/citadel/threads.h @@ -21,6 +21,7 @@ #endif #include "server.h" +#include "sysdep_decls.h" /* * Thread stuff @@ -42,7 +43,7 @@ enum CtdlThreadState { typedef struct CtdlThreadNode CtdlThreadNode; struct CtdlThreadNode{ - pthread_t tid; /* id as returned by pthread_create() */ + citthread_t tid; /* id as returned by citthread_create() */ pid_t pid; /* pid, as best the OS will let us determine */ time_t when; /* When to start a scheduled thread */ struct CitContext *Context; /* The session context that this thread mught be working on or NULL if none */ @@ -55,11 +56,11 @@ struct CtdlThreadNode{ void *user_args; /* Arguments passed to this threads work function */ long flags; /* Flags that describe this thread */ enum CtdlThreadState state; /* Flag to show state of this thread */ - pthread_mutex_t ThreadMutex; /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */ - pthread_cond_t ThreadCond; /* A condition variable to sync this thread with others */ - pthread_mutex_t SleepMutex; /* A mutex for sleeping */ - pthread_cond_t SleepCond; /* A condition variable for sleeping */ - pthread_attr_t attr; /* Attributes of this thread */ + citthread_mutex_t ThreadMutex; /* A mutex to sync this thread to others if this thread allows (also used for sleeping) */ + citthread_cond_t ThreadCond; /* A condition variable to sync this thread with others */ + citthread_mutex_t SleepMutex; /* A mutex for sleeping */ + citthread_cond_t SleepCond; /* A condition variable for sleeping */ + citthread_attr_t attr; /* Attributes of this thread */ struct timeval start_time; /* Time this thread was started */ struct timeval last_state_change; /* Time when this thread last changed state */ double avg_sleeping; /* Average sleeping time */ @@ -82,7 +83,7 @@ struct ThreadTSD { extern double CtdlThreadLoadAvg; extern double CtdlThreadWorkerAvg; -extern pthread_key_t ThreadKey; +extern citthread_key_t ThreadKey; void ctdl_thread_internal_init_tsd(void); void ctdl_internal_thread_gc (void); -- 2.30.2