From f80454e17c5f61ed4d864b8796e5f352a6ddb9de Mon Sep 17 00:00:00 2001 From: lijunyu-cn Date: Wed, 25 Mar 2026 14:34:42 +0800 Subject: [PATCH] Reimplement mutex --- src/input/api/SDL/SDLControllerProvider.cpp | 16 +++++++--------- src/input/api/SDL/SDLControllerProvider.h | 4 ++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/input/api/SDL/SDLControllerProvider.cpp b/src/input/api/SDL/SDLControllerProvider.cpp index 9a94d47d..ea88fab7 100644 --- a/src/input/api/SDL/SDLControllerProvider.cpp +++ b/src/input/api/SDL/SDLControllerProvider.cpp @@ -133,15 +133,11 @@ int SDLControllerProvider::get_index(size_t guid_index, const SDL_GUID& guid) co MotionSample SDLControllerProvider::motion_sample(SDL_JoystickID diid) { + std::shared_lock lock(m_mutex); + auto it = m_motion_states.find(diid); - if (it == m_motion_states.end()) - { - return MotionSample{}; - } - - std::scoped_lock lock(it->second.mtx); - return it->second.data; + return (it != m_motion_states.end()) ? it->second.data : MotionSample{}; } void SDLControllerProvider::event_thread() @@ -203,9 +199,12 @@ void SDLControllerProvider::event_thread() { SDL_JoystickID id = event.gsensor.which; uint64_t ts = event.gsensor.timestamp; - auto& state = m_motion_states[id]; + std::scoped_lock lock(m_mutex); + + auto& state = m_motion_states[id]; auto& tracking = state.tracking; + if (event.gsensor.sensor == SDL_SENSOR_ACCEL) { const auto dif = ts - tracking.lastTimestampAccel; @@ -266,7 +265,6 @@ void SDLControllerProvider::event_thread() } state.handler.processMotionSample(tsDifD, tracking.gyro.x, tracking.gyro.y, tracking.gyro.z, tracking.acc.x, -tracking.acc.y, -tracking.acc.z); - std::scoped_lock lock(state.mtx); state.data = state.handler.getMotionSample(); } diff --git a/src/input/api/SDL/SDLControllerProvider.h b/src/input/api/SDL/SDLControllerProvider.h index e979dc94..4763885e 100644 --- a/src/input/api/SDL/SDLControllerProvider.h +++ b/src/input/api/SDL/SDLControllerProvider.h @@ -33,7 +33,8 @@ private: std::atomic_bool m_running = false; std::thread m_thread; - + mutable std::shared_mutex m_mutex; + struct MotionInfoTracking { uint64 lastTimestampGyro{}; @@ -49,7 +50,6 @@ private: { WiiUMotionHandler handler; MotionSample data; - mutable std::mutex mtx; MotionInfoTracking tracking; MotionState() = default;