Fix emulator crash when a pad gets disconnected (e.g. due to inactivity) (#17478)
Some checks are pending
Generate Translation Template / Generate Translation Template (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (/rpcs3/.ci/build-linux-aarch64.sh, gcc, rpcs3/rpcs3-ci-jammy-aarch64:1.6, ubuntu-24.04-arm) (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (/rpcs3/.ci/build-linux.sh, gcc, rpcs3/rpcs3-ci-jammy:1.6, ubuntu-24.04) (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (a1d35836e8d45bfc6f63c26f0a3e5d46ef622fe1, rpcs3/rpcs3-binaries-linux-arm64, /rpcs3/.ci/build-linux-aarch64.sh, clang, rpcs3/rpcs3-ci-jammy-aarch64:1.6, ubuntu-24.04-arm) (push) Waiting to run
Build RPCS3 / RPCS3 Linux ${{ matrix.os }} ${{ matrix.compiler }} (d812f1254a1157c80fd402f94446310560f54e5f, rpcs3/rpcs3-binaries-linux, /rpcs3/.ci/build-linux.sh, clang, rpcs3/rpcs3-ci-jammy:1.6, ubuntu-24.04) (push) Waiting to run
Build RPCS3 / RPCS3 Mac ${{ matrix.name }} (51ae32f468089a8169aaf1567de355ff4a3e0842, rpcs3/rpcs3-binaries-mac, arch -X86_64 .ci/build-mac.sh, Intel) (push) Waiting to run
Build RPCS3 / RPCS3 Mac ${{ matrix.name }} (8e21bdbc40711a3fccd18fbf17b742348b0f4281, rpcs3/rpcs3-binaries-mac-arm64, .ci/build-mac-arm64.sh, Apple Silicon) (push) Waiting to run
Build RPCS3 / RPCS3 Windows (push) Waiting to run
Build RPCS3 / RPCS3 Windows Clang (win64, clang, clang64) (push) Waiting to run
Build RPCS3 / RPCS3 FreeBSD (push) Waiting to run

This commit is contained in:
Antonino Di Guardo 2025-09-07 21:21:35 +02:00 committed by GitHub
parent e457dbdea3
commit 28920f1781
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,10 +21,13 @@ LOG_CHANNEL(hid_log, "HID");
#ifdef ANDROID
std::vector<android_usb_device> g_android_usb_devices;
std::mutex g_android_usb_devices_mutex;
#elif defined(__APPLE__)
std::mutex g_hid_mutex;
#endif
// Global mutex to allow "hid_enumerate()" and "hid_open_path()" are accessed by one thread at a time
// (e.g. thread running "process()" and thread running enumerate_devices()).
// It avoids the emulation crash in case the controller gets disconnected (e.g. due to inactivity)
std::mutex g_hid_mutex;
struct hid_instance
{
public:
@ -90,15 +93,18 @@ private:
hid_device* HidDevice::open()
{
#ifdef ANDROID
hidDevice = hid_libusb_wrap_sys_device(path, -1);
#elif defined(__APPLE__)
std::unique_lock static_lock(g_hid_mutex, std::defer_lock);
if (!static_lock.try_lock())
// Lock before calling "hid_open_path()"
std::unique_lock lock(g_hid_mutex, std::defer_lock);
if (!lock.try_lock())
{
// The enumeration thread is busy. If we lock and open the device, we might get input stutter on other devices.
return nullptr;
}
#ifdef ANDROID
hidDevice = hid_libusb_wrap_sys_device(path, -1);
#elif defined(__APPLE__)
Emu.BlockingCallFromMainThread([this]()
{
hidDevice = hid_open_path(path.c_str());
@ -170,9 +176,8 @@ hid_pad_handler<Device>::~hid_pad_handler()
// Join thread
m_enumeration_thread.reset();
#if defined(__APPLE__)
std::lock_guard static_lock(g_hid_mutex);
#endif
// Lock before accessing any controller (e.g. just to close it with "close()")
std::lock_guard lock(g_hid_mutex);
for (auto& controller : m_controllers)
{
@ -268,9 +273,10 @@ void hid_pad_handler<Device>::enumerate_devices()
#else
for (const auto& [vid, pid] : m_ids)
{
#if defined(__APPLE__)
// Let's make sure hid_enumerate is only done one thread at a time
std::lock_guard static_lock(g_hid_mutex);
std::lock_guard lock(g_hid_mutex);
#if defined(__APPLE__)
Emu.BlockingCallFromMainThread([&]()
{
#endif
@ -345,9 +351,8 @@ void hid_pad_handler<Device>::update_devices()
m_enumerated_serials = std::move(m_new_enumerated_serials);
}
#if defined(__APPLE__)
std::lock_guard static_lock(g_hid_mutex);
#endif
// Lock before accessing any controller (e.g. just to close it with "close()") or before calling "hid_open_path()"
std::lock_guard lock(g_hid_mutex);
// Scrap devices that are not in the new list
for (auto& controller : m_controllers)