diff options
author | Chris Robinson <[email protected]> | 2018-09-20 19:58:01 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-09-20 19:58:01 -0700 |
commit | cb8545346d227ba85a4a98c3897b5cdb1ea86d26 (patch) | |
tree | 8e014b38ec4815636ef2d4c70b6313eb2631d0a5 | |
parent | 5c6b8eda4f4defc85faf76edb2772f6b340c7c1a (diff) |
Always start the event thread with the context
-rw-r--r-- | Alc/ALc.c | 20 | ||||
-rw-r--r-- | OpenAL32/Include/alMain.h | 3 | ||||
-rw-r--r-- | OpenAL32/event.c | 53 |
3 files changed, 31 insertions, 45 deletions
@@ -2665,6 +2665,11 @@ static ALvoid InitContext(ALCcontext *Context) listener->Params.MetersPerUnit; listener->Params.SourceDistanceModel = Context->SourceDistanceModel; listener->Params.DistanceModel = Context->DistanceModel; + + + Context->AsyncEvents = ll_ringbuffer_create(63, sizeof(AsyncEvent), false); + if(althrd_create(&Context->EventThread, EventThread, Context) != althrd_success) + ERR("Failed to start event thread! Expect problems.\n"); } @@ -2675,6 +2680,7 @@ static ALvoid InitContext(ALCcontext *Context) */ static void FreeContext(ALCcontext *context) { + static const AsyncEvent kill_evt = { 0 }; ALlistener *listener = context->Listener; struct ALeffectslotArray *auxslots; struct ALeffectslotProps *eprops; @@ -2686,6 +2692,11 @@ static void FreeContext(ALCcontext *context) TRACE("%p\n", context); + while(ll_ringbuffer_write(context->AsyncEvents, (const char*)&kill_evt, 1) == 0) + althrd_yield(); + alsem_post(&context->EventSem); + althrd_join(context->EventThread, NULL); + if((cprops=ATOMIC_LOAD(&context->Update, almemory_order_acquire)) != NULL) { TRACE("Freed unapplied context update %p\n", cprops); @@ -2773,15 +2784,6 @@ static void FreeContext(ALCcontext *context) } TRACE("Freed "SZFMT" listener property object%s\n", count, (count==1)?"":"s"); - if(ATOMIC_EXCHANGE(&context->EnabledEvts, 0, almemory_order_acq_rel)) - { - static const AsyncEvent kill_evt = { 0 }; - while(ll_ringbuffer_write(context->AsyncEvents, (const char*)&kill_evt, 1) == 0) - althrd_yield(); - alsem_post(&context->EventSem); - althrd_join(context->EventThread, NULL); - } - almtx_destroy(&context->EventCbLock); almtx_destroy(&context->EventThrdLock); alsem_destroy(&context->EventSem); diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h index f90108d6..f1bc4469 100644 --- a/OpenAL32/Include/alMain.h +++ b/OpenAL32/Include/alMain.h @@ -892,6 +892,9 @@ inline void UnlockEffectSlotList(ALCcontext *context) { almtx_unlock(&context->EffectSlotLock); } +int EventThread(void *arg); + + vector_al_string SearchDataFiles(const char *match, const char *subdir); #ifdef __cplusplus diff --git a/OpenAL32/event.c b/OpenAL32/event.c index 12636489..b719c371 100644 --- a/OpenAL32/event.c +++ b/OpenAL32/event.c @@ -9,16 +9,12 @@ #include "ringbuffer.h" -static int EventThread(void *arg) +int EventThread(void *arg) { ALCcontext *context = arg; + bool quitnow = false; - /* Clear all pending posts on the semaphore. */ - while(alsem_trywait(&context->EventSem) == althrd_success) - { - } - - while(1) + while(!quitnow) { ALbitfieldSOFT enabledevts; AsyncEvent evt; @@ -28,14 +24,17 @@ static int EventThread(void *arg) alsem_wait(&context->EventSem); continue; } - if(!evt.EnumType) - break; almtx_lock(&context->EventCbLock); - enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_acquire); - if(context->EventCb && (enabledevts&evt.EnumType) == evt.EnumType) - context->EventCb(evt.Type, evt.ObjectId, evt.Param, (ALsizei)strlen(evt.Message), - evt.Message, context->EventParam); + do { + quitnow = !evt.EnumType; + if(quitnow) break; + + enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_acquire); + if(context->EventCb && (enabledevts&evt.EnumType) == evt.EnumType) + context->EventCb(evt.Type, evt.ObjectId, evt.Param, (ALsizei)strlen(evt.Message), + evt.Message, context->EventParam); + } while(ll_ringbuffer_read(context->AsyncEvents, (char*)&evt, 1) != 0); almtx_unlock(&context->EventCbLock); } return 0; @@ -46,7 +45,6 @@ AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, A ALCcontext *context; ALbitfieldSOFT enabledevts; ALbitfieldSOFT flags = 0; - bool isrunning; ALsizei i; context = GetContextRef(); @@ -77,10 +75,7 @@ AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, A almtx_lock(&context->EventThrdLock); if(enable) { - if(!context->AsyncEvents) - context->AsyncEvents = ll_ringbuffer_create(63, sizeof(AsyncEvent), false); enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed); - isrunning = !!enabledevts; while(ATOMIC_COMPARE_EXCHANGE_WEAK(&context->EnabledEvts, &enabledevts, enabledevts|flags, almemory_order_acq_rel, almemory_order_acquire) == 0) { @@ -88,33 +83,19 @@ AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, A * just try again. */ } - if(!isrunning && flags) - althrd_create(&context->EventThread, EventThread, context); } else { enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed); - isrunning = !!enabledevts; while(ATOMIC_COMPARE_EXCHANGE_WEAK(&context->EnabledEvts, &enabledevts, enabledevts&~flags, almemory_order_acq_rel, almemory_order_acquire) == 0) { } - if(isrunning && !(enabledevts&~flags)) - { - static const AsyncEvent kill_evt = { 0 }; - while(ll_ringbuffer_write(context->AsyncEvents, (const char*)&kill_evt, 1) == 0) - althrd_yield(); - alsem_post(&context->EventSem); - althrd_join(context->EventThread, NULL); - } - else - { - /* Wait to ensure the event handler sees the changed flags before - * returning. - */ - almtx_lock(&context->EventCbLock); - almtx_unlock(&context->EventCbLock); - } + /* Wait to ensure the event handler sees the changed flags before + * returning. + */ + almtx_lock(&context->EventCbLock); + almtx_unlock(&context->EventCbLock); } almtx_unlock(&context->EventThrdLock); |