Title: [231158] trunk
Revision
231158
Author
[email protected]
Date
2018-04-30 10:30:26 -0700 (Mon, 30 Apr 2018)

Log Message

Use WordLock instead of std::mutex for Threading
https://bugs.webkit.org/show_bug.cgi?id=185121

Reviewed by Geoffrey Garen.

Source/bmalloc:

Add constexpr to explicitly describe that bmalloc::Mutex constructor is constexpr.

* bmalloc/Mutex.h:

Source/_javascript_Core:

ThreadGroup starts using WordLock.

* heap/MachineStackMarker.h:
(JSC::MachineThreads::getLock):

Source/WTF:

Before r231151, WordLock depends on ThreadSpecific. It means that our Threading implementation
cannot use this lock since Threading primitives could touch these locks after ThreadSpecific
for that WordLock is destroyed.

Now WordLock is changed not to use ThreadSpecific. So it does not depend on our Threading
mechanism and our Threading can start using WordLock internally.

This patch changes WTF::Thread and WTF::ThreadGroup to use WordLock instead of std::mutex.

And add constexpr to explicitly describe that Lock, Condition, and WordLock constructors are constexpr.

* wtf/Condition.h:
* wtf/Lock.h:
* wtf/ThreadGroup.h:
(WTF::ThreadGroup::getLock):
* wtf/Threading.cpp:
(WTF::Thread::didExit):
(WTF::Thread::addToThreadGroup):
(WTF::Thread::removeFromThreadGroup):
* wtf/Threading.h:
* wtf/ThreadingPthreads.cpp:
(WTF::Thread::changePriority):
(WTF::Thread::waitForCompletion):
(WTF::Thread::detach):
(WTF::Thread::signal):
(WTF::Thread::establishPlatformSpecificHandle):
* wtf/ThreadingWin.cpp:
(WTF::Thread::changePriority):
(WTF::Thread::waitForCompletion):
(WTF::Thread::detach):
(WTF::Thread::establishPlatformSpecificHandle):
(WTF::Thread::initializeTLSKey):
(WTF::Thread::currentDying):
(WTF::Thread::get):
(WTF::Thread::initializeTLS):
(WTF::Thread::destructTLS):
(WTF::threadMapMutex): Deleted.
* wtf/WordLock.h:

Tools:

* TestWebKitAPI/Tests/WTF/Signals.cpp:
(TEST):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (231157 => 231158)


--- trunk/Source/_javascript_Core/ChangeLog	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/_javascript_Core/ChangeLog	2018-04-30 17:30:26 UTC (rev 231158)
@@ -1,3 +1,15 @@
+2018-04-30  Yusuke Suzuki  <[email protected]>
+
+        Use WordLock instead of std::mutex for Threading
+        https://bugs.webkit.org/show_bug.cgi?id=185121
+
+        Reviewed by Geoffrey Garen.
+
+        ThreadGroup starts using WordLock.
+
+        * heap/MachineStackMarker.h:
+        (JSC::MachineThreads::getLock):
+
 2018-04-29  Filip Pizlo  <[email protected]>
 
         B3 should run tail duplication at the bitter end

Modified: trunk/Source/_javascript_Core/heap/MachineStackMarker.h (231157 => 231158)


--- trunk/Source/_javascript_Core/heap/MachineStackMarker.h	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/_javascript_Core/heap/MachineStackMarker.h	2018-04-30 17:30:26 UTC (rev 231158)
@@ -50,7 +50,7 @@
     // Only needs to be called by clients that can use the same heap from multiple threads.
     void addCurrentThread() { m_threadGroup->addCurrentThread(); }
 
-    std::mutex& getLock() { return m_threadGroup->getLock(); }
+    WordLock& getLock() { return m_threadGroup->getLock(); }
     const ListHashSet<Ref<Thread>>& threads(const AbstractLocker& locker) const { return m_threadGroup->threads(locker); }
 
 private:

Modified: trunk/Source/WTF/ChangeLog (231157 => 231158)


