Lock joystick states for thread safety.

This commit is contained in:
Andrzej Kapolka 2014-05-16 11:18:21 -07:00
parent 5a2f61084e
commit 20eadab970
2 changed files with 9 additions and 1 deletions

View file

@ -44,10 +44,16 @@ JoystickManager::~JoystickManager() {
#endif #endif
} }
QVector<JoystickState> JoystickManager::getJoystickStates() {
QMutexLocker locker(&_joystickMutex);
return _joystickStates;
}
void JoystickManager::update() { void JoystickManager::update() {
#ifdef HAVE_SDL #ifdef HAVE_SDL
SDL_JoystickUpdate(); SDL_JoystickUpdate();
QMutexLocker locker(&_joystickMutex);
for (int i = 0; i < _joystickStates.size(); i++) { for (int i = 0; i < _joystickStates.size(); i++) {
SDL_Joystick* joystick = _joysticks.at(i); SDL_Joystick* joystick = _joysticks.at(i);
JoystickState& state = _joystickStates[i]; JoystickState& state = _joystickStates[i];

View file

@ -12,6 +12,7 @@
#ifndef hifi_JoystickManager_h #ifndef hifi_JoystickManager_h
#define hifi_JoystickManager_h #define hifi_JoystickManager_h
#include <QMutex>
#include <QObject> #include <QObject>
#include <QVector> #include <QVector>
@ -31,12 +32,13 @@ public:
JoystickManager(); JoystickManager();
virtual ~JoystickManager(); virtual ~JoystickManager();
const QVector<JoystickState>& getJoystickStates() const { return _joystickStates; } QVector<JoystickState> getJoystickStates();
void update(); void update();
private: private:
QVector<JoystickState> _joystickStates; QVector<JoystickState> _joystickStates;
QMutex _joystickMutex;
#ifdef HAVE_SDL #ifdef HAVE_SDL
QVector<SDL_Joystick*> _joysticks; QVector<SDL_Joystick*> _joysticks;