Index: Source/platform/heap/ThreadState.h |
diff --git a/Source/platform/heap/ThreadState.h b/Source/platform/heap/ThreadState.h |
index 54e561338f10ccdbeae0478031f2e27c7411f688..02cfd83b47fe5659d0421b054c8a272fb34f814c 100644 |
--- a/Source/platform/heap/ThreadState.h |
+++ b/Source/platform/heap/ThreadState.h |
@@ -375,7 +375,21 @@ public: |
// can no longer use the garbage collected heap after this call. |
static void detach(); |
- static ThreadState* current() { return **s_threadSpecific; } |
+ static ThreadState* current() |
+ { |
+ intptr_t dummy; |
+ unsigned addressDiff = s_mainThreadStackStart - reinterpret_cast<intptr_t>(&dummy); |
+ // This is a hacky, fast way to determine if we are in the main thread. |
+ // If the address of the |dummy| is within 1 MB from the stack start of |
+ // the main thread, we judge that we are in the main thread. |
+ if (LIKELY(addressDiff <= 1024 * 1024)) { |
Mads Ager (chromium)
2014/10/10 13:44:36
There is no way to guarantee that the stack is at
|
+ ASSERT(**s_threadSpecific == mainThreadState()); |
+ return mainThreadState(); |
+ } |
+ // TLS lookup is slow. |
+ return **s_threadSpecific; |
+ } |
+ |
static ThreadState* mainThreadState() |
{ |
return reinterpret_cast<ThreadState*>(s_mainThreadStateStorage); |
@@ -740,6 +754,7 @@ private: |
void invokePreFinalizers(Visitor&); |
static WTF::ThreadSpecific<ThreadState*>* s_threadSpecific; |
+ static intptr_t s_mainThreadStackStart; |
static SafePointBarrier* s_safePointBarrier; |
// This variable is flipped to true after all threads are stoped |