Thread: Adjust checks for whether current thread is the game thread

This commit is contained in:
Jonathan G Rennison
2021-06-20 10:00:36 +01:00
parent 4577b547ea
commit 87948d8029
6 changed files with 23 additions and 1 deletions

View File

@@ -223,3 +223,4 @@ void PerThreadSetupInit() { }
bool IsMainThread() { return false; } bool IsMainThread() { return false; }
bool IsNonMainThread() { return false; } bool IsNonMainThread() { return false; }
bool IsGameThread() { return false; } bool IsGameThread() { return false; }
bool IsNonGameThread() { return false; }

View File

@@ -380,6 +380,15 @@ bool IsGameThread()
{ {
#if !defined(NO_THREADS) #if !defined(NO_THREADS)
return game_thread == pthread_self(); return game_thread == pthread_self();
#else
return true;
#endif
}
bool IsNonGameThread()
{
#if !defined(NO_THREADS)
return game_thread != pthread_self();
#else #else
return false; return false;
#endif #endif

View File

@@ -757,6 +757,11 @@ bool IsGameThread()
return game_thread_id == GetCurrentThreadId(); return game_thread_id == GetCurrentThreadId();
} }
bool IsNonGameThread()
{
return game_thread_id != GetCurrentThreadId();
}
static std::map<DWORD, std::string> _thread_name_map; static std::map<DWORD, std::string> _thread_name_map;
static std::mutex _thread_name_map_mutex; static std::mutex _thread_name_map_mutex;

View File

@@ -386,7 +386,7 @@ void NORETURN SlError(StringID string, const char *extra_msg, bool already_mallo
str = already_malloced ? const_cast<char *>(extra_msg) : stredup(extra_msg); str = already_malloced ? const_cast<char *>(extra_msg) : stredup(extra_msg);
} }
if (IsNonMainThread() && !IsGameThread() && _sl.action != SLA_SAVE) { if (IsNonMainThread() && IsNonGameThread() && _sl.action != SLA_SAVE) {
throw ThreadSlErrorException{ string, extra_msg }; throw ThreadSlErrorException{ string, extra_msg };
} }

View File

@@ -76,6 +76,11 @@ bool IsNonMainThread();
*/ */
bool IsGameThread(); bool IsGameThread();
/**
* @return true if the current thread is definitely a "non-game" thread. If in doubt returns false.
*/
bool IsNonGameThread();
/** /**
* Start a new thread. * Start a new thread.

View File

@@ -11,6 +11,7 @@
#include "../gfx_func.h" #include "../gfx_func.h"
#include "../blitter/factory.hpp" #include "../blitter/factory.hpp"
#include "../window_func.h" #include "../window_func.h"
#include "../thread.h"
#include "null_v.h" #include "null_v.h"
#include "../safeguards.h" #include "../safeguards.h"
@@ -48,6 +49,7 @@ void VideoDriver_Null::MakeDirty(int left, int top, int width, int height) {}
void VideoDriver_Null::MainLoop() void VideoDriver_Null::MainLoop()
{ {
SetSelfAsGameThread();
if (this->until_exit) { if (this->until_exit) {
while (!_exit_game) { while (!_exit_game) {
::GameLoop(); ::GameLoop();