#include "config.h"
#include <wtf/RunLoop.h>
#include <wtf/WindowsExtras.h>
namespace WTF {
static const UINT PerformWorkMessage = WM_USER + 1;
static const LPCWSTR kRunLoopMessageWindowClassName = L"RunLoopMessageWindow";
LRESULT CALLBACK RunLoop::RunLoopWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (RunLoop* runLoop = static_cast<RunLoop*>(getWindowPointer(hWnd, 0)))
return runLoop->wndProc(hWnd, message, wParam, lParam);
if (message == WM_CREATE) {
LPCREATESTRUCT createStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
setWindowPointer(hWnd, 0, createStruct->lpCreateParams);
return 0;
}
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
LRESULT RunLoop::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case PerformWorkMessage:
performWork();
return 0;
case WM_TIMER:
bitwise_cast<RunLoop::TimerBase*>(wParam)->timerFired();
return 0;
}
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
void RunLoop::run()
{
MSG message;
while (BOOL result = ::GetMessage(&message, nullptr, 0, 0)) {
if (result == -1)
break;
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
void RunLoop::iterate()
{
MSG message;
while (::PeekMessage(&message, nullptr, 0, 0, PM_REMOVE)) {
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
void RunLoop::setWakeUpCallback(WTF::Function<void()>&& function)
{
RunLoop::current().m_wakeUpCallback = WTFMove(function);
}
void RunLoop::stop()
{
dispatch([] {
::PostQuitMessage(0);
});
}
void RunLoop::registerRunLoopMessageWindowClass()
{
static std::once_flag onceKey;
std::call_once(onceKey, [&] {
WNDCLASS windowClass = { };
windowClass.lpfnWndProc = RunLoop::RunLoopWndProc;
windowClass.cbWndExtra = sizeof(RunLoop*);
windowClass.lpszClassName = kRunLoopMessageWindowClassName;
bool result = ::RegisterClass(&windowClass);
RELEASE_ASSERT(result);
});
}
RunLoop::RunLoop()
{
registerRunLoopMessageWindowClass();
m_runLoopMessageWindow = ::CreateWindow(kRunLoopMessageWindowClassName, nullptr, 0,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_MESSAGE, nullptr, nullptr, this);
RELEASE_ASSERT(::IsWindow(m_runLoopMessageWindow));
}
RunLoop::~RunLoop()
{
}
void RunLoop::wakeUp()
{
::PostMessage(m_runLoopMessageWindow, PerformWorkMessage, reinterpret_cast<WPARAM>(this), 0);
if (m_wakeUpCallback)
m_wakeUpCallback();
}
RunLoop::CycleResult RunLoop::cycle(RunLoopMode)
{
MSG message;
if (!::GetMessage(&message, nullptr, 0, 0))
return CycleResult::Stop;
::TranslateMessage(&message);
::DispatchMessage(&message);
return CycleResult::Continue;
}
void RunLoop::TimerBase::timerFired()
{
{
LockHolder locker(m_runLoop->m_loopLock);
if (!m_isActive)
return;
if (!m_isRepeating) {
m_isActive = false;
::KillTimer(m_runLoop->m_runLoopMessageWindow, bitwise_cast<uintptr_t>(this));
} else
m_nextFireDate = MonotonicTime::now() + m_interval;
}
fired();
}
RunLoop::TimerBase::TimerBase(RunLoop& runLoop)
: m_runLoop(runLoop)
{
}
RunLoop::TimerBase::~TimerBase()
{
stop();
}
void RunLoop::TimerBase::start(Seconds interval, bool repeat)
{
LockHolder locker(m_runLoop->m_loopLock);
m_isRepeating = repeat;
m_isActive = true;
m_interval = interval;
m_nextFireDate = MonotonicTime::now() + m_interval;
::SetTimer(m_runLoop->m_runLoopMessageWindow, bitwise_cast<uintptr_t>(this), interval.millisecondsAs<UINT>(), nullptr);
}
void RunLoop::TimerBase::stop()
{
LockHolder locker(m_runLoop->m_loopLock);
if (!isActive(locker))
return;
m_isActive = false;
::KillTimer(m_runLoop->m_runLoopMessageWindow, bitwise_cast<uintptr_t>(this));
}
bool RunLoop::TimerBase::isActive(const AbstractLocker&) const
{
return m_isActive;
}
bool RunLoop::TimerBase::isActive() const
{
LockHolder locker(m_runLoop->m_loopLock);
return isActive(locker);
}
Seconds RunLoop::TimerBase::secondsUntilFire() const
{
LockHolder locker(m_runLoop->m_loopLock);
if (isActive(locker))
return std::max<Seconds>(m_nextFireDate - MonotonicTime::now(), 0_s);
return 0_s;
}
}