--- trunk/Source/WTF/ChangeLog	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/ChangeLog	2018-04-30 17:30:26 UTC (rev 231158)
@@ -1,3 +1,49 @@
+2018-04-30  Yusuke Suzuki  <[email protected]>
+
+        Use WordLock instead of std::mutex for Threading
+        https://bugs.webkit.org/show_bug.cgi?id=185121
+
+        Reviewed by Geoffrey Garen.
+
+        Before r231151, WordLock depends on ThreadSpecific. It means that our Threading implementation
+        cannot use this lock since Threading primitives could touch these locks after ThreadSpecific
+        for that WordLock is destroyed.
+
+        Now WordLock is changed not to use ThreadSpecific. So it does not depend on our Threading
+        mechanism and our Threading can start using WordLock internally.
+
+        This patch changes WTF::Thread and WTF::ThreadGroup to use WordLock instead of std::mutex.
+
+        And add constexpr to explicitly describe that Lock, Condition, and WordLock constructors are constexpr.
+
+        * wtf/Condition.h:
+        * wtf/Lock.h:
+        * wtf/ThreadGroup.h:
+        (WTF::ThreadGroup::getLock):
+        * wtf/Threading.cpp:
+        (WTF::Thread::didExit):
+        (WTF::Thread::addToThreadGroup):
+        (WTF::Thread::removeFromThreadGroup):
+        * wtf/Threading.h:
+        * wtf/ThreadingPthreads.cpp:
+        (WTF::Thread::changePriority):
+        (WTF::Thread::waitForCompletion):
+        (WTF::Thread::detach):
+        (WTF::Thread::signal):
+        (WTF::Thread::establishPlatformSpecificHandle):
+        * wtf/ThreadingWin.cpp:
+        (WTF::Thread::changePriority):
+        (WTF::Thread::waitForCompletion):
+        (WTF::Thread::detach):
+        (WTF::Thread::establishPlatformSpecificHandle):
+        (WTF::Thread::initializeTLSKey):
+        (WTF::Thread::currentDying):
+        (WTF::Thread::get):
+        (WTF::Thread::initializeTLS):
+        (WTF::Thread::destructTLS):
+        (WTF::threadMapMutex): Deleted.
+        * wtf/WordLock.h:
+
 2018-04-29  Michael Catanzaro  <[email protected]>
 
         [CMake] Require GCC 6

Modified: trunk/Source/WTF/wtf/Condition.h (231157 => 231158)


--- trunk/Source/WTF/wtf/Condition.h	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/wtf/Condition.h	2018-04-30 17:30:26 UTC (rev 231158)
@@ -48,7 +48,7 @@
     // are unlikely to be affected by the cost of conversions, it is better to use MonotonicTime.
     using Time = ParkingLot::Time;
 
-    Condition() = default;
+    constexpr Condition() = default;
 
     // Wait on a parking queue while releasing the given lock. It will unlock the lock just before
     // parking, and relock it upon wakeup. Returns true if we woke up due to some call to

Modified: trunk/Source/WTF/wtf/Lock.h (231157 => 231158)


--- trunk/Source/WTF/wtf/Lock.h	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/wtf/Lock.h	2018-04-30 17:30:26 UTC (rev 231158)
@@ -52,7 +52,7 @@
     WTF_MAKE_NONCOPYABLE(Lock);
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    Lock() = default;
+    constexpr Lock() = default;
 
     void lock()
     {

Modified: trunk/Source/WTF/wtf/ThreadGroup.h (231157 => 231158)


--- trunk/Source/WTF/wtf/ThreadGroup.h	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/wtf/ThreadGroup.h	2018-04-30 17:30:26 UTC (rev 231158)
@@ -51,7 +51,7 @@
 
     const ListHashSet<Ref<Thread>>& threads(const AbstractLocker&) const { return m_threads; }
 
-    std::mutex& getLock() { return m_lock; }
+    WordLock& getLock() { return m_lock; }
 
     WTF_EXPORT_PRIVATE ~ThreadGroup();
 
@@ -63,8 +63,8 @@
         return shared_from_this();
     }
 
