Don't use DEPRECATED_DEFINE_STATIC_LOCAL for mutexes
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Sep 2014 22:56:48 +0000 (22:56 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Sep 2014 22:56:48 +0000 (22:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=136510

Reviewed by Andreas Kling.

Mutexes are intended to be used from multiple threads, and DEPRECATED_DEFINE_STATIC_LOCAL is not thread safe.

* bindings/objc/DOMInternal.mm:
(wrapperCacheLock):
(getDOMWrapper):
(addDOMWrapper):
(removeDOMWrapper):
Use LazyNeverDestroyed + std::call_once, and switch the mutex over to an std::mutex.

* dom/EventListenerMap.cpp:
(WebCore::EventListenerMap::assertNoActiveIterators):
(WebCore::EventListenerIterator::EventListenerIterator):
(WebCore::EventListenerIterator::~EventListenerIterator):
(WebCore::activeIteratorCountMutex):
(WebCore::EventListenerMap::EventListenerMap): Deleted.
* dom/EventListenerMap.h:
Use an std::atomic<int> instead of a mutex here.

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

Source/WebCore/ChangeLog
Source/WebCore/bindings/objc/DOMInternal.mm
Source/WebCore/dom/EventListenerMap.cpp
Source/WebCore/dom/EventListenerMap.h

index b97f0bf..913311b 100644 (file)
@@ -1,3 +1,28 @@
+2014-09-03  Anders Carlsson  <andersca@apple.com>
+
+        Don't use DEPRECATED_DEFINE_STATIC_LOCAL for mutexes
+        https://bugs.webkit.org/show_bug.cgi?id=136510
+
+        Reviewed by Andreas Kling.
+
+        Mutexes are intended to be used from multiple threads, and DEPRECATED_DEFINE_STATIC_LOCAL is not thread safe.
+
+        * bindings/objc/DOMInternal.mm:
+        (wrapperCacheLock):
+        (getDOMWrapper):
+        (addDOMWrapper):
+        (removeDOMWrapper):
+        Use LazyNeverDestroyed + std::call_once, and switch the mutex over to an std::mutex.
+
+        * dom/EventListenerMap.cpp:
+        (WebCore::EventListenerMap::assertNoActiveIterators):
+        (WebCore::EventListenerIterator::EventListenerIterator):
+        (WebCore::EventListenerIterator::~EventListenerIterator):
+        (WebCore::activeIteratorCountMutex):
+        (WebCore::EventListenerMap::EventListenerMap): Deleted.
+        * dom/EventListenerMap.h:
+        Use an std::atomic<int> instead of a mutex here.
+
 2014-09-03  Enrica Casucci  <enrica@apple.com>
 
         Remove PLATFORM(IOS) from WebCore/editing (Part 1).
 2014-09-03  Enrica Casucci  <enrica@apple.com>
 
         Remove PLATFORM(IOS) from WebCore/editing (Part 1).
index bb3fc69..5e77361 100644 (file)
@@ -32,6 +32,7 @@
 #import "ScriptController.h"
 #import "WebScriptObjectPrivate.h"
 #import "runtime_root.h"
 #import "ScriptController.h"
 #import "WebScriptObjectPrivate.h"
 #import "runtime_root.h"
+#import <wtf/NeverDestroyed.h>
 
 #if PLATFORM(IOS)
 #define NEEDS_WRAPPER_CACHE_LOCK 1
 
 #if PLATFORM(IOS)
 #define NEEDS_WRAPPER_CACHE_LOCK 1
 static NSMapTable* DOMWrapperCache;
     
 #ifdef NEEDS_WRAPPER_CACHE_LOCK
 static NSMapTable* DOMWrapperCache;
     
 #ifdef NEEDS_WRAPPER_CACHE_LOCK
-static Mutex& wrapperCacheLock()
+static std::mutex& wrapperCacheLock()
 {
 {
-    DEPRECATED_DEFINE_STATIC_LOCAL(Mutex, wrapperCacheMutex, ());
-    return wrapperCacheMutex;
+    static std::once_flag onceFlag;
+    static LazyNeverDestroyed<std::mutex> mutex;
+
+    std::call_once(onceFlag, [] {
+        mutex.construct();
+    });
+    return mutex;
 }
 #endif
 
 }
 #endif
 
@@ -70,7 +76,7 @@ NSMapTable* createWrapperCache()
 NSObject* getDOMWrapper(DOMObjectInternal* impl)
 {
 #ifdef NEEDS_WRAPPER_CACHE_LOCK
 NSObject* getDOMWrapper(DOMObjectInternal* impl)
 {
 #ifdef NEEDS_WRAPPER_CACHE_LOCK
-    MutexLocker locker(wrapperCacheLock());
+    std::lock_guard<std::mutex> lock(wrapperCacheLock());
 #endif
     if (!DOMWrapperCache)
         return nil;
 #endif
     if (!DOMWrapperCache)
         return nil;
@@ -80,7 +86,7 @@ NSObject* getDOMWrapper(DOMObjectInternal* impl)
 void addDOMWrapper(NSObject* wrapper, DOMObjectInternal* impl)
 {
 #ifdef NEEDS_WRAPPER_CACHE_LOCK
 void addDOMWrapper(NSObject* wrapper, DOMObjectInternal* impl)
 {
 #ifdef NEEDS_WRAPPER_CACHE_LOCK
-    MutexLocker locker(wrapperCacheLock());
+    std::lock_guard<std::mutex> lock(wrapperCacheLock());
 #endif
     if (!DOMWrapperCache)
         DOMWrapperCache = createWrapperCache();
 #endif
     if (!DOMWrapperCache)
         DOMWrapperCache = createWrapperCache();
@@ -90,7 +96,7 @@ void addDOMWrapper(NSObject* wrapper, DOMObjectInternal* impl)
 void removeDOMWrapper(DOMObjectInternal* impl)
 {
 #ifdef NEEDS_WRAPPER_CACHE_LOCK
 void removeDOMWrapper(DOMObjectInternal* impl)
 {
 #ifdef NEEDS_WRAPPER_CACHE_LOCK
-    MutexLocker locker(wrapperCacheLock());
+    std::lock_guard<std::mutex> lock(wrapperCacheLock());
 #endif
     if (!DOMWrapperCache)
         return;
 #endif
     if (!DOMWrapperCache)
         return;
index 08541b0..b60535b 100644 (file)
 #include "EventException.h"
 #include "EventTarget.h"
 #include <wtf/MainThread.h>
 #include "EventException.h"
 #include "EventTarget.h"
 #include <wtf/MainThread.h>
+#include <wtf/NeverDestroyed.h>
 #include <wtf/StdLibExtras.h>
 #include <wtf/Vector.h>
 
 #include <wtf/StdLibExtras.h>
 #include <wtf/Vector.h>
 
-#ifndef NDEBUG
-#include <wtf/Threading.h>
-#endif
-
 using namespace WTF;
 
 namespace WebCore {
 
 #ifndef NDEBUG
 using namespace WTF;
 
 namespace WebCore {
 
 #ifndef NDEBUG
-static Mutex& activeIteratorCountMutex()
-{
-    DEPRECATED_DEFINE_STATIC_LOCAL(Mutex, mutex, ());
-    return mutex;
-}
-
 void EventListenerMap::assertNoActiveIterators()
 {
 void EventListenerMap::assertNoActiveIterators()
 {
-    MutexLocker locker(activeIteratorCountMutex());
     ASSERT(!m_activeIteratorCount);
 }
 #endif
 
 EventListenerMap::EventListenerMap()
     ASSERT(!m_activeIteratorCount);
 }
 #endif
 
 EventListenerMap::EventListenerMap()
-#ifndef NDEBUG
-    : m_activeIteratorCount(0)
-#endif
 {
 }
 
 {
 }
 
@@ -240,20 +227,15 @@ EventListenerIterator::EventListenerIterator(EventTarget* target)
     m_map = &data->eventListenerMap;
 
 #ifndef NDEBUG
     m_map = &data->eventListenerMap;
 
 #ifndef NDEBUG
-    {
-        MutexLocker locker(activeIteratorCountMutex());
-        m_map->m_activeIteratorCount++;
-    }
+    m_map->m_activeIteratorCount++;
 #endif
 }
 
 #ifndef NDEBUG
 EventListenerIterator::~EventListenerIterator()
 {
 #endif
 }
 
 #ifndef NDEBUG
 EventListenerIterator::~EventListenerIterator()
 {
-    if (m_map) {
-        MutexLocker locker(activeIteratorCountMutex());
+    if (m_map)
         m_map->m_activeIteratorCount--;
         m_map->m_activeIteratorCount--;
-    }
 }
 #endif
 
 }
 #endif
 
index 76b30ab..f3ed103 100644 (file)
@@ -69,7 +69,7 @@ private:
     Vector<std::pair<AtomicString, std::unique_ptr<EventListenerVector>>, 2> m_entries;
 
 #ifndef NDEBUG
     Vector<std::pair<AtomicString, std::unique_ptr<EventListenerVector>>, 2> m_entries;
 
 #ifndef NDEBUG
-    int m_activeIteratorCount;
+    std::atomic<int> m_activeIteratorCount { 0 };
 #endif
 };
 
 #endif
 };