Browse Source

Added GetCurrentThreadID() static function to Thread. Store the thread which created the Context.

Lasse Öörni 11 years ago
parent
commit
f3024b9f4b

+ 4 - 0
Source/Engine/Core/Context.cpp

@@ -22,6 +22,7 @@
 
 
 #include "Precompiled.h"
 #include "Precompiled.h"
 #include "Context.h"
 #include "Context.h"
+#include "Thread.h"
 
 
 #include "DebugNew.h"
 #include "DebugNew.h"
 
 
@@ -57,6 +58,9 @@ Context::Context() :
     // Always reset the random seed on Android, as the Urho3D library might not be unloaded between runs
     // Always reset the random seed on Android, as the Urho3D library might not be unloaded between runs
     SetRandomSeed(1);
     SetRandomSeed(1);
     #endif
     #endif
+    
+    // Set the main thread ID (assuming the Context is created in it)
+    Thread::SetMainThread();
 }
 }
 
 
 Context::~Context()
 Context::~Context()

+ 1 - 1
Source/Engine/Core/Context.h

@@ -58,7 +58,7 @@ public:
     void UpdateAttributeDefaultValue(StringHash objectType, const char* name, const Variant& defaultValue);
     void UpdateAttributeDefaultValue(StringHash objectType, const char* name, const Variant& defaultValue);
     /// Return a preallocated map for event data. Used for optimization to avoid constant re-allocation of event data maps.
     /// Return a preallocated map for event data. Used for optimization to avoid constant re-allocation of event data maps.
     VariantMap& GetEventDataMap();
     VariantMap& GetEventDataMap();
-    
+
     /// Copy base class attributes to derived class.
     /// Copy base class attributes to derived class.
     void CopyBaseAttributes(StringHash baseType, StringHash derivedType);
     void CopyBaseAttributes(StringHash baseType, StringHash derivedType);
     /// Template version of registering an object factory.
     /// Template version of registering an object factory.

+ 21 - 0
Source/Engine/Core/Thread.cpp

@@ -51,6 +51,8 @@ void* ThreadFunctionStatic(void* data)
 }
 }
 #endif
 #endif
 
 
+ThreadID Thread::mainThreadID;
+
 Thread::Thread() :
 Thread::Thread() :
     handle_(0),
     handle_(0),
     shouldRun_(false)
     shouldRun_(false)
@@ -113,4 +115,23 @@ void Thread::SetPriority(int priority)
     #endif
     #endif
 }
 }
 
 
+void Thread::SetMainThread()
+{
+    mainThreadID = GetCurrentThreadID();
+}
+
+ThreadID Thread::GetCurrentThreadID()
+{
+    #ifdef WIN32
+    return GetCurrentThreadId();
+    #else
+    return pthread_self();
+    #endif
+}
+
+bool Thread::IsMainThread()
+{
+    return GetCurrentThreadID() == mainThreadID;
+}
+
 }
 }

+ 17 - 0
Source/Engine/Core/Thread.h

@@ -24,6 +24,13 @@
 
 
 #include "Urho3D.h"
 #include "Urho3D.h"
 
 
+#ifndef WIN32
+#include <pthread.h>
+typedef pthread_t ThreadID;
+#else
+typedef unsigned ThreadID;
+#endif
+
 namespace Urho3D
 namespace Urho3D
 {
 {
 
 
@@ -48,12 +55,22 @@ public:
     
     
     /// Return whether thread exists.
     /// Return whether thread exists.
     bool IsStarted() const { return handle_ != 0; }
     bool IsStarted() const { return handle_ != 0; }
+
+    /// Set the current thread as the main thread.
+    static void SetMainThread();
+    /// Return the current thread's ID.
+    static ThreadID GetCurrentThreadID();
+    /// Return whether is executing in the main thread.
+    static bool IsMainThread();
     
     
 protected:
 protected:
     /// Thread handle.
     /// Thread handle.
     void* handle_;
     void* handle_;
     /// Running flag.
     /// Running flag.
     volatile bool shouldRun_;
     volatile bool shouldRun_;
+    
+    /// Main thread's thread ID.
+    static ThreadID mainThreadID;
 };
 };
 
 
 }
 }