UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VVMWeakBarrier.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#if WITH_VERSE_VM || defined(__INTELLISENSE__)
6
7#include "VVMAux.h"
8#include "VVMContext.h"
9#include "VVMContextImpl.h"
10#include "VVMValue.h"
11
12namespace Verse
13{
14template <typename T>
15struct TWeakBarrier
16{
17 static constexpr bool bIsVValue = std::is_same_v<T, VValue>;
18 static constexpr bool bIsAux = IsTAux<T>;
19 using TValue = typename std::conditional_t<bIsAux, T, typename std::conditional_t<bIsVValue, VValue, T*>>;
20 using TEncodedValue = typename std::conditional<bIsVValue, uint64, T*>::type;
21
22 TWeakBarrier() = default;
23
26 {
28 }
29
30 TWeakBarrier& operator=(const TWeakBarrier& Other)
31 {
33 return *this;
34 }
35
38 {
40 Other.Reset();
41 }
42
43 TWeakBarrier& operator=(TWeakBarrier&& Other)
44 {
46 Other.Reset();
47 return *this;
48 }
49
50 TWeakBarrier(TValue Value)
51 : Value(Value)
52 {
53 }
54
55 TWeakBarrier& operator=(TValue InValue)
56 {
58 return *this;
59 }
60
61 template <typename U = T>
62 TWeakBarrier(std::enable_if_t<!bIsVValue, U>& Value)
63 : Value(&Value)
64 {
65 }
66
67 bool operator==(const TWeakBarrier& Other) const
68 {
69 return Value == Other.Value;
70 }
71
72 template <typename TResult = void>
73 std::enable_if_t<!bIsVValue, TResult> Set(T& NewValue)
74 {
75 Value = &NewValue;
76 }
77
78 void Set(TValue NewValue)
79 {
80 Value = NewValue;
81 }
82
83 void Reset()
84 {
85 if constexpr (bIsVValue)
86 {
87 Value = {};
88 }
89 else
90 {
91 Value = nullptr;
92 }
93 }
94
95 TValue Get(FAccessContext Context) const { return RunReadBarrier(Context, Value); }
96 TValue Get() const { return RunReadBarrier(FAccessContextPromise(), Value); }
97
98 // nb: operators "*" and "->" disabled for TWeakBarrier<VValue>;
99 // use Get() + VValue member functions to check/access boxed values
100
101 template <typename TResult = TValue>
102 std::enable_if_t<!bIsVValue && !bIsAux, TResult> operator->() const { return Get(); }
103
104 template <typename TResult = T>
105 std::enable_if_t<!bIsVValue && !bIsAux, TResult&> operator*() const { return *Get(); }
106
107 explicit operator bool() const { return !!Get(); }
108
115 {
116 std::atomic<TEncodedValue>* ValuePtr = reinterpret_cast<std::atomic<TEncodedValue>*>(&Value);
117 TValue ValueCopy = Value;
118 if constexpr (bIsVValue)
119 {
120 uint64 EncodedValueCopy = ValueCopy.GetEncodedBits();
121 if (const VCell* Cell = ValueCopy.ExtractCell())
122 {
123 if (!FHeap::IsMarked(Cell))
124 {
125 return ValuePtr->compare_exchange_strong(EncodedValueCopy, VValue().GetEncodedBits(), std::memory_order_relaxed);
126 }
127 }
128 }
129 else
130 {
131 if (ValueCopy && !FHeap::IsMarked(ValueCopy))
132 {
133 return ValuePtr->compare_exchange_strong(ValueCopy, nullptr, std::memory_order_relaxed);
134 }
135 }
136 return false;
137 }
138
139private:
140 TValue Value{};
141
142 template <typename ContextType>
143 static TValue RunReadBarrier(ContextType Context, TValue Value)
144 {
145 if (FHeap::GetWeakBarrierState() == EWeakBarrierState::Inactive)
146 {
147 return Value;
148 }
149 if constexpr (bIsAux)
150 {
151 if (!FHeap::IsMarked(Value.GetPtr()))
152 {
153 FAccessContext(Context).RunAuxWeakReadBarrierUnmarkedWhenActive(Value.GetPtr());
154 }
155 }
156 else if constexpr (bIsVValue)
157 {
158 if (VCell* Cell = Value.ExtractCell())
159 {
160 if (!FHeap::IsMarked(Cell))
161 {
162 // Delay construction of the context (which does the expensive TLS lookup), until we actually need the mark stack to do marking.
163 Cell = FAccessContext(Context).RunWeakReadBarrierUnmarkedWhenActive(Cell);
164 if (Cell)
165 {
166 return *Cell;
167 }
168 else
169 {
170 return VValue();
171 }
172 }
173 }
174 return Value;
175 }
176 else
177 {
178 if (Value)
179 {
180 if (FHeap::IsMarked(Value))
181 {
182 return Value;
183 }
184 else
185 {
187 }
188 }
189 else
190 {
191 return nullptr;
192 }
193 }
194 }
195};
196
197template <typename T>
199{
200 using ::GetTypeHash;
201 return GetTypeHash(WeakBarrier.Get());
202}
203} // namespace Verse
204#endif // WITH_VERSE_VM
UE_FORCEINLINE_HINT FLinearColor operator*(float Scalar, const FLinearColor &Color)
Definition Color.h:473
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
uint32_t uint32
Definition binka_ue_file_header.h:6
FORCEINLINE T * Get(const FObjectPtr &ObjectPtr)
Definition ObjectPtr.h:426
bool operator==(const FCachedAssetKey &A, const FCachedAssetKey &B)
Definition AssetDataMap.h:501
Definition Archive.h:36
uint32 GetTypeHash(TPtrVariant< Ts... > Ptr)
Definition VVMPtrVariant.h:83