WwiseDeferredQueue.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #include "Wwise/WwiseDeferredQueue.h"
  16. #include "Async/Async.h"
  17. #include "Wwise/Stats/AsyncStats.h"
  18. #include "Wwise/Stats/Concurrency.h"
  19. FWwiseDeferredQueue::FWwiseDeferredQueue()
  20. {
  21. }
  22. FWwiseDeferredQueue::~FWwiseDeferredQueue()
  23. {
  24. bClosing = true;
  25. if (!IsEmpty())
  26. {
  27. Wait();
  28. UE_CLOG(UNLIKELY(!IsEmpty()), LogWwiseConcurrency, Error, TEXT("Still operations in queue while deleting Deferred Queue"));
  29. }
  30. }
  31. void FWwiseDeferredQueue::AsyncDefer(FFunction&& InFunction)
  32. {
  33. if (!bClosing)
  34. {
  35. AsyncOpQueue.Enqueue(MoveTemp(InFunction));
  36. }
  37. }
  38. void FWwiseDeferredQueue::SyncDefer(FSyncFunction&& InFunction)
  39. {
  40. if (!bClosing)
  41. {
  42. SyncOpQueue.Enqueue(MoveTemp(InFunction));
  43. }
  44. }
  45. void FWwiseDeferredQueue::GameDefer(FFunction&& InFunction)
  46. {
  47. if (!bClosing)
  48. {
  49. GameOpQueue.Enqueue(MoveTemp(InFunction));
  50. }
  51. }
  52. void FWwiseDeferredQueue::Run(AK::IAkGlobalPluginContext* InContext)
  53. {
  54. SCOPED_WWISECONCURRENCY_EVENT_4(TEXT("FWwiseDeferredQueue::Run"));
  55. FWwiseAsyncCycleCounter OpCycleCounter(GET_STATID(STAT_WwiseConcurrencySync));
  56. UE_CLOG(UNLIKELY(Context), LogWwiseConcurrency, Error, TEXT("Executing two Run() at the same time."));
  57. Context = InContext;
  58. if (!AsyncOpQueue.IsEmpty())
  59. {
  60. AsyncExecutionQueue.Async([this]() mutable
  61. {
  62. AsyncExec();
  63. });
  64. }
  65. if (!GameOpQueue.IsEmpty() || OnGameRun.IsBound())
  66. {
  67. GameThreadExec();
  68. }
  69. if (!SyncOpQueue.IsEmpty() || OnSyncRunTS.IsBound())
  70. {
  71. SyncExec();
  72. }
  73. OnSyncRunTS.Broadcast(Context);
  74. Context = nullptr;
  75. }
  76. void FWwiseDeferredQueue::Wait()
  77. {
  78. const bool bIsInGameThread = IsInGameThread();
  79. SCOPED_WWISECONCURRENCY_EVENT_4(bIsInGameThread ? TEXT("FWwiseDeferredQueue::Wait GameThread") : TEXT("FWwiseDeferredQueue::Wait"));
  80. CONDITIONAL_SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyGameThreadWait, bIsInGameThread);
  81. CONDITIONAL_SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyWait, !bIsInGameThread);
  82. if (!AsyncOpQueue.IsEmpty())
  83. {
  84. AsyncExecutionQueue.AsyncWait([this]() mutable
  85. {
  86. AsyncExec();
  87. });
  88. }
  89. if (!GameOpQueue.IsEmpty())
  90. {
  91. FEventRef Done;
  92. if (bIsInGameThread)
  93. {
  94. const bool bNeedToStartLoop = GameThreadExecuting.IncrementExchange() == 0;
  95. GameOpQueue.Enqueue([this, &Done]() mutable
  96. {
  97. Done->Trigger();
  98. GameThreadExecuting.DecrementExchange();
  99. return EWwiseDeferredAsyncResult::Done;
  100. });
  101. if (bNeedToStartLoop)
  102. {
  103. FFunction Func;
  104. while (GameThreadExecuting.Load() > 0 && GameOpQueue.Dequeue(Func))
  105. {
  106. if (Func() == EWwiseDeferredAsyncResult::KeepRunning)
  107. {
  108. GameDefer(MoveTemp(Func));
  109. }
  110. }
  111. }
  112. }
  113. else
  114. {
  115. GameOpQueue.Enqueue([&Done]() mutable
  116. {
  117. Done->Trigger();
  118. return EWwiseDeferredAsyncResult::Done;
  119. });
  120. GameThreadExec();
  121. }
  122. Done->Wait();
  123. }
  124. if (!SyncOpQueue.IsEmpty())
  125. {
  126. FWwiseAsyncCycleCounter OpCycleCounter(GET_STATID(STAT_WwiseConcurrencySync));
  127. SyncExec();
  128. }
  129. }
  130. void FWwiseDeferredQueue::AsyncExec()
  131. {
  132. SCOPED_WWISECONCURRENCY_EVENT_4(TEXT("FWwiseDeferredQueue::AsyncExec"));
  133. SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyAsync);
  134. bool bDone = false;
  135. AsyncOpQueue.Enqueue([&bDone]() mutable
  136. {
  137. bDone = true;
  138. return EWwiseDeferredAsyncResult::Done;
  139. });
  140. while (!bDone)
  141. {
  142. FFunction Func;
  143. const bool bResult = AsyncOpQueue.Dequeue(Func);
  144. if (UNLIKELY(!bResult))
  145. {
  146. UE_LOG(LogWwiseConcurrency, Error, TEXT("FWwiseDeferredQueue: No Result dequeuing Async Deferred Queue"));
  147. break;
  148. }
  149. if (Func() == EWwiseDeferredAsyncResult::KeepRunning)
  150. {
  151. AsyncDefer(MoveTemp(Func));
  152. }
  153. }
  154. }
  155. void FWwiseDeferredQueue::SyncExec()
  156. {
  157. SyncOpQueue.Enqueue([this](AK::IAkGlobalPluginContext*) mutable
  158. {
  159. bSyncThreadDone = true;
  160. return EWwiseDeferredAsyncResult::Done;
  161. });
  162. SyncExecLoop();
  163. }
  164. void FWwiseDeferredQueue::SyncExecLoop()
  165. {
  166. FSyncFunction Func;
  167. while (!bSyncThreadDone && SyncOpQueue.Dequeue(Func))
  168. {
  169. if (Func(Context) == EWwiseDeferredAsyncResult::KeepRunning)
  170. {
  171. SyncDefer(MoveTemp(Func));
  172. }
  173. }
  174. OnSyncRunTS.Broadcast(Context);
  175. bSyncThreadDone = false;
  176. }
  177. void FWwiseDeferredQueue::GameThreadExec()
  178. {
  179. const bool bNeedToStartLoop = GameThreadExecuting.IncrementExchange() == 0;
  180. GameOpQueue.Enqueue([this]() mutable
  181. {
  182. GameThreadExecuting.DecrementExchange();
  183. return EWwiseDeferredAsyncResult::Done;
  184. });
  185. if (bNeedToStartLoop)
  186. {
  187. GameThreadExecLoop();
  188. }
  189. }
  190. void FWwiseDeferredQueue::GameThreadExecLoop()
  191. {
  192. AsyncTask(ENamedThreads::GameThread, [this]() mutable
  193. {
  194. SCOPED_WWISECONCURRENCY_EVENT_4(TEXT("FWwiseDeferredQueue::GameThreadExecLoop"));
  195. SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyGameThread);
  196. FFunction Func;
  197. while (GameThreadExecuting.Load() > 0 && GameOpQueue.Dequeue(Func))
  198. {
  199. if (Func() == EWwiseDeferredAsyncResult::KeepRunning)
  200. {
  201. GameDefer(MoveTemp(Func));
  202. }
  203. }
  204. OnGameRun.Broadcast();
  205. });
  206. }