Added SDL_TryLockJoysticks()

This commit is contained in:
Sam Lantinga
2026-01-29 16:15:37 -08:00
parent 5f0c889082
commit e3e8adcb76
5 changed files with 27 additions and 0 deletions

View File

@@ -188,6 +188,21 @@ typedef enum SDL_JoystickConnectionState
*/
extern SDL_DECLSPEC void SDLCALL SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock);
/**
* Locking for atomic access to the joystick API.
*
* The SDL joystick functions are thread-safe, however you can lock the
* joysticks while processing to guarantee that the joystick list won't change
* and joystick and gamepad events will not be delivered.
*
* \returns true if the joysticks were successfully locked, false otherwise.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.2.0.
*/
extern SDL_DECLSPEC bool SDLCALL SDL_TryLockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock);
/**
* Unlocking for atomic access to the joystick API.
*

View File

@@ -1271,6 +1271,7 @@ SDL3_0.0.0 {
SDL_LoadSurface_IO;
SDL_LoadSurface;
SDL_SetWindowFillDocument;
SDL_TryLockJoysticks;
# extra symbols go here (don't modify this line)
local: *;
};

View File

@@ -1297,3 +1297,4 @@
#define SDL_LoadSurface_IO SDL_LoadSurface_IO_REAL
#define SDL_LoadSurface SDL_LoadSurface_REAL
#define SDL_SetWindowFillDocument SDL_SetWindowFillDocument_REAL
#define SDL_TryLockJoysticks SDL_TryLockJoysticks_REAL

View File

@@ -1305,3 +1305,4 @@ SDL_DYNAPI_PROC(SDL_Surface*,SDL_RotateSurface,(SDL_Surface *a,float b),(a,b),re
SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadSurface_IO,(SDL_IOStream *a,bool b),(a,b),return)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadSurface,(const char *a),(a),return)
SDL_DYNAPI_PROC(bool,SDL_SetWindowFillDocument,(SDL_Window *a,bool b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_TryLockJoysticks,(void),(),return)

View File

@@ -653,6 +653,15 @@ void SDL_LockJoysticks(void)
++SDL_joysticks_locked;
}
bool SDL_TryLockJoysticks(void)
{
if (SDL_TryLockMutex(SDL_joystick_lock)) {
++SDL_joysticks_locked;
return true;
}
return false;
}
void SDL_UnlockJoysticks(void)
{
bool last_unlock = false;