Exposed the joystick locking functions for multi-threaded access to the joystick API
authorSam Lantinga <slouken@libsdl.org>
Tue, 10 Oct 2017 11:10:15 -0700
changeset 1158149393a9dffe4
parent 11579 5789a90f2804
child 11582 cc0b1273a381
Exposed the joystick locking functions for multi-threaded access to the joystick API
include/SDL_joystick.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/joystick/SDL_gamecontroller.c
src/joystick/SDL_joystick.c
src/joystick/SDL_joystick_c.h
     1.1 --- a/include/SDL_joystick.h	Mon Oct 09 11:45:15 2017 -0700
     1.2 +++ b/include/SDL_joystick.h	Tue Oct 10 11:10:15 2017 -0700
     1.3 @@ -106,6 +106,20 @@
     1.4  } SDL_JoystickPowerLevel;
     1.5  
     1.6  /* Function prototypes */
     1.7 +
     1.8 +/**
     1.9 + * Locking for multi-threaded access to the joystick API
    1.10 + *
    1.11 + * If you are using the joystick API or handling events from multiple threads
    1.12 + * you should use these locking functions to protect access to the joysticks.
    1.13 + *
    1.14 + * In particular, you are guaranteed that the joystick list won't change, so
    1.15 + * the API functions that take a joystick index will be valid, and joystick
    1.16 + * and game controller events will not be delivered.
    1.17 + */
    1.18 +extern DECLSPEC void SDLCALL SDL_LockJoysticks(void);
    1.19 +extern DECLSPEC void SDLCALL SDL_UnlockJoysticks(void);
    1.20 +
    1.21  /**
    1.22   *  Count the number of joysticks attached to the system right now
    1.23   */
     2.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Mon Oct 09 11:45:15 2017 -0700
     2.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Tue Oct 10 11:10:15 2017 -0700
     2.3 @@ -635,3 +635,5 @@
     2.4  #define SDL_Vulkan_GetInstanceExtensions SDL_Vulkan_GetInstanceExtensions_REAL
     2.5  #define SDL_Vulkan_CreateSurface SDL_Vulkan_CreateSurface_REAL
     2.6  #define SDL_Vulkan_GetDrawableSize SDL_Vulkan_GetDrawableSize_REAL
     2.7 +#define SDL_LockJoysticks SDL_LockJoysticks_REAL
     2.8 +#define SDL_UnlockJoysticks SDL_UnlockJoysticks_REAL
     3.1 --- a/src/dynapi/SDL_dynapi_procs.h	Mon Oct 09 11:45:15 2017 -0700
     3.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Tue Oct 10 11:10:15 2017 -0700
     3.3 @@ -669,3 +669,5 @@
     3.4  SDL_DYNAPI_PROC(SDL_bool,SDL_Vulkan_GetInstanceExtensions,(SDL_Window *a, unsigned int *b, const char **c),(a,b,c),return)
     3.5  SDL_DYNAPI_PROC(SDL_bool,SDL_Vulkan_CreateSurface,(SDL_Window *a, VkInstance b, VkSurfaceKHR *c),(a,b,c),return)
     3.6  SDL_DYNAPI_PROC(void,SDL_Vulkan_GetDrawableSize,(SDL_Window *a, int *b, int *c),(a,b,c),)
     3.7 +SDL_DYNAPI_PROC(void,SDL_LockJoysticks,(void),(),)
     3.8 +SDL_DYNAPI_PROC(void,SDL_UnlockJoysticks,(void),(),)
     4.1 --- a/src/joystick/SDL_gamecontroller.c	Mon Oct 09 11:45:15 2017 -0700
     4.2 +++ b/src/joystick/SDL_gamecontroller.c	Tue Oct 10 11:10:15 2017 -0700
     4.3 @@ -24,9 +24,9 @@
     4.4  
     4.5  #include "SDL_events.h"
     4.6  #include "SDL_assert.h"
     4.7 +#include "SDL_hints.h"
     4.8  #include "SDL_sysjoystick.h"
     4.9  #include "SDL_joystick_c.h"
    4.10 -#include "SDL_hints.h"
    4.11  #include "SDL_gamecontrollerdb.h"
    4.12  
    4.13  #if !SDL_EVENTS_DISABLED
    4.14 @@ -910,7 +910,7 @@
    4.15      SDL_JoystickGUID guid;
    4.16      ControllerMapping_t *mapping;
    4.17  
    4.18 -    SDL_LockJoystickList();
    4.19 +    SDL_LockJoysticks();
    4.20      name = SDL_JoystickNameForIndex(device_index);
    4.21      guid = SDL_JoystickGetDeviceGUID(device_index);
    4.22      mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
    4.23 @@ -919,7 +919,7 @@
    4.24          mapping = s_pXInputMapping;
    4.25      }
    4.26  #endif
    4.27 -    SDL_UnlockJoystickList();
    4.28 +    SDL_UnlockJoysticks();
    4.29      return mapping;
    4.30  }
    4.31  
    4.32 @@ -1349,7 +1349,7 @@
    4.33          return (NULL);
    4.34      }
    4.35  
    4.36 -    SDL_LockJoystickList();
    4.37 +    SDL_LockJoysticks();
    4.38  
    4.39      gamecontrollerlist = SDL_gamecontrollers;
    4.40      /* If the controller is already open, return it */
    4.41 @@ -1357,7 +1357,7 @@
    4.42          if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
    4.43                  gamecontroller = gamecontrollerlist;
    4.44                  ++gamecontroller->ref_count;
    4.45 -                SDL_UnlockJoystickList();
    4.46 +                SDL_UnlockJoysticks();
    4.47                  return (gamecontroller);
    4.48          }
    4.49          gamecontrollerlist = gamecontrollerlist->next;
    4.50 @@ -1367,7 +1367,7 @@
    4.51      pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
    4.52      if (!pSupportedController) {
    4.53          SDL_SetError("Couldn't find mapping for device (%d)", device_index);
    4.54 -        SDL_UnlockJoystickList();
    4.55 +        SDL_UnlockJoysticks();
    4.56          return NULL;
    4.57      }
    4.58  
    4.59 @@ -1375,14 +1375,14 @@
    4.60      gamecontroller = (SDL_GameController *) SDL_calloc(1, sizeof(*gamecontroller));
    4.61      if (gamecontroller == NULL) {
    4.62          SDL_OutOfMemory();
    4.63 -        SDL_UnlockJoystickList();
    4.64 +        SDL_UnlockJoysticks();
    4.65          return NULL;
    4.66      }
    4.67  
    4.68      gamecontroller->joystick = SDL_JoystickOpen(device_index);
    4.69      if (!gamecontroller->joystick) {
    4.70          SDL_free(gamecontroller);
    4.71 -        SDL_UnlockJoystickList();
    4.72 +        SDL_UnlockJoysticks();
    4.73          return NULL;
    4.74      }
    4.75  
    4.76 @@ -1392,7 +1392,7 @@
    4.77              SDL_OutOfMemory();
    4.78              SDL_JoystickClose(gamecontroller->joystick);
    4.79              SDL_free(gamecontroller);
    4.80 -            SDL_UnlockJoystickList();
    4.81 +            SDL_UnlockJoysticks();
    4.82              return NULL;
    4.83          }
    4.84      }
    4.85 @@ -1403,7 +1403,7 @@
    4.86              SDL_JoystickClose(gamecontroller->joystick);
    4.87              SDL_free(gamecontroller->last_match_axis);
    4.88              SDL_free(gamecontroller);
    4.89 -            SDL_UnlockJoystickList();
    4.90 +            SDL_UnlockJoysticks();
    4.91              return NULL;
    4.92          }
    4.93      }
    4.94 @@ -1416,7 +1416,7 @@
    4.95      gamecontroller->next = SDL_gamecontrollers;
    4.96      SDL_gamecontrollers = gamecontroller;
    4.97  
    4.98 -    SDL_UnlockJoystickList();
    4.99 +    SDL_UnlockJoysticks();
   4.100  
   4.101      return (gamecontroller);
   4.102  }
   4.103 @@ -1589,16 +1589,16 @@
   4.104  {
   4.105      SDL_GameController *gamecontroller;
   4.106  
   4.107 -    SDL_LockJoystickList();
   4.108 +    SDL_LockJoysticks();
   4.109      gamecontroller = SDL_gamecontrollers;
   4.110      while (gamecontroller) {
   4.111          if (gamecontroller->joystick->instance_id == joyid) {
   4.112 -            SDL_UnlockJoystickList();
   4.113 +            SDL_UnlockJoysticks();
   4.114              return gamecontroller;
   4.115          }
   4.116          gamecontroller = gamecontroller->next;
   4.117      }
   4.118 -    SDL_UnlockJoystickList();
   4.119 +    SDL_UnlockJoysticks();
   4.120      return NULL;
   4.121  }
   4.122  
   4.123 @@ -1674,11 +1674,11 @@
   4.124      if (!gamecontroller)
   4.125          return;
   4.126  
   4.127 -    SDL_LockJoystickList();
   4.128 +    SDL_LockJoysticks();
   4.129  
   4.130      /* First decrement ref count */
   4.131      if (--gamecontroller->ref_count > 0) {
   4.132 -        SDL_UnlockJoystickList();
   4.133 +        SDL_UnlockJoysticks();
   4.134          return;
   4.135      }
   4.136  
   4.137 @@ -1705,7 +1705,7 @@
   4.138      SDL_free(gamecontroller->last_hat_mask);
   4.139      SDL_free(gamecontroller);
   4.140  
   4.141 -    SDL_UnlockJoystickList();
   4.142 +    SDL_UnlockJoysticks();
   4.143  }
   4.144  
   4.145  
   4.146 @@ -1715,12 +1715,12 @@
   4.147  void
   4.148  SDL_GameControllerQuit(void)
   4.149  {
   4.150 -    SDL_LockJoystickList();
   4.151 +    SDL_LockJoysticks();
   4.152      while (SDL_gamecontrollers) {
   4.153          SDL_gamecontrollers->ref_count = 1;
   4.154          SDL_GameControllerClose(SDL_gamecontrollers);
   4.155      }
   4.156 -    SDL_UnlockJoystickList();
   4.157 +    SDL_UnlockJoysticks();
   4.158  }
   4.159  
   4.160  void
     5.1 --- a/src/joystick/SDL_joystick.c	Mon Oct 09 11:45:15 2017 -0700
     5.2 +++ b/src/joystick/SDL_joystick.c	Tue Oct 10 11:10:15 2017 -0700
     5.3 @@ -40,7 +40,7 @@
     5.4  static SDL_mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */
     5.5  
     5.6  void
     5.7 -SDL_LockJoystickList(void)
     5.8 +SDL_LockJoysticks(void)
     5.9  {
    5.10      if (SDL_joystick_lock) {
    5.11          SDL_LockMutex(SDL_joystick_lock);
    5.12 @@ -48,7 +48,7 @@
    5.13  }
    5.14  
    5.15  void
    5.16 -SDL_UnlockJoystickList(void)
    5.17 +SDL_UnlockJoysticks(void)
    5.18  {
    5.19      if (SDL_joystick_lock) {
    5.20          SDL_UnlockMutex(SDL_joystick_lock);
    5.21 @@ -168,7 +168,7 @@
    5.22          return (NULL);
    5.23      }
    5.24  
    5.25 -    SDL_LockJoystickList();
    5.26 +    SDL_LockJoysticks();
    5.27  
    5.28      joysticklist = SDL_joysticks;
    5.29      /* If the joystick is already open, return it
    5.30 @@ -178,7 +178,7 @@
    5.31          if (SDL_JoystickGetDeviceInstanceID(device_index) == joysticklist->instance_id) {
    5.32                  joystick = joysticklist;
    5.33                  ++joystick->ref_count;
    5.34 -                SDL_UnlockJoystickList();
    5.35 +                SDL_UnlockJoysticks();
    5.36                  return (joystick);
    5.37          }
    5.38          joysticklist = joysticklist->next;
    5.39 @@ -188,13 +188,13 @@
    5.40      joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
    5.41      if (joystick == NULL) {
    5.42          SDL_OutOfMemory();
    5.43 -        SDL_UnlockJoystickList();
    5.44 +        SDL_UnlockJoysticks();
    5.45          return NULL;
    5.46      }
    5.47  
    5.48      if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) {
    5.49          SDL_free(joystick);
    5.50 -        SDL_UnlockJoystickList();
    5.51 +        SDL_UnlockJoysticks();
    5.52          return NULL;
    5.53      }
    5.54  
    5.55 @@ -222,7 +222,7 @@
    5.56          || ((joystick->nbuttons > 0) && !joystick->buttons)) {
    5.57          SDL_OutOfMemory();
    5.58          SDL_JoystickClose(joystick);
    5.59 -        SDL_UnlockJoystickList();
    5.60 +        SDL_UnlockJoysticks();
    5.61          return NULL;
    5.62      }
    5.63      joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
    5.64 @@ -244,7 +244,7 @@
    5.65      joystick->next = SDL_joysticks;
    5.66      SDL_joysticks = joystick;
    5.67  
    5.68 -    SDL_UnlockJoystickList();
    5.69 +    SDL_UnlockJoysticks();
    5.70  
    5.71      SDL_SYS_JoystickUpdate(joystick);
    5.72  
    5.73 @@ -460,14 +460,14 @@
    5.74  {
    5.75      SDL_Joystick *joystick;
    5.76  
    5.77 -    SDL_LockJoystickList();
    5.78 +    SDL_LockJoysticks();
    5.79      for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
    5.80          if (joystick->instance_id == joyid) {
    5.81 -            SDL_UnlockJoystickList();
    5.82 +            SDL_UnlockJoysticks();
    5.83              return joystick;
    5.84          }
    5.85      }
    5.86 -    SDL_UnlockJoystickList();
    5.87 +    SDL_UnlockJoysticks();
    5.88      return NULL;
    5.89  }
    5.90  
    5.91 @@ -497,16 +497,16 @@
    5.92          return;
    5.93      }
    5.94  
    5.95 -    SDL_LockJoystickList();
    5.96 +    SDL_LockJoysticks();
    5.97  
    5.98      /* First decrement ref count */
    5.99      if (--joystick->ref_count > 0) {
   5.100 -        SDL_UnlockJoystickList();
   5.101 +        SDL_UnlockJoysticks();
   5.102          return;
   5.103      }
   5.104  
   5.105      if (SDL_updating_joystick) {
   5.106 -        SDL_UnlockJoystickList();
   5.107 +        SDL_UnlockJoysticks();
   5.108          return;
   5.109      }
   5.110  
   5.111 @@ -538,7 +538,7 @@
   5.112      SDL_free(joystick->buttons);
   5.113      SDL_free(joystick);
   5.114  
   5.115 -    SDL_UnlockJoystickList();
   5.116 +    SDL_UnlockJoysticks();
   5.117  }
   5.118  
   5.119  void
   5.120 @@ -547,7 +547,7 @@
   5.121      /* Make sure we're not getting called in the middle of updating joysticks */
   5.122      SDL_assert(!SDL_updating_joystick);
   5.123  
   5.124 -    SDL_LockJoystickList();
   5.125 +    SDL_LockJoysticks();
   5.126  
   5.127      /* Stop the event polling */
   5.128      while (SDL_joysticks) {
   5.129 @@ -558,7 +558,7 @@
   5.130      /* Quit the joystick setup */
   5.131      SDL_SYS_JoystickQuit();
   5.132  
   5.133 -    SDL_UnlockJoystickList();
   5.134 +    SDL_UnlockJoysticks();
   5.135  
   5.136  #if !SDL_EVENTS_DISABLED
   5.137      SDL_QuitSubSystem(SDL_INIT_EVENTS);
   5.138 @@ -847,18 +847,18 @@
   5.139  {
   5.140      SDL_Joystick *joystick;
   5.141  
   5.142 -    SDL_LockJoystickList();
   5.143 +    SDL_LockJoysticks();
   5.144  
   5.145      if (SDL_updating_joystick) {
   5.146          /* The joysticks are already being updated */
   5.147 -        SDL_UnlockJoystickList();
   5.148 +        SDL_UnlockJoysticks();
   5.149          return;
   5.150      }
   5.151  
   5.152      SDL_updating_joystick = SDL_TRUE;
   5.153  
   5.154      /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
   5.155 -    SDL_UnlockJoystickList();
   5.156 +    SDL_UnlockJoysticks();
   5.157  
   5.158      for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
   5.159          SDL_SYS_JoystickUpdate(joystick);
   5.160 @@ -885,7 +885,7 @@
   5.161          }
   5.162      }
   5.163  
   5.164 -    SDL_LockJoystickList();
   5.165 +    SDL_LockJoysticks();
   5.166  
   5.167      SDL_updating_joystick = SDL_FALSE;
   5.168  
   5.169 @@ -901,7 +901,7 @@
   5.170       */
   5.171      SDL_SYS_JoystickDetect();
   5.172  
   5.173 -    SDL_UnlockJoystickList();
   5.174 +    SDL_UnlockJoysticks();
   5.175  }
   5.176  
   5.177  int
     6.1 --- a/src/joystick/SDL_joystick_c.h	Mon Oct 09 11:45:15 2017 -0700
     6.2 +++ b/src/joystick/SDL_joystick_c.h	Tue Oct 10 11:10:15 2017 -0700
     6.3 @@ -33,10 +33,6 @@
     6.4  extern int SDL_GameControllerInit(void);
     6.5  extern void SDL_GameControllerQuit(void);
     6.6  
     6.7 -/* Locking for multi-threaded access to the joystick API */
     6.8 -extern void SDL_LockJoystickList(void);
     6.9 -extern void SDL_UnlockJoystickList(void);
    6.10 -
    6.11  /* Function to extract information from an SDL joystick GUID */
    6.12  extern void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version);
    6.13