From c2710ffe87dae7d292e77ed9663783e9d5c227dc Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 28 Jan 2018 16:58:41 -0800 Subject: [PATCH] Make EnabledEvts atomic --- Alc/ALc.c | 2 +- OpenAL32/Include/alMain.h | 2 +- OpenAL32/alError.c | 6 ++++-- OpenAL32/alState.c | 6 ++++-- OpenAL32/event.c | 19 +++++++++++++++++-- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Alc/ALc.c b/Alc/ALc.c index ea387c6d..21cabf5f 100644 --- a/Alc/ALc.c +++ b/Alc/ALc.c @@ -2554,7 +2554,7 @@ static ALvoid InitContext(ALCcontext *Context) ATOMIC_FLAG_TEST_AND_SET(&Context->PropsClean, almemory_order_relaxed); ATOMIC_INIT(&Context->DeferUpdates, AL_FALSE); almtx_init(&Context->EventLock, almtx_plain); - Context->EnabledEvts = 0; + ATOMIC_INIT(&Context->EnabledEvts, 0); Context->EventCb = NULL; Context->EventParam = NULL; diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h index 5b31696e..ef5ad0e9 100644 --- a/OpenAL32/Include/alMain.h +++ b/OpenAL32/Include/alMain.h @@ -665,7 +665,7 @@ struct ALCcontext_struct { ATOMIC(struct ALeffectslotArray*) ActiveAuxSlots; almtx_t EventLock; - ALbitfieldSOFT EnabledEvts; + ATOMIC(ALbitfieldSOFT) EnabledEvts; ALEVENTPROCSOFT EventCb; void *EventParam; diff --git a/OpenAL32/alError.c b/OpenAL32/alError.c index e3909742..22aed458 100644 --- a/OpenAL32/alError.c +++ b/OpenAL32/alError.c @@ -72,10 +72,12 @@ void alSetError(ALCcontext *context, ALenum errorCode, const char *msg, ...) } ATOMIC_COMPARE_EXCHANGE_STRONG_SEQ(&context->LastError, &curerr, errorCode); - if((context->EnabledEvts&EventType_Error)) + if((ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed)&EventType_Error)) { + ALbitfieldSOFT enabledevts; almtx_lock(&context->EventLock); - if((context->EnabledEvts&EventType_Error) && context->EventCb) + enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed); + if((enabledevts&EventType_Error) && context->EventCb) (*context->EventCb)(AL_EVENT_TYPE_ERROR_SOFT, 0, errorCode, msglen, msg, context->EventParam); almtx_unlock(&context->EventLock); diff --git a/OpenAL32/alState.c b/OpenAL32/alState.c index d4f3c6cf..10fce3b5 100644 --- a/OpenAL32/alState.c +++ b/OpenAL32/alState.c @@ -716,13 +716,15 @@ AL_API ALvoid AL_APIENTRY alDopplerVelocity(ALfloat value) context = GetContextRef(); if(!context) return; - if((context->EnabledEvts&EventType_Deprecated)) + if((ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed)&EventType_Deprecated)) { static const ALCchar msg[] = "alDopplerVelocity is deprecated in AL1.1, use alSpeedOfSound"; const ALsizei msglen = (ALsizei)strlen(msg); + ALbitfieldSOFT enabledevts; almtx_lock(&context->EventLock); - if((context->EnabledEvts&EventType_Deprecated) && context->EventCb) + enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed); + if((enabledevts&EventType_Deprecated) && context->EventCb) (*context->EventCb)(AL_EVENT_TYPE_DEPRECATED_SOFT, 0, 0, msglen, msg, context->EventParam); almtx_unlock(&context->EventLock); diff --git a/OpenAL32/event.c b/OpenAL32/event.c index 3b70c9f3..93d68d7a 100644 --- a/OpenAL32/event.c +++ b/OpenAL32/event.c @@ -39,9 +39,24 @@ AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, A almtx_lock(&context->EventLock); if(enable) - context->EnabledEvts |= flags; + { + ALbitfieldSOFT enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed); + while(ATOMIC_COMPARE_EXCHANGE_WEAK(&context->EnabledEvts, &enabledevts, enabledevts|flags, + almemory_order_acq_rel, almemory_order_acquire) == 0) + { + /* enabledevts is (re-)filled with the current value on failure, so + * just try again. + */ + } + } else - context->EnabledEvts &= ~flags; + { + ALbitfieldSOFT enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed); + while(ATOMIC_COMPARE_EXCHANGE_WEAK(&context->EnabledEvts, &enabledevts, enabledevts&~flags, + almemory_order_acq_rel, almemory_order_acquire) == 0) + { + } + } almtx_unlock(&context->EventLock); done: -- 2.11.4.GIT