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 void *thread = (void *)SDL_CreateThread(fn, filename, data);
105 Sys_PrintfToTerminal("%p thread create %s:%i\n" , thread, filename, fileline);
110 int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline)
114 Sys_PrintfToTerminal("%p thread wait %s:%i\n" , thread, filename, fileline);
116 SDL_WaitThread((SDL_Thread *)thread, &status);
120 // standard barrier implementation using conds and mutexes
121 // see: http://www.howforge.com/implementing-barrier-in-pthreads
129 void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline)
131 volatile barrier_t *b = (volatile barrier_t *) Z_Malloc(sizeof(barrier_t));
133 Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline);
137 b->mutex = Thread_CreateMutex();
138 b->cond = Thread_CreateCond();
142 void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline)
144 volatile barrier_t *b = (volatile barrier_t *) barrier;
146 Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline);
148 Thread_DestroyMutex(b->mutex);
149 Thread_DestroyCond(b->cond);
152 void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline)
154 volatile barrier_t *b = (volatile barrier_t *) barrier;
156 Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline);
158 Thread_LockMutex(b->mutex);
160 if (b->called == b->needed) {
162 Thread_CondBroadcast(b->cond);
165 Thread_CondWait(b->cond, b->mutex);
168 Thread_UnlockMutex(b->mutex);
171 int _Thread_AtomicGet(Thread_Atomic *a, const char *filename, int fileline)
174 Sys_PrintfToTerminal("%p atomic get at %s:%i\n", a, filename, fileline);
176 return SDL_AtomicGet((SDL_atomic_t *)a);
179 int _Thread_AtomicSet(Thread_Atomic *a, int v, const char *filename, int fileline)
182 Sys_PrintfToTerminal("%p atomic set %v at %s:%i\n", a, v, filename, fileline);
184 return SDL_AtomicSet((SDL_atomic_t *)a, v);
187 int _Thread_AtomicAdd(Thread_Atomic *a, int v, const char *filename, int fileline)
190 Sys_PrintfToTerminal("%p atomic add %v at %s:%i\n", a, v, filename, fileline);
192 return SDL_AtomicAdd((SDL_atomic_t *)a, v);
195 void _Thread_AtomicIncRef(Thread_Atomic *a, const char *filename, int fileline)
198 Sys_PrintfToTerminal("%p atomic incref %s:%i\n", a, filename, fileline);
200 SDL_AtomicIncRef((SDL_atomic_t *)a);
203 qboolean _Thread_AtomicDecRef(Thread_Atomic *a, const char *filename, int fileline)
206 Sys_PrintfToTerminal("%p atomic decref %s:%i\n", a, filename, fileline);
208 return SDL_AtomicDecRef((SDL_atomic_t *)a) != SDL_FALSE;
211 qboolean _Thread_AtomicTryLock(Thread_SpinLock *lock, const char *filename, int fileline)
214 Sys_PrintfToTerminal("%p atomic try lock %s:%i\n", lock, filename, fileline);
216 return SDL_AtomicTryLock(lock) != SDL_FALSE;
219 void _Thread_AtomicLock(Thread_SpinLock *lock, const char *filename, int fileline)
222 Sys_PrintfToTerminal("%p atomic lock %s:%i\n", lock, filename, fileline);
224 SDL_AtomicLock(lock);
227 void _Thread_AtomicUnlock(Thread_SpinLock *lock, const char *filename, int fileline)
230 Sys_PrintfToTerminal("%p atomic unlock %s:%i\n", lock, filename, fileline);
232 SDL_AtomicUnlock(lock);