Add a function to test whether the current thread is the main thread.

This commit is contained in:
Jonathan G Rennison
2016-02-16 19:53:22 +00:00
parent 48e4c35fdc
commit e89efa8581
7 changed files with 48 additions and 0 deletions

View File

@@ -63,6 +63,7 @@
#include "subsidy_func.h" #include "subsidy_func.h"
#include "gfx_layout.h" #include "gfx_layout.h"
#include "viewport_sprite_sorter.h" #include "viewport_sprite_sorter.h"
#include "thread/thread.h"
#include "linkgraph/linkgraphschedule.h" #include "linkgraph/linkgraphschedule.h"
@@ -553,6 +554,7 @@ static const OptionData _options[] = {
*/ */
int openttd_main(int argc, char *argv[]) int openttd_main(int argc, char *argv[])
{ {
SetSelfAsMainThread();
char *musicdriver = NULL; char *musicdriver = NULL;
char *sounddriver = NULL; char *sounddriver = NULL;
char *videodriver = NULL; char *videodriver = NULL;

View File

@@ -122,4 +122,14 @@ private:
*/ */
uint GetCPUCoreCount(); 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 */ #endif /* THREAD_H */

View File

@@ -199,3 +199,7 @@ private:
if (thread != NULL) *thread = to; if (thread != NULL) *thread = to;
return true; return true;
} }
void SetSelfAsMainThread() { }
bool IsMainThread() { return false; }

View File

@@ -33,3 +33,7 @@ public:
{ {
return new ThreadMutex_None(); return new ThreadMutex_None();
} }
void SetSelfAsMainThread() { }
bool IsMainThread() { return true; }

View File

@@ -145,3 +145,7 @@ public:
{ {
return new ThreadMutex_OS2(); return new ThreadMutex_OS2();
} }
void SetSelfAsMainThread() { }
bool IsMainThread() { return false; }

View File

@@ -172,3 +172,15 @@ public:
{ {
return new ThreadMutex_pthread(); return new ThreadMutex_pthread();
} }
static pthread_t main_thread;
void SetSelfAsMainThread()
{
main_thread = pthread_self();
}
bool IsMainThread()
{
return main_thread == pthread_self();
}

View File

@@ -158,3 +158,15 @@ public:
{ {
return new ThreadMutex_Win32(); return new ThreadMutex_Win32();
} }
static uint main_thread_id;
void SetSelfAsMainThread()
{
main_thread_id = GetCurrentThreadId();
}
bool IsMainThread()
{
return main_thread_id == GetCurrentThreadId();
}