Title: [161818] trunk/Source
Revision
161818
Author
ander...@apple.com
Date
2014-01-12 11:45:00 -0800 (Sun, 12 Jan 2014)

Log Message

Replace more uses of AtomicallyInitializedStatic with std::call_once
https://bugs.webkit.org/show_bug.cgi?id=126847

Reviewed by Sam Weinig.

Source/WebCore:

* crypto/CryptoAlgorithmRegistry.cpp:
(WebCore::CryptoAlgorithmRegistry::shared):
(WebCore::registryMutex):
(WebCore::CryptoAlgorithmRegistry::getIdentifierForName):
(WebCore::CryptoAlgorithmRegistry::nameForIdentifier):
(WebCore::CryptoAlgorithmRegistry::create):
(WebCore::CryptoAlgorithmRegistry::registerAlgorithm):
* crypto/CryptoAlgorithmRegistry.h:
* inspector/WorkerDebuggerAgent.cpp:
(WebCore::WorkerDebuggerAgent::WorkerDebuggerAgent):
(WebCore::WorkerDebuggerAgent::~WorkerDebuggerAgent):
(WebCore::WorkerDebuggerAgent::interruptAndDispatchInspectorCommands):
* workers/WorkerThread.cpp:
(WebCore::threadSetMutex):
(WebCore::workerThreads):
(WebCore::WorkerThread::workerThreadCount):
(WebCore::WorkerThread::WorkerThread):
(WebCore::WorkerThread::~WorkerThread):
(WebCore::WorkerThread::releaseFastMallocFreeMemoryInAllThreads):

Source/WTF:

* wtf/Forward.h:
Forward declare NeverDestroyed.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (161817 => 161818)


--- trunk/Source/WTF/ChangeLog	2014-01-12 19:24:29 UTC (rev 161817)
+++ trunk/Source/WTF/ChangeLog	2014-01-12 19:45:00 UTC (rev 161818)
@@ -1,3 +1,13 @@
+2014-01-12  Anders Carlsson  <ander...@apple.com>
+
+        Replace more uses of AtomicallyInitializedStatic with std::call_once
+        https://bugs.webkit.org/show_bug.cgi?id=126847
+
+        Reviewed by Sam Weinig.
+
+        * wtf/Forward.h:
+        Forward declare NeverDestroyed.
+
 2014-01-11  Zan Dobersek  <zdober...@igalia.com>
 
         Unreviewed build fix for ports using GCC after r161770.

Modified: trunk/Source/WTF/wtf/Forward.h (161817 => 161818)


