diff options
author | Chris Robinson <[email protected]> | 2018-11-16 05:57:14 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-16 06:00:28 -0800 |
commit | fedd5ebbea24883dc4767caf675778e2ca630d95 (patch) | |
tree | 284f3a5504c87ae76e3e4ffc45c1c6a27594f6a2 | |
parent | 5cdd28c7365ae534c4ea381c135bfbeaa5dca6b7 (diff) |
Convert event.c to C++
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | OpenAL32/event.c | 127 | ||||
-rw-r--r-- | OpenAL32/event.cpp | 119 |
3 files changed, 120 insertions, 128 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d5066a69..0db47791 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -793,7 +793,7 @@ SET(OPENAL_OBJS OpenAL32/Include/alSource.h OpenAL32/alSource.cpp OpenAL32/alState.c - OpenAL32/event.c + OpenAL32/event.cpp OpenAL32/Include/sample_cvt.h OpenAL32/sample_cvt.cpp ) diff --git a/OpenAL32/event.c b/OpenAL32/event.c deleted file mode 100644 index 4c9c0be2..00000000 --- a/OpenAL32/event.c +++ /dev/null @@ -1,127 +0,0 @@ - -#include "config.h" - -#include "AL/alc.h" -#include "AL/al.h" -#include "AL/alext.h" -#include "alMain.h" -#include "alError.h" -#include "alAuxEffectSlot.h" -#include "ringbuffer.h" - - -int EventThread(void *arg) -{ - ALCcontext *context = arg; - bool quitnow = false; - - while(!quitnow) - { - ALbitfieldSOFT enabledevts; - AsyncEvent evt; - - if(ll_ringbuffer_read(context->AsyncEvents, (char*)&evt, 1) == 0) - { - alsem_wait(&context->EventSem); - continue; - } - - almtx_lock(&context->EventCbLock); - do { - quitnow = evt.EnumType == EventType_KillThread; - if(quitnow) break; - - if(evt.EnumType == EventType_ReleaseEffectState) - { - ALeffectState_DecRef(evt.u.EffectState); - continue; - } - - enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_acquire); - if(context->EventCb && (enabledevts&evt.EnumType) == evt.EnumType) - context->EventCb(evt.u.user.type, evt.u.user.id, evt.u.user.param, - (ALsizei)strlen(evt.u.user.msg), evt.u.user.msg, context->EventParam - ); - } while(ll_ringbuffer_read(context->AsyncEvents, (char*)&evt, 1) != 0); - almtx_unlock(&context->EventCbLock); - } - return 0; -} - -AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, ALboolean enable) -{ - ALCcontext *context; - ALbitfieldSOFT enabledevts; - ALbitfieldSOFT flags = 0; - ALsizei i; - - context = GetContextRef(); - if(!context) return; - - if(count < 0) SETERR_GOTO(context, AL_INVALID_VALUE, done, "Controlling %d events", count); - if(count == 0) goto done; - if(!types) SETERR_GOTO(context, AL_INVALID_VALUE, done, "NULL pointer"); - - for(i = 0;i < count;i++) - { - if(types[i] == AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT) - flags |= EventType_BufferCompleted; - else if(types[i] == AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT) - flags |= EventType_SourceStateChange; - else if(types[i] == AL_EVENT_TYPE_ERROR_SOFT) - flags |= EventType_Error; - else if(types[i] == AL_EVENT_TYPE_PERFORMANCE_SOFT) - flags |= EventType_Performance; - else if(types[i] == AL_EVENT_TYPE_DEPRECATED_SOFT) - flags |= EventType_Deprecated; - else if(types[i] == AL_EVENT_TYPE_DISCONNECTED_SOFT) - flags |= EventType_Disconnected; - else - SETERR_GOTO(context, AL_INVALID_ENUM, done, "Invalid event type 0x%04x", types[i]); - } - - if(enable) - { - 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 - { - 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) - { - } - /* Wait to ensure the event handler sees the changed flags before - * returning. - */ - almtx_lock(&context->EventCbLock); - almtx_unlock(&context->EventCbLock); - } - -done: - ALCcontext_DecRef(context); -} - -AL_API void AL_APIENTRY alEventCallbackSOFT(ALEVENTPROCSOFT callback, void *userParam) -{ - ALCcontext *context; - - context = GetContextRef(); - if(!context) return; - - almtx_lock(&context->PropLock); - almtx_lock(&context->EventCbLock); - context->EventCb = callback; - context->EventParam = userParam; - almtx_unlock(&context->EventCbLock); - almtx_unlock(&context->PropLock); - - ALCcontext_DecRef(context); -} diff --git a/OpenAL32/event.cpp b/OpenAL32/event.cpp new file mode 100644 index 00000000..bc3e3178 --- /dev/null +++ b/OpenAL32/event.cpp @@ -0,0 +1,119 @@ + +#include "config.h" + +#include <algorithm> + +#include "AL/alc.h" +#include "AL/al.h" +#include "AL/alext.h" +#include "alMain.h" +#include "alError.h" +#include "alAuxEffectSlot.h" +#include "ringbuffer.h" +#include "threads.h" + + +int EventThread(void *arg) +{ + auto context = static_cast<ALCcontext*>(arg); + + bool quitnow{false}; + while(LIKELY(!quitnow)) + { + AsyncEvent evt; + if(ll_ringbuffer_read(context->AsyncEvents, (char*)&evt, 1) == 0) + { + alsem_wait(&context->EventSem); + continue; + } + + std::lock_guard<almtx_t> _{context->EventCbLock}; + do { + quitnow = evt.EnumType == EventType_KillThread; + if(UNLIKELY(quitnow)) break; + + if(evt.EnumType == EventType_ReleaseEffectState) + { + ALeffectState_DecRef(evt.u.EffectState); + continue; + } + + ALbitfieldSOFT enabledevts{ATOMIC_LOAD(&context->EnabledEvts, almemory_order_acquire)}; + if(context->EventCb && (enabledevts&evt.EnumType) == evt.EnumType) + context->EventCb(evt.u.user.type, evt.u.user.id, evt.u.user.param, + (ALsizei)strlen(evt.u.user.msg), evt.u.user.msg, context->EventParam + ); + } while(ll_ringbuffer_read(context->AsyncEvents, (char*)&evt, 1) != 0); + } + return 0; +} + +AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, ALboolean enable) +{ + ContextRef context{GetContextRef()}; + if(UNLIKELY(!context)) return; + + if(count < 0) SETERR_RETURN(context.get(), AL_INVALID_VALUE,, "Controlling %d events", count); + if(count == 0) return; + if(!types) SETERR_RETURN(context.get(), AL_INVALID_VALUE,, "NULL pointer"); + + ALbitfieldSOFT flags{0}; + const ALenum *types_end = types+count; + auto bad_type = std::find_if_not(types, types_end, + [&flags](ALenum type) noexcept -> bool + { + if(type == AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT) + flags |= EventType_BufferCompleted; + else if(type == AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT) + flags |= EventType_SourceStateChange; + else if(type == AL_EVENT_TYPE_ERROR_SOFT) + flags |= EventType_Error; + else if(type == AL_EVENT_TYPE_PERFORMANCE_SOFT) + flags |= EventType_Performance; + else if(type == AL_EVENT_TYPE_DEPRECATED_SOFT) + flags |= EventType_Deprecated; + else if(type == AL_EVENT_TYPE_DISCONNECTED_SOFT) + flags |= EventType_Disconnected; + else + return false; + return true; + } + ); + if(bad_type != types_end) + SETERR_RETURN(context.get(), AL_INVALID_ENUM,, "Invalid event type 0x%04x", *bad_type); + + if(enable) + { + 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 + { + 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) + { + } + /* Wait to ensure the event handler sees the changed flags before + * returning. + */ + std::lock_guard<almtx_t>{context->EventCbLock}; + } +} + +AL_API void AL_APIENTRY alEventCallbackSOFT(ALEVENTPROCSOFT callback, void *userParam) +{ + ContextRef context{GetContextRef()}; + if(UNLIKELY(!context)) return; + + std::lock_guard<almtx_t> _{context->PropLock}; + std::lock_guard<almtx_t> __{context->EventCbLock}; + context->EventCb = callback; + context->EventParam = userParam; +} |