1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
#include "config.h"
#include <algorithm>
#include "AL/alc.h"
#include "AL/al.h"
#include "AL/alext.h"
#include "alMain.h"
#include "alcontext.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;
}
|