WinRT: prevented a potential race condition in the XInput backend
authorDavid Ludwig <dludwig@pobox.com>
Wed, 25 Dec 2013 14:42:38 -0500
changeset 8562f1aac2d56c76
parent 8561 c10e5bfa6459
child 8563 c0e68f3b6bbb
WinRT: prevented a potential race condition in the XInput backend

The race condition could've been triggered on device removal.
src/joystick/winrt/SDL_xinputjoystick.c
     1.1 --- a/src/joystick/winrt/SDL_xinputjoystick.c	Wed Dec 25 14:20:40 2013 -0500
     1.2 +++ b/src/joystick/winrt/SDL_xinputjoystick.c	Wed Dec 25 14:42:38 2013 -0500
     1.3 @@ -106,25 +106,27 @@
     1.4           */
     1.5  
     1.6          /* See if any new devices are connected. */
     1.7 +        SDL_LockMutex(g_DeviceInfoLock);
     1.8          for (i = 0; i < XUSER_MAX_COUNT; ++i) {
     1.9              if (!g_XInputData[i].isDeviceConnected &&
    1.10                  !g_XInputData[i].isDeviceConnectionEventPending &&
    1.11                  !g_XInputData[i].isDeviceRemovalEventPending)
    1.12              {
    1.13 +                SDL_UnlockMutex(g_DeviceInfoLock);
    1.14                  result = XInputGetCapabilities(i, 0, &tempXInputCaps);
    1.15 +                SDL_LockMutex(g_DeviceInfoLock);
    1.16                  if (result == ERROR_SUCCESS) {
    1.17                      /* Yes, a device is connected.  Mark it as such.
    1.18                         Others will be told about this (via an
    1.19                         SDL_JOYDEVICEADDED event) in the next call to
    1.20                         SDL_SYS_JoystickDetect.
    1.21                       */
    1.22 -                    SDL_LockMutex(g_DeviceInfoLock);
    1.23                      g_XInputData[i].isDeviceConnected = SDL_TRUE;
    1.24                      g_XInputData[i].isDeviceConnectionEventPending = SDL_TRUE;
    1.25 -                    SDL_UnlockMutex(g_DeviceInfoLock);
    1.26                  }
    1.27              }
    1.28          }
    1.29 +        SDL_UnlockMutex(g_DeviceInfoLock);
    1.30      }
    1.31  
    1.32      return 0;