diff --git a/neo/sys/sys_public.h b/neo/sys/sys_public.h index fbac1a88..fe2bde14 100644 --- a/neo/sys/sys_public.h +++ b/neo/sys/sys_public.h @@ -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 { diff --git a/neo/sys/threads.cpp b/neo/sys/threads.cpp index a0fe7c8d..0c1f0e87 100644 --- a/neo/sys/threads.cpp +++ b/neo/sys/threads.cpp @@ -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; +}