UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TripleBuffer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
7#include "HAL/PlatformMisc.h"
8
38template<typename BufferType>
40{
42 enum EBufferFlag
43 {
45 Dirty = 0x40,
46
48 Initial = 0x06,
49
51 ReaderMask = 0x03,
52
54 TempMask = 0x30,
55
57 TempShift = 4,
58
60 WriterMask = 0x0c,
61
63 WriterShift = 2,
64 };
65
66public:
67
70 {
71 Initialize();
72 Buffers[0] = Buffers[1] = Buffers[2] = BufferType();
73 }
74
77 {
78 Initialize();
79 }
80
86 [[nodiscard]] explicit TTripleBuffer(const BufferType& InValue)
87 {
88 Initialize();
89 Buffers[0] = Buffers[1] = Buffers[2] = InValue;
90 }
91
103 [[nodiscard]] TTripleBuffer(BufferType (&InBuffers)[3])
104 {
105 Buffers = &InBuffers[0];
106 Flags = EBufferFlag::Initial | EBufferFlag::Dirty;
107 OwnsMemory = false;
108 }
109
112 {
113 if (OwnsMemory)
114 {
115 delete[] Buffers;
116 }
117 }
118
119public:
120
126 [[nodiscard]] bool IsDirty() const
127 {
128 return ((Flags & EBufferFlag::Dirty) != 0);
129 }
130
137 [[nodiscard]] BufferType& Read()
138 {
139 return Buffers[Flags & EBufferFlag::ReaderMask];
140 }
141
150 {
151 if (!IsDirty())
152 {
153 return;
154 }
155
157
158 do
159 {
161 }
162 while (FPlatformAtomics::InterlockedCompareExchange(&Flags, SwapReadWithTempFlags(CurrentFlags), CurrentFlags) != CurrentFlags);
163 }
164
165public:
166
172 [[nodiscard]] BufferType& GetWriteBuffer()
173 {
174 return Buffers[(Flags & EBufferFlag::WriterMask) >> EBufferFlag::WriterShift];
175 }
176
183 {
185
186 do
187 {
189 }
190 while (FPlatformAtomics::InterlockedCompareExchange(&Flags, SwapWriteWithTempFlags(CurrentFlags), CurrentFlags) != CurrentFlags);
191 }
192
199 void Write(const BufferType Value)
200 {
201 Buffers[(Flags & EBufferFlag::WriterMask) >> EBufferFlag::WriterShift] = Value;
202 }
203
204public:
205
207 void Reset()
208 {
209 Flags = EBufferFlag::Initial;
211 }
212
219 [[nodiscard]] const BufferType& SwapAndRead()
220 {
222 return Read();
223 }
224
231 void WriteAndSwap(const BufferType Value)
232 {
233 Write(Value);
235 }
236
237protected:
238
241 {
242 Buffers = new BufferType[3];
243 Flags = EBufferFlag::Initial;
244 OwnsMemory = true;
245 }
246
247private:
248
250 [[nodiscard]] static UE_FORCEINLINE_HINT int32 SwapReadWithTempFlags(int32 Flags)
251 {
252 return ((Flags & EBufferFlag::ReaderMask) << 4) | ((Flags & EBufferFlag::TempMask) >> 4) | (Flags & EBufferFlag::WriterMask);
253 }
254
256 [[nodiscard]] static UE_FORCEINLINE_HINT int32 SwapWriteWithTempFlags(int32 Flags)
257 {
258 return ((Flags & EBufferFlag::TempMask) >> 2) | ((Flags & EBufferFlag::WriterMask) << 2) | (Flags & EBufferFlag::ReaderMask) | EBufferFlag::Dirty;
259 }
260
261private:
262
265
267 TTripleBuffer& operator=(const TTripleBuffer&);
268
269private:
270
272 BufferType* Buffers;
273
276
278 bool OwnsMemory;
279};
#define GCC_ALIGN(n)
Definition AndroidPlatform.h:163
ENoInit
Definition CoreMiscDefines.h:158
#define MS_ALIGN(n)
Definition Platform.h:916
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition TripleBuffer.h:40
void Reset()
Definition TripleBuffer.h:207
~TTripleBuffer()
Definition TripleBuffer.h:111
TTripleBuffer()
Definition TripleBuffer.h:69
void SwapReadBuffers()
Definition TripleBuffer.h:149
BufferType & Read()
Definition TripleBuffer.h:137
void SwapWriteBuffers()
Definition TripleBuffer.h:182
void Write(const BufferType Value)
Definition TripleBuffer.h:199
bool IsDirty() const
Definition TripleBuffer.h:126
const BufferType & SwapAndRead()
Definition TripleBuffer.h:219
TTripleBuffer(ENoInit)
Definition TripleBuffer.h:76
TTripleBuffer(const BufferType &InValue)
Definition TripleBuffer.h:86
BufferType & GetWriteBuffer()
Definition TripleBuffer.h:172
TTripleBuffer(BufferType(&InBuffers)[3])
Definition TripleBuffer.h:103
void WriteAndSwap(const BufferType Value)
Definition TripleBuffer.h:231
void Initialize()
Definition TripleBuffer.h:240
static UE_FORCEINLINE_HINT void MemoryBarrier()
Definition AndroidPlatformMisc.h:249