Use std::call_once instead of AtomicallyInitializedStatic in DatabaseBackendBase
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 12 Jan 2014 21:31:09 +0000 (21:31 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 12 Jan 2014 21:31:09 +0000 (21:31 +0000)
https://bugs.webkit.org/show_bug.cgi?id=126851

Reviewed by Sam Weinig.

* Modules/webdatabase/DatabaseBackendBase.cpp:
(WebCore::guidMutex):
(WebCore::guidToVersionMap):
(WebCore::updateGuidVersionMap):
(WebCore::guidToDatabaseMap):
(WebCore::guidForOriginAndName):
(WebCore::DatabaseBackendBase::DatabaseBackendBase):
(WebCore::DatabaseBackendBase::closeDatabase):
(WebCore::DatabaseBackendBase::performOpenAndVerify):
(WebCore::DatabaseBackendBase::getCachedVersion):
(WebCore::DatabaseBackendBase::setCachedVersion):

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

Source/WebCore/ChangeLog
Source/WebCore/Modules/webdatabase/DatabaseBackendBase.cpp

index 76395dc..47804ba 100644 (file)
@@ -1,3 +1,22 @@
+2014-01-12  Anders Carlsson  <andersca@apple.com>
+
+        Use std::call_once instead of AtomicallyInitializedStatic in DatabaseBackendBase
+        https://bugs.webkit.org/show_bug.cgi?id=126851
+
+        Reviewed by Sam Weinig.
+
+        * Modules/webdatabase/DatabaseBackendBase.cpp:
+        (WebCore::guidMutex):
+        (WebCore::guidToVersionMap):
+        (WebCore::updateGuidVersionMap):
+        (WebCore::guidToDatabaseMap):
+        (WebCore::guidForOriginAndName):
+        (WebCore::DatabaseBackendBase::DatabaseBackendBase):
+        (WebCore::DatabaseBackendBase::closeDatabase):
+        (WebCore::DatabaseBackendBase::performOpenAndVerify):
+        (WebCore::DatabaseBackendBase::getCachedVersion):
+        (WebCore::DatabaseBackendBase::setCachedVersion):
+
 2014-01-12  Dan Bernstein  <mitz@apple.com>
 
         Try to fix the Windows build after r161830.
index 745d563..f4a56e0 100644 (file)
@@ -141,18 +141,25 @@ static bool setTextValueInDatabase(SQLiteDatabase& db, const String& query, cons
 }
 
 // FIXME: move all guid-related functions to a DatabaseVersionTracker class.
-static Mutex& guidMutex()
+static std::mutex& guidMutex()
 {
-    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<DatabaseGuid, String> GuidVersionMap;
 static GuidVersionMap& guidToVersionMap()
 {
     // Ensure the the mutex is locked.
-    ASSERT(!guidMutex().tryLock());
-    DEFINE_STATIC_LOCAL(GuidVersionMap, map, ());
+    ASSERT(!guidMutex().try_lock());
+
+    static NeverDestroyed<GuidVersionMap> map;
     return map;
 }
 
@@ -160,7 +167,7 @@ static GuidVersionMap& guidToVersionMap()
 static inline void updateGuidVersionMap(DatabaseGuid guid, String newVersion)
 {
     // Ensure the the mutex is locked.
-    ASSERT(!guidMutex().tryLock());
+    ASSERT(!guidMutex().try_lock());
 
     // Note: It is not safe to put an empty string into the guidToVersionMap() map.
     // That's because the map is cross-thread, but empty strings are per-thread.
@@ -177,7 +184,8 @@ typedef HashMap<DatabaseGuid, std::unique_ptr<HashSet<DatabaseBackendBase*>>> Gu
 static GuidDatabaseMap& guidToDatabaseMap()
 {
     // Ensure the the mutex is locked.
-    ASSERT(!guidMutex().tryLock());
+    ASSERT(!guidMutex().try_lock());
+
     static NeverDestroyed<GuidDatabaseMap> map;
     return map;
 }
@@ -185,7 +193,7 @@ static GuidDatabaseMap& guidToDatabaseMap()
 static DatabaseGuid guidForOriginAndName(const String& origin, const String& name)
 {
     // Ensure the the mutex is locked.
-    ASSERT(!guidMutex().tryLock());
+    ASSERT(!guidMutex().try_lock());
 
     String stringID = origin + "/" + name;
 
@@ -233,7 +241,8 @@ DatabaseBackendBase::DatabaseBackendBase(PassRefPtr<DatabaseBackendContext> data
         m_name = emptyString();
 
     {
-        MutexLocker locker(guidMutex());
+        std::lock_guard<std::mutex> locker(guidMutex());
+
         m_guid = guidForOriginAndName(securityOrigin()->toString(), name);
         std::unique_ptr<HashSet<DatabaseBackendBase*>>& hashSet = guidToDatabaseMap().add(m_guid, nullptr).iterator->value;
         if (!hashSet)
@@ -268,7 +277,7 @@ void DatabaseBackendBase::closeDatabase()
     // See comment at the top this file regarding calling removeOpenDatabase().
     DatabaseTracker::tracker().removeOpenDatabase(this);
     {
-        MutexLocker locker(guidMutex());
+        std::lock_guard<std::mutex> locker(guidMutex());
 
         auto it = guidToDatabaseMap().find(m_guid);
         ASSERT(it != guidToDatabaseMap().end());
@@ -337,7 +346,7 @@ bool DatabaseBackendBase::performOpenAndVerify(bool shouldSetVersionInNewDatabas
 
     String currentVersion;
     {
-        MutexLocker locker(guidMutex());
+        std::lock_guard<std::mutex> locker(guidMutex());
 
         auto entry = guidToVersionMap().find(m_guid);
         if (entry != guidToVersionMap().end()) {
@@ -496,14 +505,16 @@ void DatabaseBackendBase::setExpectedVersion(const String& version)
 
 String DatabaseBackendBase::getCachedVersion() const
 {
-    MutexLocker locker(guidMutex());
+    std::lock_guard<std::mutex> locker(guidMutex());
+
     return guidToVersionMap().get(m_guid).isolatedCopy();
 }
 
 void DatabaseBackendBase::setCachedVersion(const String& actualVersion)
 {
     // Update the in memory database version map.
-    MutexLocker locker(guidMutex());
+    std::lock_guard<std::mutex> locker(guidMutex());
+
     updateGuidVersionMap(m_guid, actualVersion);
 }