all repos — mgba @ ca67e63abb787756cac5b4692ef7a0b072e03d6e

mGBA Game Boy Advance Emulator

Fix a race condition on `InputController::m_pendingEvents`

This member could be accessed at the same from different threads leading
to random (rare) crashes.

Fixes #1875
Bastien Orivel eijebong@bananium.fr
Sun, 13 Sep 2020 14:14:09 +0200
commit

ca67e63abb787756cac5b4692ef7a0b072e03d6e

parent

e232e5ce411091dbc077733c19fdf4aea19943e3

2 files changed, 5 insertions(+), 0 deletions(-)

jump to
M src/platform/qt/InputController.cppsrc/platform/qt/InputController.cpp

@@ -384,6 +384,7 @@ SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;

SDL_JoystickUpdate(); int numButtons = SDL_JoystickNumButtons(joystick); int i; + QReadLocker l(&m_eventsLock); for (i = 0; i < numButtons; ++i) { GBAKey key = static_cast<GBAKey>(mInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i)); if (key == GBA_KEY_NONE) {

@@ -396,6 +397,7 @@ if (SDL_JoystickGetButton(joystick, i)) {

activeButtons |= 1 << key; } } + l.unlock(); int numHats = SDL_JoystickNumHats(joystick); for (i = 0; i < numHats; ++i) { int hat = SDL_JoystickGetHat(joystick, i);

@@ -561,6 +563,7 @@ mInputBindHat(&m_inputMap, type, hat, &bindings);

} void InputController::testGamepad(int type) { + QWriteLocker l(&m_eventsLock); auto activeAxes = activeGamepadAxes(type); auto oldAxes = m_activeAxes; m_activeAxes = activeAxes;
M src/platform/qt/InputController.hsrc/platform/qt/InputController.h

@@ -10,6 +10,7 @@ #include "GamepadHatEvent.h"

#include <QImage> #include <QMutex> +#include <QReadWriteLock> #include <QObject> #include <QSet> #include <QTimer>

@@ -181,6 +182,7 @@ QSet<QPair<int, GamepadHatEvent::Direction>> m_activeHats;

QTimer m_gamepadTimer{nullptr}; QSet<GBAKey> m_pendingEvents; + QReadWriteLock m_eventsLock; }; }