event.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "config.h"
  2. #include "event.h"
  3. #include <algorithm>
  4. #include <atomic>
  5. #include <cstring>
  6. #include <exception>
  7. #include <memory>
  8. #include <mutex>
  9. #include <new>
  10. #include <string>
  11. #include <thread>
  12. #include <utility>
  13. #include "AL/al.h"
  14. #include "AL/alc.h"
  15. #include "albyte.h"
  16. #include "alcontext.h"
  17. #include "alexcpt.h"
  18. #include "almalloc.h"
  19. #include "effects/base.h"
  20. #include "inprogext.h"
  21. #include "logging.h"
  22. #include "opthelpers.h"
  23. #include "ringbuffer.h"
  24. #include "threads.h"
  25. static int EventThread(ALCcontext *context)
  26. {
  27. RingBuffer *ring{context->mAsyncEvents.get()};
  28. bool quitnow{false};
  29. while LIKELY(!quitnow)
  30. {
  31. auto evt_data = ring->getReadVector().first;
  32. if(evt_data.len == 0)
  33. {
  34. context->mEventSem.wait();
  35. continue;
  36. }
  37. std::lock_guard<std::mutex> _{context->mEventCbLock};
  38. do {
  39. auto *evt_ptr = reinterpret_cast<AsyncEvent*>(evt_data.buf);
  40. evt_data.buf += sizeof(AsyncEvent);
  41. evt_data.len -= 1;
  42. AsyncEvent evt{*evt_ptr};
  43. al::destroy_at(evt_ptr);
  44. ring->readAdvance(1);
  45. quitnow = evt.EnumType == EventType_KillThread;
  46. if UNLIKELY(quitnow) break;
  47. if(evt.EnumType == EventType_ReleaseEffectState)
  48. {
  49. evt.u.mEffectState->release();
  50. continue;
  51. }
  52. ALbitfieldSOFT enabledevts{context->mEnabledEvts.load(std::memory_order_acquire)};
  53. if(!context->mEventCb) continue;
  54. if(evt.EnumType == EventType_SourceStateChange)
  55. {
  56. if(!(enabledevts&EventType_SourceStateChange))
  57. continue;
  58. std::string msg{"Source ID " + std::to_string(evt.u.srcstate.id)};
  59. msg += " state has changed to ";
  60. msg += (evt.u.srcstate.state==AL_INITIAL) ? "AL_INITIAL" :
  61. (evt.u.srcstate.state==AL_PLAYING) ? "AL_PLAYING" :
  62. (evt.u.srcstate.state==AL_PAUSED) ? "AL_PAUSED" :
  63. (evt.u.srcstate.state==AL_STOPPED) ? "AL_STOPPED" : "<unknown>";
  64. context->mEventCb(AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT, evt.u.srcstate.id,
  65. static_cast<ALuint>(evt.u.srcstate.state), static_cast<ALsizei>(msg.length()),
  66. msg.c_str(), context->mEventParam);
  67. }
  68. else if(evt.EnumType == EventType_BufferCompleted)
  69. {
  70. if(!(enabledevts&EventType_BufferCompleted))
  71. continue;
  72. std::string msg{std::to_string(evt.u.bufcomp.count)};
  73. if(evt.u.bufcomp.count == 1) msg += " buffer completed";
  74. else msg += " buffers completed";
  75. context->mEventCb(AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT, evt.u.bufcomp.id,
  76. evt.u.bufcomp.count, static_cast<ALsizei>(msg.length()), msg.c_str(),
  77. context->mEventParam);
  78. }
  79. else if((enabledevts&evt.EnumType) == evt.EnumType)
  80. context->mEventCb(evt.u.user.type, evt.u.user.id, evt.u.user.param,
  81. static_cast<ALsizei>(strlen(evt.u.user.msg)), evt.u.user.msg,
  82. context->mEventParam);
  83. } while(evt_data.len != 0);
  84. }
  85. return 0;
  86. }
  87. void StartEventThrd(ALCcontext *ctx)
  88. {
  89. try {
  90. ctx->mEventThread = std::thread{EventThread, ctx};
  91. }
  92. catch(std::exception& e) {
  93. ERR("Failed to start event thread: %s\n", e.what());
  94. }
  95. catch(...) {
  96. ERR("Failed to start event thread! Expect problems.\n");
  97. }
  98. }
  99. void StopEventThrd(ALCcontext *ctx)
  100. {
  101. RingBuffer *ring{ctx->mAsyncEvents.get()};
  102. auto evt_data = ring->getWriteVector().first;
  103. if(evt_data.len == 0)
  104. {
  105. do {
  106. std::this_thread::yield();
  107. evt_data = ring->getWriteVector().first;
  108. } while(evt_data.len == 0);
  109. }
  110. new (evt_data.buf) AsyncEvent{EventType_KillThread};
  111. ring->writeAdvance(1);
  112. ctx->mEventSem.post();
  113. if(ctx->mEventThread.joinable())
  114. ctx->mEventThread.join();
  115. }
  116. AL_API void AL_APIENTRY alEventControlSOFT(ALsizei count, const ALenum *types, ALboolean enable)
  117. START_API_FUNC
  118. {
  119. ContextRef context{GetContextRef()};
  120. if UNLIKELY(!context) return;
  121. if(count < 0) context->setError(AL_INVALID_VALUE, "Controlling %d events", count);
  122. if(count <= 0) return;
  123. if(!types) SETERR_RETURN(context, AL_INVALID_VALUE,, "NULL pointer");
  124. ALbitfieldSOFT flags{0};
  125. const ALenum *types_end = types+count;
  126. auto bad_type = std::find_if_not(types, types_end,
  127. [&flags](ALenum type) noexcept -> bool
  128. {
  129. if(type == AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT)
  130. flags |= EventType_BufferCompleted;
  131. else if(type == AL_EVENT_TYPE_SOURCE_STATE_CHANGED_SOFT)
  132. flags |= EventType_SourceStateChange;
  133. else if(type == AL_EVENT_TYPE_ERROR_SOFT)
  134. flags |= EventType_Error;
  135. else if(type == AL_EVENT_TYPE_PERFORMANCE_SOFT)
  136. flags |= EventType_Performance;
  137. else if(type == AL_EVENT_TYPE_DEPRECATED_SOFT)
  138. flags |= EventType_Deprecated;
  139. else if(type == AL_EVENT_TYPE_DISCONNECTED_SOFT)
  140. flags |= EventType_Disconnected;
  141. else
  142. return false;
  143. return true;
  144. }
  145. );
  146. if(bad_type != types_end)
  147. SETERR_RETURN(context, AL_INVALID_ENUM,, "Invalid event type 0x%04x", *bad_type);
  148. if(enable)
  149. {
  150. ALbitfieldSOFT enabledevts{context->mEnabledEvts.load(std::memory_order_relaxed)};
  151. while(context->mEnabledEvts.compare_exchange_weak(enabledevts, enabledevts|flags,
  152. std::memory_order_acq_rel, std::memory_order_acquire) == 0)
  153. {
  154. /* enabledevts is (re-)filled with the current value on failure, so
  155. * just try again.
  156. */
  157. }
  158. }
  159. else
  160. {
  161. ALbitfieldSOFT enabledevts{context->mEnabledEvts.load(std::memory_order_relaxed)};
  162. while(context->mEnabledEvts.compare_exchange_weak(enabledevts, enabledevts&~flags,
  163. std::memory_order_acq_rel, std::memory_order_acquire) == 0)
  164. {
  165. }
  166. /* Wait to ensure the event handler sees the changed flags before
  167. * returning.
  168. */
  169. std::lock_guard<std::mutex>{context->mEventCbLock};
  170. }
  171. }
  172. END_API_FUNC
  173. AL_API void AL_APIENTRY alEventCallbackSOFT(ALEVENTPROCSOFT callback, void *userParam)
  174. START_API_FUNC
  175. {
  176. ContextRef context{GetContextRef()};
  177. if UNLIKELY(!context) return;
  178. std::lock_guard<std::mutex> _{context->mPropLock};
  179. std::lock_guard<std::mutex> __{context->mEventCbLock};
  180. context->mEventCb = callback;
  181. context->mEventParam = userParam;
  182. }
  183. END_API_FUNC