From ccc59c8cec5c9e1a17a753b97778b8ca592e3ae8 Mon Sep 17 00:00:00 2001 From: Katharine Chui Date: Thu, 1 May 2025 16:52:59 +0200 Subject: [PATCH] refactor usb_device_logitech_g27 house keeping thread --- rpcs3/Emu/Io/LogitechG27.cpp | 65 +++++++++++++++--------------------- rpcs3/Emu/Io/LogitechG27.h | 13 ++++---- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/rpcs3/Emu/Io/LogitechG27.cpp b/rpcs3/Emu/Io/LogitechG27.cpp index 7fb986915c..21171e4c71 100644 --- a/rpcs3/Emu/Io/LogitechG27.cpp +++ b/rpcs3/Emu/Io/LogitechG27.cpp @@ -11,8 +11,6 @@ #ifdef HAVE_SDL3 -#include - #include "LogitechG27.h" #include "Emu/Cell/lv2/sys_usbd.h" #include "Emu/system_config.h" @@ -21,26 +19,6 @@ LOG_CHANNEL(logitech_g27_log, "LOGIG27"); -static int SDLCALL refresh_thread(void* arg) -{ - auto ctx = reinterpret_cast(arg); - - while (true) - { - ctx->thread_control_mutex.lock(); - if (ctx->stop_thread) - { - break; - } - ctx->thread_control_mutex.unlock(); - ctx->sdl_refresh(); - std::this_thread::sleep_for(std::chrono::seconds(5)); - } - ctx->thread_control_mutex.unlock(); - - return 0; -} - usb_device_logitech_g27::usb_device_logitech_g27(u32 controller_index, const std::array& location) : usb_device_emulated(location), m_controller_index(controller_index) { @@ -77,9 +55,10 @@ usb_device_logitech_g27::usb_device_logitech_g27(u32 controller_index, const std m_default_spring_effect.condition.left_coeff[i] = 0x7FFF; } - thread_control_mutex.lock(); - stop_thread = false; - thread_control_mutex.unlock(); + { + const std::lock_guard lock(m_thread_control_mutex); + m_stop_thread = false; + } g_cfg_logitech_g27.load(); @@ -90,12 +69,21 @@ usb_device_logitech_g27::usb_device_logitech_g27(u32 controller_index, const std if (!m_enabled) return; - sprintf(thread_name, "LogiG27 %p", this); - thread = SDL_CreateThread(refresh_thread, thread_name, this); - if (thread == nullptr) - { - logitech_g27_log.error("Failed creating sdl housekeeping thread, %s", SDL_GetError()); - } + m_house_keeping_thread = std::thread([this]() + { + while (true) + { + this->m_thread_control_mutex.lock(); + if (this->m_stop_thread) + { + break; + } + this->m_thread_control_mutex.unlock(); + this->sdl_refresh(); + std::this_thread::sleep_for(std::chrono::seconds(5)); + } + this->m_thread_control_mutex.unlock(); + }); } bool usb_device_logitech_g27::open_device() @@ -118,10 +106,11 @@ static void clear_sdl_joysticks(std::map>& usb_device_logitech_g27::~usb_device_logitech_g27() { - // stop the background thread - thread_control_mutex.lock(); - stop_thread = true; - thread_control_mutex.unlock(); + // stop the house keeping thread + { + const std::lock_guard lock(m_thread_control_mutex); + m_stop_thread = true; + } // Close sdl handles { @@ -133,9 +122,9 @@ usb_device_logitech_g27::~usb_device_logitech_g27() clear_sdl_joysticks(m_joysticks); } - // wait for the background thread to finish - if (thread != nullptr) - SDL_WaitThread(thread, nullptr); + // wait for the house keeping thread to finish + if (m_enabled) + m_house_keeping_thread.join(); } std::shared_ptr usb_device_logitech_g27::make_instance(u32 controller_index, const std::array& location) diff --git a/rpcs3/Emu/Io/LogitechG27.h b/rpcs3/Emu/Io/LogitechG27.h index 5b04a15279..72979d0316 100644 --- a/rpcs3/Emu/Io/LogitechG27.h +++ b/rpcs3/Emu/Io/LogitechG27.h @@ -14,6 +14,7 @@ #include #include +#include enum logitech_g27_ffb_state { @@ -96,12 +97,6 @@ public: void interrupt_transfer(u32 buf_size, u8* buf, u32 endpoint, UsbTransfer* transfer) override; bool open_device() override; - std::mutex thread_control_mutex; - bool stop_thread; - char thread_name[64]; - SDL_Thread* thread = nullptr; - void sdl_refresh(); - private: u32 m_controller_index; @@ -119,4 +114,10 @@ private: int m_default_spring_effect_id = -1; bool m_enabled = false; + + std::thread m_house_keeping_thread; + std::mutex m_thread_control_mutex; + bool m_stop_thread; + + void sdl_refresh(); };