refactor usb_device_logitech_g27 house keeping thread

This commit is contained in:
Katharine Chui 2025-05-01 16:52:59 +02:00
parent cf52f90db9
commit ccc59c8cec
2 changed files with 34 additions and 44 deletions

View file

@ -11,8 +11,6 @@
#ifdef HAVE_SDL3 #ifdef HAVE_SDL3
#include <thread>
#include "LogitechG27.h" #include "LogitechG27.h"
#include "Emu/Cell/lv2/sys_usbd.h" #include "Emu/Cell/lv2/sys_usbd.h"
#include "Emu/system_config.h" #include "Emu/system_config.h"
@ -21,26 +19,6 @@
LOG_CHANNEL(logitech_g27_log, "LOGIG27"); LOG_CHANNEL(logitech_g27_log, "LOGIG27");
static int SDLCALL refresh_thread(void* arg)
{
auto ctx = reinterpret_cast<usb_device_logitech_g27*>(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<u8, 7>& location) usb_device_logitech_g27::usb_device_logitech_g27(u32 controller_index, const std::array<u8, 7>& location)
: usb_device_emulated(location), m_controller_index(controller_index) : 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; m_default_spring_effect.condition.left_coeff[i] = 0x7FFF;
} }
thread_control_mutex.lock(); {
stop_thread = false; const std::lock_guard<std::mutex> lock(m_thread_control_mutex);
thread_control_mutex.unlock(); m_stop_thread = false;
}
g_cfg_logitech_g27.load(); 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) if (!m_enabled)
return; return;
sprintf(thread_name, "LogiG27 %p", this); m_house_keeping_thread = std::thread([this]()
thread = SDL_CreateThread(refresh_thread, thread_name, this); {
if (thread == nullptr) while (true)
{ {
logitech_g27_log.error("Failed creating sdl housekeeping thread, %s", SDL_GetError()); 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() bool usb_device_logitech_g27::open_device()
@ -118,10 +106,11 @@ static void clear_sdl_joysticks(std::map<uint32_t, std::vector<SDL_Joystick*>>&
usb_device_logitech_g27::~usb_device_logitech_g27() usb_device_logitech_g27::~usb_device_logitech_g27()
{ {
// stop the background thread // stop the house keeping thread
thread_control_mutex.lock(); {
stop_thread = true; const std::lock_guard<std::mutex> lock(m_thread_control_mutex);
thread_control_mutex.unlock(); m_stop_thread = true;
}
// Close sdl handles // Close sdl handles
{ {
@ -133,9 +122,9 @@ usb_device_logitech_g27::~usb_device_logitech_g27()
clear_sdl_joysticks(m_joysticks); clear_sdl_joysticks(m_joysticks);
} }
// wait for the background thread to finish // wait for the house keeping thread to finish
if (thread != nullptr) if (m_enabled)
SDL_WaitThread(thread, nullptr); m_house_keeping_thread.join();
} }
std::shared_ptr<usb_device> usb_device_logitech_g27::make_instance(u32 controller_index, const std::array<u8, 7>& location) std::shared_ptr<usb_device> usb_device_logitech_g27::make_instance(u32 controller_index, const std::array<u8, 7>& location)

View file

@ -14,6 +14,7 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include <thread>
enum logitech_g27_ffb_state enum logitech_g27_ffb_state
{ {
@ -96,12 +97,6 @@ public:
void interrupt_transfer(u32 buf_size, u8* buf, u32 endpoint, UsbTransfer* transfer) override; void interrupt_transfer(u32 buf_size, u8* buf, u32 endpoint, UsbTransfer* transfer) override;
bool open_device() 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: private:
u32 m_controller_index; u32 m_controller_index;
@ -119,4 +114,10 @@ private:
int m_default_spring_effect_id = -1; int m_default_spring_effect_id = -1;
bool m_enabled = false; bool m_enabled = false;
std::thread m_house_keeping_thread;
std::mutex m_thread_control_mutex;
bool m_stop_thread;
void sdl_refresh();
}; };