]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - thread_sdl.c
Add gl_debug cvar, if enabled the GL context will have GL_ARB_debug_output enabled...
[xonotic/darkplaces.git] / thread_sdl.c
index b49069c3d55d36bb4e544b995042fb2257391374..65d14d7218b05d82e1b48d7d8f5682e7a2dedfd3 100644 (file)
@@ -1,7 +1,7 @@
-#include "quakedef.h"
-#include "thread.h"
 #include <SDL.h>
 #include <SDL_thread.h>
+#include "quakedef.h"
+#include "thread.h"
 
 int Thread_Init(void)
 {
@@ -100,7 +100,7 @@ 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)
 {
-       void *thread = (void *)SDL_CreateThread(fn, data);
+       void *thread = (void *)SDL_CreateThread(fn, filename, data);
 #ifdef THREADDEBUG
        Sys_PrintfToTerminal("%p thread create %s:%i\n"   , thread, filename, fileline);
 #endif
@@ -117,4 +117,53 @@ int _Thread_WaitThread(void *thread, int retval, const char *filename, int filel
        return status;
 }
 
-       
+// 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_PrintfToTerminal("%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_PrintfToTerminal("%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_PrintfToTerminal("%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);
+}