Convert some JSC code over to std::mutex
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 9 Feb 2014 18:47:28 +0000 (18:47 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 9 Feb 2014 18:47:28 +0000 (18:47 +0000)
https://bugs.webkit.org/show_bug.cgi?id=128500

Reviewed by Dan Bernstein.

* API/JSVirtualMachine.mm:
(wrapperCacheMutex):
(+[JSVMWrapperCache addWrapper:forJSContextGroupRef:]):
(+[JSVMWrapperCache wrapperForJSContextGroupRef:]):
* heap/GCThreadSharedData.h:
* heap/SlotVisitor.cpp:
(JSC::SlotVisitor::mergeOpaqueRoots):
* heap/SlotVisitorInlines.h:
(JSC::SlotVisitor::containsOpaqueRootTriState):
* inspector/remote/RemoteInspector.h:
* inspector/remote/RemoteInspector.mm:
(Inspector::RemoteInspector::registerDebuggable):
(Inspector::RemoteInspector::unregisterDebuggable):
(Inspector::RemoteInspector::updateDebuggable):
(Inspector::RemoteInspector::sendMessageToRemoteFrontend):
(Inspector::RemoteInspector::start):
(Inspector::RemoteInspector::stop):
(Inspector::RemoteInspector::setupXPCConnectionIfNeeded):
(Inspector::RemoteInspector::xpcConnectionReceivedMessage):
(Inspector::RemoteInspector::xpcConnectionFailed):
(Inspector::RemoteInspector::pushListingSoon):
(Inspector::RemoteInspector::receivedIndicateMessage):
* inspector/remote/RemoteInspectorDebuggableConnection.h:
* inspector/remote/RemoteInspectorDebuggableConnection.mm:
(Inspector::RemoteInspectorDebuggableConnection::setup):
(Inspector::RemoteInspectorDebuggableConnection::closeFromDebuggable):
(Inspector::RemoteInspectorDebuggableConnection::close):
(Inspector::RemoteInspectorDebuggableConnection::sendMessageToBackend):
* jit/ExecutableAllocator.cpp:
(JSC::DemandExecutableAllocator::DemandExecutableAllocator):
(JSC::DemandExecutableAllocator::~DemandExecutableAllocator):
(JSC::DemandExecutableAllocator::bytesAllocatedByAllAllocators):
(JSC::DemandExecutableAllocator::bytesCommittedByAllocactors):
(JSC::DemandExecutableAllocator::dumpProfileFromAllAllocators):
(JSC::DemandExecutableAllocator::allocatorsMutex):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@163752 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/API/JSVirtualMachine.mm
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/heap/GCThreadSharedData.h
Source/JavaScriptCore/heap/SlotVisitor.cpp
Source/JavaScriptCore/heap/SlotVisitorInlines.h
Source/JavaScriptCore/inspector/remote/RemoteInspector.h
Source/JavaScriptCore/inspector/remote/RemoteInspector.mm
Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h
Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm
Source/JavaScriptCore/jit/ExecutableAllocator.cpp

index f3d53af..1cd94c9 100644 (file)
 #import "JSVirtualMachineInternal.h"
 #import "JSWrapperMap.h"
 #import "SlotVisitorInlines.h"
+#import <mutex>
+#import <wtf/NeverDestroyed.h>
 
 static NSMapTable *globalWrapperCache = 0;
 
-static Mutex& wrapperCacheLock()
+static std::mutex& wrapperCacheMutex()
 {
-    DEFINE_STATIC_LOCAL(Mutex, mutex, ());
+    static NeverDestroyed<std::mutex> mutex;
+
     return mutex;
 }
 
@@ -69,13 +72,13 @@ static NSMapTable *wrapperCache()
 
 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group
 {
-    MutexLocker locker(wrapperCacheLock());
+    std::lock_guard<std::mutex> lock(wrapperCacheMutex());
     NSMapInsert(wrapperCache(), group, wrapper);
 }
 
 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group
 {
-    MutexLocker locker(wrapperCacheLock());
+    std::lock_guard<std::mutex> lock(wrapperCacheMutex());
     return static_cast<JSVirtualMachine *>(NSMapGet(wrapperCache(), group));
 }
 
index 6f97d53..39f6fbc 100644 (file)
@@ -1,3 +1,46 @@
+2014-02-09  Anders Carlsson  <andersca@apple.com>
+
+        Convert some JSC code over to std::mutex
+        https://bugs.webkit.org/show_bug.cgi?id=128500
+
+        Reviewed by Dan Bernstein.
+
+        * API/JSVirtualMachine.mm:
+        (wrapperCacheMutex):
+        (+[JSVMWrapperCache addWrapper:forJSContextGroupRef:]):
+        (+[JSVMWrapperCache wrapperForJSContextGroupRef:]):
+        * heap/GCThreadSharedData.h:
+        * heap/SlotVisitor.cpp:
+        (JSC::SlotVisitor::mergeOpaqueRoots):
+        * heap/SlotVisitorInlines.h:
+        (JSC::SlotVisitor::containsOpaqueRootTriState):
+        * inspector/remote/RemoteInspector.h:
+        * inspector/remote/RemoteInspector.mm:
+        (Inspector::RemoteInspector::registerDebuggable):
+        (Inspector::RemoteInspector::unregisterDebuggable):
+        (Inspector::RemoteInspector::updateDebuggable):
+        (Inspector::RemoteInspector::sendMessageToRemoteFrontend):
+        (Inspector::RemoteInspector::start):
+        (Inspector::RemoteInspector::stop):
+        (Inspector::RemoteInspector::setupXPCConnectionIfNeeded):
+        (Inspector::RemoteInspector::xpcConnectionReceivedMessage):
+        (Inspector::RemoteInspector::xpcConnectionFailed):
+        (Inspector::RemoteInspector::pushListingSoon):
+        (Inspector::RemoteInspector::receivedIndicateMessage):
+        * inspector/remote/RemoteInspectorDebuggableConnection.h:
+        * inspector/remote/RemoteInspectorDebuggableConnection.mm:
+        (Inspector::RemoteInspectorDebuggableConnection::setup):
+        (Inspector::RemoteInspectorDebuggableConnection::closeFromDebuggable):
+        (Inspector::RemoteInspectorDebuggableConnection::close):
+        (Inspector::RemoteInspectorDebuggableConnection::sendMessageToBackend):
+        * jit/ExecutableAllocator.cpp:
+        (JSC::DemandExecutableAllocator::DemandExecutableAllocator):
+        (JSC::DemandExecutableAllocator::~DemandExecutableAllocator):
+        (JSC::DemandExecutableAllocator::bytesAllocatedByAllAllocators):
+        (JSC::DemandExecutableAllocator::bytesCommittedByAllocactors):
+        (JSC::DemandExecutableAllocator::dumpProfileFromAllAllocators):
+        (JSC::DemandExecutableAllocator::allocatorsMutex):
+
 2014-02-09  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r163737.
index 915c2c9..83624e5 100644 (file)
@@ -92,7 +92,7 @@ private:
     unsigned m_numberOfActiveParallelMarkers;
     bool m_parallelMarkersShouldExit;
 
-    Mutex m_opaqueRootsLock;
+    std::mutex m_opaqueRootsMutex;
     HashSet<void*> m_opaqueRoots;
 
     SpinLock m_copyLock;
index b912925..0a7eea3 100644 (file)
@@ -240,11 +240,9 @@ void SlotVisitor::mergeOpaqueRoots()
     StackStats::probe();
     ASSERT(!m_opaqueRoots.isEmpty()); // Should only be called when opaque roots are non-empty.
     {
-        MutexLocker locker(m_shared.m_opaqueRootsLock);
-        HashSet<void*>::iterator begin = m_opaqueRoots.begin();
-        HashSet<void*>::iterator end = m_opaqueRoots.end();
-        for (HashSet<void*>::iterator iter = begin; iter != end; ++iter)
-            m_shared.m_opaqueRoots.add(*iter);
+        std::lock_guard<std::mutex> lock(m_shared.m_opaqueRootsMutex);
+        for (auto* root : m_opaqueRoots)
+            m_shared.m_opaqueRoots.add(root);
     }
     m_opaqueRoots.clear();
 }
