make platform objects typesafer and add mutex

This commit is contained in:
RSDuck 2020-11-03 19:40:14 +01:00
parent 052079afeb
commit 2720df9650
3 changed files with 56 additions and 22 deletions

View File

@ -61,12 +61,12 @@ bool Enabled;
// threading // threading
bool Threaded; bool Threaded;
void* RenderThread; Platform::Thread* RenderThread;
bool RenderThreadRunning; bool RenderThreadRunning;
bool RenderThreadRendering; bool RenderThreadRendering;
void* Sema_RenderStart; Platform::Semaphore* Sema_RenderStart;
void* Sema_RenderDone; Platform::Semaphore* Sema_RenderDone;
void* Sema_ScanlineCount; Platform::Semaphore* Sema_ScanlineCount;
void RenderThreadFunc(); void RenderThreadFunc();

View File

@ -67,15 +67,24 @@ inline bool LocalFileExists(const char* name)
return true; return true;
} }
void* Thread_Create(void (*func)()); struct Thread;
void Thread_Free(void* thread); Thread* Thread_Create(void (*func)());
void Thread_Wait(void* thread); void Thread_Free(Thread* thread);
void Thread_Wait(Thread* thread);
void* Semaphore_Create(); struct Semaphore;
void Semaphore_Free(void* sema); Semaphore* Semaphore_Create();
void Semaphore_Reset(void* sema); void Semaphore_Free(Semaphore* sema);
void Semaphore_Wait(void* sema); void Semaphore_Reset(Semaphore* sema);
void Semaphore_Post(void* 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); void* GL_GetProcAddress(const char* proc);

View File

@ -23,6 +23,7 @@
#include <QDir> #include <QDir>
#include <QThread> #include <QThread>
#include <QSemaphore> #include <QSemaphore>
#include <QMutex>
#include <QOpenGLContext> #include <QOpenGLContext>
#include "Platform.h" #include "Platform.h"
@ -187,53 +188,77 @@ FILE* OpenLocalFile(const char* path, const char* mode)
return OpenFile(fullpath.toUtf8(), mode, mode[0] != 'w'); return OpenFile(fullpath.toUtf8(), mode, mode[0] != 'w');
} }
void* Thread_Create(void (* func)()) Thread* Thread_Create(void (* func)())
{ {
QThread* t = QThread::create(func); QThread* t = QThread::create(func);
t->start(); t->start();
return (void*) t; return (Thread*) t;
} }
void Thread_Free(void* thread) void Thread_Free(Thread* thread)
{ {
QThread* t = (QThread*) thread; QThread* t = (QThread*) thread;
t->terminate(); t->terminate();
delete t; delete t;
} }
void Thread_Wait(void* thread) void Thread_Wait(Thread* thread)
{ {
((QThread*) thread)->wait(); ((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; delete (QSemaphore*) sema;
} }
void Semaphore_Reset(void* sema) void Semaphore_Reset(Semaphore* sema)
{ {
QSemaphore* s = (QSemaphore*) sema; QSemaphore* s = (QSemaphore*) sema;
s->acquire(s->available()); s->acquire(s->available());
} }
void Semaphore_Wait(void* sema) void Semaphore_Wait(Semaphore* sema)
{ {
((QSemaphore*) sema)->acquire(); ((QSemaphore*) sema)->acquire();
} }
void Semaphore_Post(void* sema) void Semaphore_Post(Semaphore* sema)
{ {
((QSemaphore*) sema)->release(); ((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) void* GL_GetProcAddress(const char* proc)
{ {