Changeset 161818 in webkit


Ignore:
Timestamp:
Jan 12, 2014 11:45:00 AM (10 years ago)
Author:
andersca@apple.com
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.

Location:
trunk/Source
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r161802 r161818  
     12014-01-12  Anders Carlsson  <andersca@apple.com>
     2
     3        Replace more uses of AtomicallyInitializedStatic with std::call_once
     4        https://bugs.webkit.org/show_bug.cgi?id=126847
     5
     6        Reviewed by Sam Weinig.
     7
     8        * wtf/Forward.h:
     9        Forward declare NeverDestroyed.
     10
    1112014-01-11  Zan Dobersek  <zdobersek@igalia.com>
    212
  • trunk/Source/WTF/wtf/Forward.h

    r161518 r161818  
    2727
    2828template<typename T> class Function;
     29template<typename T> class NeverDestroyed;
    2930template<typename T> class OwnPtr;
    3031template<typename T> class PassOwnPtr;
     
    6061using WTF::Function;
    6162using WTF::FunctionDispatcher;
     63using WTF::NeverDestroyed;
    6264using WTF::OwnPtr;
    6365using WTF::PassOwnPtr;
  • trunk/Source/WebCore/ChangeLog

    r161817 r161818  
     12014-01-12  Anders Carlsson  <andersca@apple.com>
     2
     3        Replace more uses of AtomicallyInitializedStatic with std::call_once
     4        https://bugs.webkit.org/show_bug.cgi?id=126847
     5
     6        Reviewed by Sam Weinig.
     7
     8        * crypto/CryptoAlgorithmRegistry.cpp:
     9        (WebCore::CryptoAlgorithmRegistry::shared):
     10        (WebCore::registryMutex):
     11        (WebCore::CryptoAlgorithmRegistry::getIdentifierForName):
     12        (WebCore::CryptoAlgorithmRegistry::nameForIdentifier):
     13        (WebCore::CryptoAlgorithmRegistry::create):
     14        (WebCore::CryptoAlgorithmRegistry::registerAlgorithm):
     15        * crypto/CryptoAlgorithmRegistry.h:
     16        * inspector/WorkerDebuggerAgent.cpp:
     17        (WebCore::WorkerDebuggerAgent::WorkerDebuggerAgent):
     18        (WebCore::WorkerDebuggerAgent::~WorkerDebuggerAgent):
     19        (WebCore::WorkerDebuggerAgent::interruptAndDispatchInspectorCommands):
     20        * workers/WorkerThread.cpp:
     21        (WebCore::threadSetMutex):
     22        (WebCore::workerThreads):
     23        (WebCore::WorkerThread::workerThreadCount):
     24        (WebCore::WorkerThread::WorkerThread):
     25        (WebCore::WorkerThread::~WorkerThread):
     26        (WebCore::WorkerThread::releaseFastMallocFreeMemoryInAllThreads):
     27
    1282014-01-11  Sam Weinig  <sam@webkit.org>
    229
  • trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.cpp

    r160491 r161818  
    3030
    3131#include "CryptoAlgorithm.h"
    32 #include <wtf/MainThread.h>
     32#include <mutex>
     33#include <wtf/NeverDestroyed.h>
    3334
    3435namespace WebCore {
     
    3637CryptoAlgorithmRegistry& CryptoAlgorithmRegistry::shared()
    3738{
    38     DEFINE_STATIC_LOCAL(CryptoAlgorithmRegistry, registry, ());
     39    static NeverDestroyed<CryptoAlgorithmRegistry> registry;
     40
    3941    return registry;
    4042}
    4143
    42 static Mutex& registryMutex()
     44static std::mutex& registryMutex()
    4345{
    44     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
    45     return mutex;
     46    static std::once_flag onceFlag;
     47    static std::mutex* mutex;
     48
     49    std::call_once(onceFlag, []{
     50        mutex = std::make_unique<std::mutex>().release();
     51    });
     52
     53    return *mutex;
    4654}
    4755
     
    5664        return false;
    5765
    58     MutexLocker lock(registryMutex());
     66    std::lock_guard<std::mutex> lock(registryMutex());
    5967
    6068    auto iter = m_nameToIdentifierMap.find(name.isolatedCopy());
     
    6876String CryptoAlgorithmRegistry::nameForIdentifier(CryptoAlgorithmIdentifier identifier)
    6977{
    70     MutexLocker lock(registryMutex());
     78    std::lock_guard<std::mutex> lock(registryMutex());
    7179
    7280    return m_identifierToNameMap.get(static_cast<unsigned>(identifier)).isolatedCopy();
     
    7583std::unique_ptr<CryptoAlgorithm> CryptoAlgorithmRegistry::create(CryptoAlgorithmIdentifier identifier)
    7684{
    77     MutexLocker lock(registryMutex());
     85    std::lock_guard<std::mutex> lock(registryMutex());
    7886
    7987    auto iter = m_identifierToConstructorMap.find(static_cast<unsigned>(identifier));
     
    8694void CryptoAlgorithmRegistry::registerAlgorithm(const String& name, CryptoAlgorithmIdentifier identifier, CryptoAlgorithmConstructor constructor)
    8795{
    88     MutexLocker lock(registryMutex());
     96    std::lock_guard<std::mutex> lock(registryMutex());
    8997
    9098    bool added = m_nameToIdentifierMap.add(name, identifier).isNewEntry;
  • trunk/Source/WebCore/crypto/CryptoAlgorithmRegistry.h

    r160491 r161818  
    4343class CryptoAlgorithmRegistry {
    4444    WTF_MAKE_NONCOPYABLE(CryptoAlgorithmRegistry);
     45    friend class NeverDestroyed<CryptoAlgorithmRegistry>;
    4546
    4647public:
     
    6465
    6566    void registerAlgorithm(const String& name, CryptoAlgorithmIdentifier, CryptoAlgorithmConstructor);
     67
    6668    HashMap<String, CryptoAlgorithmIdentifier, CaseFoldingHash> m_nameToIdentifierMap;
    6769    HashMap<unsigned, String> m_identifierToNameMap;
  • trunk/Source/WebCore/inspector/WorkerDebuggerAgent.cpp

    r161784 r161818  
    3939#include <inspector/InjectedScript.h>
    4040#include <inspector/InjectedScriptManager.h>
     41#include <mutex>
    4142#include <wtf/MessageQueue.h>
     43#include <wtf/NeverDestroyed.h>
    4244
    4345using namespace Inspector;
     
    4749namespace {
    4850
    49 Mutex& workerDebuggerAgentsMutex()
     51std::mutex& workerDebuggerAgentsMutex()
    5052{
    51     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
    52     return mutex;
     53    static std::once_flag onceFlag;
     54    static std::mutex* mutex;
     55
     56    std::call_once(onceFlag, []{
     57        mutex = std::make_unique<std::mutex>().release();
     58    });
     59
     60    return *mutex;
    5361}
    5462
     
    5765WorkerDebuggerAgents& workerDebuggerAgents()
    5866{
    59     DEFINE_STATIC_LOCAL(WorkerDebuggerAgents, agents, ());
     67    static NeverDestroyed<WorkerDebuggerAgents> agents;
     68
    6069    return agents;
    6170}
    62 
    6371
    6472class RunInspectorCommandsTask : public ScriptDebugServer::Task {
     
    9098    , m_inspectedWorkerGlobalScope(inspectedWorkerGlobalScope)
    9199{
    92     MutexLocker lock(workerDebuggerAgentsMutex());
     100    std::lock_guard<std::mutex> lock(workerDebuggerAgentsMutex());
    93101    workerDebuggerAgents().set(inspectedWorkerGlobalScope->thread(), this);
    94102}
     
    96104WorkerDebuggerAgent::~WorkerDebuggerAgent()
    97105{
    98     MutexLocker lock(workerDebuggerAgentsMutex());
     106    std::lock_guard<std::mutex> lock(workerDebuggerAgentsMutex());
     107
    99108    ASSERT(workerDebuggerAgents().contains(m_inspectedWorkerGlobalScope->thread()));
    100109    workerDebuggerAgents().remove(m_inspectedWorkerGlobalScope->thread());
     
    103112void WorkerDebuggerAgent::interruptAndDispatchInspectorCommands(WorkerThread* thread)
    104113{
    105     MutexLocker lock(workerDebuggerAgentsMutex());
    106     WorkerDebuggerAgent* agent = workerDebuggerAgents().get(thread);
    107     if (agent)
     114    std::lock_guard<std::mutex> lock(workerDebuggerAgentsMutex());
     115
     116    if (WorkerDebuggerAgent* agent = workerDebuggerAgents().get(thread))
    108117        agent->m_scriptDebugServer.interruptAndRunTask(adoptPtr(new RunInspectorCommandsTask(thread, agent->m_inspectedWorkerGlobalScope)));
    109118}
  • trunk/Source/WebCore/workers/WorkerThread.cpp

    r161638 r161818  
    3636#include "URL.h"
    3737#include <utility>
     38#include <wtf/NeverDestroyed.h>
    3839#include <wtf/Noncopyable.h>
    3940#include <wtf/text/WTFString.h>
     
    5051namespace WebCore {
    5152
    52 static Mutex& threadSetMutex()
    53 {
    54     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
    55     return mutex;
     53static std::mutex& threadSetMutex()
     54{
     55    static std::once_flag onceFlag;
     56    static std::mutex* mutex;
     57
     58    std::call_once(onceFlag, []{
     59        mutex = std::make_unique<std::mutex>().release();
     60    });
     61
     62    return *mutex;
    5663}
    5764
    5865static HashSet<WorkerThread*>& workerThreads()
    5966{
    60     DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ());
    61     return threads;
     67    static NeverDestroyed<HashSet<WorkerThread*>> workerThreads;
     68
     69    return workerThreads;
    6270}
    6371
    6472unsigned WorkerThread::workerThreadCount()
    6573{
    66     MutexLocker lock(threadSetMutex());
     74    std::lock_guard<std::mutex> lock(threadSetMutex());
     75
    6776    return workerThreads().size();
    6877}
     
    115124#endif
    116125{
    117     MutexLocker lock(threadSetMutex());
     126    std::lock_guard<std::mutex> lock(threadSetMutex());
     127
    118128    workerThreads().add(this);
    119129}
     
    121131WorkerThread::~WorkerThread()
    122132{
    123     MutexLocker lock(threadSetMutex());
     133    std::lock_guard<std::mutex> lock(threadSetMutex());
     134
    124135    ASSERT(workerThreads().contains(this));
    125136    workerThreads().remove(this);
     
    280291void WorkerThread::releaseFastMallocFreeMemoryInAllThreads()
    281292{
    282     MutexLocker lock(threadSetMutex());
    283     HashSet<WorkerThread*>& threads = workerThreads();
    284     HashSet<WorkerThread*>::iterator end = threads.end();
    285     for (HashSet<WorkerThread*>::iterator it = threads.begin(); it != end; ++it)
    286         (*it)->runLoop().postTask(adoptPtr(new ReleaseFastMallocFreeMemoryTask));
     293    std::lock_guard<std::mutex> lock(threadSetMutex());
     294
     295    for (auto* workerThread : workerThreads())
     296        workerThread->runLoop().postTask(adoptPtr(new ReleaseFastMallocFreeMemoryTask));
    287297}
    288298
Note: See TracChangeset for help on using the changeset viewer.