From dc599901bc4c10947c77aed609290d72b06da693 Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 12 Aug 2019 19:37:11 +0200 Subject: [PATCH] Work around shutdown hang when compiling with SIMPLE_THREADS=1 This is a workaround for a specific case of #508 --- panda/src/device/winInputDeviceManager.cxx | 17 +++++++++++++++++ panda/src/device/winInputDeviceManager.h | 2 ++ panda/src/display/graphicsEngine.cxx | 10 ++++++++++ 3 files changed, 29 insertions(+) diff --git a/panda/src/device/winInputDeviceManager.cxx b/panda/src/device/winInputDeviceManager.cxx index 303400a380..2ca4540ae6 100644 --- a/panda/src/device/winInputDeviceManager.cxx +++ b/panda/src/device/winInputDeviceManager.cxx @@ -464,6 +464,23 @@ destroy_message_loop() { } } +/** + * Sends a signal to the thread input thread, asking it to shut itself down. + */ +void WinInputDeviceManager:: +stop_thread() { +#ifdef HAVE_THREADS + WinInputDeviceManager *mgr = (WinInputDeviceManager *)_global_ptr; + if (mgr != nullptr) { + LightMutexHolder holder(mgr->_lock); + HWND hwnd = mgr->_message_hwnd; + if (hwnd) { + PostMessage(hwnd, WM_QUIT, 0, 0); + } + } +#endif +} + /** * Implementation of the message loop. */ diff --git a/panda/src/device/winInputDeviceManager.h b/panda/src/device/winInputDeviceManager.h index 44eaa053dc..13cab3d7c0 100644 --- a/panda/src/device/winInputDeviceManager.h +++ b/panda/src/device/winInputDeviceManager.h @@ -44,6 +44,8 @@ public: HWND setup_message_loop(); void destroy_message_loop(); + static void stop_thread(); + private: // There are always exactly four of these in existence. XInputDevice _xinput_device0; diff --git a/panda/src/display/graphicsEngine.cxx b/panda/src/display/graphicsEngine.cxx index 80973ae071..c7951ee95a 100644 --- a/panda/src/display/graphicsEngine.cxx +++ b/panda/src/display/graphicsEngine.cxx @@ -49,6 +49,10 @@ #include "callbackGraphicsWindow.h" #include "depthTestAttrib.h" +#if defined(_WIN32) && defined(HAVE_THREADS) && defined(SIMPLE_THREADS) +#include "winInputDeviceManager.h" +#endif + #if defined(WIN32) #define WINDOWS_LEAN_AND_MEAN #include @@ -637,6 +641,12 @@ remove_all_windows() { PStatClient::get_global_pstats()->disconnect(); #endif +#if defined(_WIN32) && defined(HAVE_THREADS) && defined(SIMPLE_THREADS) + // Send a message to the input message pump asking it to shut itself down. + // If we don't do that, the next call will deadlock. + WinInputDeviceManager::stop_thread(); +#endif + // Well, and why not clean up all threads here? Thread::prepare_for_exit(); }