index ccd2e4a..53e8832 100644 (file)
@@ -176,7 +176,7 @@ inline TriState SlotVisitor::containsOpaqueRootTriState(void* root)
 {
     if (m_opaqueRoots.contains(root))
         return TrueTriState;
-    MutexLocker locker(m_shared.m_opaqueRootsLock);
+    std::lock_guard<std::mutex> lock(m_shared.m_opaqueRootsMutex);
     if (m_shared.m_opaqueRoots.contains(root))
         return TrueTriState;
     return MixedTriState;
index 4f3f4e6..95d9b64 100644 (file)
@@ -31,8 +31,7 @@
 #import "RemoteInspectorXPCConnection.h"
 #import <wtf/Forward.h>
 #import <wtf/HashMap.h>
-#import <wtf/NeverDestroyed.h>
-#import <wtf/Threading.h>
+#import <wtf/Forward.h>
 
 OBJC_CLASS NSDictionary;
 OBJC_CLASS NSString;
@@ -90,7 +89,7 @@ private:
     // Any debuggable can send messages over the XPC connection.
     // So lock access to all maps and state as they can change
     // from any thread.
-    Mutex m_lock;
+    std::mutex m_mutex;
 
     HashMap<unsigned, std::pair<RemoteInspectorDebuggable*, RemoteInspectorDebuggableInfo>> m_debuggableMap;
     HashMap<unsigned, RefPtr<RemoteInspectorDebuggableConnection>> m_connectionMap;
index 153dd2e..3676277 100644 (file)
@@ -34,6 +34,7 @@
 #import "RemoteInspectorDebuggableConnection.h"
 #import <Foundation/Foundation.h>
 #import <notify.h>
+#import <wtf/NeverDestroyed.h>
 #import <wtf/Assertions.h>
 #import <wtf/MainThread.h>
 #import <wtf/text/WTFString.h>
@@ -100,7 +101,7 @@ unsigned RemoteInspector::nextAvailableIdentifier()
 
 void RemoteInspector::registerDebuggable(RemoteInspectorDebuggable* debuggable)
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
     unsigned identifier = nextAvailableIdentifier();
     debuggable->setIdentifier(identifier);
@@ -114,7 +115,7 @@ void RemoteInspector::registerDebuggable(RemoteInspectorDebuggable* debuggable)
 
 void RemoteInspector::unregisterDebuggable(RemoteInspectorDebuggable* debuggable)
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
     unsigned identifier = debuggable->identifier();
     if (!identifier)
@@ -132,7 +133,7 @@ void RemoteInspector::unregisterDebuggable(RemoteInspectorDebuggable* debuggable
 
 void RemoteInspector::updateDebuggable(RemoteInspectorDebuggable* debuggable)
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
     unsigned identifier = debuggable->identifier();
     if (!identifier)
@@ -146,7 +147,7 @@ void RemoteInspector::updateDebuggable(RemoteInspectorDebuggable* debuggable)
 
 void RemoteInspector::sendMessageToRemoteFrontend(unsigned identifier, const String& message)
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
     if (!m_xpcConnection)
         return;
@@ -166,7 +167,7 @@ void RemoteInspector::sendMessageToRemoteFrontend(unsigned identifier, const Str
 
 void RemoteInspector::start()
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
     if (m_enabled)
         return;
@@ -182,7 +183,7 @@ void RemoteInspector::start()
 
 void RemoteInspector::stop()
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
     if (!m_enabled)
         return;
@@ -207,7 +208,7 @@ void RemoteInspector::stop()
 
 void RemoteInspector::setupXPCConnectionIfNeeded()
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
     if (m_xpcConnection)
         return;
@@ -227,7 +228,7 @@ void RemoteInspector::setupXPCConnectionIfNeeded()
 
 void RemoteInspector::xpcConnectionReceivedMessage(RemoteInspectorXPCConnection*, NSString *messageName, NSDictionary *userInfo)
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
 
     if ([messageName isEqualToString:WIRPermissionDenied]) {
         stop();
@@ -252,7 +253,7 @@ void RemoteInspector::xpcConnectionReceivedMessage(RemoteInspectorXPCConnection*
 
 void RemoteInspector::xpcConnectionFailed(RemoteInspectorXPCConnection* connection)
 {
-    MutexLocker locker(m_lock);
+    std::lock_guard<std::mutex> lock(m_mutex);
     if (connection != m_xpcConnection)
         return;
 
@@ -350,7 +351,7 @@ void RemoteInspector::pushListingSoon()
 
     m_pushScheduled = true;
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.02 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-        MutexLocker locker(m_lock);
+        std::lock_guard<std::mutex> lock(m_mutex);
         if (m_pushScheduled)
             pushListingNow();
     });
@@ -467,7 +468,7 @@ void RemoteInspector::receivedIndicateMessage(NSDictionary *userInfo)
     BOOL indicateEnabled = [[userInfo objectForKey:WIRIndicateEnabledKey] boolValue];
 
     dispatchAsyncOnQueueSafeForAnyDebuggable(^{
-        MutexLocker locker(m_lock);
+        std::lock_guard<std::mutex> lock(m_mutex);
 
         auto it = m_debuggableMap.find(identifier);
         if (it == m_debuggableMap.end())
index 2acb66a..34b748b 100644 (file)
@@ -31,8 +31,8 @@
 #import "InspectorFrontendChannel.h"
 #import "RemoteInspectorDebuggable.h"
 #import <dispatch/dispatch.h>
+#import <mutex>
 #import <wtf/RetainPtr.h>
-#import <wtf/Threading.h>
 #import <wtf/ThreadSafeRefCounted.h>
 
 OBJC_CLASS NSString;
@@ -62,8 +62,8 @@ private:
 
     // This connection from the RemoteInspector singleton to the Debuggable
     // can be used on multiple threads. So any access to the debuggable
-    // itself must take this lock to ensure m_debuggable is valid.
-    Mutex m_debuggableLock;
+    // itself must take this mutex to ensure m_debuggable is valid.
+    std::mutex m_debuggableMutex;
 
     RemoteInspectorDebuggable* m_debuggable;
     dispatch_queue_t m_queueForDebuggable;
index 5fffea7..bdf215e 100644 (file)
@@ -94,7 +94,7 @@ void RemoteInspectorDebuggableConnection::dispatchAsyncOnDebuggable(void (^block
 
 bool RemoteInspectorDebuggableConnection::setup()
 {
-    MutexLocker locker(m_debuggableLock);
+    std::lock_guard<std::mutex> lock(m_debuggableMutex);
 
     if (!m_debuggable)
         return false;
@@ -115,7 +115,7 @@ bool RemoteInspectorDebuggableConnection::setup()
 
 void RemoteInspectorDebuggableConnection::closeFromDebuggable()
 {
-    MutexLocker locker(m_debuggableLock);
+    std::lock_guard<std::mutex> lock(m_debuggableMutex);
 
     m_debuggable = nullptr;
 }
@@ -125,7 +125,7 @@ void RemoteInspectorDebuggableConnection::close()
     ref();
     dispatchAsyncOnDebuggable(^{
         {
-            MutexLocker locker(m_debuggableLock);
+            std::lock_guard<std::mutex> lock(m_debuggableMutex);
 
             if (m_debuggable) {
                 if (m_connected)
@@ -143,7 +143,7 @@ void RemoteInspectorDebuggableConnection::sendMessageToBackend(NSString *message
     ref();
     dispatchAsyncOnDebuggable(^{
         {
-            MutexLocker locker(m_debuggableLock);
+            std::lock_guard<std::mutex> lock(m_debuggableMutex);
 
             if (m_debuggable)
                 m_debuggable->dispatchMessageFromRemoteFrontend(message);
index 5ac6cc4..7bb25d8 100644 (file)
@@ -31,6 +31,7 @@
 #include "CodeProfiling.h"
 #include <wtf/HashSet.h>
 #include <wtf/MetaAllocator.h>
+#include <wtf/NeverDestroyed.h>
 #include <wtf/PageReservation.h>
 #if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
 #include <wtf/PassOwnPtr.h>
@@ -57,7 +58,7 @@ public:
     DemandExecutableAllocator()
         : MetaAllocator(jitAllocationGranule)
     {
-        MutexLocker lock(allocatorsMutex());
+        std::lock_guard<std::mutex> lock(allocatorsMutex());
         allocators().add(this);
         // Don't preallocate any memory here.
     }
@@ -65,7 +66,7 @@ public:
     virtual ~DemandExecutableAllocator()
     {
         {
-            MutexLocker lock(allocatorsMutex());
+            std::lock_guard<std::mutex> lock(allocatorsMutex());
             allocators().remove(this);
         }
         for (unsigned i = 0; i < reservations.size(); ++i)
@@ -75,7 +76,7 @@ public:
     static size_t bytesAllocatedByAllAllocators()
     {
         size_t total = 0;
-        MutexLocker lock(allocatorsMutex());
+        std::lock_guard<std::mutex> lock(allocatorsMutex());
         for (HashSet<DemandExecutableAllocator*>::const_iterator allocator = allocators().begin(); allocator != allocators().end(); ++allocator)
             total += (*allocator)->bytesAllocated();
         return total;
@@ -84,7 +85,7 @@ public:
     static size_t bytesCommittedByAllocactors()
     {
         size_t total = 0;
-        MutexLocker lock(allocatorsMutex());
+        std::lock_guard<std::mutex> lock(allocatorsMutex());
         for (HashSet<DemandExecutableAllocator*>::const_iterator allocator = allocators().begin(); allocator != allocators().end(); ++allocator)
             total += (*allocator)->bytesCommitted();
         return total;
@@ -93,7 +94,7 @@ public:
 #if ENABLE(META_ALLOCATOR_PROFILE)
     static void dumpProfileFromAllAllocators()
     {
-        MutexLocker lock(allocatorsMutex());
+        std::lock_guard<std::mutex> lock(allocatorsMutex());
         for (HashSet<DemandExecutableAllocator*>::const_iterator allocator = allocators().begin(); allocator != allocators().end(); ++allocator)
             (*allocator)->dumpProfile();
     }
@@ -138,9 +139,11 @@ private:
         DEFINE_STATIC_LOCAL(HashSet<DemandExecutableAllocator*>, sAllocators, ());
         return sAllocators;
     }
-    static Mutex& allocatorsMutex()
+
+    static std::mutex& allocatorsMutex()
     {
-        DEFINE_STATIC_LOCAL(Mutex, mutex, ());
+        static NeverDestroyed<std::mutex> mutex;
+
         return mutex;
     }
 };