DatabaseThread.cpp [plain text]
#include "config.h"
#include "DatabaseThread.h"
#include "Database.h"
#include "DatabaseTask.h"
#include "Logging.h"
#include "SQLTransactionClient.h"
#include "SQLTransactionCoordinator.h"
#include <wtf/AutodrainedPool.h>
namespace WebCore {
DatabaseThread::DatabaseThread()
: m_threadID(0)
, m_transactionClient(std::make_unique<SQLTransactionClient>())
, m_transactionCoordinator(std::make_unique<SQLTransactionCoordinator>())
, m_cleanupSync(nullptr)
{
m_selfRef = this;
}
DatabaseThread::~DatabaseThread()
{
ASSERT(terminationRequested());
}
bool DatabaseThread::start()
{
LockHolder lock(m_threadCreationMutex);
if (m_threadID)
return true;
m_threadID = createThread(DatabaseThread::databaseThreadStart, this, "WebCore: Database");
return m_threadID;
}
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::databaseThreadStart(void* vDatabaseThread)
{
DatabaseThread* dbThread = static_cast<DatabaseThread*>(vDatabaseThread);
dbThread->databaseThread();
}
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 %i and clear the ref to DatabaseThread %p, which currently has %i ref(s)", m_threadID, this, refCount());
DatabaseSet openSetCopy;
{
LockHolder lock(m_openDatabaseSetMutex);
if (m_openDatabaseSet.size() > 0) {
openSetCopy.swap(m_openDatabaseSet);
}
}
for (auto& openDatabase : openSetCopy)
openDatabase->performClose();
detachThread(m_threadID);
DatabaseTaskSynchronizer* cleanupSync = m_cleanupSync;
m_selfRef = nullptr;
if (cleanupSync) cleanupSync->taskCompleted();
}
void DatabaseThread::recordDatabaseOpen(Database* database)
{
LockHolder lock(m_openDatabaseSetMutex);
ASSERT(currentThread() == m_threadID);
ASSERT(database);
ASSERT(!m_openDatabaseSet.contains(database));
m_openDatabaseSet.add(database);
}
void DatabaseThread::recordDatabaseClosed(Database* database)
{
LockHolder lock(m_openDatabaseSetMutex);
ASSERT(currentThread() == m_threadID);
ASSERT(database);
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));
}
class SameDatabasePredicate {
public:
SameDatabasePredicate(const Database* database) : m_database(database) { }
bool operator()(const DatabaseTask& task) const { return &task.database() == m_database; }
private:
const Database* m_database;
};
void DatabaseThread::unscheduleDatabaseTasks(Database* database)
{
SameDatabasePredicate predicate(database);
m_queue.removeIf(predicate);
}
bool DatabaseThread::hasPendingDatabaseActivity() const
{
LockHolder lock(m_openDatabaseSetMutex);
for (auto& database : m_openDatabaseSet) {
if (database->hasPendingCreationEvent() || database->hasPendingTransaction())
return true;
}
return false;
}
}