#include "config.h"
#include "WorkQueue.h"
#include <wtf/MathExtras.h>
#include <wtf/Threading.h>
#include <wtf/win/WorkItemWin.h>
namespace WTF {
void WorkQueue::handleCallback(void* context, BOOLEAN timerOrWaitFired)
{
ASSERT_ARG(context, context);
ASSERT_ARG(timerOrWaitFired, !timerOrWaitFired);
WorkItemWin* item = static_cast<WorkItemWin*>(context);
RefPtr<WorkQueue> queue = item->queue();
{
MutexLocker lock(queue->m_workItemQueueLock);
queue->m_workItemQueue.append(item);
if (!queue->tryRegisterAsWorkThread()) {
return;
}
}
queue->performWorkOnRegisteredWorkThread();
}
DWORD WorkQueue::workThreadCallback(void* context)
{
ASSERT_ARG(context, context);
WorkQueue* queue = static_cast<WorkQueue*>(context);
if (!queue->tryRegisterAsWorkThread())
return 0;
queue->performWorkOnRegisteredWorkThread();
return 0;
}
void WorkQueue::performWorkOnRegisteredWorkThread()
{
ASSERT(m_isWorkThreadRegistered);
m_workItemQueueLock.lock();
while (!m_workItemQueue.isEmpty()) {
Vector<RefPtr<WorkItemWin>> workItemQueue;
m_workItemQueue.swap(workItemQueue);
m_workItemQueueLock.unlock();
for (auto& workItem : workItemQueue) {
workItem->function()();
deref();
}
m_workItemQueueLock.lock();
}
unregisterAsWorkThread();
m_workItemQueueLock.unlock();
}
void WorkQueue::platformInitialize(const char* name, Type, QOS)
{
m_isWorkThreadRegistered = 0;
m_timerQueue = ::CreateTimerQueue();
ASSERT_WITH_MESSAGE(m_timerQueue, "::CreateTimerQueue failed with error %lu", ::GetLastError());
}
bool WorkQueue::tryRegisterAsWorkThread()
{
LONG result = ::InterlockedCompareExchange(&m_isWorkThreadRegistered, 1, 0);
ASSERT(!result || result == 1);
return !result;
}
void WorkQueue::unregisterAsWorkThread()
{
LONG result = ::InterlockedCompareExchange(&m_isWorkThreadRegistered, 0, 1);
ASSERT_UNUSED(result, result == 1);
}
void WorkQueue::platformInvalidate()
{
#if !ASSERT_DISABLED
MutexLocker lock(m_handlesLock);
ASSERT(m_handles.isEmpty());
#endif
::DeleteTimerQueueEx(m_timerQueue, 0);
}
void WorkQueue::dispatch(std::function<void()> function)
{
MutexLocker locker(m_workItemQueueLock);
ref();
m_workItemQueue.append(WorkItemWin::create(function, this));
if (!m_isWorkThreadRegistered)
::QueueUserWorkItem(workThreadCallback, this, WT_EXECUTEDEFAULT);
}
struct TimerContext : public ThreadSafeRefCounted<TimerContext> {
static RefPtr<TimerContext> create() { return adoptRef(new TimerContext); }
WorkQueue* queue;
std::function<void()> function;
Mutex timerMutex;
HANDLE timer;
private:
TimerContext()
: queue(nullptr)
, timer(0)
{
}
};
void WorkQueue::timerCallback(void* context, BOOLEAN timerOrWaitFired)
{
ASSERT_ARG(context, context);
ASSERT_UNUSED(timerOrWaitFired, timerOrWaitFired);
RefPtr<TimerContext> timerContext = adoptRef(static_cast<TimerContext*>(context));
timerContext->queue->dispatch(timerContext->function);
MutexLocker lock(timerContext->timerMutex);
ASSERT(timerContext->timer);
ASSERT(timerContext->queue->m_timerQueue);
if (!::DeleteTimerQueueTimer(timerContext->queue->m_timerQueue, timerContext->timer, 0)) {
ASSERT_WITH_MESSAGE(::GetLastError() == ERROR_IO_PENDING, "::DeleteTimerQueueTimer failed with error %lu", ::GetLastError());
}
}
void WorkQueue::dispatchAfter(std::chrono::nanoseconds duration, std::function<void()> function)
{
ASSERT(m_timerQueue);
ref();
RefPtr<TimerContext> context = TimerContext::create();
context->queue = this;
context->function = function;
{
MutexLocker lock(context->timerMutex);
int64_t milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
if (!::CreateTimerQueueTimer(&context->timer, m_timerQueue, timerCallback, context.get(), clampTo<DWORD>(milliseconds), 0, WT_EXECUTEINTIMERTHREAD)) {
ASSERT_WITH_MESSAGE(false, "::CreateTimerQueueTimer failed with error %lu", ::GetLastError());
return;
}
}
context.release().leakRef();
}
void WorkQueue::unregisterWaitAndDestroyItemSoon(PassRefPtr<HandleWorkItem> item)
{
::QueueUserWorkItem(unregisterWaitAndDestroyItemCallback, item.leakRef(), WT_EXECUTEDEFAULT);
}
DWORD WINAPI WorkQueue::unregisterWaitAndDestroyItemCallback(void* context)
{
ASSERT_ARG(context, context);
RefPtr<HandleWorkItem> item = adoptRef(static_cast<HandleWorkItem*>(context));
if (!::UnregisterWaitEx(item->waitHandle(), INVALID_HANDLE_VALUE)) {
DWORD error = ::GetLastError();
ASSERT_NOT_REACHED();
}
return 0;
}
}