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

pull/6/merge
Jonathan G Rennison 8 years ago
parent 48e4c35fdc
commit e89efa8581

@ -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;

@ -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 */

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

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

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

@ -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();
}

@ -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();
}

Loading…
Cancel
Save