From c331b64d796d2abf2fd03ec72f733642483d3623 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 20 Nov 2023 00:07:15 -0500
Subject: [PATCH] audio: Tighten up possession of detectionLock during device
open.
We don't need it for most of the time, and holding it during the backend's
OpenDevice upsets ThreadSanitizer vs our PulseAudio code.
Fixes #7427.
---
src/audio/SDL_audio.c | 51 +++++++++++++++++++------------------------
1 file changed, 23 insertions(+), 28 deletions(-)
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index b256c7b3ed36..939d151ad020 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -1292,25 +1292,10 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
return 0;
}
- SDL_LockMutex(current_audio.detectionLock);
- /* Find an available device ID... */
- for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
- if (open_devices[id] == NULL) {
- break;
- }
- }
-
- if (id == SDL_arraysize(open_devices)) {
- SDL_SetError("Too many open audio devices");
- SDL_UnlockMutex(current_audio.detectionLock);
- return 0;
- }
-
if (!obtained) {
obtained = &_obtained;
}
if (!prepare_audiospec(desired, obtained)) {
- SDL_UnlockMutex(current_audio.detectionLock);
return 0;
}
@@ -1332,11 +1317,11 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
if ((iscapture) && (current_audio.impl.OnlyHasDefaultCaptureDevice)) {
if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
SDL_SetError("No such device");
- SDL_UnlockMutex(current_audio.detectionLock);
return 0;
}
devname = NULL;
+ SDL_LockMutex(current_audio.detectionLock);
for (i = 0; i < SDL_arraysize(open_devices); i++) {
if ((open_devices[i]) && (open_devices[i]->iscapture)) {
SDL_SetError("Audio device already open");
@@ -1344,14 +1329,15 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
return 0;
}
}
+ SDL_UnlockMutex(current_audio.detectionLock);
} else if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
- SDL_UnlockMutex(current_audio.detectionLock);
SDL_SetError("No such device");
return 0;
}
devname = NULL;
+ SDL_LockMutex(current_audio.detectionLock);
for (i = 0; i < SDL_arraysize(open_devices); i++) {
if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
SDL_UnlockMutex(current_audio.detectionLock);
@@ -1359,6 +1345,7 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
return 0;
}
}
+ SDL_UnlockMutex(current_audio.detectionLock);
} else if (devname != NULL) {
/* if the app specifies an exact string, we can pass the backend
an actual device handle thingey, which saves them the effort of
@@ -1367,19 +1354,20 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
It might still need to open a device based on the string for,
say, a network audio server, but this optimizes some cases. */
SDL_AudioDeviceItem *item;
+ SDL_LockMutex(current_audio.detectionLock);
for (item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; item; item = item->next) {
if ((item->handle != NULL) && (SDL_strcmp(item->name, devname) == 0)) {
handle = item->handle;
break;
}
}
+ SDL_UnlockMutex(current_audio.detectionLock);
}
if (!current_audio.impl.AllowsArbitraryDeviceNames) {
/* has to be in our device list, or the default device. */
if ((handle == NULL) && (devname != NULL)) {
SDL_SetError("No such device.");
- SDL_UnlockMutex(current_audio.detectionLock);
return 0;
}
}
@@ -1387,10 +1375,8 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
device = (SDL_AudioDevice *)SDL_calloc(1, sizeof(SDL_AudioDevice));
if (device == NULL) {
SDL_OutOfMemory();
- SDL_UnlockMutex(current_audio.detectionLock);
return 0;
}
- device->id = id + 1;
device->spec = *obtained;
device->iscapture = iscapture ? SDL_TRUE : SDL_FALSE;
device->handle = handle;
@@ -1404,7 +1390,6 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
device->mixer_lock = SDL_CreateMutex();
if (device->mixer_lock == NULL) {
close_audio_device(device);
- SDL_UnlockMutex(current_audio.detectionLock);
SDL_SetError("Couldn't create mixer lock");
return 0;
}
@@ -1419,7 +1404,6 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
if (current_audio.impl.OpenDevice(device, devname) < 0) {
close_audio_device(device);
- SDL_UnlockMutex(current_audio.detectionLock);
return 0;
}
@@ -1475,7 +1459,6 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
if (!device->stream) {
close_audio_device(device);
- SDL_UnlockMutex(current_audio.detectionLock);
return 0;
}
}
@@ -1485,7 +1468,6 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
device->buffer_queue = SDL_NewDataQueue(SDL_AUDIOBUFFERQUEUE_PACKETLEN, obtained->size * 2);
if (!device->buffer_queue) {
close_audio_device(device);
- SDL_UnlockMutex(current_audio.detectionLock);
SDL_SetError("Couldn't create audio buffer queue");
return 0;
}
@@ -1503,12 +1485,28 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
device->work_buffer = (Uint8 *)SDL_malloc(device->work_buffer_len);
if (device->work_buffer == NULL) {
close_audio_device(device);
- SDL_UnlockMutex(current_audio.detectionLock);
SDL_OutOfMemory();
return 0;
}
+ /* Find an available device ID... */
+ SDL_LockMutex(current_audio.detectionLock);
+ for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
+ if (open_devices[id] == NULL) {
+ break;
+ }
+ }
+
+ if (id == SDL_arraysize(open_devices)) {
+ close_audio_device(device);
+ SDL_SetError("Too many open audio devices");
+ SDL_UnlockMutex(current_audio.detectionLock);
+ return 0;
+ }
+
+ device->id = id + 1;
open_devices[id] = device; /* add it to our list of open devices. */
+ SDL_UnlockMutex(current_audio.detectionLock);
/* Start the audio thread if necessary */
if (!current_audio.impl.ProvidesOwnCallbackThread) {
@@ -1521,7 +1519,6 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
if (!startup_data.startup_semaphore) {
close_audio_device(device);
SDL_SetError("Couldn't create audio thread startup semaphore");
- SDL_UnlockMutex(current_audio.detectionLock);
return 0;
}
@@ -1532,14 +1529,12 @@ static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture,
SDL_DestroySemaphore(startup_data.startup_semaphore);
close_audio_device(device);
SDL_SetError("Couldn't create audio thread");
- SDL_UnlockMutex(current_audio.detectionLock);
return 0;
}
SDL_SemWait(startup_data.startup_semaphore);
SDL_DestroySemaphore(startup_data.startup_semaphore);
}
- SDL_UnlockMutex(current_audio.detectionLock);
return device->id;
}