Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Event.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"
6#include "Math/NumericLimits.h"
7#include "Misc/Timespan.h"
8#include "Templates/Atomic.h"
9#include "Templates/SharedPointer.h"
10
11/**
12 * Interface for waitable events.
13 *
14 * This interface has platform-specific implementations that are used to wait for another
15 * thread to signal that it is ready for the waiting thread to do some work. It can also
16 * be used for telling groups of threads to exit.
17 *
18 * Consider using FEventRef as a safer and more convenient alternative.
19 */
20class FEvent
21{
22public:
23
24 /**
25 * Creates the event.
26 *
27 * Manually reset events stay triggered until reset.
28 * Named events share the same underlying event.
29 *
30 * @param bIsManualReset Whether the event requires manual reseting or not.
31 * @return true if the event was created, false otherwise.
32 */
33 UE_DEPRECATED(5.0, "Direct creation of FEvent is discouraged for performance reasons. Please use FPlatformProcess::GetSynchEventFromPool/ReturnSynchEventToPool.")
34 virtual bool Create( bool bIsManualReset = false ) = 0;
35
36 /**
37 * Whether the signaled state of this event needs to be reset manually.
38 *
39 * @return true if the state requires manual resetting, false otherwise.
40 * @see Reset
41 */
42 virtual bool IsManualReset() = 0;
43
44 /**
45 * Triggers the event so any waiting threads are activated.
46 *
47 * @see IsManualReset, Reset
48 */
49 virtual void Trigger() = 0;
50
51 /**
52 * Resets the event to an untriggered (waitable) state.
53 *
54 * @see IsManualReset, Trigger
55 */
56 virtual void Reset() = 0;
57
58 /**
59 * Waits the specified amount of time for the event to be triggered.
60 *
61 * A wait time of MAX_uint32 is treated as infinite wait.
62 *
63 * @param WaitTime The time to wait (in milliseconds).
64 * @param bIgnoreThreadIdleStats If true, ignores ThreadIdleStats
65 * @return true if the event was triggered, false if the wait timed out.
66 */
67 virtual bool Wait( uint32 WaitTime, const bool bIgnoreThreadIdleStats = false ) = 0;
68
69 /**
70 * Waits an infinite amount of time for the event to be triggered.
71 *
72 * @return true if the event was triggered.
73 */
74 bool Wait()
75 {
76 return Wait(MAX_uint32);
77 }
78
79 /**
80 * Waits the specified amount of time for the event to be triggered.
81 *
82 * @param WaitTime The time to wait.
83 * @param bIgnoreThreadIdleStats If true, ignores ThreadIdleStats
84 * @return true if the event was triggered, false if the wait timed out.
85 */
86 bool Wait( const FTimespan& WaitTime, const bool bIgnoreThreadIdleStats = false )
87 {
88 check(WaitTime.GetTicks() >= 0);
89 return Wait((uint32)FMath::Clamp<int64>(WaitTime.GetTicks() / ETimespan::TicksPerMillisecond, 0, MAX_uint32), bIgnoreThreadIdleStats);
90 }
91
92 /** Default constructor. */
94 : EventId( 0 )
95 , EventStartCycles( 0 )
96 {}
97
98 /** Virtual destructor. */
99 virtual ~FEvent()
100 {}
101
102 // DO NOT MODIFY THESE
103
104 /** Advances stats associated with this event. Used to monitor wait->trigger history. */
106
107protected:
108 /** Sends to the stats a special messages which encodes a wait for the event. */
110
111 /** Send to the stats a special message which encodes a trigger for the event. */
113
114 /** Resets start cycles to 0. */
116
117 /** Counter used to generate an unique id for the events. */
118 static TAtomic<uint32> EventUniqueId;
119
120 /** An unique id of this event. */
121 uint32 EventId;
122
123 /** Greater than 0, if the event called wait. */
124 TAtomic<uint32> EventStartCycles;
125};
126
128
129/**
130 * RAII-style pooled `FEvent`
131 *
132 * non-copyable, non-movable
133 */
134class FEventRef final
135{
136public:
138
140
141 FEventRef(const FEventRef&) = delete;
142 FEventRef& operator=(const FEventRef&) = delete;
143 FEventRef(FEventRef&& Other) = delete;
144 FEventRef& operator=(FEventRef&& Other) = delete;
145
146 FEvent* operator->() const
147 {
148 return Event;
149 }
150
152 {
153 return Event;
154 }
155
156private:
158};
159
160/**
161 * RAII-style shared and pooled `FEvent`
162 */
163class FSharedEventRef final
164{
165public:
167
168 FEvent* operator->() const
169 {
170 return Ptr.Get();
171 }
172
173private:
175};
#define check(expr)
#define UE_DEPRECATED(Version, Message)
EEventMode
Definition Enums.h:12855
#define MAX_uint32
Definition Event.h:21
FEvent()
Definition Event.h:93
virtual bool Wait(uint32 WaitTime, const bool bIgnoreThreadIdleStats=false)=0
virtual void Trigger()=0
virtual bool Create(bool bIsManualReset=false)=0
TAtomic< uint32 > EventStartCycles
Definition Event.h:124
void WaitForStats()
virtual void Reset()=0
virtual ~FEvent()
Definition Event.h:99
bool Wait()
Definition Event.h:74
void TriggerForStats()
void ResetForStats()
static TAtomic< uint32 > EventUniqueId
Definition Event.h:118
uint32 EventId
Definition Event.h:121
virtual bool IsManualReset()=0
bool Wait(const FTimespan &WaitTime, const bool bIgnoreThreadIdleStats=false)
Definition Event.h:86
void AdvanceStats()
FEvent * operator->() const
Definition Event.h:146
FEventRef & operator=(const FEventRef &)=delete
FEventRef(FEventRef &&Other)=delete
FEvent * Get()
Definition Event.h:151
FEventRef(EEventMode Mode=EEventMode::AutoReset)
FEvent * Event
Definition Event.h:157
FEventRef & operator=(FEventRef &&Other)=delete
FEventRef(const FEventRef &)=delete
TSharedPtr< FEvent > Ptr
Definition Event.h:174
FEvent * operator->() const
Definition Event.h:168
FSharedEventRef(EEventMode Mode=EEventMode::AutoReset)
constexpr int64 TicksPerMillisecond
Definition Timespan.h:41
int64 GetTicks() const
Definition Timespan.h:454