From 2b6d718636ab7e8bfd4cb0ac763cada7540e2114 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 31 Mar 2019 07:46:58 -0500 Subject: [PATCH] InputCommon: Fix Win32 init race. --- .../ControllerInterface/Win32/Win32.cpp | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/Win32/Win32.cpp b/Source/Core/InputCommon/ControllerInterface/Win32/Win32.cpp index 6c78c6b2a0..62febb4c56 100644 --- a/Source/Core/InputCommon/ControllerInterface/Win32/Win32.cpp +++ b/Source/Core/InputCommon/ControllerInterface/Win32/Win32.cpp @@ -6,6 +6,8 @@ #include +#include +#include #include #include "Common/Event.h" @@ -38,7 +40,12 @@ void ciface::Win32::Init(void* hwnd) s_hwnd = static_cast(hwnd); XInput::Init(); - s_thread = std::thread([] { + std::promise message_window_promise; + + s_thread = std::thread([&message_window_promise] { + HWND message_window = nullptr; + Common::ScopeGuard promise_guard([&] { message_window_promise.set_value(message_window); }); + if (FAILED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED))) { ERROR_LOG(SERIALINTERFACE, "CoInitializeEx failed: %i", GetLastError()); @@ -63,17 +70,17 @@ void ciface::Win32::Init(void* hwnd) ERROR_LOG(SERIALINTERFACE, "UnregisterClass failed: %i", GetLastError()); }); - s_message_window = CreateWindowEx(0, L"Message", nullptr, 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, - nullptr, nullptr); - if (!s_message_window) + message_window = CreateWindowEx(0, L"Message", nullptr, 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, + nullptr, nullptr); + promise_guard.Exit(); + if (!message_window) { ERROR_LOG(SERIALINTERFACE, "CreateWindowEx failed: %i", GetLastError()); return; } - Common::ScopeGuard destroy([] { - if (!DestroyWindow(s_message_window)) + Common::ScopeGuard destroy([&] { + if (!DestroyWindow(message_window)) ERROR_LOG(SERIALINTERFACE, "DestroyWindow failed: %i", GetLastError()); - s_message_window = nullptr; }); std::array devices; @@ -81,12 +88,12 @@ void ciface::Win32::Init(void* hwnd) devices[0].usUsagePage = 0x01; devices[0].usUsage = 0x05; devices[0].dwFlags = RIDEV_DEVNOTIFY; - devices[0].hwndTarget = s_message_window; + devices[0].hwndTarget = message_window; // joystick devices devices[1].usUsagePage = 0x01; devices[1].usUsage = 0x04; devices[1].dwFlags = RIDEV_DEVNOTIFY; - devices[1].hwndTarget = s_message_window; + devices[1].hwndTarget = message_window; if (!RegisterRawInputDevices(devices.data(), static_cast(devices.size()), static_cast(sizeof(decltype(devices)::value_type)))) @@ -104,6 +111,8 @@ void ciface::Win32::Init(void* hwnd) break; } }); + + s_message_window = message_window_promise.get_future().get(); } void ciface::Win32::PopulateDevices(void* hwnd) @@ -129,6 +138,7 @@ void ciface::Win32::DeInit() { PostMessage(s_message_window, WM_DOLPHIN_STOP, 0, 0); s_thread.join(); + s_message_window = nullptr; } XInput::DeInit();