[WTF] Make isMainThread more reliable
authoryusukesuzuki@slowstart.org <yusukesuzuki@slowstart.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 28 Sep 2018 22:32:34 +0000 (22:32 +0000)
committeryusukesuzuki@slowstart.org <yusukesuzuki@slowstart.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 28 Sep 2018 22:32:34 +0000 (22:32 +0000)
commitbd4369d3d12175c2289052c48c8a0eba0cc0f362
tree7badad0631ae61499ab5ba15d65dfe256e3de797
parent1dc15d339e25944ef7b4a28a77b240720b09357f
[WTF] Make isMainThread more reliable
https://bugs.webkit.org/show_bug.cgi?id=189880

Reviewed by Mark Lam.

.:

* Source/cmake/OptionsCommon.cmake:

Source/WTF:

isMainThread() relied on Thread::current(). This API becomes broken in Windows
when the Thread is about to be destroyed since TLS is already cleared. This causes
a bug since `isMainThread()` is called in Thread::didExit in Windows.

This patch makes this `isMainThread` more reliable in all the platforms. In Windows,
we use `Thread::currentID()` instead of `Thread::current()` since `Thread::currentID`
uses Win32 GetCurrentThreadId directly. In the other system, we use `pthread_main_np`
or `pthread_self` instead.

We also move `holdLock` code inside `if (shouldRemoveThreadFromThreadGroup())`. If
the other thread takes a mutex and destroyed, this `holdLock` waits forever. This problem
only happens in Windows since Windows calls TLS destructor for the main thread.

* WTF.xcodeproj/project.pbxproj:
* wtf/MainThread.cpp:
(WTF::initializeMainThread):
(): Deleted.
(WTF::isMainThread): Deleted.
(WTF::isMainThreadIfInitialized): Deleted.
* wtf/Platform.h:
* wtf/PlatformMac.cmake:
* wtf/Threading.cpp:
(WTF::Thread::didExit):
* wtf/cocoa/MainThreadCocoa.mm: Renamed from Source/WTF/wtf/mac/MainThreadMac.mm.
* wtf/generic/MainThreadGeneric.cpp:
(WTF::initializeMainThreadPlatform):
(WTF::isMainThread):
(WTF::isMainThreadIfInitialized):
* wtf/win/MainThreadWin.cpp:
(WTF::initializeMainThreadPlatform):
(WTF::isMainThread):
(WTF::isMainThreadIfInitialized):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@236617 268f45cc-cd09-0410-ab3c-d52691b4dbfc
ChangeLog
Source/WTF/ChangeLog
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/MainThread.cpp
Source/WTF/wtf/Platform.h
Source/WTF/wtf/PlatformMac.cmake
Source/WTF/wtf/Threading.cpp
Source/WTF/wtf/cocoa/MainThreadCocoa.mm [moved from Source/WTF/wtf/mac/MainThreadMac.mm with 99% similarity]
Source/WTF/wtf/generic/MainThreadGeneric.cpp
Source/WTF/wtf/win/MainThreadWin.cpp
Source/cmake/OptionsCommon.cmake