diff --git a/src/DOF.cpp b/src/DOF.cpp index d767240..8c3e422 100644 --- a/src/DOF.cpp +++ b/src/DOF.cpp @@ -11,15 +11,28 @@ #include "Pinball.h" #include "general/StringExtensions.h" +#ifdef __HIDAPI__ +#include +#endif + namespace DOF { DOF::DOF() - : m_pinball(new Pinball()) { +#ifdef __HIDAPI__ + hid_init(); +#endif + m_pinball = new Pinball(); } -DOF::~DOF() { delete m_pinball; } +DOF::~DOF() +{ + delete m_pinball; +#ifdef __HIDAPI__ + hid_exit(); +#endif +} void DOF::Init(const char* tableFilename, const char* romName) { diff --git a/src/cab/Cabinet.cpp b/src/cab/Cabinet.cpp index c1d1d5f..3c82c21 100644 --- a/src/cab/Cabinet.cpp +++ b/src/cab/Cabinet.cpp @@ -18,7 +18,6 @@ #include #ifdef __HIDAPI__ -#include #include "out/ps/Pinscape.h" #include "out/ps/PinscapeAutoConfigurator.h" #include "out/pspico/PinscapePico.h" @@ -48,8 +47,6 @@ Cabinet::Cabinet() m_colors = new ColorList(); #ifdef __HIDAPI__ - hid_init(); - Pinscape::Initialize(); PinscapePico::Initialize(); #endif @@ -83,9 +80,6 @@ Cabinet::~Cabinet() delete m_outputControllers; m_outputControllers = nullptr; } -#ifdef __HIDAPI__ - hid_exit(); -#endif } void Cabinet::AutoConfig() diff --git a/src/cab/out/OutputControllerCompleteBase.cpp b/src/cab/out/OutputControllerCompleteBase.cpp index a31894b..87c4b0e 100644 --- a/src/cab/out/OutputControllerCompleteBase.cpp +++ b/src/cab/out/OutputControllerCompleteBase.cpp @@ -233,25 +233,29 @@ void OutputControllerCompleteBase::FinishUpdaterThread() { if (m_updaterThread && m_updaterThread->joinable()) { - try - { - m_keepUpdaterThreadAlive = false; - UpdaterThreadSignal(); + m_keepUpdaterThreadAlive = false; + UpdaterThreadSignal(); - auto future = std::async(std::launch::async, [this]() { m_updaterThread->join(); }); + auto start = std::chrono::steady_clock::now(); + bool joined = false; - if (future.wait_for(std::chrono::milliseconds(1000)) == std::future_status::timeout) + while (std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count() < 1000) + { + if (!IsUpdaterThreadActive()) { - Log::Warning("Updater thread did not quit within timeout. Thread termination may be forceful."); - m_updaterThread->detach(); + m_updaterThread->join(); + joined = true; + break; } - - m_updaterThread.reset(); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } - catch (const std::exception& e) + + if (!joined) { - Log::Exception(StringExtensions::Build("Error occurred during termination of updater thread: {0}", e.what())); + Log::Warning("Updater thread did not quit within timeout. Thread termination may be forceful."); + m_updaterThread->detach(); } + m_updaterThread.reset(); } }