#include "config.h"
#include "StorageThread.h"
#include "AutodrainedPool.h"
#include "StorageTask.h"
#include "StorageAreaSync.h"
#include <wtf/MainThread.h>
namespace WebCore {
PassOwnPtr<StorageThread> StorageThread::create()
{
return adoptPtr(new StorageThread);
}
StorageThread::StorageThread()
: m_threadID(0)
{
}
StorageThread::~StorageThread()
{
ASSERT(isMainThread());
ASSERT(!m_threadID);
}
bool StorageThread::start()
{
ASSERT(isMainThread());
if (!m_threadID)
m_threadID = createThread(StorageThread::threadEntryPointCallback, this, "WebCore: LocalStorage");
return m_threadID;
}
void StorageThread::threadEntryPointCallback(void* thread)
{
static_cast<StorageThread*>(thread)->threadEntryPoint();
}
void StorageThread::threadEntryPoint()
{
ASSERT(!isMainThread());
AutodrainedPool pool;
while (OwnPtr<StorageTask> task = m_queue.waitForMessage()) {
task->performTask();
pool.cycle();
}
}
void StorageThread::scheduleTask(PassOwnPtr<StorageTask> task)
{
ASSERT(isMainThread());
ASSERT(!m_queue.killed() && m_threadID);
m_queue.append(task);
}
void StorageThread::terminate()
{
ASSERT(isMainThread());
ASSERT(!m_queue.killed() && m_threadID);
if (!m_threadID)
return;
m_queue.append(StorageTask::createTerminate(this));
waitForThreadCompletion(m_threadID);
ASSERT(m_queue.killed());
m_threadID = 0;
}
void StorageThread::performTerminate()
{
ASSERT(!isMainThread());
m_queue.kill();
}
}