Include fmt 11.0.2
[openal-soft.git] / al / event.cpp
blob7ceb949c6c221a3a1aa3cd5d8ad41b72e5abec2c
2 #include "config.h"
4 #include "event.h"
6 #include <atomic>
7 #include <bitset>
8 #include <exception>
9 #include <memory>
10 #include <mutex>
11 #include <new>
12 #include <optional>
13 #include <string>
14 #include <thread>
15 #include <tuple>
16 #include <variant>
18 #include "AL/al.h"
19 #include "AL/alc.h"
20 #include "AL/alext.h"
22 #include "alc/context.h"
23 #include "alsem.h"
24 #include "alspan.h"
25 #include "alstring.h"
26 #include "core/async_event.h"
27 #include "core/context.h"
28 #include "core/effects/base.h"
29 #include "core/logging.h"
30 #include "debug.h"
31 #include "direct_defs.h"
32 #include "error.h"
33 #include "intrusive_ptr.h"
34 #include "opthelpers.h"
35 #include "ringbuffer.h"
38 namespace {
40 template<typename... Ts>
41 struct overloaded : Ts... { using Ts::operator()...; };
43 template<typename... Ts>
44 overloaded(Ts...) -> overloaded<Ts...>;
46 int EventThread(ALCcontext *context)
48 RingBuffer *ring{context->mAsyncEvents.get()};
49 bool quitnow{false};
50 while(!quitnow)
52 auto evt_data = ring->getReadVector()[0];
53 if(evt_data.len == 0)
55 context->mEventSem.wait();
56 continue;
59 auto eventlock = std::lock_guard{context->mEventCbLock};
60 const auto enabledevts = context->mEnabledEvts.load(std::memory_order_acquire);
61 auto evt_span = al::span{std::launder(reinterpret_cast<AsyncEvent*>(evt_data.buf)),
62 evt_data.len};
63 for(auto &event : evt_span)
65 quitnow = std::holds_alternative<AsyncKillThread>(event);
66 if(quitnow) UNLIKELY break;
68 auto proc_killthread = [](AsyncKillThread&) { };
69 auto proc_release = [](AsyncEffectReleaseEvent &evt)
71 al::intrusive_ptr<EffectState>{evt.mEffectState};
73 auto proc_srcstate = [context,enabledevts](AsyncSourceStateEvent &evt)
75 if(!context->mEventCb
76 || !enabledevts.test(al::to_underlying(AsyncEnableBits::SourceState)))
77 return;
79 ALuint state{};
80 std::string msg{"Source ID " + std::to_string(evt.mId)};
81 msg += " state has changed to ";
82 switch(evt.mState)
84 case AsyncSrcState::Reset:
85 msg += "AL_INITIAL";
86 state = AL_INITIAL;
87 break;
88 case AsyncSrcState::Stop:
89 msg += "AL_STOPPED";
90 state = AL_STOPPED;
91 break;
92 case AsyncSrcState::Play:
93 msg += "AL_PLAYING";
94 state = AL_PLAYING;
95 break;
96 case AsyncSrcState::Pause:
97 msg += "AL_PAUSED";
98 state = AL_PAUSED;
99 break;
101 context->mEventCb(AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT, evt.mId, state,
102 al::sizei(msg), msg.c_str(), context->mEventParam);
104 auto proc_buffercomp = [context,enabledevts](AsyncBufferCompleteEvent &evt)
106 if(!context->mEventCb
107 || !enabledevts.test(al::to_underlying(AsyncEnableBits::BufferCompleted)))
108 return;
110 std::string msg{std::to_string(evt.mCount)};
111 if(evt.mCount == 1) msg += " buffer completed";
112 else msg += " buffers completed";
113 context->mEventCb(AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT, evt.mId, evt.mCount,
114 al::sizei(msg), msg.c_str(), context->mEventParam);
116 auto proc_disconnect = [context,enabledevts](AsyncDisconnectEvent &evt)
118 if(!context->mEventCb
119 || !enabledevts.test(al::to_underlying(AsyncEnableBits::Disconnected)))
120 return;
122 context->mEventCb(AL_EVENT_TYPE_DISCONNECTED_SOFT, 0, 0, al::sizei(evt.msg),
123 evt.msg.c_str(), context->mEventParam);
126 std::visit(overloaded{proc_srcstate, proc_buffercomp, proc_release, proc_disconnect,
127 proc_killthread}, event);
129 std::destroy(evt_span.begin(), evt_span.end());
130 ring->readAdvance(evt_span.size());
132 return 0;
135 constexpr std::optional<AsyncEnableBits> GetEventType(ALenum etype) noexcept
137 switch(etype)
139 case AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT: return AsyncEnableBits::BufferCompleted;
140 case AL_EVENT_TYPE_DISCONNECTED_SOFT: return AsyncEnableBits::Disconnected;
141 case AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT: return AsyncEnableBits::SourceState;
143 return std::nullopt;
146 } // namespace
149 void StartEventThrd(ALCcontext *ctx)
151 try {
152 ctx->mEventThread = std::thread{EventThread, ctx};
154 catch(std::exception& e) {
155 ERR("Failed to start event thread: %s\n", e.what());
157 catch(...) {
158 ERR("Failed to start event thread! Expect problems.\n");
162 void StopEventThrd(ALCcontext *ctx)
164 RingBuffer *ring{ctx->mAsyncEvents.get()};
165 auto evt_data = ring->getWriteVector()[0];
166 if(evt_data.len == 0)
168 do {
169 std::this_thread::yield();
170 evt_data = ring->getWriteVector()[0];
171 } while(evt_data.len == 0);
173 std::ignore = InitAsyncEvent<AsyncKillThread>(evt_data.buf);
174 ring->writeAdvance(1);
176 ctx->mEventSem.post();
177 if(ctx->mEventThread.joinable())
178 ctx->mEventThread.join();
181 AL_API DECL_FUNCEXT3(void, alEventControl,SOFT, ALsizei,count, const ALenum*,types, ALboolean,enable)
182 FORCE_ALIGN void AL_APIENTRY alEventControlDirectSOFT(ALCcontext *context, ALsizei count,
183 const ALenum *types, ALboolean enable) noexcept
184 try {
185 if(count < 0)
186 throw al::context_error{AL_INVALID_VALUE, "Controlling %d events", count};
187 if(count <= 0) UNLIKELY return;
189 if(!types)
190 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
192 ContextBase::AsyncEventBitset flags{};
193 for(ALenum evttype : al::span{types, static_cast<uint>(count)})
195 auto etype = GetEventType(evttype);
196 if(!etype)
197 throw al::context_error{AL_INVALID_ENUM, "Invalid event type 0x%04x", evttype};
198 flags.set(al::to_underlying(*etype));
201 if(enable)
203 auto enabledevts = context->mEnabledEvts.load(std::memory_order_relaxed);
204 while(context->mEnabledEvts.compare_exchange_weak(enabledevts, enabledevts|flags,
205 std::memory_order_acq_rel, std::memory_order_acquire) == 0)
207 /* enabledevts is (re-)filled with the current value on failure, so
208 * just try again.
212 else
214 auto enabledevts = context->mEnabledEvts.load(std::memory_order_relaxed);
215 while(context->mEnabledEvts.compare_exchange_weak(enabledevts, enabledevts&~flags,
216 std::memory_order_acq_rel, std::memory_order_acquire) == 0)
219 /* Wait to ensure the event handler sees the changed flags before
220 * returning.
222 std::lock_guard<std::mutex> eventlock{context->mEventCbLock};
225 catch(al::context_error& e) {
226 context->setError(e.errorCode(), "%s", e.what());
229 AL_API DECL_FUNCEXT2(void, alEventCallback,SOFT, ALEVENTPROCSOFT,callback, void*,userParam)
230 FORCE_ALIGN void AL_APIENTRY alEventCallbackDirectSOFT(ALCcontext *context,
231 ALEVENTPROCSOFT callback, void *userParam) noexcept
233 std::lock_guard<std::mutex> eventlock{context->mEventCbLock};
234 context->mEventCb = callback;
235 context->mEventParam = userParam;