19 #include "Async/Async.h"
21 #include "Async/AsyncWork.h"
23 #include "HAL/PlatformProcess.h"
25 #include "Kismet/BlueprintFunctionLibrary.h"
27 #include "RenderCommandFence.h"
29 #include "RenderingThread.h"
41 #include "RRThreadUtils.generated.h"
58 class RAPYUTASIMULATIONPLUGINS_API
URRThreadUtils :
public UBlueprintFunctionLibrary
82 auto& ThreadContext = FUObjectThreadContext::Get();
84 return (ThreadContext.IsInConstructor > 0);
108 template<
typename TFunc,
typename... TArgs>
114 if (IsInGameThread())
118 Forward<TFunc>(InTaskInGameThread)(Forward<TArgs>(InArgs)...);
126 AsyncTask(ENamedThreads::GameThread,
128 [InTaskInGameThread = Forward<TFunc>(InTaskInGameThread), InArgs = MakeTuple(Forward<TArgs>(InArgs)...)]()
134 InArgs.ApplyBefore(InTaskInGameThread);
144 template<
typename TFunc,
typename... TArgs>
150 DoAsyncTaskInThread<void>(
158 FPlatformProcess::Sleep(InWaitingTime);
162 [InTaskInGameThread = Forward<TFunc>(InTaskInGameThread)]() { DoTaskInGameThread(InTaskInGameThread); });
168 template<
typename TResult>
172 TFunction<
void()> InCompletionCallback,
174 const EAsyncExecution InExecutionThread =
178 EAsyncExecution::LargeThreadPool
182 EAsyncExecution::ThreadPool
196 TUniqueFunction<
void()>([InCompletionCallback = MoveTemp(InCompletionCallback)]() { InCompletionCallback(); }));
200 template<
typename TResult>
204 const uint64 InCurrentCaptureBatchId,
206 TFunction<TResult()> InTask,
208 TFunction<
void()> InCompletionCallback)
212 #if RAPYUTA_SIM_DEBUG
214 UE_LOG_WITH_INFO(LogTemp,
218 TEXT(
"[%ld:%s] ASYNC JOB NUM: %d"),
220 InCurrentCaptureBatchId,
228 OutAsyncJob.
AddAsyncTask(InCurrentCaptureBatchId, DoAsyncTaskInThread(MoveTemp(InTask), MoveTemp(InCompletionCallback)));
234 template<
typename TResult>
238 const uint64 InCurrentCaptureBatchId,
240 TFunction<TResult()> InTask,
242 TFunction<
void()> InCompletionCallback,
244 const EAsyncExecution InExecutionThread = EAsyncExecution::ThreadPool)
248 #if RAPYUTA_SIM_DEBUG
250 UE_LOG_WITH_INFO(LogTemp,
254 TEXT(
"[%ld:%s] ASYNC JOB NUM: %d"),
256 InCurrentCaptureBatchId,
266 DoAsyncTaskInThread(MoveTemp(InTask), MoveTemp(InCompletionCallback), InExecutionThread));
272 template<
typename TAsyncTask>
278 for (
auto& task : InAsyncTasks)
286 task->EnsureCompletion();