# SDL: Split the joystick lock and the event lock

**URL:** <https://discourse.libsdl.org/t/sdl-split-the-joystick-lock-and-the-event-lock/69731>\
**Category:** SDL Commits\
**Created:** [November 2, 2026, 9:42pm UTC](https://discourse.libsdl.org/t/sdl-split-the-joystick-lock-and-the-event-lock/69731 "2026-11-02T21:42:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [November 2, 2026, 9:42pm UTC](https://discourse.libsdl.org/t/sdl-split-the-joystick-lock-and-the-event-lock/69731/1 "2026-11-02T21:42:33Z")

</div>

> <https://github.com/libsdl-org/SDL/commit/f96e02d6de16d5a2afaf504c02d34961390ae429>
>
> This prevents joystick activity on a separate thread from blocking input process…ing on the main thread.
> 
> One side effect of this is that joystick events need to be dispatched while the joystick lock isn't being held, to prevent deadlocks if an event watcher calls a joystick function. To handle this, joystick events for the current update are sent all at once at the end of the joystick update while the joystick lock is no longer held.
> 
> The joystick and sensor subsystems call into each other, so they share the same lock and sensor events are queued and dispatched similarly.

```diff
From f96e02d6de16d5a2afaf504c02d34961390ae429 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 2 Nov 2026 11:23:47 -0800
Subject: [PATCH] Split the joystick lock and the event lock

This prevents joystick activity on a separate thread from blocking input processing on the main thread.

One side effect of this is that joystick events need to be dispatched while the joystick lock isn't being held, to prevent deadlocks if an event watcher calls a joystick function. To handle this, joystick events for the current update are sent all at once at the end of the joystick update while the joystick lock is no longer held.

The joystick and sensor subsystems call into each other, so they share the same lock and sensor events are queued and dispatched similarly.
---
 include/SDL3/SDL_joystick.h | 8 +-
 src/events/SDL_events.c | 8 +
 src/events/SDL_events_c.h | 7 +-
 src/joystick/SDL_gamepad.c | 125 ++++---
 src/joystick/SDL_gamepad_c.h | 2 +
 src/joystick/SDL_joystick.c | 402 ++++++++++++++---------
 src/joystick/SDL_joystick_c.h | 15 +-
 src/joystick/SDL_steam_virtual_gamepad.c | 10 +-
 src/joystick/SDL_sysjoystick.h | 2 +-
 src/joystick/android/SDL_sysjoystick.c | 6 +-
 src/joystick/hidapi/SDL_hidapijoystick.c | 4 +-
 src/joystick/linux/SDL_sysjoystick.c | 6 +-
 src/sensor/SDL_sensor.c | 118 ++++++-
 src/sensor/SDL_sensor_c.h | 11 +-
 src/sensor/SDL_syssensor.h | 2 +-
 src/sensor/windows/SDL_windowssensor.c | 2 +
 16 files changed, 471 insertions(+), 257 deletions(-)

diff --git a/include/SDL3/SDL_joystick.h b/include/SDL3/SDL_joystick.h
index 75e166ff42f6f..a1ab60ff8914e 100644
--- a/include/SDL3/SDL_joystick.h
+++ b/include/SDL3/SDL_joystick.h
@@ -81,7 +81,7 @@ extern "C" {
  * help Clang's thread safety analysis tools to function. Do not attempt
  * to access this symbol from your app, it will not work!
  */
-extern SDL_Mutex *SDL_event_lock;
+extern SDL_Mutex *SDL_joystick_lock;
 #endif
 
 /**
@@ -186,7 +186,7 @@ typedef enum SDL_JoystickConnectionState
  *
  * \since This function is available since SDL 3.2.0.
  */
-extern SDL_DECLSPEC void SDLCALL SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_event_lock);
+extern SDL_DECLSPEC void SDLCALL SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_joystick_lock);
 
 /**
  * Locking for atomic access to the joystick API.
@@ -201,7 +201,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_LockJoysticks(void) SDL_ACQUIRE(SDL_event_l
  *
  * \since This function is available since SDL 3.6.0.
  */
-extern SDL_DECLSPEC bool SDLCALL SDL_TryLockJoysticks(void) SDL_TRY_ACQUIRE(true, SDL_event_lock);
+extern SDL_DECLSPEC bool SDLCALL SDL_TryLockJoysticks(void) SDL_TRY_ACQUIRE(true, SDL_joystick_lock);
 
 /**
  * Unlocking for atomic access to the joystick API.
@@ -211,7 +211,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_TryLockJoysticks(void) SDL_TRY_ACQUIRE(true
  *
  * \since This function is available since SDL 3.2.0.
  */
-extern SDL_DECLSPEC void SDLCALL SDL_UnlockJoysticks(void) SDL_RELEASE(SDL_event_lock);
+extern SDL_DECLSPEC void SDLCALL SDL_UnlockJoysticks(void) SDL_RELEASE(SDL_joystick_lock);
 
 /**
  * Return whether a joystick is currently connected.
diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index 3e78495e69a72..74e219604aa05 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -162,12 +162,16 @@ static struct
 
 
 SDL_Mutex *SDL_event_lock = NULL; // This needs to support recursive locks
+SDL_Mutex *SDL_joystick_lock = NULL; // This needs to support recursive locks
 
 void SDL_CreateEventLock(void)
 {
     if (!SDL_event_lock) {
         SDL_event_lock = SDL_CreateMutex();
     }
+ if (!SDL_joystick_lock) {
+ SDL_joystick_lock = SDL_CreateMutex();
+ }
 }
 
 void SDL_DestroyEventLock(void)
@@ -176,6 +180,10 @@ void SDL_DestroyEventLock(void)
         SDL_DestroyMutex(SDL_event_lock);
         SDL_event_lock = NULL;
     }
+ if (SDL_joystick_lock) {
+ SDL_DestroyMutex(SDL_joystick_lock);
+ SDL_joystick_lock = NULL;
+ }
 }
 
 static void SDL_CleanupTemporaryMemory(void *data)
diff --git a/src/events/SDL_events_c.h b/src/events/SDL_events_c.h
index cfb467b9ed8ef..4ac36d34f04fe 100644
--- a/src/events/SDL_events_c.h
+++ b/src/events/SDL_events_c.h
@@ -39,12 +39,15 @@
 // The event mutex
 //
 // This mutex prevents multiple threads from watching multiple events
-// simultaneously and also protects resources like joysticks that may
-// be accessed from multiple threads and also generate events.
+// simultaneously and also protects resources that may be accessed from
+// multiple threads and also generate events.
 extern SDL_Mutex *SDL_event_lock;
 extern void SDL_CreateEventLock(void);
 extern void SDL_DestroyEventLock(void);
 
+// This mutex prevents multiple threads from simultaneously accessing joysticks
+extern SDL_Mutex *SDL_joystick_lock;
+
 // Start and stop the event processing loop
 extern bool SDL_StartEventLoop(void);
 extern void SDL_StopEventLoop(void);
diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c
index a2de0959169b4..f7d70ccbb23ce 100644
--- a/src/joystick/SDL_gamepad.c
+++ b/src/joystick/SDL_gamepad.c
@@ -75,8 +75,8 @@
     } while (0)
 
 static bool SDL_gamepads_initialized;
-static SDL_Gamepad *SDL_gamepads SDL_GUARDED_BY(SDL_event_lock) = NULL;
-static SDL_HashTable *SDL_gamepad_names SDL_GUARDED_BY(SDL_event_lock) = NULL;
+static SDL_Gamepad *SDL_gamepads SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+static SDL_HashTable *SDL_gamepad_names SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
 
 // The face button style of a gamepad
 typedef enum
@@ -96,7 +96,7 @@ typedef enum
     SDL_GAMEPAD_MAPPING_PRIORITY_USER,
 } SDL_GamepadMappingPriority;
 
-#define _guarded SDL_GUARDED_BY(SDL_event_lock)
+#define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
 
 typedef struct GamepadMapping_t
 {
@@ -121,13 +121,13 @@ typedef struct
 #undef _guarded
 
 static SDL_GUID s_zeroGUID;
-static GamepadMapping_t *s_pSupportedGamepads SDL_GUARDED_BY(SDL_event_lock) = NULL;
-static GamepadMapping_t *s_pDefaultMapping SDL_GUARDED_BY(SDL_event_lock) = NULL;
-static GamepadMapping_t *s_pXInputMapping SDL_GUARDED_BY(SDL_event_lock) = NULL;
-static MappingChangeTracker *s_mappingChangeTracker SDL_GUARDED_BY(SDL_event_lock) = NULL;
-static SDL_HashTable *s_gamepadInstanceIDs SDL_GUARDED_BY(SDL_event_lock) = NULL;
+static GamepadMapping_t *s_pSupportedGamepads SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+static GamepadMapping_t *s_pDefaultMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+static GamepadMapping_t *s_pXInputMapping SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+static MappingChangeTracker *s_mappingChangeTracker SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
+static SDL_HashTable *s_gamepadInstanceIDs SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
 
-#define _guarded SDL_GUARDED_BY(SDL_event_lock)
+#define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
 
 // The SDL gamepad structure
 struct SDL_Gamepad
@@ -421,23 +421,22 @@ static const char *SDL_UpdateGamepadNameForID(SDL_JoystickID instance_id)
 
 void SDL_PrivateGamepadAdded(SDL_JoystickID instance_id)
 {
- SDL_Event event;
-
     if (!SDL_gamepads_initialized || SDL_IsJoystickBeingAdded()) {
         return;
     }
 
     SDL_UpdateGamepadNameForID(instance_id);
 
- event.type = SDL_EVENT_GAMEPAD_ADDED;
- event.common.timestamp = 0;
- event.gdevice.which = instance_id;
- SDL_PushEvent(&event);
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_GAMEPAD_ADDED, 0, &event);
+ if (entry) {
+ event->gdevice.which = instance_id;
+ SDL_QueueJoystickEvent(entry);
+ }
 }
 
 void SDL_PrivateGamepadRemoved(SDL_JoystickID instance_id)
 {
- SDL_Event event;
     SDL_Gamepad *gamepad;
 
     SDL_AssertJoysticksLocked();
@@ -453,38 +452,42 @@ void SDL_PrivateGamepadRemoved(SDL_JoystickID instance_id)
         }
     }
 
- event.type = SDL_EVENT_GAMEPAD_REMOVED;
- event.common.timestamp = 0;
- event.gdevice.which = instance_id;
- SDL_PushEvent(&event);
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_GAMEPAD_REMOVED, 0, &event);
+ if (entry) {
+ event->gdevice.which = instance_id;
+ SDL_QueueJoystickEvent(entry);
+ }
 }
 
 static void SDL_PrivateGamepadRemapped(SDL_JoystickID instance_id)
 {
- SDL_Event event;
-
     if (!SDL_gamepads_initialized || SDL_IsJoystickBeingAdded()) {
         return;
     }
 
- event.type = SDL_EVENT_GAMEPAD_REMAPPED;
- event.common.timestamp = 0;
- event.gdevice.which = instance_id;
- SDL_PushEvent(&event);
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_GAMEPAD_REMAPPED, 0, &event);
+ if (entry) {
+ event->gdevice.which = instance_id;
+ SDL_QueueJoystickEvent(entry);
+ }
 }
 
 /*
  * Event filter to fire gamepad events from joystick ones
+ *
+ * If the events filtered here change, update SDL_QueueJoystickEvent() to match
  */
-static bool SDLCALL SDL_GamepadEventWatcher(void *userdata, SDL_Event *event)
+void SDL_GamepadEventWatcher(SDL_Event *event)
 {
     SDL_Gamepad *gamepad;
 
+ SDL_AssertJoysticksLocked();
+
     switch (event->type) {
     case SDL_EVENT_JOYSTICK_AXIS_MOTION:
     {
- SDL_AssertJoysticksLocked();
-
         for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
             if (gamepad->joystick->instance_id == event->jaxis.which) {
                 HandleJoystickAxis(event->common.timestamp, gamepad, event->jaxis.axis, event->jaxis.value);
@@ -495,8 +498,6 @@ static bool SDLCALL SDL_GamepadEventWatcher(void *userdata, SDL_Event *event)
     case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
     case SDL_EVENT_JOYSTICK_BUTTON_UP:
     {
- SDL_AssertJoysticksLocked();
-
         for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
             if (gamepad->joystick->instance_id == event->jbutton.which) {
                 HandleJoystickButton(event->common.timestamp, gamepad, event->jbutton.button, event->jbutton.down);
@@ -506,8 +507,6 @@ static bool SDLCALL SDL_GamepadEventWatcher(void *userdata, SDL_Event *event)
     } break;
     case SDL_EVENT_JOYSTICK_HAT_MOTION:
     {
- SDL_AssertJoysticksLocked();
-
         for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
             if (gamepad->joystick->instance_id == event->jhat.which) {
                 HandleJoystickHat(event->common.timestamp, gamepad, event->jhat.hat, event->jhat.value);
@@ -517,17 +516,15 @@ static bool SDLCALL SDL_GamepadEventWatcher(void *userdata, SDL_Event *event)
     } break;
     case SDL_EVENT_JOYSTICK_UPDATE_COMPLETE:
     {
- SDL_AssertJoysticksLocked();
-
         if (SDL_EventEnabled(SDL_EVENT_GAMEPAD_UPDATE_COMPLETE)) {
             for (gamepad = SDL_gamepads; gamepad; gamepad = gamepad->next) {
                 if (gamepad->joystick->instance_id == event->jdevice.which) {
- SDL_Event deviceevent;
-
- deviceevent.type = SDL_EVENT_GAMEPAD_UPDATE_COMPLETE;
- deviceevent.common.timestamp = event->jdevice.timestamp;
- deviceevent.gdevice.which = event->jdevice.which;
- SDL_PushEvent(&deviceevent);
+ SDL_Event *deviceevent;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_GAMEPAD_UPDATE_COMPLETE, event->jdevice.timestamp, &deviceevent);
+ if (entry) {
+ deviceevent->gdevice.which = event->jdevice.which;
+ SDL_QueueJoystickEvent(entry);
+ }
                     break;
                 }
             }
@@ -536,8 +533,6 @@ static bool SDLCALL SDL_GamepadEventWatcher(void *userdata, SDL_Event *event)
     default:
         break;
     }
-
- return true;
 }
 
 /* SDL defines sensor orientation relative to the device natural
@@ -3014,9 +3009,6 @@ bool SDL_InitGamepads(void)
 
     SDL_gamepad_names = SDL_CreateHashTable(0, false, SDL_HashID, SDL_KeyMatchID, SDL_DestroyHashValue, NULL);
 
- // Watch for joystick events and fire gamepad ones if needed
- SDL_AddEventWatch(SDL_GamepadEventWatcher, NULL);
-
     // Send added events for gamepads currently attached
     joysticks = SDL_GetJoysticks(NULL);
     if (joysticks) {
@@ -4226,8 +4218,6 @@ void SDL_QuitGamepads(void)
 
     SDL_gamepads_initialized = false;
 
- SDL_RemoveEventWatch(SDL_GamepadEventWatcher, NULL);
-
     while (SDL_gamepads) {
         SDL_gamepads->ref_count = 1;
         SDL_CloseGamepad(SDL_gamepads);
@@ -4273,32 +4263,25 @@ static void SDL_SendGamepadAxis(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_Game
 
     // translate the event, if desired
     if (SDL_EventEnabled(SDL_EVENT_GAMEPAD_AXIS_MOTION)) {
- SDL_Event event;
- event.type = SDL_EVENT_GAMEPAD_AXIS_MOTION;
- event.common.timestamp = timestamp;
- event.gaxis.which = gamepad->joystick->instance_id;
- event.gaxis.axis = axis;
- event.gaxis.value = value;
- SDL_PushEvent(&event);
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_GAMEPAD_AXIS_MOTION, timestamp, &event);
+ if (entry) {
+ event->gaxis.which = gamepad->joystick->instance_id;
+ event->gaxis.axis = axis;
+ event->gaxis.value = value;
+ SDL_QueueJoystickEvent(entry);
+ }
     }
 }
 
 static void SDL_SendGamepadButton(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadButton button, bool down)
 {
- SDL_Event event;
-
     SDL_AssertJoysticksLocked();
 
     if (button == SDL_GAMEPAD_BUTTON_INVALID) {
         return;
     }
 
- if (down) {
- event.type = SDL_EVENT_GAMEPAD_BUTTON_DOWN;
- } else {
- event.type = SDL_EVENT_GAMEPAD_BUTTON_UP;
- }
-
     if (button == SDL_GAMEPAD_BUTTON_GUIDE) {
         Uint64 now = SDL_GetTicks();
         if (down) {
@@ -4318,12 +4301,16 @@ static void SDL_SendGamepadButton(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_Ga
     }
 
     // translate the event, if desired
- if (SDL_EventEnabled(event.type)) {
- event.common.timestamp = timestamp;
- event.gbutton.which = gamepad->joystick->instance_id;
- event.gbutton.button = button;
- event.gbutton.down = down;
- SDL_PushEvent(&event);
+ SDL_EventType event_type = down ? SDL_EVENT_GAMEPAD_BUTTON_DOWN : SDL_EVENT_GAMEPAD_BUTTON_UP;
+ if (SDL_EventEnabled(event_type)) {
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(event_type, timestamp, &event);
+ if (entry) {
+ event->gbutton.which = gamepad->joystick->instance_id;
+ event->gbutton.button = button;
+ event->gbutton.down = down;
+ SDL_QueueJoystickEvent(entry);
+ }
     }
 }
 
diff --git a/src/joystick/SDL_gamepad_c.h b/src/joystick/SDL_gamepad_c.h
index ff6574386ca05..c52e85932b74e 100644
--- a/src/joystick/SDL_gamepad_c.h
+++ b/src/joystick/SDL_gamepad_c.h
@@ -35,6 +35,8 @@ extern void SDL_QuitGamepads(void);
 extern void SDL_PrivateGamepadAdded(SDL_JoystickID instance_id);
 extern void SDL_PrivateGamepadRemoved(SDL_JoystickID instance_id);
 
+extern void SDL_GamepadEventWatcher(SDL_Event *event);
+
 // Function to return whether a joystick name and GUID is a gamepad
 extern bool SDL_IsGamepadNameAndGUID(const char *name, SDL_GUID guid);
 
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 78e8036d2ac36..5606cb67b8d4e 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -112,18 +112,22 @@ static bool SDL_joystick_drivers_initialized[SDL_arraysize(SDL_joystick_drivers)
 
 static int SDL_joysticks_locked;
 static bool SDL_joysticks_initialized;
+static bool SDL_joysticks_opened;
 static bool SDL_joysticks_quitting;
 static bool SDL_joystick_being_added;
-static bool SDL_joystick_hid_notifications SDL_GUARDED_BY(SDL_event_lock);
-static Uint32 SDL_joystick_hid_change_count SDL_GUARDED_BY(SDL_event_lock);
-static Uint64 SDL_last_device_update SDL_GUARDED_BY(SDL_event_lock);
-static SDL_JoystickDevice *SDL_joystick_devices SDL_GUARDED_BY(SDL_event_lock);
-static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_event_lock);
-static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_event_lock);
-static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_event_lock);
-static SDL_HashTable *SDL_joystick_names SDL_GUARDED_BY(SDL_event_lock);
-static Uint64 SDL_joystick_device_change_time SDL_GUARDED_BY(SDL_event_lock);
-static int SDL_joystick_device_update_step SDL_GUARDED_BY(SDL_event_lock);
+static bool SDL_joystick_hid_notifications SDL_GUARDED_BY(SDL_joystick_lock);
+static Uint32 SDL_joystick_hid_change_count SDL_GUARDED_BY(SDL_joystick_lock);
+static Uint64 SDL_last_device_update SDL_GUARDED_BY(SDL_joystick_lock);
+static SDL_JoystickDevice *SDL_joystick_devices SDL_GUARDED_BY(SDL_joystick_lock);
+static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_joystick_lock);
+static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_joystick_lock);
+static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_joystick_lock);
+static SDL_HashTable *SDL_joystick_names SDL_GUARDED_BY(SDL_joystick_lock);
+static SDL_JoystickEventEntry *SDL_queued_joystick_events_head SDL_GUARDED_BY(SDL_joystick_lock);
+static SDL_JoystickEventEntry *SDL_queued_joystick_events_tail SDL_GUARDED_BY(SDL_joystick_lock);
+static SDL_JoystickEventEntry *SDL_free_joystick_events SDL_GUARDED_BY(SDL_joystick_lock);
+static Uint64 SDL_joystick_device_change_time SDL_GUARDED_BY(SDL_joystick_lock);
+static int SDL_joystick_device_update_step SDL_GUARDED_BY(SDL_joystick_lock);
 static SDL_AtomicInt SDL_joystick_devices_changed;
 static SDL_AtomicInt SDL_joystick_devices_update;
 static bool SDL_joystick_allows_background_events;
@@ -710,13 +714,13 @@ bool SDL_JoysticksQuitting(void)
 
 void SDL_LockJoysticks(void)
 {
- SDL_LockMutex(SDL_event_lock);
+ SDL_LockMutex(SDL_joystick_lock);
     ++SDL_joysticks_locked;
 }
 
 bool SDL_TryLockJoysticks(void)
 {
- if (SDL_TryLockMutex(SDL_event_lock)) {
+ if (SDL_TryLockMutex(SDL_joystick_lock)) {
         ++SDL_joysticks_locked;
         return true;
     }
@@ -726,7 +730,7 @@ bool SDL_TryLockJoysticks(void)
 void SDL_UnlockJoysticks(void)
 {
     --SDL_joysticks_locked;
- SDL_UnlockMutex(SDL_event_lock);
+ SDL_UnlockMutex(SDL_joystick_lock);
 }
 
 bool SDL_JoysticksLocked(void)
@@ -747,6 +751,106 @@ static SDL_JoystickDriver *SDL_GetJoystickDriver(int index)
     return NULL;
 }
 
+SDL_JoystickEventEntry *SDL_AllocJoystickEvent(SDL_EventType type, Uint64 timestamp, SDL_Event **event)
+{
+ SDL_AssertJoysticksLocked();
+
+ if (!SDL_EventEnabled(type)) {
+ return NULL;
+ }
+
+ SDL_JoystickEventEntry *entry = SDL_free_joystick_events;
+ if (entry) {
+ SDL_free_joystick_events = entry->next;
+ } else {
+ entry = (SDL_JoystickEventEntry *)SDL_calloc(1, sizeof(*entry));
+ if (!entry) {
+ return NULL;
+ }
+ }
+
+ SDL_zerop(entry);
+ entry->event.common.type = type;
+ if (timestamp) {
+ entry->event.common.timestamp = timestamp;
+ } else {
+ entry->event.common.timestamp = SDL_GetTicksNS();
+ }
+ *event = &entry->event;
+ return entry;
+}
+
+void SDL_QueueJoystickEvent(SDL_JoystickEventEntry *entry)
+{
+ SDL_AssertJoysticksLocked();
+
+ switch (entry->event.type) {
+ case SDL_EVENT_JOYSTICK_AXIS_MOTION:
+ case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
+ case SDL_EVENT_JOYSTICK_BUTTON_UP:
+ case SDL_EVENT_JOYSTICK_HAT_MOTION:
+ case SDL_EVENT_JOYSTICK_UPDATE_COMPLETE:
+ SDL_GamepadEventWatcher(&entry->event);
+ break;
+ }
+
+ if (SDL_queued_joystick_events_head) {
+ SDL_queued_joystick_events_tail->next = entry;
+ } else {
+ SDL_queued_joystick_events_head = entry;
+ }
+ SDL_queued_joystick_events_tail = entry;
+}
+
+static void SDL_DispatchJoystickEvents(void)
+{
+ SDL_JoystickEventEntry *entries, *tail;
+ SDL_LockJoysticks();
+ if (!SDL_queued_joystick_events_head) {
+ SDL_UnlockJoysticks();
+ return;
+ }
+ entries = SDL_queued_joystick_events_head;
+ tail = SDL_queued_joystick_events_tail;
+ SDL_queued_joystick_events_head = NULL;
+ SDL_queued_joystick_events_tail = NULL;
+ SDL_UnlockJoysticks();
+
+ SDL_LockMutex(SDL_event_lock);
+ for (SDL_JoystickEventEntry *entry = entries; entry; entry = entry->next) {
+ SDL_PushEvent(&entry->event);
+ }
+ SDL_UnlockMutex(SDL_event_lock);
+
+ SDL_LockJoysticks();
+ tail->next = SDL_free_joystick_events;
+ SDL_free_joystick_events = entries;
+ SDL_UnlockJoysticks();
+}
+
+static void SDL_DispatchFinalJoystickEvents(SDL_JoystickEventEntry *entries)
+{
+ SDL_LockMutex(SDL_event_lock);
+ SDL_JoystickEventEntry *entry = entries;
+ while (entry) {
+ SDL_JoystickEventEntry *next = entry->next;
+ SDL_PushEvent(&entry->event);
+ SDL_free(entry);
+ entry = next;
+ }
+ SDL_UnlockMutex(SDL_event_lock);
+}
+
+static void SDL_FreeJoystickEvents(SDL_JoystickEventEntry *entries)
+{
+ SDL_JoystickEventEntry *entry = entries;
+ while (entry) {
+ SDL_JoystickEventEntry *next = entry->next;
+ SDL_free(entry);
+ entry = next;
+ }
+}
+
 SDL_JoystickDevice *SDL_CreateJoystickDevice(SDL_JoystickDriver *driver, size_t extra_length)
 {
     SDL_JoystickDevice *device = (SDL_JoystickDevice *)SDL_calloc(1, sizeof(*device) + extra_length);
@@ -1334,6 +1438,8 @@ bool SDL_InitJoysticks(void)
 
     SDL_UnlockJoysticks();
 
+ SDL_DispatchJoystickEvents();
+
     if (!result) {
         SDL_QuitJoysticks();
     }
@@ -1343,19 +1449,7 @@ bool SDL_InitJoysticks(void)
 
 bool SDL_JoysticksOpened(void)
 {
- bool opened;
-
- SDL_LockJoysticks();
- {
- if (SDL_joysticks != NULL) {
- opened = true;
- } else {
- opened = false;
- }
- }
- SDL_UnlockJoysticks();
-
- return opened;
+ return SDL_joysticks_opened;
 }
 
 bool SDL_HasJoystick(void)
@@ -1906,6 +2000,8 @@ SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
 
     driver->Update(joystick);
 
+ SDL_joysticks_opened = true;
+
     SDL_UnlockJoysticks();
 
     return joystick;
@@ -2864,6 +2960,10 @@ void SDL_CloseJoystick(SDL_Joystick *joystick)
         SDL_free(joystick->sensors);
         SDL_free(joystick->capsenses);
         SDL_free(joystick);
+
+ if (!SDL_joysticks) {
+ SDL_joysticks_opened = false;
+ }
     }
     SDL_UnlockJoysticks();
 }
@@ -2872,6 +2972,10 @@ void SDL_QuitJoysticks(void)
 {
     int i;
     SDL_JoystickID *joysticks;
+ SDL_JoystickEventEntry *final_events = NULL;
+
+ // Dispatch any pending events while joysticks are still valid
+ SDL_DispatchJoystickEvents();
 
     SDL_LockJoysticks();
 
@@ -2943,7 +3047,21 @@ void SDL_QuitJoysticks(void)
     SDL_joysticks_quitting = false;
     SDL_joysticks_initialized = false;
 
+ if (SDL_queued_joystick_events_head) {
+ final_events = SDL_queued_joystick_events_head;
+ SDL_queued_joystick_events_head = NULL;
+ SDL_queued_joystick_events_tail = NULL;
+ }
+
+ if (SDL_free_joystick_events) {
+ SDL_FreeJoystickEvents(SDL_free_joystick_events);
+ SDL_free_joystick_events = NULL;
+ }
+
     SDL_UnlockJoysticks();
+
+ // Deliver any final joystick removed events
+ SDL_DispatchFinalJoystickEvents(final_events);
 }
 
 static bool SDL_PrivateJoystickShouldIgnoreEvent(void)
@@ -3074,16 +3192,12 @@ void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id)
 
     SDL_UpdateJoystickNameForID(instance_id);
 
- {
- SDL_Event event;
-
- event.type = SDL_EVENT_JOYSTICK_ADDED;
- event.common.timestamp = 0;
-
- if (SDL_EventEnabled(event.type)) {
- event.jdevice.which = instance_id;
- SDL_PushEvent(&event);
- }
+ // Post the event
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_JOYSTICK_ADDED, 0, &event);
+ if (entry) {
+ event->jdevice.which = instance_id;
+ SDL_QueueJoystickEvent(entry);
     }
 
     // This might create an automatic gamepad mapping, so wait to send the event
@@ -3136,7 +3250,6 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID instance_id)
 {
     SDL_Joystick *joystick = NULL;
     int player_index;
- SDL_Event event;
 
     SDL_AssertJoysticksLocked();
 
@@ -3153,12 +3266,12 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID instance_id)
         SDL_PrivateGamepadRemoved(instance_id);
     }
 
- event.type = SDL_EVENT_JOYSTICK_REMOVED;
- event.common.timestamp = 0;
-
- if (SDL_EventEnabled(event.type)) {
- event.jdevice.which = instance_id;
- SDL_PushEvent(&event);
+ // Post the event
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_JOYSTICK_REMOVED, 0, &event);
+ if (entry) {
+ event->jdevice.which = instance_id;
+ SDL_QueueJoystickEvent(entry);
     }
 
     player_index = SDL_GetPlayerIndexForJoystickID(instance_id);
@@ -3219,15 +3332,14 @@ void SDL_SendJoystickAxis(Uint64 timestamp, SDL_Joystick *joystick, Uint8 axis,
     info->value = value;
     joystick->update_complete = timestamp;
 
- // Post the event, if desired
- if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_AXIS_MOTION)) {
- SDL_Event event;
- event.type = SDL_EVENT_JOYSTICK_AXIS_MOTION;
- event.common.timestamp = timestamp;
- event.jaxis.which = joystick->instance_id;
- event.jaxis.axis = axis;
- event.jaxis.value = value;
- SDL_PushEvent(&event);
+ // Post the event
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_JOYSTICK_AXIS_MOTION, timestamp, &event);
+ if (entry) {
+ event->jaxis.which = joystick->instance_id;
+ event->jaxis.axis = axis;
+ event->jaxis.value = value;
+ SDL_QueueJoystickEvent(entry);
     }
 }
 
@@ -3249,16 +3361,15 @@ void SDL_SendJoystickBall(Uint64 timestamp, SDL_Joystick *joystick, Uint8 ball,
     joystick->balls[ball].dx += xrel;
     joystick->balls[ball].dy += yrel;
 
- // Post the event, if desired
- if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_BALL_MOTION)) {
- SDL_Event event;
- event.type = SDL_EVENT_JOYSTICK_BALL_MOTION;
- event.common.timestamp = timestamp;
- event.jball.which = joystick->instance_id;
- event.jball.ball = ball;
- event.jball.xrel = xrel;
- event.jball.yrel = yrel;
- SDL_PushEvent(&event);
+ // Post the event
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_JOYSTICK_BALL_MOTION, timestamp, &event);
+ if (entry) {
+ event->jball.which = joystick->instance_id;
+ event->jball.ball = ball;
+ event->jball.xrel = xrel;
+ event->jball.yrel = yrel;
+ SDL_QueueJoystickEvent(entry);
     }
 }
 
@@ -3288,30 +3399,21 @@ void SDL_SendJoystickHat(Uint64 timestamp, SDL_Joystick *joystick, Uint8 hat, Ui
     joystick->hats[hat] = value;
     joystick->update_complete = timestamp;
 
- // Post the event, if desired
- if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_HAT_MOTION)) {
- SDL_Event event;
- event.type = SDL_EVENT_JOYSTICK_HAT_MOTION;
- event.common.timestamp = timestamp;
- event.jhat.which = joystick->instance_id;
- event.jhat.hat = hat;
- event.jhat.value = value;
- SDL_PushEvent(&event);
+ // Post the event
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_JOYSTICK_HAT_MOTION, timestamp, &event);
+ if (entry) {
+ event->jhat.which = joystick->instance_id;
+ event->jhat.hat = hat;
+ event->jhat.value = value;
+ SDL_QueueJoystickEvent(entry);
     }
 }
 
 void SDL_SendJoystickButton(Uint64 timestamp, SDL_Joystick *joystick, Uint8 button, bool down)
 {
- SDL_Event event;
-
     SDL_AssertJoysticksLocked();
 
- if (down) {
- event.type = SDL_EVENT_JOYSTICK_BUTTON_DOWN;
- } else {
- event.type = SDL_EVENT_JOYSTICK_BUTTON_UP;
- }
-
     if (joystick->swap_face_buttons) {
         switch (button) {
         case 0:
@@ -3352,13 +3454,15 @@ void SDL_SendJoystickButton(Uint64 timestamp, SDL_Joystick *joystick, Uint8 butt
     joystick->buttons[button] = down;
     joystick->update_complete = timestamp;
 
- // Post the event, if desired
- if (SDL_EventEnabled(event.type)) {
- event.common.timestamp = timestamp;
- event.jbutton.which = joystick->instance_id;
- event.jbutton.button = button;
- event.jbutton.down = down;
- SDL_PushEvent(&event);
+ // Post the event
+ SDL_EventType event_type = down ? SDL_EVENT_JOYSTICK_BUTTON_DOWN : SDL_EVENT_JOYSTICK_BUTTON_UP;
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(event_type, timestamp, &event);
+ if (entry) {
+ event->jbutton.which = joystick->instance_id;
+ event->jbutton.button = button;
+ event->jbutton.down = down;
+ SDL_QueueJoystickEvent(entry);
     }
 }
 
@@ -3414,13 +3518,12 @@ static void SendSteamHandleUpdateEvents(void)
             }
         }
         if (changed) {
- SDL_Event event;
-
- SDL_zero(event);
- event.type = SDL_EVENT_GAMEPAD_STEAM_HANDLE_UPDATED;
- event.common.timestamp = 0;
- event.gdevice.which = joystick->instance_id;
- SDL_PushEvent(&event);
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_GAMEPAD_STEAM_HANDLE_UPDATED, 0, &event);
+ if (entry) {
+ event->gdevice.which = joystick->instance_id;
+ SDL_QueueJoystickEvent(entry);
+ }
         }
     }
 }
@@ -3488,13 +3591,12 @@ void SDL_UpdateJoysticks(void)
     if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE)) {
         for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
             if (joystick->update_complete) {
- SDL_Event event;
-
- event.type = SDL_EVENT_JOYSTICK_UPDATE_COMPLETE;
- event.common.timestamp = joystick->update_complete;
- event.jdevice.which = joystick->instance_id;
- SDL_PushEvent(&event);
-
+ SDL_Event *event;
+ SDL_JoystickEventEntry *entry = SDL_AllocJoystickEvent(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE, joystick->update_complete, &event);
+ if (entry) {
+ event->jdevice.which = joystick->instance_id;
+ SDL_QueueJoystickEvent(entry);
+ }
                 joystick->update_complete = 0;
             }
         }
@@ -3503,6 +3605,8 @@ void SDL_UpdateJoysticks(void)
     SDL_UpdateJoystickDevices(false);
 
     SDL_UnlockJoysticks();
+
+ SDL_DispatchJoystickEvents();
 }
 
 static const Uint32 SDL_joystick_event_list[] = {
@@ -4405,19 +4509,23 @@ void SDL_SendJoystickPowerInfo(SDL_Joystick *joystick, SDL_PowerState state, int
 {
     SDL_AssertJoysticksLocked();
 
- if (state != joystick->battery_state || percent != joystick->battery_percent) {
- joystick->battery_state = state;
- joystick->battery_percent = percent;
+ if 

```

(Patch may be truncated, please check the link at the top of this post.)
