SDL: audio: unify device thread naming.

From 8473e522e0cf02bf73096007f679b3582fb079bb Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 3 Jul 2023 20:35:58 -0400
Subject: [PATCH] audio: unify device thread naming.

---
 src/audio/SDL_audio.c             | 9 ++++++++-
 src/audio/SDL_sysaudio.h          | 3 +++
 src/audio/pipewire/SDL_pipewire.c | 2 +-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index a031686abdf7..87c4df866523 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -1191,6 +1191,13 @@ void SDL_UpdatedAudioDeviceFormat(SDL_AudioDevice *device)
     device->buffer_size = device->sample_frames * (SDL_AUDIO_BITSIZE(device->spec.format) / 8) * device->spec.channels;
 }
 
+char *SDL_GetAudioThreadName(SDL_AudioDevice *device, char *buf, size_t buflen)
+{
+    (void)SDL_snprintf(buf, buflen, "SDLAudio%c%d", (device->iscapture) ? 'C' : 'P', (int) device->instance_id);
+    return buf;
+}
+
+
 // this expects the device lock to be held.
 static int OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec *inspec)
 {
@@ -1236,7 +1243,7 @@ static int OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec
     if (!current_audio.impl.ProvidesOwnCallbackThread) {
         const size_t stacksize = 0;  // just take the system default, since audio streams might have callbacks.
         char threadname[64];
-        (void)SDL_snprintf(threadname, sizeof(threadname), "SDLAudio%c%d", (device->iscapture) ? 'C' : 'P', (int) device->instance_id);
+        SDL_GetAudioThreadName(device, threadname, sizeof (threadname));
         device->thread = SDL_CreateThreadInternal(device->iscapture ? CaptureAudioThread : OutputAudioThread, threadname, stacksize, device);
 
         if (device->thread == NULL) {
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index ecf2fcb0ac38..1653ea44bea1 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -91,6 +91,9 @@ extern SDL_AudioDevice *SDL_ObtainPhysicalAudioDeviceByHandle(void *handle);
 /* Backends should call this if they change the device format, channels, freq, or sample_frames to keep other state correct. */
 extern void SDL_UpdatedAudioDeviceFormat(SDL_AudioDevice *device);
 
+// Backends can call this to get a standardized name for a thread to power a specific audio device.
+char *SDL_GetAudioThreadName(SDL_AudioDevice *device, char *buf, size_t buflen);
+
 
 /* These functions are the heart of the audio threads. Backends can call them directly if they aren't using the SDL-provided thread. */
 extern void SDL_OutputAudioThreadSetup(SDL_AudioDevice *device);
diff --git a/src/audio/pipewire/SDL_pipewire.c b/src/audio/pipewire/SDL_pipewire.c
index eac531bf19f2..170b6d746cb5 100644
--- a/src/audio/pipewire/SDL_pipewire.c
+++ b/src/audio/pipewire/SDL_pipewire.c
@@ -1122,7 +1122,7 @@ static int PIPEWIRE_OpenDevice(SDL_AudioDevice *device)
 
     SDL_UpdatedAudioDeviceFormat(device);
 
-    (void)SDL_snprintf(thread_name, sizeof(thread_name), "SDLAudio%c%ld", (iscapture) ? 'C' : 'P', (long)device->handle);
+    SDL_GetAudioThreadName(device, thread_name, sizeof(thread_name));
     priv->loop = PIPEWIRE_pw_thread_loop_new(thread_name, NULL);
     if (priv->loop == NULL) {
         return SDL_SetError("Pipewire: Failed to create stream loop (%i)", errno);