From 2720df965025b75a77656db523606dadbcbb6067 Mon Sep 17 00:00:00 2001
From: RSDuck <rsduck@users.noreply.github.com>
Date: Tue, 3 Nov 2020 19:40:14 +0100
Subject: [PATCH] make platform objects typesafer and add mutex

---
 src/GPU3D_Soft.cpp               |  8 +++---
 src/Platform.h                   | 25 ++++++++++++------
 src/frontend/qt_sdl/Platform.cpp | 45 +++++++++++++++++++++++++-------
 3 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/src/GPU3D_Soft.cpp b/src/GPU3D_Soft.cpp
index e9d8e75f..7ee9e5df 100644
--- a/src/GPU3D_Soft.cpp
+++ b/src/GPU3D_Soft.cpp
@@ -61,12 +61,12 @@ bool Enabled;
 // threading
 
 bool Threaded;
-void* RenderThread;
+Platform::Thread* RenderThread;
 bool RenderThreadRunning;
 bool RenderThreadRendering;
-void* Sema_RenderStart;
-void* Sema_RenderDone;
-void* Sema_ScanlineCount;
+Platform::Semaphore* Sema_RenderStart;
+Platform::Semaphore* Sema_RenderDone;
+Platform::Semaphore* Sema_ScanlineCount;
 
 void RenderThreadFunc();
 
diff --git a/src/Platform.h b/src/Platform.h
index fea98dd5..deb37853 100644
--- a/src/Platform.h
+++ b/src/Platform.h
@@ -67,15 +67,24 @@ inline bool LocalFileExists(const char* name)
     return true;
 }
 
-void* Thread_Create(void (*func)());
-void Thread_Free(void* thread);
-void Thread_Wait(void* thread);
+struct Thread;
+Thread* Thread_Create(void (*func)());
+void Thread_Free(Thread* thread);
+void Thread_Wait(Thread* thread);
 
-void* Semaphore_Create();
-void Semaphore_Free(void* sema);
-void Semaphore_Reset(void* sema);
-void Semaphore_Wait(void* sema);
-void Semaphore_Post(void* sema);
+struct Semaphore;
+Semaphore* Semaphore_Create();
+void Semaphore_Free(Semaphore* sema);
+void Semaphore_Reset(Semaphore* sema);
+void Semaphore_Wait(Semaphore* sema);
+void Semaphore_Post(Semaphore* sema);
+
+struct Mutex;
+Mutex* Mutex_Create();
+void Mutex_Free(Mutex* mutex);
+void Mutex_Lock(Mutex* mutex);
+void Mutex_Unlock(Mutex* mutex);
+bool Mutex_TryLock(Mutex* mutex);
 
 void* GL_GetProcAddress(const char* proc);
 
diff --git a/src/frontend/qt_sdl/Platform.cpp b/src/frontend/qt_sdl/Platform.cpp
index a716feb6..a51a9854 100644
--- a/src/frontend/qt_sdl/Platform.cpp
+++ b/src/frontend/qt_sdl/Platform.cpp
@@ -23,6 +23,7 @@
 #include <QDir>
 #include <QThread>
 #include <QSemaphore>
+#include <QMutex>
 #include <QOpenGLContext>
 
 #include "Platform.h"
@@ -187,53 +188,77 @@ FILE* OpenLocalFile(const char* path, const char* mode)
     return OpenFile(fullpath.toUtf8(), mode, mode[0] != 'w');
 }
 
-void* Thread_Create(void (* func)())
+Thread* Thread_Create(void (* func)())
 {
     QThread* t = QThread::create(func);
     t->start();
-    return (void*) t;
+    return (Thread*) t;
 }
 
-void Thread_Free(void* thread)
+void Thread_Free(Thread* thread)
 {
     QThread* t = (QThread*) thread;
     t->terminate();
     delete t;
 }
 
-void Thread_Wait(void* thread)
+void Thread_Wait(Thread* thread)
 {
     ((QThread*) thread)->wait();
 }
 
 
-void* Semaphore_Create()
+Semaphore* Semaphore_Create()
 {
-    return new QSemaphore();
+    return (Semaphore*)new QSemaphore();
 }
 
-void Semaphore_Free(void* sema)
+void Semaphore_Free(Semaphore* sema)
 {
     delete (QSemaphore*) sema;
 }
 
-void Semaphore_Reset(void* sema)
+void Semaphore_Reset(Semaphore* sema)
 {
     QSemaphore* s = (QSemaphore*) sema;
 
     s->acquire(s->available());
 }
 
-void Semaphore_Wait(void* sema)
+void Semaphore_Wait(Semaphore* sema)
 {
     ((QSemaphore*) sema)->acquire();
 }
 
-void Semaphore_Post(void* sema)
+void Semaphore_Post(Semaphore* sema)
 {
     ((QSemaphore*) sema)->release();
 }
 
+Mutex* Mutex_Create()
+{
+    return (Mutex*)new QMutex();
+}
+
+void Mutex_Free(Mutex* mutex)
+{
+    delete (QMutex*) mutex;
+}
+
+void Mutex_Lock(Mutex* mutex)
+{
+    ((QMutex*) mutex)->lock();
+}
+
+void Mutex_Unlock(Mutex* mutex)
+{
+    ((QMutex*) mutex)->unlock();
+}
+
+bool Mutex_TryLock(Mutex* mutex)
+{
+    return ((QMutex*) mutex)->try_lock();
+}
 
 void* GL_GetProcAddress(const char* proc)
 {