-    // We use std::mutex since it can be used when deallocating TLS.
-    std::mutex m_lock;
+    // We use WordLock since it can be used when deallocating TLS.
+    WordLock m_lock;
     ListHashSet<Ref<Thread>> m_threads;
 };
 

Modified: trunk/Source/WTF/wtf/Threading.cpp (231157 => 231158)


--- trunk/Source/WTF/wtf/Threading.cpp	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/wtf/Threading.cpp	2018-04-30 17:30:26 UTC (rev 231158)
@@ -180,7 +180,7 @@
     if (shouldRemoveThreadFromThreadGroup()) {
         Vector<std::shared_ptr<ThreadGroup>> threadGroups;
         {
-            std::lock_guard<std::mutex> locker(m_mutex);
+            auto locker = holdLock(m_mutex);
             for (auto& threadGroup : m_threadGroups) {
                 // If ThreadGroup is just being destroyed,
                 // we do not need to perform unregistering.
@@ -190,8 +190,8 @@
             m_isShuttingDown = true;
         }
         for (auto& threadGroup : threadGroups) {
-            std::lock_guard<std::mutex> threadGroupLocker(threadGroup->getLock());
-            std::lock_guard<std::mutex> locker(m_mutex);
+            auto threadGroupLocker = holdLock(threadGroup->getLock());
+            auto locker = holdLock(m_mutex);
             threadGroup->m_threads.remove(*this);
         }
     }
@@ -200,7 +200,7 @@
 
     // We would like to say "thread is exited" after unregistering threads from thread groups.
     // So we need to separate m_isShuttingDown from m_didExit.
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     m_didExit = true;
 }
 
