Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
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
7#include "Misc/AssertionMacros.h"
8#include "HAL/Event.h"
9
10/**
11 * This is the PThreads version of FEvent.
12 */
14 : public FEvent
15{
16 // This is a little complicated, in an attempt to match Win32 Event semantics...
17 typedef enum
18 {
22 } TriggerType;
23
26 volatile TriggerType Triggered;
27 volatile int32 WaitingThreads;
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;
71 }
72
73 virtual ~FPThreadEvent()
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...
81 bIsManualReset = true;
83 Trigger(); // any waiting threads should start to wake up now.
84
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...
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.
96 pthread_mutex_destroy(&Mutex);
97 }
98 }
99
100 // FEvent interface
101
102 virtual bool Create( bool _bIsManualReset = false ) override
103 {
104 check(!bInitialized);
105 bool RetVal = false;
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
136
137 if (bIsManualReset)
138 {
139 // Release all waiting threads at once.
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).
149 int rc = pthread_cond_signal(&Condition); // may release multiple threads anyhow!
150 check(rc == 0);
151 }
152
154 }
155
156 virtual void Reset() override
157 {
159
160 check(bInitialized);
164 }
165
166 virtual bool Wait( uint32 WaitTime = (uint32)-1, const bool bIgnoreThreadIdleStats = false ) override;
167};
#define check(expr)
Definition Event.h:21
void TriggerForStats()
void ResetForStats()
virtual bool IsManualReset() override
volatile TriggerType Triggered
volatile int32 WaitingThreads
void LockEventMutex()
virtual bool Wait(uint32 WaitTime=(uint32) -1, const bool bIgnoreThreadIdleStats=false) override
pthread_cond_t Condition
virtual ~FPThreadEvent()
virtual void Reset() override
virtual bool Create(bool _bIsManualReset=false) override
void UnlockEventMutex()
pthread_mutex_t Mutex
virtual void Trigger() override