SDL: audio: Fixed race condition in subsystem shutdown.

From 0dc0434a3ed5d4e7dd77095889faa67fa2e3673e Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Wed, 20 Sep 2023 10:00:44 -0400
Subject: [PATCH] audio: Fixed race condition in subsystem shutdown.

This makes sure new devices can't be created when we're in the process of
shutting down.
---
 src/audio/SDL_audio.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index fdf5eece0b99..05048e7537df 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -246,7 +246,10 @@ static SDL_AudioDevice *CreatePhysicalAudioDevice(const char *name, SDL_bool isc
 {
     SDL_assert(name != NULL);
 
-    if (SDL_AtomicGet(&current_audio.shutting_down)) {
+    SDL_LockRWLockForReading(current_audio.device_list_lock);
+    const int shutting_down = SDL_AtomicGet(&current_audio.shutting_down);
+    SDL_UnlockRWLock(current_audio.device_list_lock);
+    if (shutting_down) {
         return NULL;  // we're shutting down, don't add any devices that are hotplugged at the last possible moment.
     }