@@ -207,7 +207,7 @@
 ThreadGroupAddResult Thread::addToThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup& threadGroup)
 {
     UNUSED_PARAM(threadGroupLocker);
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     if (m_isShuttingDown)
         return ThreadGroupAddResult::NotAdded;
     if (threadGroup.m_threads.add(*this).isNewEntry) {
@@ -220,7 +220,7 @@
 void Thread::removeFromThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup& threadGroup)
 {
     UNUSED_PARAM(threadGroupLocker);
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     if (m_isShuttingDown)
         return;
     m_threadGroups.removeFirstMatching([&] (auto weakPtr) {

Modified: trunk/Source/WTF/wtf/Threading.h (231157 => 231158)


--- trunk/Source/WTF/wtf/Threading.h	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/wtf/Threading.h	2018-04-30 17:30:26 UTC (rev 231158)
@@ -45,6 +45,7 @@
 #include <wtf/ThreadSafeRefCounted.h>
 #include <wtf/ThreadSpecific.h>
 #include <wtf/Vector.h>
+#include <wtf/WordLock.h>
 
 #if USE(PTHREADS) && !OS(DARWIN)
 #include <signal.h>
@@ -273,8 +274,9 @@
     bool m_didExit { false };
     bool m_isDestroyedOnce { false };
 
-    // WordLock & Lock rely on ThreadSpecific. But Thread object can be destroyed even after ThreadSpecific things are destroyed.
-    std::mutex m_mutex;
+    // Lock & ParkingLot rely on ThreadSpecific. But Thread object can be destroyed even after ThreadSpecific things are destroyed.
+    // Use WordLock since WordLock does not depend on ThreadSpecific and this "Thread".
+    WordLock m_mutex;
     StackBounds m_stack { StackBounds::emptyBounds() };
     Vector<std::weak_ptr<ThreadGroup>> m_threadGroups;
     PlatformThreadHandle m_handle;

Modified: trunk/Source/WTF/wtf/ThreadingPthreads.cpp (231157 => 231158)


--- trunk/Source/WTF/wtf/ThreadingPthreads.cpp	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/wtf/ThreadingPthreads.cpp	2018-04-30 17:30:26 UTC (rev 231158)
@@ -256,7 +256,7 @@
 
 void Thread::changePriority(int delta)
 {
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
 
     int policy;
     struct sched_param param;
@@ -273,7 +273,7 @@
 {
     pthread_t handle;
     {
-        std::lock_guard<std::mutex> locker(m_mutex);
+        auto locker = holdLock(m_mutex);
         handle = m_handle;
     }
 
@@ -284,7 +284,7 @@
     else if (joinResult)
         LOG_ERROR("Thread %p was unable to be joined.\n", this);
 
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     ASSERT(joinableState() == Joinable);
 
     // If the thread has already exited, then do nothing. If the thread hasn't exited yet, then just signal that we've already joined on it.
@@ -297,7 +297,7 @@
 
 void Thread::detach()
 {
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     int detachResult = pthread_detach(m_handle);
     if (detachResult)
         LOG_ERROR("Thread %p was unable to be detached\n", this);
@@ -319,7 +319,7 @@
 
 bool Thread::signal(int signalNumber)
 {
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     if (hasExited())
         return false;
     int errNo = pthread_kill(m_handle, signalNumber);
@@ -440,7 +440,7 @@
 
 void Thread::establishPlatformSpecificHandle(pthread_t handle)
 {
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     m_handle = handle;
 #if OS(DARWIN)
     m_platformThread = pthread_mach_thread_np(handle);

Modified: trunk/Source/WTF/wtf/ThreadingWin.cpp (231157 => 231158)


--- trunk/Source/WTF/wtf/ThreadingWin.cpp	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/wtf/ThreadingWin.cpp	2018-04-30 17:30:26 UTC (rev 231158)
@@ -170,7 +170,7 @@
 
 void Thread::changePriority(int delta)
 {
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     SetThreadPriority(m_handle, THREAD_PRIORITY_NORMAL + delta);
 }
 
@@ -178,7 +178,7 @@
 {
     HANDLE handle;
     {
-        std::lock_guard<std::mutex> locker(m_mutex);
+        auto locker = holdLock(m_mutex);
         handle = m_handle;
     }
 
@@ -186,7 +186,7 @@
     if (joinResult == WAIT_FAILED)
         LOG_ERROR("Thread %p was found to be deadlocked trying to quit", this);
 
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     ASSERT(joinableState() == Joinable);
 
     // The thread has already exited, do nothing.
@@ -208,7 +208,7 @@
     // FlsCallback automatically. FlsCallback will call CloseHandle to clean up
     // resource. So in this function, we just mark the thread as detached to
     // avoid calling waitForCompletion for this thread.
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     if (!hasExited())
         didBecomeDetached();
 }
@@ -261,7 +261,7 @@
 
 void Thread::establishPlatformSpecificHandle(HANDLE handle, ThreadIdentifier threadID)
 {
-    std::lock_guard<std::mutex> locker(m_mutex);
+    auto locker = holdLock(m_mutex);
     m_handle = handle;
     m_id = threadID;
 }
@@ -268,11 +268,7 @@
 
 #define InvalidThread reinterpret_cast<Thread*>(static_cast<uintptr_t>(0xbbadbeef))
 
-static std::mutex& threadMapMutex()
-{
-    static NeverDestroyed<std::mutex> mutex;
-    return mutex.get();
-}
+static WordLock threadMapMutex;
 
 static HashMap<ThreadIdentifier, Thread*>& threadMap()
 {
@@ -282,7 +278,6 @@
 
 void Thread::initializeTLSKey()
 {
-    threadMapMutex();
     threadMap();
     threadSpecificKeyCreate(&s_key, destructTLS);
 }
@@ -291,7 +286,7 @@
 {
     ASSERT(s_key != InvalidThreadSpecificKey);
     // After FLS is destroyed, this map offers the value until the second thread exit callback is called.
-    std::lock_guard<std::mutex> locker(threadMapMutex());
+    auto locker = holdLock(threadMapMutex);
     return threadMap().get(currentID());
 }
 
@@ -298,7 +293,7 @@
 // FIXME: Remove this workaround code once <rdar://problem/31793213> is fixed.
 RefPtr<Thread> Thread::get(ThreadIdentifier id)
 {
-    std::lock_guard<std::mutex> locker(threadMapMutex());
+    auto locker = holdLock(threadMapMutex);
     Thread* thread = threadMap().get(id);
     if (thread)
         return thread;
@@ -314,7 +309,7 @@
     auto& threadInTLS = thread.leakRef();
     threadSpecificSet(s_key, &threadInTLS);
     {
-        std::lock_guard<std::mutex> locker(threadMapMutex());
+        auto locker = holdLock(threadMapMutex);
         threadMap().add(id, &threadInTLS);
     }
     return threadInTLS;
@@ -348,7 +343,7 @@
 
     if (thread->m_isDestroyedOnce) {
         {
-            std::lock_guard<std::mutex> locker(threadMapMutex());
+            auto locker = holdLock(threadMapMutex);
             ASSERT(threadMap().contains(thread->id()));
             threadMap().remove(thread->id());
         }

Modified: trunk/Source/WTF/wtf/WordLock.h (231157 => 231158)


--- trunk/Source/WTF/wtf/WordLock.h	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/WTF/wtf/WordLock.h	2018-04-30 17:30:26 UTC (rev 231158)
@@ -50,7 +50,7 @@
     WTF_MAKE_NONCOPYABLE(WordLock);
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    WordLock() = default;
+    constexpr WordLock() = default;
 
     void lock()
     {

Modified: trunk/Source/bmalloc/ChangeLog (231157 => 231158)


--- trunk/Source/bmalloc/ChangeLog	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/bmalloc/ChangeLog	2018-04-30 17:30:26 UTC (rev 231158)
@@ -1,3 +1,14 @@
+2018-04-30  Yusuke Suzuki  <[email protected]>
+
+        Use WordLock instead of std::mutex for Threading
+        https://bugs.webkit.org/show_bug.cgi?id=185121
+
+        Reviewed by Geoffrey Garen.
+
+        Add constexpr to explicitly describe that bmalloc::Mutex constructor is constexpr.
+
+        * bmalloc/Mutex.h:
+
 2018-04-23  Ting-Wei Lan  <[email protected]>
 
         Include stdio.h before using stderr

Modified: trunk/Source/bmalloc/bmalloc/Mutex.h (231157 => 231158)


--- trunk/Source/bmalloc/bmalloc/Mutex.h	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Source/bmalloc/bmalloc/Mutex.h	2018-04-30 17:30:26 UTC (rev 231158)
@@ -37,7 +37,7 @@
 
 class Mutex {
 public:
-    Mutex() = default;
+    constexpr Mutex() = default;
 
     void lock();
     bool try_lock();

Modified: trunk/Tools/ChangeLog (231157 => 231158)


--- trunk/Tools/ChangeLog	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Tools/ChangeLog	2018-04-30 17:30:26 UTC (rev 231158)
@@ -1,3 +1,13 @@
+2018-04-30  Yusuke Suzuki  <[email protected]>
+
+        Use WordLock instead of std::mutex for Threading
+        https://bugs.webkit.org/show_bug.cgi?id=185121
+
+        Reviewed by Geoffrey Garen.
+
+        * TestWebKitAPI/Tests/WTF/Signals.cpp:
+        (TEST):
+
 2018-04-30  Michael Catanzaro  <[email protected]>
 
         [GTK] Webkit should spoof as Safari on a Mac when on Chase.com

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Signals.cpp (231157 => 231158)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Signals.cpp	2018-04-30 16:16:40 UTC (rev 231157)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Signals.cpp	2018-04-30 17:30:26 UTC (rev 231158)
@@ -54,7 +54,7 @@
 
     bool signalFired;
     {
-        std::unique_lock<std::mutex> locker(static_cast<ReflectedThread&>(receiverThread.get()).m_mutex);
+        std::unique_lock<WordLock> locker(static_cast<ReflectedThread&>(receiverThread.get()).m_mutex);
         receiverShouldKeepRunning.store(false);
         EXPECT_FALSE(static_cast<ReflectedThread&>(receiverThread.get()).hasExited());
         sleep(1_s);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to