DatabaseThread.cpp [plain text]
#include "config.h"
#include "DatabaseThread.h"
#include "Database.h"
#include "DatabaseTask.h"
#include "Logging.h"
#include "SQLTransaction.h"
#include "SQLTransactionCoordinator.h"
#include <wtf/AutodrainedPool.h>
namespace WebCore {
DatabaseThread::DatabaseThread()
: m_transactionCoordinator(std::make_unique<SQLTransactionCoordinator>())
{
m_selfRef = this;
}
DatabaseThread::~DatabaseThread()
{
ASSERT(terminationRequested());
}
void DatabaseThread::start()
{
LockHolder lock(m_threadCreationMutex);
if (m_thread)
return;
m_thread = Thread::create("WebCore: Database", [this] {
databaseThread();
});
}
void DatabaseThread::requestTermination(DatabaseTaskSynchronizer* cleanupSync)
{
m_cleanupSync = cleanupSync;
LOG(StorageAPI, "DatabaseThread %p was asked to terminate\n", this);
m_queue.kill();
}
bool DatabaseThread::terminationRequested(DatabaseTaskSynchronizer* taskSynchronizer) const
{
#ifndef NDEBUG
if (taskSynchronizer)
taskSynchronizer->setHasCheckedForTermination();
#else
UNUSED_PARAM(taskSynchronizer);
#endif
return m_queue.killed();
}
void DatabaseThread::databaseThread()
{
{
LockHolder lock(m_threadCreationMutex);
LOG(StorageAPI, "Started DatabaseThread %p", this);
}
while (auto task = m_queue.waitForMessage()) {
AutodrainedPool pool;
task->performTask();
}
m_transactionCoordinator->shutdown();
LOG(StorageAPI, "About to detach thread %p and clear the ref to DatabaseThread %p, which currently has %i ref(s)", m_thread.get(), this, refCount());
DatabaseSet openSetCopy;
{
LockHolder lock(m_openDatabaseSetMutex);
if (m_openDatabaseSet.size() > 0) {
openSetCopy.swap(m_openDatabaseSet);
}
}
for (auto& openDatabase : openSetCopy)
openDatabase->performClose();
m_thread->detach();
DatabaseTaskSynchronizer* cleanupSync = m_cleanupSync;
m_selfRef = nullptr;
if (cleanupSync) cleanupSync->taskCompleted();
}
void DatabaseThread::recordDatabaseOpen(Database& database)
{
LockHolder lock(m_openDatabaseSetMutex);
ASSERT(m_thread == &Thread::current());
ASSERT(!m_openDatabaseSet.contains(&database));
m_openDatabaseSet.add(&database);
}
void DatabaseThread::recordDatabaseClosed(Database& database)
{
LockHolder lock(m_openDatabaseSetMutex);
ASSERT(m_thread == &Thread::current());
ASSERT(m_queue.killed() || m_openDatabaseSet.contains(&database));
m_openDatabaseSet.remove(&database);
}
void DatabaseThread::scheduleTask(std::unique_ptr<DatabaseTask>&& task)
{
ASSERT(!task->hasSynchronizer() || task->hasCheckedForTermination());
m_queue.append(WTFMove(task));
}
void DatabaseThread::scheduleImmediateTask(std::unique_ptr<DatabaseTask>&& task)
{
ASSERT(!task->hasSynchronizer() || task->hasCheckedForTermination());
m_queue.prepend(WTFMove(task));
}
void DatabaseThread::unscheduleDatabaseTasks(Database& database)
{
m_queue.removeIf([&database] (const DatabaseTask& task) {
return &task.database() == &database;
});
}
bool DatabaseThread::hasPendingDatabaseActivity() const
{
LockHolder lock(m_openDatabaseSetMutex);
for (auto& database : m_openDatabaseSet) {
if (database->hasPendingCreationEvent() || database->hasPendingTransaction())
return true;
}
return false;
}
}