Title: [219696] trunk/Source
Revision
219696
Author
[email protected]
Date
2017-07-20 11:32:13 -0700 (Thu, 20 Jul 2017)

Log Message

[WTF] Use ThreadGroup to bookkeep active threads for Mach exception
https://bugs.webkit.org/show_bug.cgi?id=174678

Reviewed by Mark Lam.

Source/_javascript_Core:

Use Thread& instead.

* runtime/JSLock.cpp:
(JSC::JSLock::didAcquireLock):

Source/WTF:

We can use ThreadGroup to bookkeep active threads for Mach exceptions.
When the thread dies, it is automatically removed from the thread groups.
So we do not need to call unregisterThreadForMachExceptionHandling.

* wtf/ThreadGroup.cpp:
(WTF::ThreadGroup::~ThreadGroup):
(WTF::ThreadGroup::add):
* wtf/ThreadGroup.h:
* wtf/ThreadHolder.cpp:
(WTF::ThreadHolder::~ThreadHolder):
* wtf/Threading.cpp:
(WTF::Thread::addToThreadGroup):
(WTF::Thread::removeFromThreadGroup):
* wtf/Threading.h:
* wtf/threads/Signals.cpp:
(WTF::setExceptionPorts):
(WTF::activeThreads):
(WTF::registerThreadForMachExceptionHandling):
(WTF::installSignalHandler):
(WTF::unregisterThreadForMachExceptionHandling): Deleted.
* wtf/threads/Signals.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (219695 => 219696)


--- trunk/Source/_javascript_Core/ChangeLog	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/_javascript_Core/ChangeLog	2017-07-20 18:32:13 UTC (rev 219696)
@@ -1,3 +1,15 @@
+2017-07-20  Yusuke Suzuki  <[email protected]>
+
+        [WTF] Use ThreadGroup to bookkeep active threads for Mach exception
+        https://bugs.webkit.org/show_bug.cgi?id=174678
+
+        Reviewed by Mark Lam.
+
+        Use Thread& instead.
+
+        * runtime/JSLock.cpp:
+        (JSC::JSLock::didAcquireLock):
+
 2017-07-19  Yusuke Suzuki  <[email protected]>
 
         [WTF] Implement WTF::ThreadGroup

Modified: trunk/Source/_javascript_Core/runtime/JSLock.cpp (219695 => 219696)


--- trunk/Source/_javascript_Core/runtime/JSLock.cpp	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/_javascript_Core/runtime/JSLock.cpp	2017-07-20 18:32:13 UTC (rev 219696)
@@ -151,7 +151,7 @@
 #endif
 
 #if HAVE(MACH_EXCEPTIONS)
-    registerThreadForMachExceptionHandling(&Thread::current());
+    registerThreadForMachExceptionHandling(Thread::current());
 #endif
 
     // Note: everything below must come after addCurrentThread().

Modified: trunk/Source/WTF/ChangeLog (219695 => 219696)


--- trunk/Source/WTF/ChangeLog	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/WTF/ChangeLog	2017-07-20 18:32:13 UTC (rev 219696)
@@ -1,3 +1,32 @@
+2017-07-20  Yusuke Suzuki  <[email protected]>
+
+        [WTF] Use ThreadGroup to bookkeep active threads for Mach exception
+        https://bugs.webkit.org/show_bug.cgi?id=174678
+
+        Reviewed by Mark Lam.
+
+        We can use ThreadGroup to bookkeep active threads for Mach exceptions.
+        When the thread dies, it is automatically removed from the thread groups.
+        So we do not need to call unregisterThreadForMachExceptionHandling.
+
+        * wtf/ThreadGroup.cpp:
+        (WTF::ThreadGroup::~ThreadGroup):
+        (WTF::ThreadGroup::add):
+        * wtf/ThreadGroup.h:
+        * wtf/ThreadHolder.cpp:
+        (WTF::ThreadHolder::~ThreadHolder):
+        * wtf/Threading.cpp:
+        (WTF::Thread::addToThreadGroup):
+        (WTF::Thread::removeFromThreadGroup):
+        * wtf/Threading.h:
+        * wtf/threads/Signals.cpp:
+        (WTF::setExceptionPorts):
+        (WTF::activeThreads):
+        (WTF::registerThreadForMachExceptionHandling):
+        (WTF::installSignalHandler):
+        (WTF::unregisterThreadForMachExceptionHandling): Deleted.
+        * wtf/threads/Signals.h:
+
 2017-07-19  Yusuke Suzuki  <[email protected]>
 
         [WTF] Remove unnecessary indirection of WTF::Thread entry point