--- trunk/Source/WTF/wtf/Forward.h	2014-01-12 19:24:29 UTC (rev 161817)
+++ trunk/Source/WTF/wtf/Forward.h	2014-01-12 19:45:00 UTC (rev 161818)
@@ -26,6 +26,7 @@
 namespace WTF {
 
 template<typename T> class Function;
+template<typename T> class NeverDestroyed;
 template<typename T> class OwnPtr;
 template<typename T> class PassOwnPtr;
 template<typename T> class PassRef;
@@ -59,6 +60,7 @@
 using WTF::Encoder;
 using WTF::Function;
 using WTF::FunctionDispatcher;
+using WTF::NeverDestroyed;
 using WTF::OwnPtr;
 using WTF::PassOwnPtr;
 using WTF::PassRef;

Modified: trunk/Source/WebCore/ChangeLog (161817 => 161818)


--- trunk/Source/WebCore/ChangeLog	2014-01-12 19:24:29 UTC (rev 161817)
+++ trunk/Source/WebCore/ChangeLog	2014-01-12 19:45:00 UTC (rev 161818)
@@ -1,3 +1,30 @@
+2014-01-12  Anders Carlsson  <ander...@apple.com>
+
+        Replace more uses of AtomicallyInitializedStatic with std::call_once
+        https://bugs.webkit.org/show_bug.cgi?id=126847
+
+        Reviewed by Sam Weinig.
+
+        * crypto/CryptoAlgorithmRegistry.cpp:
+        (WebCore::CryptoAlgorithmRegistry::shared):
+        (WebCore::registryMutex):
+        (WebCore::CryptoAlgorithmRegistry::getIdentifierForName):
+        (WebCore::CryptoAlgorithmRegistry::nameForIdentifier):
+        (WebCore::CryptoAlgorithmRegistry::create):
+        (WebCore::CryptoAlgorithmRegistry::registerAlgorithm):
+        * crypto/CryptoAlgorithmRegistry.h:
+        * inspector/WorkerDebuggerAgent.cpp:
+        (WebCore::WorkerDebuggerAgent::WorkerDebuggerAgent):
+        (WebCore::WorkerDebuggerAgent::~WorkerDebuggerAgent):
+        (WebCore::WorkerDebuggerAgent::interruptAndDispatchInspectorCommands):
+        * workers/WorkerThread.cpp:
+        (WebCore::threadSetMutex):
+        (WebCore::workerThreads):
+        (WebCore::WorkerThread::workerThreadCount):
+        (WebCore::WorkerThread::WorkerThread):
+        (WebCore::WorkerThread::~WorkerThread):
+        (WebCore::WorkerThread::releaseFastMallocFreeMemoryInAllThreads):
+
 2014-01-11  Sam Weinig  <s...@webkit.org>
 
         Split ICU UText providers out into their own files

Modified: trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.cpp (161817 => 161818)


--- trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.cpp	2014-01-12 19:24:29 UTC (rev 161817)
+++ trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.cpp	2014-01-12 19:45:00 UTC (rev 161818)
@@ -29,20 +29,28 @@
 #if ENABLE(SUBTLE_CRYPTO)
 
 #include "CryptoAlgorithm.h"
-#include <wtf/MainThread.h>
+#include <mutex>
+#include <wtf/NeverDestroyed.h>
 
 namespace WebCore {
 
 CryptoAlgorithmRegistry& CryptoAlgorithmRegistry::shared()
 {
-    DEFINE_STATIC_LOCAL(CryptoAlgorithmRegistry, registry, ());
+    static NeverDestroyed<CryptoAlgorithmRegistry> registry;
+
     return registry;
 }
 
-static Mutex& registryMutex()
+static std::mutex& registryMutex()
 {
-    AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
-    return mutex;
+    static std::once_flag onceFlag;
+    static std::mutex* mutex;
+
+    std::call_once(onceFlag, []{
+        mutex = std::make_unique<std::mutex>().release();
+    });
+
+    return *mutex;
 }
 
 CryptoAlgorithmRegistry::CryptoAlgorithmRegistry()
@@ -55,7 +63,7 @@
     if (name.isEmpty())
         return false;
 
-    MutexLocker lock(registryMutex());
+    std::lock_guard<std::mutex> lock(registryMutex());
 
     auto iter = m_nameToIdentifierMap.find(name.isolatedCopy());
     if (iter == m_nameToIdentifierMap.end())
@@ -67,14 +75,14 @@
 
 String CryptoAlgorithmRegistry::nameForIdentifier(CryptoAlgorithmIdentifier identifier)
 {
-    MutexLocker lock(registryMutex());
+    std::lock_guard<std::mutex> lock(registryMutex());
 
     return m_identifierToNameMap.get(static_cast<unsigned>(identifier)).isolatedCopy();
 }
 
 std::unique_ptr<CryptoAlgorithm> CryptoAlgorithmRegistry::create(CryptoAlgorithmIdentifier identifier)
 {
-    MutexLocker lock(registryMutex());
+    std::lock_guard<std::mutex> lock(registryMutex());
 
     auto iter = m_identifierToConstructorMap.find(static_cast<unsigned>(identifier));
     if (iter == m_identifierToConstructorMap.end())
@@ -85,7 +93,7 @@
 
 void CryptoAlgorithmRegistry::registerAlgorithm(const String& name, CryptoAlgorithmIdentifier identifier, CryptoAlgorithmConstructor constructor)
 {
-    MutexLocker lock(registryMutex());
+    std::lock_guard<std::mutex> lock(registryMutex());
 
     bool added = m_nameToIdentifierMap.add(name, identifier).isNewEntry;
     ASSERT_UNUSED(added, added);

Modified: trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.h (161817 => 161818)


--- trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.h	2014-01-12 19:24:29 UTC (rev 161817)
+++ trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.h	2014-01-12 19:45:00 UTC (rev 161818)
@@ -42,6 +42,7 @@
 
 class CryptoAlgorithmRegistry {
     WTF_MAKE_NONCOPYABLE(CryptoAlgorithmRegistry);
+    friend class NeverDestroyed<CryptoAlgorithmRegistry>;
 
 public:
     static CryptoAlgorithmRegistry& shared();
@@ -63,6 +64,7 @@
     }
 
     void registerAlgorithm(const String& name, CryptoAlgorithmIdentifier, CryptoAlgorithmConstructor);
+
     HashMap<String, CryptoAlgorithmIdentifier, CaseFoldingHash> m_nameToIdentifierMap;
     HashMap<unsigned, String> m_identifierToNameMap;
     HashMap<unsigned, CryptoAlgorithmConstructor> m_identifierToConstructorMap;

Modified: trunk/Source/WebCore/inspector/WorkerDebuggerAgent.cpp (161817 => 161818)


--- trunk/Source/WebCore/inspector/WorkerDebuggerAgent.cpp	2014-01-12 19:24:29 UTC (rev 161817)
+++ trunk/Source/WebCore/inspector/WorkerDebuggerAgent.cpp	2014-01-12 19:45:00 UTC (rev 161818)
@@ -38,7 +38,9 @@
 #include "WorkerThread.h"
 #include <inspector/InjectedScript.h>
 #include <inspector/InjectedScriptManager.h>
+#include <mutex>
 #include <wtf/MessageQueue.h>
+#include <wtf/NeverDestroyed.h>
 
 using namespace Inspector;
 
@@ -46,21 +48,27 @@
 
 namespace {
 
-Mutex& workerDebuggerAgentsMutex()
+std::mutex& workerDebuggerAgentsMutex()
 {
-    AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
-    return mutex;
+    static std::once_flag onceFlag;
+    static std::mutex* mutex;
+
+    std::call_once(onceFlag, []{
+        mutex = std::make_unique<std::mutex>().release();
+    });
+
+    return *mutex;
 }
 
 typedef HashMap<WorkerThread*, WorkerDebuggerAgent*> WorkerDebuggerAgents;
 
 WorkerDebuggerAgents& workerDebuggerAgents()
 {
-    DEFINE_STATIC_LOCAL(WorkerDebuggerAgents, agents, ());
+    static NeverDestroyed<WorkerDebuggerAgents> agents;
+
     return agents;
 }
 
-
 class RunInspectorCommandsTask : public ScriptDebugServer::Task {
 public:
     RunInspectorCommandsTask(WorkerThread* thread, WorkerGlobalScope* workerGlobalScope)
@@ -89,22 +97,23 @@
     , m_scriptDebugServer(inspectedWorkerGlobalScope, WorkerDebuggerAgent::debuggerTaskMode)
     , m_inspectedWorkerGlobalScope(inspectedWorkerGlobalScope)
 {
-    MutexLocker lock(workerDebuggerAgentsMutex());
+    std::lock_guard<std::mutex> lock(workerDebuggerAgentsMutex());
     workerDebuggerAgents().set(inspectedWorkerGlobalScope->thread(), this);
 }
 
 WorkerDebuggerAgent::~WorkerDebuggerAgent()
 {
-    MutexLocker lock(workerDebuggerAgentsMutex());
+    std::lock_guard<std::mutex> lock(workerDebuggerAgentsMutex());
+
     ASSERT(workerDebuggerAgents().contains(m_inspectedWorkerGlobalScope->thread()));
     workerDebuggerAgents().remove(m_inspectedWorkerGlobalScope->thread());
 }
 
 void WorkerDebuggerAgent::interruptAndDispatchInspectorCommands(WorkerThread* thread)
 {
-    MutexLocker lock(workerDebuggerAgentsMutex());
-    WorkerDebuggerAgent* agent = workerDebuggerAgents().get(thread);
-    if (agent)
+    std::lock_guard<std::mutex> lock(workerDebuggerAgentsMutex());
+
+    if (WorkerDebuggerAgent* agent = workerDebuggerAgents().get(thread))
         agent->m_scriptDebugServer.interruptAndRunTask(adoptPtr(new RunInspectorCommandsTask(thread, agent->m_inspectedWorkerGlobalScope)));
 }
 

Modified: trunk/Source/WebCore/workers/WorkerThread.cpp (161817 => 161818)


--- trunk/Source/WebCore/workers/WorkerThread.cpp	2014-01-12 19:24:29 UTC (rev 161817)
+++ trunk/Source/WebCore/workers/WorkerThread.cpp	2014-01-12 19:45:00 UTC (rev 161818)
@@ -35,6 +35,7 @@
 #include "ThreadGlobalData.h"
 #include "URL.h"
 #include <utility>
+#include <wtf/NeverDestroyed.h>
 #include <wtf/Noncopyable.h>
 #include <wtf/text/WTFString.h>
 
@@ -49,21 +50,29 @@
 
 namespace WebCore {
 
-static Mutex& threadSetMutex()
+static std::mutex& threadSetMutex()
 {
-    AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
-    return mutex;
+    static std::once_flag onceFlag;
+    static std::mutex* mutex;
+
+    std::call_once(onceFlag, []{
+        mutex = std::make_unique<std::mutex>().release();
+    });
+
+    return *mutex;
 }
 
 static HashSet<WorkerThread*>& workerThreads()
 {
-    DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ());
-    return threads;
+    static NeverDestroyed<HashSet<WorkerThread*>> workerThreads;
+
+    return workerThreads;
 }
 
 unsigned WorkerThread::workerThreadCount()
 {
-    MutexLocker lock(threadSetMutex());
+    std::lock_guard<std::mutex> lock(threadSetMutex());
+
     return workerThreads().size();
 }
 
@@ -114,13 +123,15 @@
     , m_notificationClient(0)
 #endif
 {
-    MutexLocker lock(threadSetMutex());
+    std::lock_guard<std::mutex> lock(threadSetMutex());
+
     workerThreads().add(this);
 }
 
 WorkerThread::~WorkerThread()
 {
-    MutexLocker lock(threadSetMutex());
+    std::lock_guard<std::mutex> lock(threadSetMutex());
+
     ASSERT(workerThreads().contains(this));
     workerThreads().remove(this);
 }
@@ -279,11 +290,10 @@
 
 void WorkerThread::releaseFastMallocFreeMemoryInAllThreads()
 {
-    MutexLocker lock(threadSetMutex());
-    HashSet<WorkerThread*>& threads = workerThreads();
-    HashSet<WorkerThread*>::iterator end = threads.end();
-    for (HashSet<WorkerThread*>::iterator it = threads.begin(); it != end; ++it)
-        (*it)->runLoop().postTask(adoptPtr(new ReleaseFastMallocFreeMemoryTask));
+    std::lock_guard<std::mutex> lock(threadSetMutex());
+
+    for (auto* workerThread : workerThreads())
+        workerThread->runLoop().postTask(adoptPtr(new ReleaseFastMallocFreeMemoryTask));
 }
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to