diff options
-rw-r--r-- | Alc/alc.cpp | 9 | ||||
-rw-r--r-- | Alc/alcontext.h | 4 | ||||
-rw-r--r-- | OpenAL32/Include/alMain.h | 3 | ||||
-rw-r--r-- | OpenAL32/event.cpp | 27 |
4 files changed, 31 insertions, 12 deletions
diff --git a/Alc/alc.cpp b/Alc/alc.cpp index 86150794..5de72b4c 100644 --- a/Alc/alc.cpp +++ b/Alc/alc.cpp @@ -2581,8 +2581,7 @@ static ALvoid InitContext(ALCcontext *Context) 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"); + StartEventThrd(Context); } @@ -2696,7 +2695,6 @@ ALCcontext_struct::~ALCcontext_struct() */ static bool ReleaseContext(ALCcontext *context, ALCdevice *device) { - static const AsyncEvent kill_evt = ASYNC_EVENT(EventType_KillThread); ALCcontext *origctx, *newhead; bool ret = true; @@ -2734,10 +2732,7 @@ static bool ReleaseContext(ALCcontext *context, ALCdevice *device) * this, although waiting for a non-odd mix count would work too. */ - while(ll_ringbuffer_write(context->AsyncEvents, (const char*)&kill_evt, 1) == 0) - althrd_yield(); - alsem_post(&context->EventSem); - althrd_join(context->EventThread, nullptr); + StopEventThrd(context); ALCcontext_DecRef(context); return ret; diff --git a/Alc/alcontext.h b/Alc/alcontext.h index 6a7ac376..5eec2e96 100644 --- a/Alc/alcontext.h +++ b/Alc/alcontext.h @@ -1,6 +1,8 @@ #ifndef ALCONTEXT_H #define ALCONTEXT_H +#include <thread> + #include "AL/al.h" #include "AL/alc.h" #include "AL/alext.h" @@ -95,7 +97,7 @@ struct ALCcontext_struct { ATOMIC(ALeffectslotArray*) ActiveAuxSlots{nullptr}; - althrd_t EventThread; + std::thread EventThread; alsem_t EventSem; ll_ringbuffer *AsyncEvents{nullptr}; ATOMIC(ALbitfieldSOFT) EnabledEvts{0u}; diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h index e31d0146..a81d3f3d 100644 --- a/OpenAL32/Include/alMain.h +++ b/OpenAL32/Include/alMain.h @@ -803,7 +803,8 @@ inline void LockFilterList(ALCdevice *device) { almtx_lock(&device->FilterLock); inline void UnlockFilterList(ALCdevice *device) { almtx_unlock(&device->FilterLock); } -int EventThread(void *arg); +void StartEventThrd(ALCcontext *ctx); +void StopEventThrd(ALCcontext *ctx); char *alstrdup(const char *str); diff --git a/OpenAL32/event.cpp b/OpenAL32/event.cpp index 67a10645..e4b5eee0 100644 --- a/OpenAL32/event.cpp +++ b/OpenAL32/event.cpp @@ -15,10 +15,8 @@ #include "threads.h" -int EventThread(void *arg) +static int EventThread(ALCcontext *context) { - auto context = static_cast<ALCcontext*>(arg); - bool quitnow{false}; while(LIKELY(!quitnow)) { @@ -50,6 +48,29 @@ int EventThread(void *arg) return 0; } +void StartEventThrd(ALCcontext *ctx) +{ + try { + ctx->EventThread = std::thread(EventThread, ctx); + } + catch(std::exception& e) { + ERR("Failed to start event thread: %s\n", e.what()); + } + catch(...) { + ERR("Failed to start event thread! Expect problems.\n"); + } +} + +void StopEventThrd(ALCcontext *ctx) +{ + static constexpr AsyncEvent kill_evt = ASYNC_EVENT(EventType_KillThread); + while(ll_ringbuffer_write(ctx->AsyncEvents, (const char*)&kill_evt, 1) == 0) + althrd_yield(); + alsem_post(&ctx->EventSem); + if(ctx->EventThread.joinable()) + ctx->EventThread.join(); +} + AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, ALboolean enable) { ContextRef context{GetContextRef()}; |