Modified: trunk/Source/WTF/wtf/ThreadGroup.cpp (219695 => 219696)


--- trunk/Source/WTF/wtf/ThreadGroup.cpp	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/WTF/wtf/ThreadGroup.cpp	2017-07-20 18:32:13 UTC (rev 219696)
@@ -32,17 +32,22 @@
 
 ThreadGroup::~ThreadGroup()
 {
-    std::lock_guard<std::mutex> locker(m_lock);
+    auto locker = holdLock(m_lock);
     for (auto& thread : m_threads)
         thread->removeFromThreadGroup(locker, *this);
 }
 
-bool ThreadGroup::add(Thread& thread)
+bool ThreadGroup::add(const AbstractLocker& locker, Thread& thread)
 {
-    std::lock_guard<std::mutex> locker(m_lock);
     return thread.addToThreadGroup(locker, *this);
 }
 
+bool ThreadGroup::add(Thread& thread)
+{
+    auto locker = holdLock(m_lock);
+    return add(locker, thread);
+}
+
 void ThreadGroup::addCurrentThread()
 {
     bool isAdded = add(Thread::current());

Modified: trunk/Source/WTF/wtf/ThreadGroup.h (219695 => 219696)


--- trunk/Source/WTF/wtf/ThreadGroup.h	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/WTF/wtf/ThreadGroup.h	2017-07-20 18:32:13 UTC (rev 219696)
@@ -42,6 +42,7 @@
     }
 
     WTF_EXPORT_PRIVATE bool add(Thread&);
+    WTF_EXPORT_PRIVATE bool add(const AbstractLocker&, Thread&);
     WTF_EXPORT_PRIVATE void addCurrentThread();
 
     const ListHashSet<Ref<Thread>>& threads(const AbstractLocker&) const { return m_threads; }

Modified: trunk/Source/WTF/wtf/ThreadHolder.cpp (219695 => 219696)


--- trunk/Source/WTF/wtf/ThreadHolder.cpp	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/WTF/wtf/ThreadHolder.cpp	2017-07-20 18:32:13 UTC (rev 219696)
@@ -34,8 +34,6 @@
 
 #include "Threading.h"
 
-#include <wtf/threads/Signals.h>
-
 namespace WTF {
 
 ThreadSpecificKey ThreadHolder::m_key = InvalidThreadSpecificKey;
@@ -42,9 +40,6 @@
 
 ThreadHolder::~ThreadHolder()
 {
-#if HAVE(MACH_EXCEPTIONS)
-    unregisterThreadForMachExceptionHandling(&m_thread.get());
-#endif
     m_thread->didExit();
 }
 

Modified: trunk/Source/WTF/wtf/Threading.cpp (219695 => 219696)


--- trunk/Source/WTF/wtf/Threading.cpp	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/WTF/wtf/Threading.cpp	2017-07-20 18:32:13 UTC (rev 219696)
@@ -173,7 +173,7 @@
     m_didExit = true;
 }
 
-bool Thread::addToThreadGroup(const std::lock_guard<std::mutex>& threadGroupLocker, ThreadGroup& threadGroup)
+bool Thread::addToThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup& threadGroup)
 {
     UNUSED_PARAM(threadGroupLocker);
     std::lock_guard<std::mutex> locker(m_mutex);
@@ -184,7 +184,7 @@
     return true;
 }
 
-void Thread::removeFromThreadGroup(const std::lock_guard<std::mutex>& threadGroupLocker, ThreadGroup& threadGroup)
+void Thread::removeFromThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup& threadGroup)
 {
     UNUSED_PARAM(threadGroupLocker);
     std::lock_guard<std::mutex> locker(m_mutex);

Modified: trunk/Source/WTF/wtf/Threading.h (219695 => 219696)


--- trunk/Source/WTF/wtf/Threading.h	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/WTF/wtf/Threading.h	2017-07-20 18:32:13 UTC (rev 219696)
@@ -182,8 +182,8 @@
     bool hasExited() { return m_didExit; }
 
     // These functions are only called from ThreadGroup.
-    bool addToThreadGroup(const std::lock_guard<std::mutex>& threadGroupLocker, ThreadGroup&);
-    void removeFromThreadGroup(const std::lock_guard<std::mutex>& threadGroupLocker, ThreadGroup&);
+    bool addToThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup&);
+    void removeFromThreadGroup(const AbstractLocker& threadGroupLocker, ThreadGroup&);
 
     // WordLock & Lock rely on ThreadSpecific. But Thread object can be destroyed even after ThreadSpecific things are destroyed.
     std::mutex m_mutex;

