SDL: coreaudio: Possibly fixed another shutdown race condition. (b0a21)

From b0a2164b2c9c9d8b40ab4e9202d438d961002ff4 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 30 Sep 2022 14:53:07 -0400
Subject: [PATCH] coreaudio: Possibly fixed another shutdown race condition.

Reference Issue #6159.
(cherry picked from commit e6640ef2d46a1baa34b4d893df661d17f0070b76)
---
 src/audio/coreaudio/SDL_coreaudio.m | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m
index de8f7210619..feca5b5aa0c 100644
--- a/src/audio/coreaudio/SDL_coreaudio.m
+++ b/src/audio/coreaudio/SDL_coreaudio.m
@@ -701,6 +701,14 @@ static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrec
     }
 #endif
 
+    /* if callback fires again, feed silence; don't call into the app. */
+    SDL_AtomicSet(&this->paused, 1);
+
+    if (this->hidden->thread) {
+        SDL_assert(SDL_AtomicGet(&this->shutdown) != 0);  /* should have been set by SDL_audio.c */
+        SDL_WaitThread(this->hidden->thread, NULL);
+    }
+
     if (iscapture) {
         open_capture_devices--;
     } else {
@@ -725,18 +733,10 @@ static BOOL update_audio_session(_THIS, SDL_bool open, SDL_bool allow_playandrec
         open_devices = NULL;
     }
 
-    /* if callback fires again, feed silence; don't call into the app. */
-    SDL_AtomicSet(&this->paused, 1);
-
     if (this->hidden->audioQueue) {
         AudioQueueDispose(this->hidden->audioQueue, 1);
     }
 
-    if (this->hidden->thread) {
-        SDL_assert(SDL_AtomicGet(&this->shutdown) != 0);  /* should have been set by SDL_audio.c */
-        SDL_WaitThread(this->hidden->thread, NULL);
-    }
-
     if (this->hidden->ready_semaphore) {
         SDL_DestroySemaphore(this->hidden->ready_semaphore);
     }