Fixed thread-safety warnings

This commit is contained in:
Sam Lantinga 2023-08-08 22:03:52 -07:00
parent 12deed91f8
commit 4ee0e5a984
3 changed files with 28 additions and 5 deletions

View file

@ -150,26 +150,33 @@ void SDL_LockJoysticks(void)
void SDL_UnlockJoysticks(void)
{
SDL_Mutex *joystick_lock = SDL_joystick_lock;
SDL_bool last_unlock = SDL_FALSE;
--SDL_joysticks_locked;
if (!SDL_joysticks_initialized) {
/* NOTE: There's a small window here where another thread could lock the mutex after we've checked for pending locks */
if (!SDL_joysticks_locked && SDL_AtomicGet(&SDL_joystick_lock_pending) == 0) {
/* NOTE: There's a small window here where another thread could lock the mutex */
SDL_joystick_lock = NULL;
last_unlock = SDL_TRUE;
}
}
SDL_UnlockMutex(joystick_lock);
/* The last unlock after joysticks are uninitialized will cleanup the mutex,
* allowing applications to lock joysticks while reinitializing the system.
*/
if (last_unlock) {
SDL_Mutex *joystick_lock = SDL_joystick_lock;
SDL_LockMutex(joystick_lock);
{
SDL_UnlockMutex(SDL_joystick_lock);
SDL_joystick_lock = NULL;
}
SDL_UnlockMutex(joystick_lock);
SDL_DestroyMutex(joystick_lock);
} else {
SDL_UnlockMutex(SDL_joystick_lock);
}
}
@ -560,6 +567,8 @@ static SDL_bool ShouldAttemptSensorFusion(SDL_Joystick *joystick, SDL_bool *inve
const char *hint;
int hint_value;
SDL_AssertJoysticksLocked();
*invert_sensors = SDL_FALSE;
/* The SDL controller sensor API is only available for gamepads (at the moment) */
@ -620,6 +629,8 @@ static void AttemptSensorFusion(SDL_Joystick *joystick, SDL_bool invert_sensors)
SDL_SensorID *sensors;
unsigned int i, j;
SDL_AssertJoysticksLocked();
if (SDL_InitSubSystem(SDL_INIT_SENSOR) < 0) {
return;
}
@ -686,6 +697,8 @@ static void AttemptSensorFusion(SDL_Joystick *joystick, SDL_bool invert_sensors)
static void CleanupSensorFusion(SDL_Joystick *joystick)
{
SDL_AssertJoysticksLocked();
if (joystick->accel_sensor || joystick->gyro_sensor) {
if (joystick->accel_sensor) {
if (joystick->accel) {