Exposed the joystick locking functions for multi-threaded access to the joystick API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
diff --git a/include/SDL_joystick.h b/include/SDL_joystick.h
index 698b09c..f598dc8 100644
--- a/include/SDL_joystick.h
+++ b/include/SDL_joystick.h
@@ -106,6 +106,20 @@ typedef enum
} SDL_JoystickPowerLevel;
/* Function prototypes */
+
+/**
+ * Locking for multi-threaded access to the joystick API
+ *
+ * If you are using the joystick API or handling events from multiple threads
+ * you should use these locking functions to protect access to the joysticks.
+ *
+ * In particular, you are guaranteed that the joystick list won't change, so
+ * the API functions that take a joystick index will be valid, and joystick
+ * and game controller events will not be delivered.
+ */
+extern DECLSPEC void SDLCALL SDL_LockJoysticks(void);
+extern DECLSPEC void SDLCALL SDL_UnlockJoysticks(void);
+
/**
* Count the number of joysticks attached to the system right now
*/
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 95943bc..3ef56d2 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -635,3 +635,5 @@
#define SDL_Vulkan_GetInstanceExtensions SDL_Vulkan_GetInstanceExtensions_REAL
#define SDL_Vulkan_CreateSurface SDL_Vulkan_CreateSurface_REAL
#define SDL_Vulkan_GetDrawableSize SDL_Vulkan_GetDrawableSize_REAL
+#define SDL_LockJoysticks SDL_LockJoysticks_REAL
+#define SDL_UnlockJoysticks SDL_UnlockJoysticks_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index b7466fd..730fbcc 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -669,3 +669,5 @@ SDL_DYNAPI_PROC(void,SDL_Vulkan_UnloadLibrary,(void),(),)
SDL_DYNAPI_PROC(SDL_bool,SDL_Vulkan_GetInstanceExtensions,(SDL_Window *a, unsigned int *b, const char **c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_Vulkan_CreateSurface,(SDL_Window *a, VkInstance b, VkSurfaceKHR *c),(a,b,c),return)
SDL_DYNAPI_PROC(void,SDL_Vulkan_GetDrawableSize,(SDL_Window *a, int *b, int *c),(a,b,c),)
+SDL_DYNAPI_PROC(void,SDL_LockJoysticks,(void),(),)
+SDL_DYNAPI_PROC(void,SDL_UnlockJoysticks,(void),(),)
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 0802c88..13953de 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -24,9 +24,9 @@
#include "SDL_events.h"
#include "SDL_assert.h"
+#include "SDL_hints.h"
#include "SDL_sysjoystick.h"
#include "SDL_joystick_c.h"
-#include "SDL_hints.h"
#include "SDL_gamecontrollerdb.h"
#if !SDL_EVENTS_DISABLED
@@ -910,7 +910,7 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
SDL_JoystickGUID guid;
ControllerMapping_t *mapping;
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
name = SDL_JoystickNameForIndex(device_index);
guid = SDL_JoystickGetDeviceGUID(device_index);
mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
@@ -919,7 +919,7 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
mapping = s_pXInputMapping;
}
#endif
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return mapping;
}
@@ -1349,7 +1349,7 @@ SDL_GameControllerOpen(int device_index)
return (NULL);
}
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
gamecontrollerlist = SDL_gamecontrollers;
/* If the controller is already open, return it */
@@ -1357,7 +1357,7 @@ SDL_GameControllerOpen(int device_index)
if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
gamecontroller = gamecontrollerlist;
++gamecontroller->ref_count;
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return (gamecontroller);
}
gamecontrollerlist = gamecontrollerlist->next;
@@ -1367,7 +1367,7 @@ SDL_GameControllerOpen(int device_index)
pSupportedController = SDL_PrivateGetControllerMapping(device_index);
if (!pSupportedController) {
SDL_SetError("Couldn't find mapping for device (%d)", device_index);
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
@@ -1375,14 +1375,14 @@ SDL_GameControllerOpen(int device_index)
gamecontroller = (SDL_GameController *) SDL_calloc(1, sizeof(*gamecontroller));
if (gamecontroller == NULL) {
SDL_OutOfMemory();
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
gamecontroller->joystick = SDL_JoystickOpen(device_index);
if (!gamecontroller->joystick) {
SDL_free(gamecontroller);
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
@@ -1392,7 +1392,7 @@ SDL_GameControllerOpen(int device_index)
SDL_OutOfMemory();
SDL_JoystickClose(gamecontroller->joystick);
SDL_free(gamecontroller);
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
}
@@ -1403,7 +1403,7 @@ SDL_GameControllerOpen(int device_index)
SDL_JoystickClose(gamecontroller->joystick);
SDL_free(gamecontroller->last_match_axis);
SDL_free(gamecontroller);
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
}
@@ -1416,7 +1416,7 @@ SDL_GameControllerOpen(int device_index)
gamecontroller->next = SDL_gamecontrollers;
SDL_gamecontrollers = gamecontroller;
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return (gamecontroller);
}
@@ -1589,16 +1589,16 @@ SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
{
SDL_GameController *gamecontroller;
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
gamecontroller = SDL_gamecontrollers;
while (gamecontroller) {
if (gamecontroller->joystick->instance_id == joyid) {
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return gamecontroller;
}
gamecontroller = gamecontroller->next;
}
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
@@ -1674,11 +1674,11 @@ SDL_GameControllerClose(SDL_GameController * gamecontroller)
if (!gamecontroller)
return;
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
/* First decrement ref count */
if (--gamecontroller->ref_count > 0) {
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return;
}
@@ -1705,7 +1705,7 @@ SDL_GameControllerClose(SDL_GameController * gamecontroller)
SDL_free(gamecontroller->last_hat_mask);
SDL_free(gamecontroller);
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
}
@@ -1715,12 +1715,12 @@ SDL_GameControllerClose(SDL_GameController * gamecontroller)
void
SDL_GameControllerQuit(void)
{
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
while (SDL_gamecontrollers) {
SDL_gamecontrollers->ref_count = 1;
SDL_GameControllerClose(SDL_gamecontrollers);
}
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
}
void
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 0724db2..4c4cae7 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -40,7 +40,7 @@ static SDL_bool SDL_updating_joystick = SDL_FALSE;
static SDL_mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */
void
-SDL_LockJoystickList(void)
+SDL_LockJoysticks(void)
{
if (SDL_joystick_lock) {
SDL_LockMutex(SDL_joystick_lock);
@@ -48,7 +48,7 @@ SDL_LockJoystickList(void)
}
void
-SDL_UnlockJoystickList(void)
+SDL_UnlockJoysticks(void)
{
if (SDL_joystick_lock) {
SDL_UnlockMutex(SDL_joystick_lock);
@@ -168,7 +168,7 @@ SDL_JoystickOpen(int device_index)
return (NULL);
}
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
joysticklist = SDL_joysticks;
/* If the joystick is already open, return it
@@ -178,7 +178,7 @@ SDL_JoystickOpen(int device_index)
if (SDL_JoystickGetDeviceInstanceID(device_index) == joysticklist->instance_id) {
joystick = joysticklist;
++joystick->ref_count;
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return (joystick);
}
joysticklist = joysticklist->next;
@@ -188,13 +188,13 @@ SDL_JoystickOpen(int device_index)
joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
if (joystick == NULL) {
SDL_OutOfMemory();
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) {
SDL_free(joystick);
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
@@ -222,7 +222,7 @@ SDL_JoystickOpen(int device_index)
|| ((joystick->nbuttons > 0) && !joystick->buttons)) {
SDL_OutOfMemory();
SDL_JoystickClose(joystick);
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
@@ -244,7 +244,7 @@ SDL_JoystickOpen(int device_index)
joystick->next = SDL_joysticks;
SDL_joysticks = joystick;
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
SDL_SYS_JoystickUpdate(joystick);
@@ -460,14 +460,14 @@ SDL_JoystickFromInstanceID(SDL_JoystickID joyid)
{
SDL_Joystick *joystick;
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
if (joystick->instance_id == joyid) {
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return joystick;
}
}
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return NULL;
}
@@ -497,16 +497,16 @@ SDL_JoystickClose(SDL_Joystick * joystick)
return;
}
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
/* First decrement ref count */
if (--joystick->ref_count > 0) {
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return;
}
if (SDL_updating_joystick) {
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return;
}
@@ -538,7 +538,7 @@ SDL_JoystickClose(SDL_Joystick * joystick)
SDL_free(joystick->buttons);
SDL_free(joystick);
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
}
void
@@ -547,7 +547,7 @@ SDL_JoystickQuit(void)
/* Make sure we're not getting called in the middle of updating joysticks */
SDL_assert(!SDL_updating_joystick);
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
/* Stop the event polling */
while (SDL_joysticks) {
@@ -558,7 +558,7 @@ SDL_JoystickQuit(void)
/* Quit the joystick setup */
SDL_SYS_JoystickQuit();
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
#if !SDL_EVENTS_DISABLED
SDL_QuitSubSystem(SDL_INIT_EVENTS);
@@ -847,18 +847,18 @@ SDL_JoystickUpdate(void)
{
SDL_Joystick *joystick;
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
if (SDL_updating_joystick) {
/* The joysticks are already being updated */
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
return;
}
SDL_updating_joystick = SDL_TRUE;
/* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
SDL_SYS_JoystickUpdate(joystick);
@@ -885,7 +885,7 @@ SDL_JoystickUpdate(void)
}
}
- SDL_LockJoystickList();
+ SDL_LockJoysticks();
SDL_updating_joystick = SDL_FALSE;
@@ -901,7 +901,7 @@ SDL_JoystickUpdate(void)
*/
SDL_SYS_JoystickDetect();
- SDL_UnlockJoystickList();
+ SDL_UnlockJoysticks();
}
int
diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h
index 5339b83..85d3920 100644
--- a/src/joystick/SDL_joystick_c.h
+++ b/src/joystick/SDL_joystick_c.h
@@ -33,10 +33,6 @@ extern void SDL_GameControllerQuitMappings(void);
extern int SDL_GameControllerInit(void);
extern void SDL_GameControllerQuit(void);
-/* Locking for multi-threaded access to the joystick API */
-extern void SDL_LockJoystickList(void);
-extern void SDL_UnlockJoystickList(void);
-
/* Function to extract information from an SDL joystick GUID */
extern void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version);