From 981d05ae8d3ad1f1f8d957c2b5f96a32ebc32912 Mon Sep 17 00:00:00 2001 From: David Reid Date: Thu, 13 Jan 2022 18:28:40 +1000 Subject: [PATCH] Fix a deadlock when starting a device fails. Public issue https://github.com/mackron/miniaudio/issues/399 --- CHANGES.md | 3 ++- miniaudio.h | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index cff21a1e..c1840788 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ v0.11.5 - TBD ============= -* Support has been added for automatic stream routing with the AAudio backend. +* Support has been added for automatic stream routing with the AAudio backend. +* A bug has been fixed that results in a deadlock when starting a device. --------------------------------------------------------------------------------------------------- diff --git a/miniaudio.h b/miniaudio.h index 35bce10f..91412cce 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -39062,13 +39062,18 @@ static ma_thread_result MA_THREADCALL ma_worker_thread(void* pData) startResult = MA_SUCCESS; } + /* + If starting was not successful we'll need to loop back to the start and wait for something + to happen (pDevice->wakeupEvent). + */ if (startResult != MA_SUCCESS) { pDevice->workResult = startResult; - continue; /* Failed to start. Loop back to the start and wait for something to happen (pDevice->wakeupEvent). */ + ma_event_signal(&pDevice->startEvent); /* <-- Always signal the start event so ma_device_start() can return as it'll be waiting on it. */ + continue; } /* Make sure the state is set appropriately. */ - ma_device__set_state(pDevice, ma_device_state_started); + ma_device__set_state(pDevice, ma_device_state_started); /* <-- Set this before signaling the event so that the state is always guaranteed to be good after ma_device_start() has returned. */ ma_event_signal(&pDevice->startEvent); ma_device__on_notification_started(pDevice);