From: Izy Date: Mon, 17 Nov 2014 14:21:17 +0000 (+0000) Subject: misc threads patches by Izy X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=ddfc5e3f21404d71ba36e9b0873044a87b3cc85c;p=xonotic%2Fdarkplaces.git misc threads patches by Izy Provided by mynick1 on IRC, whitespace- and indent-cleaned. Patch description for threads: - Added the missing header guard in the C header file - Fixed some memory leaks (by adding 5 missing calls to "free") - Removed the poor's man :( debugging system - Added the "set thread priority low/normal/high" functions - Made WaitThread looking more similar to the SDL interface --- diff --git a/sv_main.c b/sv_main.c index d17d20a1..ed07154e 100644 --- a/sv_main.c +++ b/sv_main.c @@ -4041,7 +4041,7 @@ void SV_StopThread(void) if (!svs.threaded) return; svs.threadstop = true; - Thread_WaitThread(svs.thread, 0); + Thread_WaitThread(svs.thread, NULL); Thread_DestroyMutex(svs.threadmutex); svs.threaded = false; } diff --git a/thread.h b/thread.h index 7f590a47..8534264e 100644 --- a/thread.h +++ b/thread.h @@ -1,42 +1,31 @@ #ifndef THREAD_H +#define THREAD_H -// enable Sys_PrintfToTerminal calls on nearly every threading call -//#define THREADDEBUG +// disable threads //#define THREADDISABLE + // 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_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__)) -#define Thread_CreateBarrier(count) (_Thread_CreateBarrier(count, __FILE__, __LINE__)) -#define Thread_DestroyBarrier(barrier) (_Thread_DestroyBarrier(barrier, __FILE__, __LINE__)) -#define Thread_WaitBarrier(barrier) (_Thread_WaitBarrier(barrier, __FILE__, __LINE__)) - int Thread_Init(void); void Thread_Shutdown(void); qboolean Thread_HasThreads(void); -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(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); -void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline); -void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline); -void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline); +void *Thread_CreateMutex(void); +void Thread_DestroyMutex(void *mutex); +int Thread_LockMutex(void *mutex); +int Thread_UnlockMutex(void *mutex); +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); +void Thread_WaitThread(void *thread, int *retval); +void *Thread_CreateBarrier(unsigned int count); +void Thread_DestroyBarrier(void *barrier); +void Thread_WaitBarrier(void *barrier); +void Thread_SetThreadPriorityLow(void); +void Thread_SetThreadPriorityNormal(void); +void Thread_SetThreadPriorityHigh(void); #endif diff --git a/thread_null.c b/thread_null.c index 35c4e4b5..4e9c9303 100644 --- a/thread_null.c +++ b/thread_null.c @@ -15,68 +15,82 @@ qboolean Thread_HasThreads(void) return false; } -void *_Thread_CreateMutex(const char *filename, int fileline) +void *Thread_CreateMutex(void) { return NULL; } -void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) +void Thread_DestroyMutex(void *mutex) { } -int _Thread_LockMutex(void *mutex, const char *filename, int fileline) +int Thread_LockMutex(void *mutex) { return -1; } -int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) +int Thread_UnlockMutex(void *mutex) { return -1; } -void *_Thread_CreateCond(const char *filename, int fileline) +void *Thread_CreateCond(void) { return NULL; } -void _Thread_DestroyCond(void *cond, const char *filename, int fileline) +void Thread_DestroyCond(void *cond) { } -int _Thread_CondSignal(void *cond, const char *filename, int fileline) +int Thread_CondSignal(void *cond) { return -1; } -int _Thread_CondBroadcast(void *cond, const char *filename, int fileline) +int Thread_CondBroadcast(void *cond) { return -1; } -int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline) +int Thread_CondWait(void *cond, void *mutex) { return -1; } -void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline) +void *Thread_CreateThread(int (*fn)(void *), void *data) { return NULL; } -int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline) +void Thread_WaitThread(void *thread, int *retval) { - return retval; } -void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +void *Thread_CreateBarrier(unsigned int count) { return NULL; } -void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +void Thread_DestroyBarrier(void *barrier) { } -void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +void Thread_WaitBarrier(void *barrier) { } + +void Thread_SetThreadPriorityLow(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ +} + +void Thread_SetThreadPriorityNormal(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ +} + +void Thread_SetThreadPriorityHigh(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ +} diff --git a/thread_pthread.c b/thread_pthread.c index 7bded93d..d8b1acdb 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -21,15 +21,12 @@ qboolean Thread_HasThreads(void) return true; } -void *_Thread_CreateMutex(const char *filename, int fileline) +void *Thread_CreateMutex(void) { #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 mutex create %s:%i\n" , mutexp, filename, fileline); -#endif + pthread_mutex_t *mutexp = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); #ifdef THREADRECURSIVE pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); @@ -41,135 +38,102 @@ void *_Thread_CreateMutex(const char *filename, int fileline) return mutexp; } -void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) +void Thread_DestroyMutex(void *mutex) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex destroy %s:%i\n", mutex, filename, fileline); -#endif pthread_mutex_destroy(mutexp); - Z_Free(mutexp); + free(mutexp); } -int _Thread_LockMutex(void *mutex, const char *filename, int fileline) +int Thread_LockMutex(void *mutex) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex lock %s:%i\n" , mutex, filename, fileline); -#endif return pthread_mutex_lock(mutexp); } -int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) +int Thread_UnlockMutex(void *mutex) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex unlock %s:%i\n" , mutex, filename, fileline); -#endif return pthread_mutex_unlock(mutexp); } -void *_Thread_CreateCond(const char *filename, int fileline) +void *Thread_CreateCond(void) { - pthread_cond_t *condp = (pthread_cond_t *) Z_Malloc(sizeof(pthread_cond_t)); + pthread_cond_t *condp = (pthread_cond_t *) 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, const char *filename, int fileline) +void Thread_DestroyCond(void *cond) { 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); + free(condp); } -int _Thread_CondSignal(void *cond, const char *filename, int fileline) +int Thread_CondSignal(void *cond) { 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, const char *filename, int fileline) +int Thread_CondBroadcast(void *cond) { 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, const char *filename, int fileline) +int Thread_CondWait(void *cond, void *mutex) { 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, const char *filename, int fileline) +void *Thread_CreateThread(int (*fn)(void *), void *data) { - pthread_t *threadp = (pthread_t *) Z_Malloc(sizeof(pthread_t)); -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p thread create %s:%i\n" , threadp, filename, fileline); -#endif + pthread_t *threadp = (pthread_t *) malloc(sizeof(pthread_t)); int r = pthread_create(threadp, NULL, (void * (*) (void *)) fn, data); if(r) { - Z_Free(threadp); + free(threadp); return NULL; } return threadp; } -int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline) +void Thread_WaitThread(void *thread, int *retval) { + union + { + void *p; + int i; + } status; 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; + pthread_join(*threadp, &status.p); + if(retval) + *retval = status.i; + free(threadp); } #ifdef PTHREAD_BARRIER_SERIAL_THREAD -void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +void *Thread_CreateBarrier(unsigned int count) { - pthread_barrier_t *b = (pthread_barrier_t *) Z_Malloc(sizeof(pthread_barrier_t)); -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); -#endif + pthread_barrier_t *b = (pthread_barrier_t *) malloc(sizeof(pthread_barrier_t)); pthread_barrier_init(b, NULL, count); return (void *) b; } -void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +void Thread_DestroyBarrier(void *barrier) { pthread_barrier_t *b = (pthread_barrier_t *) barrier; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline); -#endif pthread_barrier_destroy(b); + free(b); // Izy's Patch } -void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +void Thread_WaitBarrier(void *barrier) { pthread_barrier_t *b = (pthread_barrier_t *) barrier; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline); -#endif pthread_barrier_wait(b); } #else @@ -182,12 +146,9 @@ typedef struct { void *cond; } barrier_t; -void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +void *Thread_CreateBarrier(unsigned int count) { - volatile barrier_t *b = (volatile barrier_t *) Z_Malloc(sizeof(barrier_t)); -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); -#endif + volatile barrier_t *b = (volatile barrier_t *) malloc(sizeof(barrier_t)); b->needed = count; b->called = 0; b->mutex = Thread_CreateMutex(); @@ -195,22 +156,17 @@ void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileli return (void *) b; } -void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +void Thread_DestroyBarrier(void *barrier) { volatile barrier_t *b = (volatile barrier_t *) barrier; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline); -#endif Thread_DestroyMutex(b->mutex); Thread_DestroyCond(b->cond); + free((void*)b); // Izy's Patch } -void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +void Thread_WaitBarrier(void *barrier) { volatile barrier_t *b = (volatile barrier_t *) barrier; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline); -#endif Thread_LockMutex(b->mutex); b->called++; if (b->called == b->needed) { @@ -224,3 +180,53 @@ void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) Thread_UnlockMutex(b->mutex); } #endif + + +void Thread_SetThreadPriorityLow(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ + pthread_attr_t attr; + int policy = 0; + int min_prio_for_policy; + pthread_attr_init(&attr); + pthread_attr_getschedpolicy(&attr, &policy); + min_prio_for_policy = sched_get_priority_min(policy); + pthread_setschedprio(pthread_self(), min_prio_for_policy); + pthread_attr_destroy(&attr); +} + +void Thread_SetThreadPriorityNormal(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ + pthread_attr_t attr; + int policy = 0; + int min_prio_for_policy; + int max_prio_for_policy; + int normal_prio_for_policy; + pthread_attr_init(&attr); + pthread_attr_getschedpolicy(&attr, &policy); + min_prio_for_policy = sched_get_priority_min(policy); + max_prio_for_policy = sched_get_priority_max(policy); + if(min_prio_for_policy >= 0 && max_prio_for_policy >= 0) + normal_prio_for_policy = (max_prio_for_policy-min_prio_for_policy)/2+min_prio_for_policy; + else + if(min_prio_for_policy < 0 && max_prio_for_policy <= 0) + normal_prio_for_policy = (min_prio_for_policy-max_prio_for_policy)/2+max_prio_for_policy; + else + normal_prio_for_policy = (max_prio_for_policy+min_prio_for_policy)/2; + pthread_setschedprio(pthread_self(), normal_prio_for_policy); + pthread_attr_destroy(&attr); +} + +void Thread_SetThreadPriorityHigh(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ + pthread_attr_t attr; + int policy = 0; + int max_prio_for_policy; + pthread_attr_init(&attr); + pthread_attr_getschedpolicy(&attr, &policy); + max_prio_for_policy = sched_get_priority_max(policy); + pthread_setschedprio(pthread_self(), max_prio_for_policy); + pthread_attr_destroy(&attr); +} diff --git a/thread_sdl.c b/thread_sdl.c index 4f58f71d..bb69164e 100644 --- a/thread_sdl.c +++ b/thread_sdl.c @@ -24,101 +24,66 @@ qboolean Thread_HasThreads(void) #endif } -void *_Thread_CreateMutex(const char *filename, int fileline) +void *Thread_CreateMutex(void) { void *mutex = SDL_CreateMutex(); -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex create %s:%i\n" , mutex, filename, fileline); -#endif return mutex; } -void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) +void Thread_DestroyMutex(void *mutex) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex destroy %s:%i\n", mutex, filename, fileline); -#endif SDL_DestroyMutex((SDL_mutex *)mutex); } -int _Thread_LockMutex(void *mutex, const char *filename, int fileline) +int Thread_LockMutex(void *mutex) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex lock %s:%i\n" , mutex, filename, fileline); -#endif return SDL_LockMutex((SDL_mutex *)mutex); } -int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) +int Thread_UnlockMutex(void *mutex) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex unlock %s:%i\n" , mutex, filename, fileline); -#endif return SDL_UnlockMutex((SDL_mutex *)mutex); } -void *_Thread_CreateCond(const char *filename, int fileline) +void *Thread_CreateCond(void) { 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, const char *filename, int fileline) +void Thread_DestroyCond(void *cond) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p cond destroy %s:%i\n" , cond, filename, fileline); -#endif SDL_DestroyCond((SDL_cond *)cond); } -int _Thread_CondSignal(void *cond, const char *filename, int fileline) +int Thread_CondSignal(void *cond) { -#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, const char *filename, int fileline) +int Thread_CondBroadcast(void *cond) { -#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, const char *filename, int fileline) +int Thread_CondWait(void *cond, void *mutex) { -#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, const char *filename, int fileline) +void *Thread_CreateThread(int (*fn)(void *), void *data) { #if SDL_MAJOR_VERSION == 1 void *thread = (void *)SDL_CreateThread(fn, data); #else - void *thread = (void *)SDL_CreateThread(fn, filename, data); -#endif -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p thread create %s:%i\n" , thread, filename, fileline); + void *thread = (void *)SDL_CreateThread(fn, "", data); #endif return thread; } -int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline) +void Thread_WaitThread(void *thread, int *retval) { - 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; + SDL_WaitThread((SDL_Thread *)thread, retval); } // standard barrier implementation using conds and mutexes @@ -130,12 +95,9 @@ typedef struct { void *cond; } barrier_t; -void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +void *Thread_CreateBarrier(unsigned int count) { - volatile barrier_t *b = (volatile barrier_t *) Z_Malloc(sizeof(barrier_t)); -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); -#endif + volatile barrier_t *b = (volatile barrier_t *) malloc(sizeof(barrier_t)); b->needed = count; b->called = 0; b->mutex = Thread_CreateMutex(); @@ -143,22 +105,17 @@ void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileli return (void *) b; } -void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +void Thread_DestroyBarrier(void *barrier) { volatile barrier_t *b = (volatile barrier_t *) barrier; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline); -#endif Thread_DestroyMutex(b->mutex); Thread_DestroyCond(b->cond); + free((void*)b); // Izy's Patch } -void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +void Thread_WaitBarrier(void *barrier) { volatile barrier_t *b = (volatile barrier_t *) barrier; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline); -#endif Thread_LockMutex(b->mutex); b->called++; if (b->called == b->needed) { @@ -171,3 +128,27 @@ void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) } Thread_UnlockMutex(b->mutex); } + +void Thread_SetThreadPriorityLow(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ +#if SDL_VERSION_ATLEAST(2,0,3) + SDL_SetThreadPriority(SDL_THREAD_PRIORITY_LOW); +#endif +} + +void Thread_SetThreadPriorityNormal(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ +#if SDL_VERSION_ATLEAST(2,0,3) + SDL_SetThreadPriority(SDL_THREAD_PRIORITY_NORMAL); +#endif +} + +void Thread_SetThreadPriorityHigh(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ +#if SDL_VERSION_ATLEAST(2,0,3) + SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); +#endif +} diff --git a/thread_win.c b/thread_win.c index 8564ca19..adb22ca1 100644 --- a/thread_win.c +++ b/thread_win.c @@ -23,36 +23,24 @@ qboolean Thread_HasThreads(void) #endif } -void *_Thread_CreateMutex(const char *filename, int fileline) +void *Thread_CreateMutex(void) { void *mutex = (void *)CreateMutex(NULL, FALSE, NULL); -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex create %s:%i\n" , mutex, filename, fileline); -#endif return mutex; } -void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) +void Thread_DestroyMutex(void *mutex) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex destroy %s:%i\n", mutex, filename, fileline); -#endif CloseHandle(mutex); } -int _Thread_LockMutex(void *mutex, const char *filename, int fileline) +int Thread_LockMutex(void *mutex) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex lock %s:%i\n" , mutex, filename, fileline); -#endif return (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) ? -1 : 0; } -int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) +int Thread_UnlockMutex(void *mutex) { -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p mutex unlock %s:%i\n" , mutex, filename, fileline); -#endif return (ReleaseMutex(mutex) == FALSE) ? -1 : 0; } @@ -65,7 +53,7 @@ thread_semaphore_t; static thread_semaphore_t *Thread_CreateSemaphore(unsigned int v) { - thread_semaphore_t *s = (thread_semaphore_t *)calloc(sizeof(*s), 1); + thread_semaphore_t *s = (thread_semaphore_t *)malloc(sizeof(thread_semaphore_t)); s->semaphore = CreateSemaphore(NULL, v, 32768, NULL); s->value = v; return s; @@ -109,38 +97,30 @@ typedef struct thread_cond_s } thread_cond_t; -void *_Thread_CreateCond(const char *filename, int fileline) +void *Thread_CreateCond(void) { - thread_cond_t *c = (thread_cond_t *)calloc(sizeof(*c), 1); + thread_cond_t *c = (thread_cond_t *)malloc(sizeof(thread_cond_t)); c->mutex = CreateMutex(NULL, FALSE, NULL); c->sem = Thread_CreateSemaphore(0); 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, const char *filename, int fileline) +void Thread_DestroyCond(void *cond) { 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); + free(c); // Izy's Patch } -int _Thread_CondSignal(void *cond, const char *filename, int fileline) +int Thread_CondSignal(void *cond) { 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) @@ -154,14 +134,11 @@ int _Thread_CondSignal(void *cond, const char *filename, int fileline) return 0; } -int _Thread_CondBroadcast(void *cond, const char *filename, int fileline) +int Thread_CondBroadcast(void *cond) { 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) @@ -176,13 +153,10 @@ int _Thread_CondBroadcast(void *cond, const char *filename, int fileline) return 0; } -int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline) +int Thread_CondWait(void *cond, void *mutex) { 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++; @@ -224,12 +198,9 @@ unsigned int __stdcall Thread_WrapperFunc(void *d) return w->result; } -void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline) +void *Thread_CreateThread(int (*fn)(void *), void *data) { - threadwrapper_t *w = (threadwrapper_t *)calloc(sizeof(*w), 1); -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p thread create %s:%i\n" , w, filename, fileline); -#endif + threadwrapper_t *w = (threadwrapper_t *)malloc(sizeof(threadwrapper_t)); w->fn = fn; w->data = data; w->threadid = 0; @@ -238,17 +209,14 @@ void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, return (void *)w; } -int _Thread_WaitThread(void *d, int retval, const char *filename, int fileline) +void Thread_WaitThread(void *d, int *retval) { 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; + if(retval) + *retval = w->result; free(w); - return retval; } // standard barrier implementation using conds and mutexes @@ -260,12 +228,9 @@ typedef struct { void *cond; } barrier_t; -void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline) +void *Thread_CreateBarrier(unsigned int count) { - volatile barrier_t *b = (volatile barrier_t *) Z_Malloc(sizeof(barrier_t)); -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline); -#endif + volatile barrier_t *b = (volatile barrier_t *) malloc(sizeof(barrier_t)); b->needed = count; b->called = 0; b->mutex = Thread_CreateMutex(); @@ -273,22 +238,17 @@ void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileli return (void *) b; } -void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline) +void Thread_DestroyBarrier(void *barrier) { volatile barrier_t *b = (volatile barrier_t *) barrier; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline); -#endif Thread_DestroyMutex(b->mutex); Thread_DestroyCond(b->cond); + free((void*)b); // Izy's Patch } -void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) +void Thread_WaitBarrier(void *barrier) { volatile barrier_t *b = (volatile barrier_t *) barrier; -#ifdef THREADDEBUG - Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline); -#endif Thread_LockMutex(b->mutex); b->called++; if (b->called == b->needed) { @@ -301,3 +261,22 @@ void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline) } Thread_UnlockMutex(b->mutex); } + + +void Thread_SetThreadPriorityLow(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST); +} + +void Thread_SetThreadPriorityNormal(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); +} + +void Thread_SetThreadPriorityHigh(void) +{ + /* Thread Priority added by Izy (izy from http://www.izysoftware.com/) */ + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); +}