UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
EventPool.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"
9#include "HAL/Event.h"
10
11#ifndef USE_EVENT_POOLING
12 #define USE_EVENT_POOLING 1
13#endif
14
15class FSafeRecyclableEvent final: public FEvent
16{
17public:
19
24
26 {
27 InnerEvent = nullptr;
28 }
29
30 virtual bool Create(bool bIsManualReset = false)
31 {
33 return InnerEvent->Create(bIsManualReset);
35 }
36
37 virtual bool IsManualReset()
38 {
39 return InnerEvent->IsManualReset();
40 }
41
42 virtual void Trigger()
43 {
45 }
46
47 virtual void Reset()
48 {
50 }
51
52 virtual bool Wait(uint32 WaitTime, const bool bIgnoreThreadIdleStats = false)
53 {
54 return InnerEvent->Wait(WaitTime, bIgnoreThreadIdleStats);
55 }
56
57};
58
70template<EEventMode PoolType>
72{
73public:
74#if USE_EVENT_POOLING
76 {
77 EmptyPool();
78 }
79#endif
80
91
99 {
100 check(Event);
101 check(Event->IsManualReset() == (PoolType == EEventMode::ManualReset));
102
104 FEvent* InnerEvent = SafeEvent->InnerEvent;
105 // Make sure the safeevent can't be used anymore before returning the inner event to the pool
106 // This will help trap use-after-free of events that can end up triggering events while they're in the pool
107 // and causing issues for other pool users.
108 delete SafeEvent;
109 ReturnRawEvent(InnerEvent);
110 }
111
113 {
114#if USE_EVENT_POOLING
115 while (FEvent* Event = Pool.Pop())
116 {
117 delete Event;
118 }
119#endif
120 }
121
128 {
129 FEvent* Event =
130#if USE_EVENT_POOLING
131 Pool.Pop();
132#else
133 nullptr;
134#endif
135
136 if (Event == nullptr)
137 {
141 }
142
143 check(Event);
144
145 Event->AdvanceStats();
146
147 return Event;
148 }
149
155 {
156 check(Event);
157
158#if USE_EVENT_POOLING
159 Event->Reset();
160 Pool.Push(Event);
161#else
162 delete Event;
163#endif
164 }
165
166private:
167#if USE_EVENT_POOLING
170#endif
171};
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:12
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Event.h:21
virtual bool Wait(uint32 WaitTime, const bool bIgnoreThreadIdleStats=false)=0
virtual void Trigger()=0
virtual bool Create(bool bIsManualReset=false)=0
virtual void Reset()=0
virtual bool IsManualReset()=0
Definition EventPool.h:16
virtual void Trigger()
Definition EventPool.h:42
virtual bool IsManualReset()
Definition EventPool.h:37
virtual void Reset()
Definition EventPool.h:47
virtual bool Wait(uint32 WaitTime, const bool bIgnoreThreadIdleStats=false)
Definition EventPool.h:52
virtual bool Create(bool bIsManualReset=false)
Definition EventPool.h:30
~FSafeRecyclableEvent()
Definition EventPool.h:25
FSafeRecyclableEvent(FEvent *InInnerEvent)
Definition EventPool.h:20
FEvent * InnerEvent
Definition EventPool.h:18
Definition EventPool.h:72
void EmptyPool()
Definition EventPool.h:112
void ReturnToPool(FEvent *Event)
Definition EventPool.h:98
FEvent * GetRawEvent()
Definition EventPool.h:127
~TEventPool()
Definition EventPool.h:75
void ReturnRawEvent(FEvent *Event)
Definition EventPool.h:154
FEvent * GetEventFromPool()
Definition EventPool.h:87
void Push(T *NewItem)
Definition LockFreeList.h:849
T * Pop()
Definition LockFreeList.h:858
Definition LockFreeList.h:904
static CORE_API class FEvent * CreateSynchEvent(bool bIsManualReset=false)
Definition GenericPlatformProcess.cpp:542