Modified: trunk/Source/WTF/wtf/threads/Signals.cpp (219695 => 219696)


--- trunk/Source/WTF/wtf/threads/Signals.cpp	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/WTF/wtf/threads/Signals.cpp	2017-07-20 18:32:13 UTC (rev 219696)
@@ -46,9 +46,9 @@
 
 #include <wtf/Atomics.h>
 #include <wtf/DataLog.h>
-#include <wtf/HashSet.h>
 #include <wtf/LocklessBag.h>
 #include <wtf/NeverDestroyed.h>
+#include <wtf/ThreadGroup.h>
 #include <wtf/ThreadMessage.h>
 #include <wtf/Threading.h>
 
@@ -215,12 +215,12 @@
 }
 
 
-static StaticLock threadLock;
 exception_mask_t activeExceptions { 0 };
 
-inline void setExceptionPorts(const AbstractLocker&, Thread* thread)
+inline void setExceptionPorts(const AbstractLocker& threadGroupLocker, Thread& thread)
 {
-    kern_return_t result = thread_set_exception_ports(thread->machThread(), activeExceptions, exceptionPort, EXCEPTION_STATE | MACH_EXCEPTION_CODES, MACHINE_THREAD_STATE);
+    UNUSED_PARAM(threadGroupLocker);
+    kern_return_t result = thread_set_exception_ports(thread.machThread(), activeExceptions, exceptionPort, EXCEPTION_STATE | MACH_EXCEPTION_CODES, MACHINE_THREAD_STATE);
     if (result != KERN_SUCCESS) {
         dataLogLn("thread set port failed due to ", mach_error_string(result));
         CRASH();
@@ -227,27 +227,19 @@
     }
 }
 
-inline HashSet<Thread*>& activeThreads(const AbstractLocker&)
+inline ThreadGroup& activeThreads()
 {
-    static NeverDestroyed<HashSet<Thread*>> activeThreads;
-    return activeThreads;
+    static NeverDestroyed<std::shared_ptr<ThreadGroup>> activeThreads { ThreadGroup::create() };
+    return *activeThreads.get();
 }
 
-void registerThreadForMachExceptionHandling(Thread* thread)
+void registerThreadForMachExceptionHandling(Thread& thread)
 {
-    auto locker = holdLock(threadLock);
-    auto result = activeThreads(locker).add(thread);
-
-    if (result.isNewEntry)
+    auto locker = holdLock(activeThreads().getLock());
+    if (activeThreads().add(locker, thread))
         setExceptionPorts(locker, thread);
 }
 
-void unregisterThreadForMachExceptionHandling(Thread* thread)
-{
-    auto locker = holdLock(threadLock);
-    activeThreads(locker).remove(thread);
-}
-
 #else
 static constexpr bool useMach = false;
 #endif // HAVE(MACH_EXCEPTIONS)
@@ -292,12 +284,12 @@
     handlers[static_cast<size_t>(signal)]->add(WTFMove(handler));
 
 #if HAVE(MACH_EXCEPTIONS)
-    auto locker = holdLock(threadLock);
+    auto locker = holdLock(activeThreads().getLock());
     if (useMach) {
         activeExceptions |= toMachMask(signal);
 
-        for (Thread* thread : activeThreads(locker))
-            setExceptionPorts(locker, thread);
+        for (auto& thread : activeThreads().threads(locker))
+            setExceptionPorts(locker, thread.get());
     }
 #endif
 }

Modified: trunk/Source/WTF/wtf/threads/Signals.h (219695 => 219696)


--- trunk/Source/WTF/wtf/threads/Signals.h	2017-07-20 18:07:30 UTC (rev 219695)
+++ trunk/Source/WTF/wtf/threads/Signals.h	2017-07-20 18:32:13 UTC (rev 219696)
@@ -91,8 +91,7 @@
 
 #if HAVE(MACH_EXCEPTIONS)
 class Thread;
-void registerThreadForMachExceptionHandling(Thread*);
-void unregisterThreadForMachExceptionHandling(Thread*);
+void registerThreadForMachExceptionHandling(Thread&);
 
 void handleSignalsWithMach();
 #endif // HAVE(MACH_EXCEPTIONS)
@@ -101,7 +100,6 @@
 
 #if HAVE(MACH_EXCEPTIONS)
 using WTF::registerThreadForMachExceptionHandling;
-using WTF::unregisterThreadForMachExceptionHandling;
 using WTF::handleSignalsWithMach;
 #endif // HAVE(MACH_EXCEPTIONS)
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to