UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PThreadEvent.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5// HEADER_UNIT_UNSUPPORTED - Unsupported platform
6
8#include "HAL/Event.h"
9
14 : public FEvent
15{
16 // This is a little complicated, in an attempt to match Win32 Event semantics...
17 typedef enum
18 {
19 TRIGGERED_NONE,
20 TRIGGERED_ONE,
21 TRIGGERED_ALL,
22 } TriggerType;
23
24 bool bInitialized;
25 bool bIsManualReset;
26 volatile TriggerType Triggered;
27 volatile int32 WaitingThreads;
28 pthread_mutex_t Mutex;
29 pthread_cond_t Condition;
30
31 inline void LockEventMutex()
32 {
33 const int rc = pthread_mutex_lock(&Mutex);
34 check(rc == 0);
35 }
36
37 inline void UnlockEventMutex()
38 {
39 const int rc = pthread_mutex_unlock(&Mutex);
40 check(rc == 0);
41 }
42
43 static inline void SubtractTimevals( const struct timeval *FromThis, struct timeval *SubThis, struct timeval *Difference )
44 {
45 if (FromThis->tv_usec < SubThis->tv_usec)
46 {
47 int nsec = (SubThis->tv_usec - FromThis->tv_usec) / 1000000 + 1;
48 SubThis->tv_usec -= 1000000 * nsec;
49 SubThis->tv_sec += nsec;
50 }
51
52 if (FromThis->tv_usec - SubThis->tv_usec > 1000000)
53 {
54 int nsec = (FromThis->tv_usec - SubThis->tv_usec) / 1000000;
55 SubThis->tv_usec += 1000000 * nsec;
56 SubThis->tv_sec -= nsec;
57 }
58
59 Difference->tv_sec = FromThis->tv_sec - SubThis->tv_sec;
60 Difference->tv_usec = FromThis->tv_usec - SubThis->tv_usec;
61 }
62
63public:
64
66 {
67 bInitialized = false;
68 bIsManualReset = false;
69 Triggered = TRIGGERED_NONE;
70 WaitingThreads = 0;
71 }
72
74 {
75 // Safely destructing an Event is VERY delicate, so it can handle badly-designed
76 // calling code that might still be waiting on the event.
77 if (bInitialized)
78 {
79 // try to flush out any waiting threads...
80 LockEventMutex();
81 bIsManualReset = true;
82 UnlockEventMutex();
83 Trigger(); // any waiting threads should start to wake up now.
84
85 LockEventMutex();
86 bInitialized = false; // further incoming calls to this object will now crash in check().
87 while (WaitingThreads) // spin while waiting threads wake up.
88 {
89 UnlockEventMutex(); // cycle through waiting threads...
90 LockEventMutex();
91 }
92 // No threads are currently waiting on Condition and we hold the Mutex. Kill it.
93 pthread_cond_destroy(&Condition);
94 // Unlock and kill the mutex, since nothing else can grab it now.
95 UnlockEventMutex();
97 }
98 }
99
100 // FEvent interface
101
102 virtual bool Create( bool _bIsManualReset = false ) override
103 {
104 check(!bInitialized);
105 bool RetVal = false;
106 Triggered = TRIGGERED_NONE;
107 bIsManualReset = _bIsManualReset;
108
109 if (pthread_mutex_init(&Mutex, nullptr) == 0)
110 {
111 if (pthread_cond_init(&Condition, nullptr) == 0)
112 {
113 bInitialized = true;
114 RetVal = true;
115 }
116 else
117 {
118 pthread_mutex_destroy(&Mutex);
119 }
120 }
121 return RetVal;
122 }
123
124 virtual bool IsManualReset() override
125 {
126 return bIsManualReset;
127 }
128
129 virtual void Trigger() override
130 {
132
133 check(bInitialized);
134
135 LockEventMutex();
136
137 if (bIsManualReset)
138 {
139 // Release all waiting threads at once.
140 Triggered = TRIGGERED_ALL;
141 int rc = pthread_cond_broadcast(&Condition);
142 check(rc == 0);
143 }
144 else
145 {
146 // Release one or more waiting threads (first one to get the mutex
147 // will reset Triggered, rest will go back to waiting again).
148 Triggered = TRIGGERED_ONE;
149 int rc = pthread_cond_signal(&Condition); // may release multiple threads anyhow!
150 check(rc == 0);
151 }
152
153 UnlockEventMutex();
154 }
155
156 virtual void Reset() override
157 {
159
160 check(bInitialized);
161 LockEventMutex();
162 Triggered = TRIGGERED_NONE;
163 UnlockEventMutex();
164 }
165
166 virtual bool Wait( uint32 WaitTime = (uint32)-1, const bool bIgnoreThreadIdleStats = false ) override;
167};
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Event.h:21
CORE_API void TriggerForStats()
Definition ThreadingBase.cpp:744
CORE_API void ResetForStats()
Definition ThreadingBase.cpp:761
Definition PThreadEvent.h:15
virtual bool IsManualReset() override
Definition PThreadEvent.h:124
virtual bool Wait(uint32 WaitTime=(uint32) -1, const bool bIgnoreThreadIdleStats=false) override
FPThreadEvent()
Definition PThreadEvent.h:65
virtual ~FPThreadEvent()
Definition PThreadEvent.h:73
virtual void Reset() override
Definition PThreadEvent.h:156
virtual bool Create(bool _bIsManualReset=false) override
Definition PThreadEvent.h:102
virtual void Trigger() override
Definition PThreadEvent.h:129