0
0
Fork 0
mirror of https://github.com/dhewm/dhewm3.git synced 2025-03-21 10:11:01 +00:00

Add SysIsMainThread() function

returns true if called in the main thread
This commit is contained in:
Daniel Gibson 2021-06-17 04:04:03 +02:00
parent 1b4badfd41
commit a523c28c18
2 changed files with 23 additions and 0 deletions

View file

@ -308,6 +308,8 @@ const char * Sys_GetThreadName( int *index = 0 );
extern void Sys_InitThreads();
extern void Sys_ShutdownThreads();
bool Sys_IsMainThread();
const int MAX_CRITICAL_SECTIONS = 5;
enum {

View file

@ -44,6 +44,9 @@ static bool waiting[MAX_TRIGGER_EVENTS] = { };
static xthreadInfo *thread[MAX_THREADS] = { };
static size_t thread_count = 0;
static bool mainThreadIDset = false;
static SDL_threadID mainThreadID = -1;
/*
==============
Sys_Sleep
@ -68,6 +71,9 @@ Sys_InitThreads
==================
*/
void Sys_InitThreads() {
mainThreadID = SDL_ThreadID();
mainThreadIDset = true;
// critical sections
for (int i = 0; i < MAX_CRITICAL_SECTIONS; i++) {
mutex[i] = SDL_CreateMutex();
@ -314,3 +320,18 @@ const char *Sys_GetThreadName(int *index) {
return "main";
}
/*
==================
Sys_IsMainThread
returns true if the current thread is the main thread
==================
*/
bool Sys_IsMainThread() {
if ( mainThreadIDset )
return SDL_ThreadID() == mainThreadID;
// if this is called before mainThreadID is set, we haven't created
// any threads yet so it should be the main thread
return true;
}