From: havoc Date: Tue, 25 Oct 2011 23:18:48 +0000 (+0000) Subject: thread_pthread now uses recursive mutex (THREADRECURSIVE) X-Git-Tag: xonotic-v0.6.0~163^2~79 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=1e28e9178acc4f23e8750c827360517b31e62c71;p=xonotic%2Fdarkplaces.git thread_pthread now uses recursive mutex (THREADRECURSIVE) THREADDEBUG now prints about all Thread_ functions except Init/Shutdown/HasThreads git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11478 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/thread.h b/thread.h index 216f6906..5ba9be8f 100644 --- a/thread.h +++ b/thread.h @@ -1,11 +1,21 @@ #ifndef THREAD_H +// enable Sys_PrintfToTerminal calls on nearly every threading call //#define THREADDEBUG +// use recursive mutex (non-posix) extensions in thread_pthread +#define THREADRECURSIVE -#define Thread_CreateMutex() (_Thread_CreateMutex(__FILE__, __LINE__)) -#define Thread_DestroyMutex(m) (_Thread_DestroyMutex(m, __FILE__, __LINE__)) -#define Thread_LockMutex(m) (_Thread_LockMutex(m, __FILE__, __LINE__)) -#define Thread_UnlockMutex(m) (_Thread_UnlockMutex(m, __FILE__, __LINE__)) +#define Thread_CreateMutex() (_Thread_CreateMutex(__FILE__, __LINE__)) +#define Thread_DestroyMutex(m) (_Thread_DestroyMutex(m, __FILE__, __LINE__)) +#define Thread_LockMutex(m) (_Thread_LockMutex(m, __FILE__, __LINE__)) +#define Thread_UnlockMutex(m) (_Thread_UnlockMutex(m, __FILE__, __LINE__)) +#define Thread_CreateCond() (_Thread_CreateCond(__FILE__, __LINE__)) +#define Thread_DestroyCond(cond) (_Thread_DestroyCond(cond, __FILE__, __LINE__)) +#define Thread_CondSignal(cond) (_Thread_CondSignal(cond, __FILE__, __LINE__)) +#define Thread_CondBroadcast(cond) (_Thread_CondBroadcast(cond, __FILE__, __LINE__)) +#define Thread_CondWait(cond, mutex) (_Thread_CondWait(cond, mutex, __FILE__, __LINE__)) +#define Thread_CreateThread(fn, data) (_Thread_CreateThread(fn, data, __FILE__, __LINE__)) +#define Thread_WaitThread(thread, retval) (_Thread_WaitThread(thread, retval, __FILE__, __LINE__)) int Thread_Init(void); void Thread_Shutdown(void); @@ -14,13 +24,13 @@ void *_Thread_CreateMutex(const char *filename, int fileline); void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline); int _Thread_LockMutex(void *mutex, const char *filename, int fileline); int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline); -void *Thread_CreateCond(void); -void Thread_DestroyCond(void *cond); -int Thread_CondSignal(void *cond); -int Thread_CondBroadcast(void *cond); -int Thread_CondWait(void *cond, void *mutex); -void *Thread_CreateThread(int (*fn)(void *), void *data); -int Thread_WaitThread(void *thread, int retval); +void *_Thread_CreateCond(const char *filename, int fileline); +void _Thread_DestroyCond(void *cond, const char *filename, int fileline); +int _Thread_CondSignal(void *cond, const char *filename, int fileline); +int _Thread_CondBroadcast(void *cond, const char *filename, int fileline); +int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline); +void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline); +int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline); #endif diff --git a/thread_null.c b/thread_null.c index 58b22bd5..c7d439c9 100644 --- a/thread_null.c +++ b/thread_null.c @@ -17,66 +17,53 @@ qboolean Thread_HasThreads(void) void *_Thread_CreateMutex(const char *filename, int fileline) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p create %s:%i\n" , mutex, filename, fileline); -#endif return NULL; } void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline); -#endif } int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline); -#endif return -1; } int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline); -#endif return -1; } -void *Thread_CreateCond(void) +void *_Thread_CreateCond(const char *filename, int fileline) { return NULL; } -void Thread_DestroyCond(void *cond) +void _Thread_DestroyCond(void *cond, const char *filename, int fileline) { } -int Thread_CondSignal(void *cond) +int _Thread_CondSignal(void *cond, const char *filename, int fileline) { return -1; } -int Thread_CondBroadcast(void *cond) +int _Thread_CondBroadcast(void *cond, const char *filename, int fileline) { return -1; } -int Thread_CondWait(void *cond, void *mutex) +int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline) { return -1; } -void *Thread_CreateThread(int (*fn)(void *), void *data) +void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline) { return NULL; } -int Thread_WaitThread(void *thread, int retval) +int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline) { return retval; } - diff --git a/thread_pthread.c b/thread_pthread.c index c9a11663..9340403e 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -1,8 +1,12 @@ #include "quakedef.h" #include "thread.h" +#ifdef THREADRECURSIVE +#define __USE_UNIX98 #include +#endif #include + int Thread_Init(void) { return 0; @@ -19,11 +23,19 @@ qboolean Thread_HasThreads(void) void *_Thread_CreateMutex(const char *filename, int fileline) { +#ifdef THREADRECURSIVE + pthread_mutexattr_t attr; +#endif pthread_mutex_t *mutexp = (pthread_mutex_t *) Z_Malloc(sizeof(pthread_mutex_t)); #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p create %s:%i\n" , mutexp, filename, fileline); + Sys_PrintfToTerminal("%p mutex create %s:%i\n" , mutexp, filename, fileline); #endif +#ifdef THREADRECURSIVE + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(mutexp, &attr); +#else pthread_mutex_init(mutexp, NULL); +#endif return mutexp; } @@ -31,7 +43,7 @@ void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex destroy %s:%i\n", mutex, filename, fileline); #endif pthread_mutex_destroy(mutexp); Z_Free(mutexp); @@ -41,7 +53,7 @@ int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex lock %s:%i\n" , mutex, filename, fileline); #endif return pthread_mutex_lock(mutexp); } @@ -50,47 +62,65 @@ int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex unlock %s:%i\n" , mutex, filename, fileline); #endif return pthread_mutex_unlock(mutexp); } -void *Thread_CreateCond(void) +void *_Thread_CreateCond(const char *filename, int fileline) { pthread_cond_t *condp = (pthread_cond_t *) Z_Malloc(sizeof(pthread_cond_t)); pthread_cond_init(condp, NULL); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond create %s:%i\n" , condp, filename, fileline); +#endif return condp; } -void Thread_DestroyCond(void *cond) +void _Thread_DestroyCond(void *cond, const char *filename, int fileline) { pthread_cond_t *condp = (pthread_cond_t *) cond; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond destroy %s:%i\n" , cond, filename, fileline); +#endif pthread_cond_destroy(condp); Z_Free(condp); } -int Thread_CondSignal(void *cond) +int _Thread_CondSignal(void *cond, const char *filename, int fileline) { pthread_cond_t *condp = (pthread_cond_t *) cond; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond signal %s:%i\n" , cond, filename, fileline); +#endif return pthread_cond_signal(condp); } -int Thread_CondBroadcast(void *cond) +int _Thread_CondBroadcast(void *cond, const char *filename, int fileline) { pthread_cond_t *condp = (pthread_cond_t *) cond; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond broadcast %s:%i\n" , cond, filename, fileline); +#endif return pthread_cond_broadcast(condp); } -int Thread_CondWait(void *cond, void *mutex) +int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline) { pthread_cond_t *condp = (pthread_cond_t *) cond; pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond wait %s:%i\n" , cond, filename, fileline); +#endif return pthread_cond_wait(condp, mutexp); } -void *Thread_CreateThread(int (*fn)(void *), void *data) +void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline) { pthread_t *threadp = (pthread_t *) Z_Malloc(sizeof(pthread_t)); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p thread create %s:%i\n" , threadp, filename, fileline); +#endif int r = pthread_create(threadp, NULL, (void * (*) (void *)) fn, data); if(r) { @@ -100,10 +130,13 @@ void *Thread_CreateThread(int (*fn)(void *), void *data) return threadp; } -int Thread_WaitThread(void *thread, int retval) +int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline) { pthread_t *threadp = (pthread_t *) thread; void *status = (void *) (intptr_t) retval; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p thread wait %s:%i\n" , thread, filename, fileline); +#endif pthread_join(*threadp, &status); Z_Free(threadp); return (int) (intptr_t) status; diff --git a/thread_sdl.c b/thread_sdl.c index f817c987..64006dbf 100644 --- a/thread_sdl.c +++ b/thread_sdl.c @@ -21,7 +21,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) { void *mutex = SDL_CreateMutex(); #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p create %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex create %s:%i\n" , mutex, filename, fileline); #endif return mutex; } @@ -29,7 +29,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex destroy %s:%i\n", mutex, filename, fileline); #endif SDL_DestroyMutex((SDL_mutex *)mutex); } @@ -37,7 +37,7 @@ void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex lock %s:%i\n" , mutex, filename, fileline); #endif return SDL_LockMutex((SDL_mutex *)mutex); } @@ -45,44 +45,67 @@ int _Thread_LockMutex(void *mutex, const char *filename, int fileline) int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex unlock %s:%i\n" , mutex, filename, fileline); #endif return SDL_UnlockMutex((SDL_mutex *)mutex); } -void *Thread_CreateCond(void) +void *_Thread_CreateCond(const char *filename, int fileline) { - return SDL_CreateCond(); + void *cond = (void *)SDL_CreateCond(); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond create %s:%i\n" , cond, filename, fileline); +#endif + return cond; } -void Thread_DestroyCond(void *cond) +void _Thread_DestroyCond(void *cond, const char *filename, int fileline) { +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond destroy %s:%i\n" , cond, filename, fileline); +#endif SDL_DestroyCond((SDL_cond *)cond); } -int Thread_CondSignal(void *cond) +int _Thread_CondSignal(void *cond, const char *filename, int fileline) { +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond signal %s:%i\n" , cond, filename, fileline); +#endif return SDL_CondSignal((SDL_cond *)cond); } -int Thread_CondBroadcast(void *cond) +int _Thread_CondBroadcast(void *cond, const char *filename, int fileline) { +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond broadcast %s:%i\n" , cond, filename, fileline); +#endif return SDL_CondBroadcast((SDL_cond *)cond); } -int Thread_CondWait(void *cond, void *mutex) +int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline) { +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond wait %s:%i\n" , cond, filename, fileline); +#endif return SDL_CondWait((SDL_cond *)cond, (SDL_mutex *)mutex); } -void *Thread_CreateThread(int (*fn)(void *), void *data) +void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline) { - return SDL_CreateThread(fn, data); + void *thread = (void *)SDL_CreateThread(fn, data); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p thread create %s:%i\n" , thread, filename, fileline); +#endif + return thread; } -int Thread_WaitThread(void *thread, int retval) +int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline) { int status = retval; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p thread wait %s:%i\n" , thread, filename, fileline); +#endif SDL_WaitThread((SDL_Thread *)thread, &status); return status; } diff --git a/thread_win.c b/thread_win.c index a6ae7b3c..893e8306 100644 --- a/thread_win.c +++ b/thread_win.c @@ -20,7 +20,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) { void *mutex = (void *)CreateMutex(NULL, FALSE, NULL); #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p create %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex create %s:%i\n" , mutex, filename, fileline); #endif return mutex; } @@ -28,7 +28,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex destroy %s:%i\n", mutex, filename, fileline); #endif CloseHandle(mutex); } @@ -36,7 +36,7 @@ void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex lock %s:%i\n" , mutex, filename, fileline); #endif return (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) ? -1 : 0; } @@ -44,7 +44,7 @@ int _Thread_LockMutex(void *mutex, const char *filename, int fileline) int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p mutex unlock %s:%i\n" , mutex, filename, fileline); #endif return (ReleaseMutex(mutex) == FALSE) ? -1 : 0; } @@ -102,7 +102,7 @@ typedef struct thread_cond_s } thread_cond_t; -void *Thread_CreateCond(void) +void *_Thread_CreateCond(const char *filename, int fileline) { thread_cond_t *c = (thread_cond_t *)calloc(sizeof(*c), 1); c->mutex = CreateMutex(NULL, FALSE, NULL); @@ -110,21 +110,30 @@ void *Thread_CreateCond(void) c->done = Thread_CreateSemaphore(0); c->waiting = 0; c->signals = 0; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond create %s:%i\n" , c, filename, fileline); +#endif return c; } -void Thread_DestroyCond(void *cond) +void _Thread_DestroyCond(void *cond, const char *filename, int fileline) { thread_cond_t *c = (thread_cond_t *)cond; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond destroy %s:%i\n" , cond, filename, fileline); +#endif Thread_DestroySemaphore(c->sem); Thread_DestroySemaphore(c->done); CloseHandle(c->mutex); } -int Thread_CondSignal(void *cond) +int _Thread_CondSignal(void *cond, const char *filename, int fileline) { thread_cond_t *c = (thread_cond_t *)cond; int n; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond signal %s:%i\n" , cond, filename, fileline); +#endif WaitForSingleObject(c->mutex, INFINITE); n = c->waiting - c->signals; if (n > 0) @@ -138,11 +147,14 @@ int Thread_CondSignal(void *cond) return 0; } -int Thread_CondBroadcast(void *cond) +int _Thread_CondBroadcast(void *cond, const char *filename, int fileline) { thread_cond_t *c = (thread_cond_t *)cond; int i = 0; int n = 0; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond broadcast %s:%i\n" , cond, filename, fileline); +#endif WaitForSingleObject(c->mutex, INFINITE); n = c->waiting - c->signals; if (n > 0) @@ -157,10 +169,13 @@ int Thread_CondBroadcast(void *cond) return 0; } -int Thread_CondWait(void *cond, void *mutex) +int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline) { thread_cond_t *c = (thread_cond_t *)cond; int waitresult; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p cond wait %s:%i\n" , cond, filename, fileline); +#endif WaitForSingleObject(c->mutex, INFINITE); c->waiting++; @@ -202,9 +217,12 @@ unsigned int __stdcall Thread_WrapperFunc(void *d) return w->result; } -void *Thread_CreateThread(int (*fn)(void *), void *data) +void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline) { threadwrapper_t *w = (threadwrapper_t *)calloc(sizeof(*w), 1); +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p thread create %s:%i\n" , w, filename, fileline); +#endif w->fn = fn; w->data = data; w->threadid = 0; @@ -213,9 +231,12 @@ void *Thread_CreateThread(int (*fn)(void *), void *data) return (void *)w; } -int Thread_WaitThread(void *d, int retval) +int _Thread_WaitThread(void *d, int retval, const char *filename, int fileline) { threadwrapper_t *w = (threadwrapper_t *)d; +#ifdef THREADDEBUG + Sys_PrintfToTerminal("%p thread wait %s:%i\n" , w, filename, fileline); +#endif WaitForSingleObject(w->handle, INFINITE); CloseHandle(w->handle); retval = w->result;