#include "DatabaseTracker.h"
#include "ExceptionCode.h"
#include "Logging.h"
+#include "SQLiteDatabaseTracker.h"
#include "SQLiteStatement.h"
#include "SQLiteTransaction.h"
#include "SecurityOrigin.h"
#include <wtf/text/CString.h>
#include <wtf/text/StringHash.h>
-#if PLATFORM(IOS)
-#include "SQLiteDatabaseTracker.h"
-#endif
-
// Registering "opened" databases with the DatabaseTracker
// =======================================================
// The DatabaseTracker maintains a list of databases that have been
}
// 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;
}
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.
static GuidDatabaseMap& guidToDatabaseMap()
{
// Ensure the the mutex is locked.
- ASSERT(!guidMutex().tryLock());
+ ASSERT(!guidMutex().try_lock());
+
static NeverDestroyed<GuidDatabaseMap> map;
return map;
}
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;
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)
// 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());
// Make sure we wait till the background removal of the empty database files finished before trying to open any database.
MutexLocker locker(DatabaseTracker::openDatabaseMutex());
}
- SQLiteTransactionInProgressAutoCounter transactionCounter;
#endif
+ SQLiteTransactionInProgressAutoCounter transactionCounter;
+
if (!m_sqliteDatabase.open(m_filename, true)) {
errorMessage = formatErrorMessage("unable to open database", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
return false;
String currentVersion;
{
- MutexLocker locker(guidMutex());
+ std::lock_guard<std::mutex> locker(guidMutex());
auto entry = guidToVersionMap().find(m_guid);
if (entry != guidToVersionMap().end()) {
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);
}
void DatabaseBackendBase::incrementalVacuumIfNeeded()
{
-#if PLATFORM(IOS)
SQLiteTransactionInProgressAutoCounter transactionCounter;
-#endif
+
int64_t freeSpaceSize = m_sqliteDatabase.freeSpaceSize();
int64_t totalSize = m_sqliteDatabase.totalSize();
if (totalSize <= 10 * freeSpaceSize) {