UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
AsyncHelpers.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Async/Async.h"
5#include "Containers/Queue.h"
6#include "CoreMinimal.h"
9
10namespace BuildPatchServices
11{
15 namespace AsyncHelpers
16 {
17 template<typename ResultType, typename... Args>
18 static TFunction<void()> MakePromiseKeeper(const TSharedRef<TPromise<ResultType>, ESPMode::ThreadSafe>& Promise, const TFunction<ResultType(Args...)>& Function, Args... FuncArgs)
19 {
20 return [Promise, Function, FuncArgs...]()
21 {
22 Promise->SetValue(Function(FuncArgs...));
23 };
24 }
25
26 template<typename... Args>
27 static TFunction<void()> MakePromiseKeeper(const TSharedRef<TPromise<void>, ESPMode::ThreadSafe>& Promise, const TFunction<void(Args...)>& Function, Args... FuncArgs)
28 {
29 return [Promise, Function, FuncArgs...]()
30 {
32 Promise->SetValue();
33 };
34 }
35
36 template<typename ResultType, typename... Args>
37 static TFuture<ResultType> ExecuteOnGameThread(const TFunction<ResultType(Args...)>& Function, Args... FuncArgs)
38 {
40 TFunction<void()> PromiseKeeper = MakePromiseKeeper(Promise, Function, FuncArgs...);
41 if (!IsInGameThread())
42 {
44 }
45 else
46 {
48 }
49 return Promise->GetFuture();
50 }
51
52 template<typename ResultType>
54 {
56 TFunction<void()> PromiseKeeper = MakePromiseKeeper(Promise, Function);
57 if (!IsInGameThread())
58 {
60 }
61 else
62 {
64 }
65 return Promise->GetFuture();
66 }
67
68 template<typename ResultType>
69 static TFuture<ResultType> ExecuteOnCustomThread(const TFunction<ResultType()>& Function, TQueue<TFunction<void()>, EQueueMode::Spsc>& CustomThreadQueue)
70 {
72 TFunction<void()> PromiseKeeper = MakePromiseKeeper(Promise, Function);
73 if (!IsInGameThread())
74 {
76 }
77 else
78 {
80 }
81 return Promise->GetFuture();
82 }
83 }
84
88 namespace AsyncHelpers
89 {
96 template<typename IntegerType>
97 void LockFreePeak(volatile IntegerType* PeakValue, IntegerType NewSample)
98 {
99 IntegerType CurrentPeak;
100 do
101 {
102 // Read the current value.
104 }
105 // If the value was lower than the sample, try to update it to NewSample if the current value has not been set by another thread.
106 // If the current value change since we read it, try again to see if our sample is still higher.
107 while (CurrentPeak < NewSample && FPlatformAtomics::InterlockedCompareExchange(PeakValue, NewSample, CurrentPeak) != CurrentPeak);
108 }
109 }
110
113}
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
void AsyncTask(ENamedThreads::Type Thread, TUniqueFunction< void()> Function)
Definition Async.cpp:54
SharedPointerInternals::TRawPtrProxy< ObjectType > MakeShareable(ObjectType *InObject)
Definition SharedPointer.h:1947
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
CORE_API bool IsInGameThread()
Definition ThreadingBase.cpp:185
void ExecuteOnGameThread(const TCHAR *DebugName, FunctorType &&Functor)
Definition Ticker.h:168
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
FThreadSafeCounter64 FThreadSafeInt64
Definition AsyncHelpers.h:111
FThreadSafeCounter FThreadSafeInt32
Definition AsyncHelpers.h:112
Definition ThreadSafeCounter64.h:14
Definition ThreadSafeCounter.h:14
Definition AndroidPlatformMisc.h:14
Definition Future.h:393
Definition Future.h:541
Definition Queue.h:48
Definition SharedPointer.h:153
void LockFreePeak(volatile IntegerType *PeakValue, IntegerType NewSample)
Definition AsyncHelpers.h:97
Definition BuildPatchFileConstructor.h:28
@ GameThread
Definition TaskGraphInterfaces.h:61