diff --git a/src/openttd.cpp b/src/openttd.cpp index 5e3d2a6c4c..a6e6417243 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -63,6 +63,7 @@ #include "subsidy_func.h" #include "gfx_layout.h" #include "viewport_sprite_sorter.h" +#include "thread/thread.h" #include "linkgraph/linkgraphschedule.h" @@ -553,6 +554,7 @@ static const OptionData _options[] = { */ int openttd_main(int argc, char *argv[]) { + SetSelfAsMainThread(); char *musicdriver = NULL; char *sounddriver = NULL; char *videodriver = NULL; diff --git a/src/thread/thread.h b/src/thread/thread.h index b944a53b96..e660c1b2c7 100644 --- a/src/thread/thread.h +++ b/src/thread/thread.h @@ -122,4 +122,14 @@ private: */ uint GetCPUCoreCount(); +/** + * Set the current thread as the "main" thread + */ +void SetSelfAsMainThread(); + +/** + * @return true if the current thread definitely the "main" thread. If in doubt returns false. + */ +bool IsMainThread(); + #endif /* THREAD_H */ diff --git a/src/thread/thread_morphos.cpp b/src/thread/thread_morphos.cpp index cc6b2f9d3d..5fb45562ee 100644 --- a/src/thread/thread_morphos.cpp +++ b/src/thread/thread_morphos.cpp @@ -199,3 +199,7 @@ private: if (thread != NULL) *thread = to; return true; } + +void SetSelfAsMainThread() { } + +bool IsMainThread() { return false; } diff --git a/src/thread/thread_none.cpp b/src/thread/thread_none.cpp index afa799b239..6ba4b4f276 100644 --- a/src/thread/thread_none.cpp +++ b/src/thread/thread_none.cpp @@ -33,3 +33,7 @@ public: { return new ThreadMutex_None(); } + +void SetSelfAsMainThread() { } + +bool IsMainThread() { return true; } diff --git a/src/thread/thread_os2.cpp b/src/thread/thread_os2.cpp index 2dcb8288f4..8ec3104bf3 100644 --- a/src/thread/thread_os2.cpp +++ b/src/thread/thread_os2.cpp @@ -145,3 +145,7 @@ public: { return new ThreadMutex_OS2(); } + +void SetSelfAsMainThread() { } + +bool IsMainThread() { return false; } diff --git a/src/thread/thread_pthread.cpp b/src/thread/thread_pthread.cpp index ce407fd941..d98e26a341 100644 --- a/src/thread/thread_pthread.cpp +++ b/src/thread/thread_pthread.cpp @@ -172,3 +172,15 @@ public: { return new ThreadMutex_pthread(); } + +static pthread_t main_thread; + +void SetSelfAsMainThread() +{ + main_thread = pthread_self(); +} + +bool IsMainThread() +{ + return main_thread == pthread_self(); +} diff --git a/src/thread/thread_win32.cpp b/src/thread/thread_win32.cpp index c37baf7c36..ec0e45797f 100644 --- a/src/thread/thread_win32.cpp +++ b/src/thread/thread_win32.cpp @@ -158,3 +158,15 @@ public: { return new ThreadMutex_Win32(); } + +static uint main_thread_id; + +void SetSelfAsMainThread() +{ + main_thread_id = GetCurrentThreadId(); +} + +bool IsMainThread() +{ + return main_thread_id == GetCurrentThreadId(); +}