RapyutaSimulationPlugins
RRThreadUtils.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 
16 
17 // UE
18 
19 #include "Async/Async.h"
20 
21 #include "Async/AsyncWork.h"
22 
23 #include "HAL/PlatformProcess.h"
24 
25 #include "Kismet/BlueprintFunctionLibrary.h"
26 
27 #include "RenderCommandFence.h"
28 
29 #include "RenderingThread.h"
30 
31 
32 
33 // RapyutaSimulationPlugins
34 
35 #include "Core/RRActorCommon.h"
36 
37 #include "Core/RRGameSingleton.h"
38 
39 
40 
41 #include "RRThreadUtils.generated.h"
42 
43 
44 
58 class RAPYUTASIMULATIONPLUGINS_API URRThreadUtils : public UBlueprintFunctionLibrary
59 
60 {
61 
62 
63 
64 public:
65 
78  static bool IsInsideConstructor()
79 
80  {
81 
82  auto& ThreadContext = FUObjectThreadContext::Get();
83 
84  return (ThreadContext.IsInConstructor > 0);
85 
86  }
87 
88 
89 
90  // ----------------------------------------------------------------------------------------------------------
91 
92  // [ASYNC TASK SERVICES] --
93 
94  //
95 
108  template<typename TFunc, typename... TArgs>
109 
110  static void DoTaskInGameThread(TFunc&& InTaskInGameThread, TArgs&&... InArgs)
111 
112  {
113 
114  if (IsInGameThread())
115 
116  {
117 
118  Forward<TFunc>(InTaskInGameThread)(Forward<TArgs>(InArgs)...);
119 
120  }
121 
122  else
123 
124  {
125 
126  AsyncTask(ENamedThreads::GameThread,
127 
128  [InTaskInGameThread = Forward<TFunc>(InTaskInGameThread), InArgs = MakeTuple(Forward<TArgs>(InArgs)...)]()
129 
130  {
131 
132  // This function is equivalent to [std::apply], which is only available since C++17!
133 
134  InArgs.ApplyBefore(InTaskInGameThread);
135 
136  });
137 
138  }
139 
140  }
141 
142 
143 
144  template<typename TFunc, typename... TArgs>
145 
146  static void DoTaskInGameThreadLater(TFunc&& InTaskInGameThread, float InWaitingTime, TArgs&&... Args)
147 
148  {
149 
150  DoAsyncTaskInThread<void>(
151 
152  [InWaitingTime]()
153 
154  {
155 
156  // Wait for the physics to complete the computation given earlier vel cmds
157 
158  FPlatformProcess::Sleep(InWaitingTime);
159 
160  },
161 
162  [InTaskInGameThread = Forward<TFunc>(InTaskInGameThread)]() { DoTaskInGameThread(InTaskInGameThread); });
163 
164  }
165 
166 
167 
168  template<typename TResult>
169 
170  static auto DoAsyncTaskInThread(TFunction<TResult()> InTask,
171 
172  TFunction<void()> InCompletionCallback,
173 
174  const EAsyncExecution InExecutionThread =
175 
176 #if WITH_EDITOR
177 
178  EAsyncExecution::LargeThreadPool
179 
180 #else
181 
182  EAsyncExecution::ThreadPool
183 
184 #endif
185 
186  )
187 
188  {
189 
190  return Async(
191 
192  InExecutionThread,
193 
194  MoveTemp(InTask),
195 
196  TUniqueFunction<void()>([InCompletionCallback = MoveTemp(InCompletionCallback)]() { InCompletionCallback(); }));
197 
198  }
199 
200  template<typename TResult>
201 
202  static void AddAsyncTaskInThreadPool(FRRAsyncJob& OutAsyncJob,
203 
204  const uint64 InCurrentCaptureBatchId,
205 
206  TFunction<TResult()> InTask,
207 
208  TFunction<void()> InCompletionCallback)
209 
210  {
211 
212 #if RAPYUTA_SIM_DEBUG
213 
214  UE_LOG_WITH_INFO(LogTemp,
215 
216  Warning,
217 
218  TEXT("[%ld:%s] ASYNC JOB NUM: %d"),
219 
220  InCurrentCaptureBatchId,
221 
222  *OutAsyncJob.JobName,
223 
224  OutAsyncJob.GetTasksNum());
225 
226 #endif
227 
228  OutAsyncJob.AddAsyncTask(InCurrentCaptureBatchId, DoAsyncTaskInThread(MoveTemp(InTask), MoveTemp(InCompletionCallback)));
229 
230  }
231 
232 
233 
234  template<typename TResult>
235 
236  static void AddAsyncTaskToJob(FRRAsyncJob& OutAsyncJob,
237 
238  const uint64 InCurrentCaptureBatchId,
239 
240  TFunction<TResult()> InTask,
241 
242  TFunction<void()> InCompletionCallback,
243 
244  const EAsyncExecution InExecutionThread = EAsyncExecution::ThreadPool)
245 
246  {
247 
248 #if RAPYUTA_SIM_DEBUG
249 
250  UE_LOG_WITH_INFO(LogTemp,
251 
252  Warning,
253 
254  TEXT("[%ld:%s] ASYNC JOB NUM: %d"),
255 
256  InCurrentCaptureBatchId,
257 
258  *OutAsyncJob.JobName,
259 
260  OutAsyncJob.GetTasksNum());
261 
262 #endif
263 
264  OutAsyncJob.AddAsyncTask(InCurrentCaptureBatchId,
265 
266  DoAsyncTaskInThread(MoveTemp(InTask), MoveTemp(InCompletionCallback), InExecutionThread));
267 
268  }
269 
270 
271 
272  template<typename TAsyncTask>
273 
274  static void EnsureAsyncTasksCompletion(const TArray<TUniquePtr<FAsyncTask<TAsyncTask>>>& InAsyncTasks)
275 
276  {
277 
278  for (auto& task : InAsyncTasks)
279 
280  {
281 
282  if (!task->Cancel())
283 
284  {
285 
286  task->EnsureCompletion();
287 
288  }
289 
290  }
291 
292  }
293 
294 };
295 
RRGameSingleton.h
GameSingleton.
URRThreadUtils::AddAsyncTaskToJob
static void AddAsyncTaskToJob(FRRAsyncJob &OutAsyncJob, const uint64 InCurrentCaptureBatchId, TFunction< TResult()> InTask, TFunction< void()> InCompletionCallback, const EAsyncExecution InExecutionThread=EAsyncExecution::ThreadPool)
Definition: RRThreadUtils.h:236
URRThreadUtils::IsInsideConstructor
static bool IsInsideConstructor()
return true if called inside constructor.
Definition: RRThreadUtils.h:78
FRRAsyncJob::JobName
FString JobName
Definition: RRActorCommon.h:260
URRThreadUtils::DoAsyncTaskInThread
static auto DoAsyncTaskInThread(TFunction< TResult()> InTask, TFunction< void()> InCompletionCallback, const EAsyncExecution InExecutionThread=EAsyncExecution::ThreadPool)
Definition: RRThreadUtils.h:170
URRThreadUtils::AddAsyncTaskInThreadPool
static void AddAsyncTaskInThreadPool(FRRAsyncJob &OutAsyncJob, const uint64 InCurrentCaptureBatchId, TFunction< TResult()> InTask, TFunction< void()> InCompletionCallback)
Definition: RRThreadUtils.h:202
RRActorCommon.h
Asset utils.
URRThreadUtils
ThreadUtils with Async.
Definition: RRThreadUtils.h:58
FRRAsyncJob::AddAsyncTask
void AddAsyncTask(const uint64 InCaptureBatchId, TFuture< bool > &&InAsyncTask)
Definition: RRActorCommon.h:304
FRRAsyncJob::GetTasksNum
int32 GetTasksNum() const
Definition: RRActorCommon.h:280
FRRAsyncJob
Async job info (task, job name, latest capture batch id)
Definition: RRActorCommon.h:156
URRThreadUtils::DoTaskInGameThreadLater
static void DoTaskInGameThreadLater(TFunc &&InTaskInGameThread, float InWaitingTime, TArgs &&... Args)
Definition: RRThreadUtils.h:146
URRThreadUtils::DoTaskInGameThread
static void DoTaskInGameThread(TFunc &&InTaskInGameThread, TArgs &&... InArgs)
Used in URRProceduralMeshComponent.
Definition: RRThreadUtils.h:110
URRThreadUtils::EnsureAsyncTasksCompletion
static void EnsureAsyncTasksCompletion(const TArray< TUniquePtr< FAsyncTask< TAsyncTask >>> &InAsyncTasks)
Definition: RRThreadUtils.h:274