X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=thread_win.c;h=1cfad9a40031eabbd32d9412e45a4b5976de680d;hp=a882f80d00f14d1f7df1deec61a4a82ed08863c3;hb=0dfd3a30839fefb7c1d817618354350f4efd1131;hpb=276e463972834a60d2c8cc23de2a65af8589a8c6 diff --git a/thread_win.c b/thread_win.c index a882f80d..1cfad9a4 100644 --- a/thread_win.c +++ b/thread_win.c @@ -4,6 +4,9 @@ int Thread_Init(void) { +#ifdef THREADDISABLE + Con_Printf("Threading disabled in this build\n"); +#endif return 0; } @@ -11,16 +14,20 @@ void Thread_Shutdown(void) { } -qboolean Thread_HasThreads(void) +qbool Thread_HasThreads(void) { +#ifdef THREADDISABLE + return false; +#else return true; +#endif } void *_Thread_CreateMutex(const char *filename, int fileline) { void *mutex = (void *)CreateMutex(NULL, FALSE, NULL); #ifdef THREADDEBUG - printf("%p create %s:%i\n" , mutex, filename, fileline); + Sys_Printf("%p mutex create %s:%i\n" , mutex, filename, fileline); #endif return mutex; } @@ -28,7 +35,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p destroy %s:%i\n", mutex, filename, fileline); + Sys_Printf("%p mutex destroy %s:%i\n", mutex, filename, fileline); #endif CloseHandle(mutex); } @@ -36,7 +43,7 @@ void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p lock %s:%i\n" , mutex, filename, fileline); + Sys_Printf("%p mutex lock %s:%i\n" , mutex, filename, fileline); #endif return (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) ? -1 : 0; } @@ -44,9 +51,9 @@ int _Thread_LockMutex(void *mutex, const char *filename, int fileline) int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p unlock %s:%i\n" , mutex, filename, fileline); + Sys_Printf("%p mutex unlock %s:%i\n" , mutex, filename, fileline); #endif - return (ReleaseMutex(mutex) == FALSE) ? -1 : 0; + return (ReleaseMutex(mutex) == false) ? -1 : 0; } typedef struct thread_semaphore_s @@ -102,7 +109,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 +117,30 @@ void *Thread_CreateCond(void) c->done = Thread_CreateSemaphore(0); c->waiting = 0; c->signals = 0; +#ifdef THREADDEBUG + Sys_Printf("%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_Printf("%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_Printf("%p cond signal %s:%i\n" , cond, filename, fileline); +#endif WaitForSingleObject(c->mutex, INFINITE); n = c->waiting - c->signals; if (n > 0) @@ -138,11 +154,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_Printf("%p cond broadcast %s:%i\n" , cond, filename, fileline); +#endif WaitForSingleObject(c->mutex, INFINITE); n = c->waiting - c->signals; if (n > 0) @@ -157,10 +176,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_Printf("%p cond wait %s:%i\n" , cond, filename, fileline); +#endif WaitForSingleObject(c->mutex, INFINITE); c->waiting++; @@ -202,9 +224,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_Printf("%p thread create %s:%i\n" , w, filename, fileline); +#endif w->fn = fn; w->data = data; w->threadid = 0; @@ -213,12 +238,66 @@ 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_Printf("%p thread wait %s:%i\n" , w, filename, fileline); +#endif WaitForSingleObject(w->handle, INFINITE); CloseHandle(w->handle); retval = w->result; free(w); return retval; } + +// standard barrier implementation using conds and mutexes +// see: http://www.howforge.com/implementing-barrier-in-pthreads +typedef struct { + unsigned int needed; + unsigned int called; + void *mutex; + void *cond; +} barrier_t; + +void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +{ + volatile barrier_t *b = (volatile barrier_t *) Z_Malloc(sizeof(barrier_t)); +#ifdef THREADDEBUG + Sys_Printf("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); +#endif + b->needed = count; + b->called = 0; + b->mutex = Thread_CreateMutex(); + b->cond = Thread_CreateCond(); + return (void *) b; +} + +void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +{ + volatile barrier_t *b = (volatile barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_Printf("%p barrier destroy %s:%i\n", b, filename, fileline); +#endif + Thread_DestroyMutex(b->mutex); + Thread_DestroyCond(b->cond); +} + +void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +{ + volatile barrier_t *b = (volatile barrier_t *) barrier; +#ifdef THREADDEBUG + Sys_Printf("%p barrier wait %s:%i\n", b, filename, fileline); +#endif + Thread_LockMutex(b->mutex); + b->called++; + if (b->called == b->needed) { + b->called = 0; + Thread_CondBroadcast(b->cond); + } else { + do { + Thread_CondWait(b->cond, b->mutex); + } while(b->called); + } + Thread_UnlockMutex(b->mutex); +}