WwiseDeferredQueue.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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(const TCHAR* InDebugName) :
  20. AsyncExecutionQueue(InDebugName)
  21. {
  22. }
  23. FWwiseDeferredQueue::~FWwiseDeferredQueue()
  24. {
  25. bClosing = true;
  26. if (!IsEmpty())
  27. {
  28. Wait();
  29. UE_CLOG(UNLIKELY(!IsEmpty()), LogWwiseConcurrency, Error, TEXT("Still operations in queue while deleting Deferred Queue"));
  30. }
  31. }
  32. void FWwiseDeferredQueue::AsyncDefer(FFunction&& InFunction)
  33. {
  34. if (!bClosing)
  35. {
  36. AsyncOpQueue.Enqueue(MoveTemp(InFunction));
  37. }
  38. }
  39. void FWwiseDeferredQueue::SyncDefer(FSyncFunction&& InFunction)
  40. {
  41. if (!bClosing)
  42. {
  43. SyncOpQueue.Enqueue(MoveTemp(InFunction));
  44. }
  45. }
  46. void FWwiseDeferredQueue::GameDefer(FFunction&& InFunction)
  47. {
  48. if (!bClosing)
  49. {
  50. GameOpQueue.Enqueue(MoveTemp(InFunction));
  51. }
  52. }
  53. void FWwiseDeferredQueue::Run(AK::IAkGlobalPluginContext* InContext)
  54. {
  55. SCOPED_WWISECONCURRENCY_EVENT_4(TEXT("FWwiseDeferredQueue::Run"));
  56. FWwiseAsyncCycleCounter OpCycleCounter(GET_STATID(STAT_WwiseConcurrencySync));
  57. UE_CLOG(UNLIKELY(Context), LogWwiseConcurrency, Error, TEXT("Executing two Run() at the same time."));
  58. Context = InContext;
  59. if (!AsyncOpQueue.IsEmpty())
  60. {
  61. AsyncExecutionQueue.Async(WWISECONCURRENCY_ASYNC_NAME("FWwiseDeferredQueue::Run"), [this]() mutable
  62. {
  63. AsyncExec();
  64. });
  65. }
  66. if (!GameOpQueue.IsEmpty() || OnGameRun.IsBound())
  67. {
  68. GameThreadExec();
  69. }
  70. if (!SyncOpQueue.IsEmpty() || OnSyncRunTS.IsBound())
  71. {
  72. SyncExec();
  73. }
  74. OnSyncRunTS.Broadcast(Context);
  75. Context = nullptr;
  76. }
  77. void FWwiseDeferredQueue::Wait()
  78. {
  79. const bool bIsInGameThread = IsInGameThread();
  80. SCOPED_WWISECONCURRENCY_EVENT_4(bIsInGameThread ? TEXT("FWwiseDeferredQueue::Wait GameThread") : TEXT("FWwiseDeferredQueue::Wait"));
  81. CONDITIONAL_SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyGameThreadWait, bIsInGameThread);
  82. CONDITIONAL_SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyWait, !bIsInGameThread);
  83. if (!AsyncOpQueue.IsEmpty())
  84. {
  85. AsyncExecutionQueue.AsyncWait(WWISECONCURRENCY_ASYNC_NAME("FWwiseDeferredQueue::Wait"), [this]() mutable
  86. {
  87. AsyncExec();
  88. });
  89. }
  90. if (!GameOpQueue.IsEmpty())
  91. {
  92. FEventRef Done;
  93. if (bIsInGameThread)
  94. {
  95. const bool bNeedToStartLoop = GameThreadExecuting.IncrementExchange() == 0;
  96. GameOpQueue.Enqueue([this, &Done]() mutable
  97. {
  98. Done->Trigger();
  99. GameThreadExecuting.DecrementExchange();
  100. return EWwiseDeferredAsyncResult::Done;
  101. });
  102. if (bNeedToStartLoop)
  103. {
  104. FFunction Func;
  105. while (GameThreadExecuting.Load() > 0 && GameOpQueue.Dequeue(Func))
  106. {
  107. if (Func() == EWwiseDeferredAsyncResult::KeepRunning)
  108. {
  109. GameDefer(MoveTemp(Func));
  110. }
  111. }
  112. }
  113. }
  114. else
  115. {
  116. GameOpQueue.Enqueue([&Done]() mutable
  117. {
  118. Done->Trigger();
  119. return EWwiseDeferredAsyncResult::Done;
  120. });
  121. GameThreadExec();
  122. }
  123. Done->Wait();
  124. }
  125. if (!SyncOpQueue.IsEmpty())
  126. {
  127. FWwiseAsyncCycleCounter OpCycleCounter(GET_STATID(STAT_WwiseConcurrencySync));
  128. SyncExec();
  129. }
  130. }
  131. void FWwiseDeferredQueue::AsyncExec()
  132. {
  133. SCOPED_WWISECONCURRENCY_EVENT_4(TEXT("FWwiseDeferredQueue::AsyncExec"));
  134. SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyAsync);
  135. bool bDone = false;
  136. AsyncOpQueue.Enqueue([&bDone]() mutable
  137. {
  138. bDone = true;
  139. return EWwiseDeferredAsyncResult::Done;
  140. });
  141. while (!bDone)
  142. {
  143. FFunction Func;
  144. const bool bResult = AsyncOpQueue.Dequeue(Func);
  145. if (UNLIKELY(!bResult))
  146. {
  147. UE_LOG(LogWwiseConcurrency, Error, TEXT("FWwiseDeferredQueue: No Result dequeuing Async Deferred Queue"));
  148. break;
  149. }
  150. if (Func() == EWwiseDeferredAsyncResult::KeepRunning)
  151. {
  152. AsyncDefer(MoveTemp(Func));
  153. }
  154. }
  155. }
  156. void FWwiseDeferredQueue::SyncExec()
  157. {
  158. SyncOpQueue.Enqueue([this](AK::IAkGlobalPluginContext*) mutable
  159. {
  160. bSyncThreadDone = true;
  161. return EWwiseDeferredAsyncResult::Done;
  162. });
  163. SyncExecLoop();
  164. }
  165. void FWwiseDeferredQueue::SyncExecLoop()
  166. {
  167. FSyncFunction Func;
  168. while (!bSyncThreadDone && SyncOpQueue.Dequeue(Func))
  169. {
  170. if (Func(Context) == EWwiseDeferredAsyncResult::KeepRunning)
  171. {
  172. SyncDefer(MoveTemp(Func));
  173. }
  174. }
  175. OnSyncRunTS.Broadcast(Context);
  176. bSyncThreadDone = false;
  177. }
  178. void FWwiseDeferredQueue::GameThreadExec()
  179. {
  180. const bool bNeedToStartLoop = GameThreadExecuting.IncrementExchange() == 0;
  181. GameOpQueue.Enqueue([this]() mutable
  182. {
  183. GameThreadExecuting.DecrementExchange();
  184. return EWwiseDeferredAsyncResult::Done;
  185. });
  186. if (bNeedToStartLoop)
  187. {
  188. GameThreadExecLoop();
  189. }
  190. }
  191. void FWwiseDeferredQueue::GameThreadExecLoop()
  192. {
  193. AsyncTask(ENamedThreads::GameThread, [this]() mutable
  194. {
  195. SCOPED_WWISECONCURRENCY_EVENT_4(TEXT("FWwiseDeferredQueue::GameThreadExecLoop"));
  196. SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyGameThread);
  197. FFunction Func;
  198. while (GameThreadExecuting.Load() > 0 && GameOpQueue.Dequeue(Func))
  199. {
  200. if (Func() == EWwiseDeferredAsyncResult::KeepRunning)
  201. {
  202. GameDefer(MoveTemp(Func));
  203. }
  204. }
  205. OnGameRun.Broadcast();
  206. });
  207. }