From 932e9815726182b332c52d4d4e8be1e0d2642507 Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 12 Aug 2019 19:34:17 +0200 Subject: [PATCH] device: fix deadlock when building with SIMPLE_THREADS=1 GetMessage blocks without yielding, so we need to use an alternative message pump using PeekMessage instead. Fixes #704 --- panda/src/device/winInputDeviceManager.cxx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/panda/src/device/winInputDeviceManager.cxx b/panda/src/device/winInputDeviceManager.cxx index 232c612186..303400a380 100644 --- a/panda/src/device/winInputDeviceManager.cxx +++ b/panda/src/device/winInputDeviceManager.cxx @@ -62,7 +62,9 @@ WinInputDeviceManager() : } // If we have threading enabled, start a thread with a message-only window - // loop to listen for input events. + // loop to listen for input events. We can't actually just let this be + // handled by the main window loop, because the main window might actually + // have been created in a different thread. #ifdef HAVE_THREADS if (Thread::is_threading_supported()) { PT(Thread) thread = new InputThread(this); @@ -518,10 +520,26 @@ thread_main() { } MSG msg; +#ifdef SIMPLE_THREADS + // In the simple threading case, we can't block the thread waiting for a + // message; we yield control back if there are no more messages. + while (true) { + if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) { + if (msg.message == WM_QUIT) { + break; + } + TranslateMessage(&msg); + DispatchMessage(&msg); + } else { + Thread::force_yield(); + } + } +#else while (GetMessage(&msg, nullptr, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } +#endif if (device_cat.is_debug()) { device_cat.debug()