2 #include <SDL_thread.h>
9 Con_Printf("Threading disabled in this build\n");
14 void Thread_Shutdown(void)
18 qboolean Thread_HasThreads(void)
27 void *_Thread_CreateMutex(const char *filename, int fileline)
29 void *mutex = SDL_CreateMutex();
31 Sys_PrintfToTerminal("%p mutex create %s:%i\n" , mutex, filename, fileline);
36 void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline)
39 Sys_PrintfToTerminal("%p mutex destroy %s:%i\n", mutex, filename, fileline);
41 SDL_DestroyMutex((SDL_mutex *)mutex);
44 int _Thread_LockMutex(void *mutex, const char *filename, int fileline)
47 Sys_PrintfToTerminal("%p mutex lock %s:%i\n" , mutex, filename, fileline);
49 return SDL_LockMutex((SDL_mutex *)mutex);
52 int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline)
55 Sys_PrintfToTerminal("%p mutex unlock %s:%i\n" , mutex, filename, fileline);
57 return SDL_UnlockMutex((SDL_mutex *)mutex);
60 void *_Thread_CreateCond(const char *filename, int fileline)
62 void *cond = (void *)SDL_CreateCond();
64 Sys_PrintfToTerminal("%p cond create %s:%i\n" , cond, filename, fileline);
69 void _Thread_DestroyCond(void *cond, const char *filename, int fileline)
72 Sys_PrintfToTerminal("%p cond destroy %s:%i\n" , cond, filename, fileline);
74 SDL_DestroyCond((SDL_cond *)cond);
77 int _Thread_CondSignal(void *cond, const char *filename, int fileline)
80 Sys_PrintfToTerminal("%p cond signal %s:%i\n" , cond, filename, fileline);
82 return SDL_CondSignal((SDL_cond *)cond);
85 int _Thread_CondBroadcast(void *cond, const char *filename, int fileline)
88 Sys_PrintfToTerminal("%p cond broadcast %s:%i\n" , cond, filename, fileline);
90 return SDL_CondBroadcast((SDL_cond *)cond);
93 int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline)
96 Sys_PrintfToTerminal("%p cond wait %s:%i\n" , cond, filename, fileline);
98 return SDL_CondWait((SDL_cond *)cond, (SDL_mutex *)mutex);
101 void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline)
103 #if SDL_MAJOR_VERSION == 1
104 void *thread = (void *)SDL_CreateThread(fn, data);
106 void *thread = (void *)SDL_CreateThread(fn, filename, data);
109 Sys_PrintfToTerminal("%p thread create %s:%i\n" , thread, filename, fileline);
114 int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline)
118 Sys_PrintfToTerminal("%p thread wait %s:%i\n" , thread, filename, fileline);
120 SDL_WaitThread((SDL_Thread *)thread, &status);
124 // standard barrier implementation using conds and mutexes
125 // see: http://www.howforge.com/implementing-barrier-in-pthreads
133 void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline)
135 volatile barrier_t *b = (volatile barrier_t *) Z_Malloc(sizeof(barrier_t));
137 Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline);
141 b->mutex = Thread_CreateMutex();
142 b->cond = Thread_CreateCond();
146 void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline)
148 volatile barrier_t *b = (volatile barrier_t *) barrier;
150 Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline);
152 Thread_DestroyMutex(b->mutex);
153 Thread_DestroyCond(b->cond);
156 void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline)
158 volatile barrier_t *b = (volatile barrier_t *) barrier;
160 Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline);
162 Thread_LockMutex(b->mutex);
164 if (b->called == b->needed) {
166 Thread_CondBroadcast(b->cond);
169 Thread_CondWait(b->cond, b->mutex);
172 Thread_UnlockMutex(b->mutex);