Commit d6282845 authored by David Reid's avatar David Reid

Fix a thread-safety error with `ma_device_start/stop()`.

Public issue https://github.com/mackron/miniaudio/issues/919.
parent 059a25d9
...@@ -42520,6 +42520,14 @@ MA_API ma_result ma_device_start(ma_device* pDevice) ...@@ -42520,6 +42520,14 @@ MA_API ma_result ma_device_start(ma_device* pDevice)
ma_mutex_lock(&pDevice->startStopLock); ma_mutex_lock(&pDevice->startStopLock);
{ {
/*
We need to check again if the device is in a started state because it's possible for one thread to have started the device
while another was waiting on the mutex.
*/
if (ma_device_get_state(pDevice) == ma_device_state_started) {
return MA_SUCCESS; /* Already started. */
}
/* Starting and stopping are wrapped in a mutex which means we can assert that the device is in a stopped or paused state. */ /* Starting and stopping are wrapped in a mutex which means we can assert that the device is in a stopped or paused state. */
MA_ASSERT(ma_device_get_state(pDevice) == ma_device_state_stopped); MA_ASSERT(ma_device_get_state(pDevice) == ma_device_state_stopped);
...@@ -42580,6 +42588,14 @@ MA_API ma_result ma_device_stop(ma_device* pDevice) ...@@ -42580,6 +42588,14 @@ MA_API ma_result ma_device_stop(ma_device* pDevice)
ma_mutex_lock(&pDevice->startStopLock); ma_mutex_lock(&pDevice->startStopLock);
{ {
/*
We need to check again if the device is in a stopped state because it's possible for one thread to have stopped the device
while another was waiting on the mutex.
*/
if (ma_device_get_state(pDevice) == ma_device_state_stopped) {
return MA_SUCCESS; /* Already stopped. */
}
/* Starting and stopping are wrapped in a mutex which means we can assert that the device is in a started or paused state. */ /* Starting and stopping are wrapped in a mutex which means we can assert that the device is in a started or paused state. */
MA_ASSERT(ma_device_get_state(pDevice) == ma_device_state_started); MA_ASSERT(ma_device_get_state(pDevice) == ma_device_state_started);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment