Make ma_context/device_get_backend_state() atomic.

It is technically possible for a backend to create a thread which
calls `ma_context/device_get_backend_state()` before miniaudio has
set the internal pointer.
This commit is contained in:
David Reid
2025-07-21 12:43:13 +10:00
parent 93167d108e
commit bd75bcb592
+9 -6
View File
@@ -7600,7 +7600,7 @@ struct ma_context_config
struct ma_context
{
ma_device_backend_vtable* pVTable; /* New new system. */
void* pBackendState; /* Backend state created by the backend. This will be passed to the relevant backend functions. */
MA_ATOMIC(MA_SIZEOF_PTR, void*) pBackendState; /* Backend state created by the backend. This will be passed to the relevant backend functions. */
ma_log* pLog;
ma_log log; /* Only used if the log is owned by the context. The pLog member will be set to &log in this case. */
ma_thread_priority threadPriority;
@@ -7649,7 +7649,7 @@ struct ma_device
ma_device_data_proc onData; /* Set once at initialization time and should not be changed after. */
ma_device_notification_proc onNotification; /* Set once at initialization time and should not be changed after. */
void* pUserData; /* Application defined data. */
void* pBackendState; /* Backend state created by the backend. This will be passed to the relevant backend functions. */
MA_ATOMIC(MA_SIZEOF_PTR, void*) pBackendState; /* Backend state created by the backend. This will be passed to the relevant backend functions. */
ma_mutex startStopLock;
ma_event wakeupEvent;
ma_event startEvent;
@@ -44034,7 +44034,7 @@ MA_API ma_result ma_context_init(const ma_device_backend_config* pBackends, ma_u
ma_log_postf(ma_context_get_log(pContext), MA_LOG_LEVEL_DEBUG, "Successfully initialized %s backend.", backendInfo.pName);
pContext->pVTable = backend.pVTable;
pContext->pBackendState = pContextState;
ma_atomic_store_explicit_ptr((volatile void**)&pContext->pBackendState, pContextState, ma_atomic_memory_order_relaxed);
result = ma_mutex_init(&pContext->deviceEnumLock);
if (result != MA_SUCCESS) {
@@ -44122,7 +44122,7 @@ MA_API void* ma_context_get_backend_state(ma_context* pContext)
return NULL;
}
return pContext->pBackendState;
return ma_atomic_load_explicit_ptr((volatile void**)&pContext->pBackendState, ma_atomic_memory_order_relaxed);
}
@@ -44414,6 +44414,7 @@ MA_API ma_result ma_device_init(ma_context* pContext, const ma_device_config* pC
ma_result result;
ma_device_descriptor descriptorPlayback;
ma_device_descriptor descriptorCapture;
void* pBackendState;
/* The context can be null, in which case we self-manage it. */
if (pContext == NULL) {
@@ -44568,7 +44569,7 @@ MA_API ma_result ma_device_init(ma_context* pContext, const ma_device_config* pC
}
result = pContext->pVTable->onDeviceInit(pDevice, ma_device_config_find_backend_config(pConfig, pContext->pVTable), &descriptorPlayback, &descriptorCapture, &pDevice->pBackendState);
result = pContext->pVTable->onDeviceInit(pDevice, ma_device_config_find_backend_config(pConfig, pContext->pVTable), &descriptorPlayback, &descriptorCapture, &pBackendState);
if (result != MA_SUCCESS) {
ma_event_uninit(&pDevice->startEvent);
ma_event_uninit(&pDevice->wakeupEvent);
@@ -44576,6 +44577,8 @@ MA_API ma_result ma_device_init(ma_context* pContext, const ma_device_config* pC
return result;
}
ma_atomic_store_explicit_ptr((volatile void**)&pDevice->pBackendState, pBackendState, ma_atomic_memory_order_relaxed);
result = ma_device_post_init(pDevice, pConfig->deviceType, &descriptorPlayback, &descriptorCapture);
if (result != MA_SUCCESS) {
@@ -44930,7 +44933,7 @@ MA_API void* ma_device_get_backend_state(ma_device* pDevice)
return NULL;
}
return pDevice->pBackendState;
return ma_atomic_load_explicit_ptr((volatile void**)&pDevice->pBackendState, ma_atomic_memory_order_relaxed);
}