- Revision
- 253730
- Author
- [email protected]
- Date
- 2019-12-18 18:01:22 -0800 (Wed, 18 Dec 2019)
Log Message
[Win] Use thread_local to hold Ref<WTF::Thread> in the thread rather than using FLS
https://bugs.webkit.org/show_bug.cgi?id=204398
Reviewed by Yusuke Suzuki.
Use thread_local which is faster than FLS.
* wtf/Threading.cpp:
(WTF::initializeThreading):
* wtf/Threading.h:
(WTF::Thread::current):
* wtf/ThreadingPrimitives.h: Deleted threadSpecificKeyCreate and
related functions which aren't used now in Windows port. Deleted
THREAD_SPECIFIC_CALL macro which isn't used now.
* wtf/win/ThreadingWin.cpp: Added a thread_local variable s_threadHolder.
(WTF::Thread::ThreadHolder::~ThreadHolder): Added.
(WTF::Thread::currentMayBeNull):
(WTF::Thread::initializeTLS): Store a reference of Thread into s_threadHolder.
(WTF::Thread::initializeTLSKey): Deleted.
(WTF::Thread::destructTLS): Deleted.
Modified Paths
Diff
Modified: trunk/Source/WTF/ChangeLog (253729 => 253730)
--- trunk/Source/WTF/ChangeLog 2019-12-19 00:35:24 UTC (rev 253729)
+++ trunk/Source/WTF/ChangeLog 2019-12-19 02:01:22 UTC (rev 253730)
@@ -1,3 +1,26 @@
+2019-12-18 Fujii Hironori <[email protected]>
+
+ [Win] Use thread_local to hold Ref<WTF::Thread> in the thread rather than using FLS
+ https://bugs.webkit.org/show_bug.cgi?id=204398
+
+ Reviewed by Yusuke Suzuki.
+
+ Use thread_local which is faster than FLS.
+
+ * wtf/Threading.cpp:
+ (WTF::initializeThreading):
+ * wtf/Threading.h:
+ (WTF::Thread::current):
+ * wtf/ThreadingPrimitives.h: Deleted threadSpecificKeyCreate and
+ related functions which aren't used now in Windows port. Deleted
+ THREAD_SPECIFIC_CALL macro which isn't used now.
+ * wtf/win/ThreadingWin.cpp: Added a thread_local variable s_threadHolder.
+ (WTF::Thread::ThreadHolder::~ThreadHolder): Added.
+ (WTF::Thread::currentMayBeNull):
+ (WTF::Thread::initializeTLS): Store a reference of Thread into s_threadHolder.
+ (WTF::Thread::initializeTLSKey): Deleted.
+ (WTF::Thread::destructTLS): Deleted.
+
2019-12-18 Ben Nham <[email protected]>
Add network loading signposts
Modified: trunk/Source/WTF/wtf/Threading.cpp (253729 => 253730)
--- trunk/Source/WTF/wtf/Threading.cpp 2019-12-19 00:35:24 UTC (rev 253729)
+++ trunk/Source/WTF/wtf/Threading.cpp 2019-12-19 02:01:22 UTC (rev 253730)
@@ -323,7 +323,7 @@
out.print("Thread:", RawPointer(this));
}
-#if !HAVE(FAST_TLS)
+#if !HAVE(FAST_TLS) && !OS(WINDOWS)
ThreadSpecificKey Thread::s_key = InvalidThreadSpecificKey;
#endif
@@ -332,7 +332,7 @@
static std::once_flag onceKey;
std::call_once(onceKey, [] {
initializeRandomNumberGenerator();
-#if !HAVE(FAST_TLS)
+#if !HAVE(FAST_TLS) && !OS(WINDOWS)
Thread::initializeTLSKey();
#endif
initializeDates();
Modified: trunk/Source/WTF/wtf/Threading.h (253729 => 253730)
--- trunk/Source/WTF/wtf/Threading.h 2019-12-19 00:35:24 UTC (rev 253729)
+++ trunk/Source/WTF/wtf/Threading.h 2019-12-19 02:01:22 UTC (rev 253730)
@@ -127,6 +127,8 @@
};
SpecificStorage& specificStorage() { return m_specificStorage; };
+
+ class ThreadHolder;
#endif
WTF_EXPORT_PRIVATE void changePriority(int);
@@ -271,16 +273,21 @@
ThreadGroupAddResult addToThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup&);
void removeFromThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup&);
- // The Thread instance is ref'ed and held in thread-specific storage. It will be deref'ed by destructTLS at thread destruction time.
- // For pthread, it employs pthreads-specific 2-pass destruction to reliably remove Thread.
- // For Windows, we use thread_local to defer thread TLS destruction. It assumes regular ThreadSpecific
- // types don't use multiple-pass destruction.
+ // For pthread, the Thread instance is ref'ed and held in thread-specific storage. It will be deref'ed by destructTLS at thread destruction time.
+ // It employs pthreads-specific 2-pass destruction to reliably remove Thread.
-#if !HAVE(FAST_TLS)
+#if !HAVE(FAST_TLS) && !OS(WINDOWS)
static WTF_EXPORT_PRIVATE ThreadSpecificKey s_key;
// One time initialization for this class as a whole.
// This method must be called before initializeTLS() and it is not thread-safe.
static void initializeTLSKey();
+
+ // This thread-specific destructor is called 2 times when thread terminates:
+ // - first, when all the other thread-specific destructors are called, it simply remembers it was 'destroyed once'
+ // and (1) re-sets itself into the thread-specific slot or (2) constructs thread local value to call it again later.
+ // - second, after all thread-specific destructors were invoked, it gets called again - this time, we deref the
+ // Thread in the TLS, completing the cleanup.
+ static void destructTLS(void* data);
#endif
// Creates and puts an instance of Thread into thread-specific storage.
@@ -288,15 +295,12 @@
WTF_EXPORT_PRIVATE static Thread& initializeCurrentTLS();
// Returns nullptr if thread-specific storage was not initialized.
+#if OS(WINDOWS)
+ WTF_EXPORT_PRIVATE static Thread* currentMayBeNull();
+#else
static Thread* currentMayBeNull();
+#endif
- // This thread-specific destructor is called 2 times when thread terminates:
- // - first, when all the other thread-specific destructors are called, it simply remembers it was 'destroyed once'
- // and (1) re-sets itself into the thread-specific slot or (2) constructs thread local value to call it again later.
- // - second, after all thread-specific destructors were invoked, it gets called again - this time, we deref the
- // Thread in the TLS, completing the cleanup.
- static void THREAD_SPECIFIC_CALL destructTLS(void* data);
-
JoinableState m_joinableState { Joinable };
bool m_isShuttingDown : 1;
bool m_didExit : 1;
@@ -344,6 +348,7 @@
{
}
+#if !OS(WINDOWS)
inline Thread* Thread::currentMayBeNull()
{
#if !HAVE(FAST_TLS)
@@ -353,6 +358,7 @@
return static_cast<Thread*>(_pthread_getspecific_direct(WTF_THREAD_DATA_KEY));
#endif
}
+#endif
inline Thread& Thread::current()
{
@@ -362,7 +368,7 @@
// WRT _javascript_Core:
// Thread::initializeTLSKey() is initially called from initializeThreading(), ensuring
// this is initially called in a std::call_once locked context.
-#if !HAVE(FAST_TLS)
+#if !HAVE(FAST_TLS) && !OS(WINDOWS)
if (UNLIKELY(Thread::s_key == InvalidThreadSpecificKey))
WTF::initializeThreading();
#endif
Modified: trunk/Source/WTF/wtf/ThreadingPrimitives.h (253729 => 253730)
--- trunk/Source/WTF/wtf/ThreadingPrimitives.h 2019-12-19 00:35:24 UTC (rev 253729)
+++ trunk/Source/WTF/wtf/ThreadingPrimitives.h 2019-12-19 02:01:22 UTC (rev 253730)
@@ -48,12 +48,6 @@
#endif
#endif
-#if OS(WINDOWS) && CPU(X86)
-#define THREAD_SPECIFIC_CALL __stdcall
-#else
-#define THREAD_SPECIFIC_CALL
-#endif
-
namespace WTF {
using ThreadFunction = void (*)(void* argument);
@@ -145,34 +139,6 @@
return pthread_getspecific(key);
}
-#elif OS(WINDOWS)
-
-static constexpr ThreadSpecificKey InvalidThreadSpecificKey = FLS_OUT_OF_INDEXES;
-
-inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (THREAD_SPECIFIC_CALL *destructor)(void *))
-{
- DWORD flsKey = FlsAlloc(destructor);
- if (flsKey == FLS_OUT_OF_INDEXES)
- CRASH();
-
- *key = flsKey;
-}
-
-inline void threadSpecificKeyDelete(ThreadSpecificKey key)
-{
- FlsFree(key);
-}
-
-inline void threadSpecificSet(ThreadSpecificKey key, void* data)
-{
- FlsSetValue(key, data);
-}
-
-inline void* threadSpecificGet(ThreadSpecificKey key)
-{
- return FlsGetValue(key);
-}
-
#endif
} // namespace WTF
Modified: trunk/Source/WTF/wtf/win/ThreadingWin.cpp (253729 => 253730)
--- trunk/Source/WTF/wtf/win/ThreadingWin.cpp 2019-12-19 00:35:24 UTC (rev 253729)
+++ trunk/Source/WTF/wtf/win/ThreadingWin.cpp 2019-12-19 02:01:22 UTC (rev 253730)
@@ -263,42 +263,31 @@
m_id = threadID;
}
-#define InvalidThread reinterpret_cast<Thread*>(static_cast<uintptr_t>(0xbbadbeef))
+struct Thread::ThreadHolder {
+ ~ThreadHolder()
+ {
+ if (thread) {
+ thread->specificStorage().destroySlots();
+ thread->didExit();
+ }
+ }
-void Thread::initializeTLSKey()
+ RefPtr<Thread> thread;
+};
+
+thread_local static Thread::ThreadHolder s_threadHolder;
+
+Thread* Thread::currentMayBeNull()
{
- threadSpecificKeyCreate(&s_key, destructTLS);
+ return s_threadHolder.thread.get();
}
Thread& Thread::initializeTLS(Ref<Thread>&& thread)
{
- ASSERT(s_key != InvalidThreadSpecificKey);
- // FIXME: Remove this workaround code once <rdar://problem/31793213> is fixed.
- auto id = thread->id();
- // We leak the ref to keep the Thread alive while it is held in TLS. destructTLS will deref it later at thread destruction time.
- auto& threadInTLS = thread.leakRef();
- threadSpecificSet(s_key, &threadInTLS);
- return threadInTLS;
+ s_threadHolder.thread = WTFMove(thread);
+ return *s_threadHolder.thread;
}
-void Thread::destructTLS(void* data)
-{
- if (data == InvalidThread)
- return;
-
- Thread* thread = static_cast<Thread*>(data);
- ASSERT(thread);
-
- thread->specificStorage().destroySlots();
- thread->didExit();
- thread->deref();
-
- // Fill the FLS with the non-nullptr value. While FLS destructor won't be called for that,
- // non-nullptr value tells us that we already destructed Thread. This allows us to
- // detect incorrect use of Thread::current() after this point because it will crash.
- threadSpecificSet(s_key, InvalidThread);
-}
-
Atomic<int> Thread::SpecificStorage::s_numberOfKeys;
std::array<Atomic<Thread::SpecificStorage::DestroyFunction>, Thread::SpecificStorage::s_maxKeys> Thread::SpecificStorage::s_destroyFunctions;