Index: Source/platform/heap/ThreadState.h |
diff --git a/Source/platform/heap/ThreadState.h b/Source/platform/heap/ThreadState.h |
index 54e561338f10ccdbeae0478031f2e27c7411f688..a7d056c0d228ab18b8f6b0716446ba9f90ff9d11 100644 |
--- a/Source/platform/heap/ThreadState.h |
+++ b/Source/platform/heap/ThreadState.h |
@@ -375,7 +375,27 @@ public: |
// can no longer use the garbage collected heap after this call. |
static void detach(); |
- static ThreadState* current() { return **s_threadSpecific; } |
+ static ThreadState* current() |
+ { |
+#if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
+ // TLS lookup is fast in these platforms. |
+ return **s_threadSpecific; |
+#else |
+ uintptr_t dummy; |
+ uintptr_t addressDiff = s_mainThreadStackStart - reinterpret_cast<intptr_t>(&dummy); |
Vyacheslav Egorov (Google)
2014/10/13 14:07:23
any reason to cast to intptr_t not uintptr_t here?
haraken
2014/10/13 15:41:38
Done.
|
+ // This is a fast way to judge if we are in the main thread. |
+ // If |&dummy| is within |s_mainThreadUnderestimatedStackSize| byte from |
+ // the stack start of the main thread, we judge that we are in |
+ // the main thread. |
+ if (LIKELY(addressDiff <= s_mainThreadUnderestimatedStackSize)) { |
Vyacheslav Egorov (Google)
2014/10/13 14:07:23
I think s_mainThreadStackStart is exclusive, not i
haraken
2014/10/13 15:41:38
Good point. In order to avoid do the subtraction i
|
+ ASSERT(**s_threadSpecific == mainThreadState()); |
+ return mainThreadState(); |
+ } |
+ // TLS lookup is slow. |
+ return **s_threadSpecific; |
+#endif |
+ } |
+ |
static ThreadState* mainThreadState() |
{ |
return reinterpret_cast<ThreadState*>(s_mainThreadStateStorage); |
@@ -740,6 +760,8 @@ private: |
void invokePreFinalizers(Visitor&); |
static WTF::ThreadSpecific<ThreadState*>* s_threadSpecific; |
+ static uintptr_t s_mainThreadStackStart; |
+ static uintptr_t s_mainThreadUnderestimatedStackSize; |
static SafePointBarrier* s_safePointBarrier; |
// This variable is flipped to true after all threads are stoped |