Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
PlatformFileCommon.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 "HAL/ThreadSafeCounter.h"
7#include "Misc/ScopeLock.h"
8#include "HAL/DiskUtilizationTracker.h"
9#include "HAL/ThreadSafeCounter.h"
10#include "Misc/ScopeLock.h"
11#include "GenericPlatform/GenericPlatformFile.h"
12#include "HAL/PlatformProcess.h"
13
14#define MANAGE_FILE_HANDLES (#) // this is not longer used, this will error on any attempt to use it
15
17{
18public:
20 : NextLink(nullptr)
21 , PreviousLink(nullptr)
23 {
24 }
25
26private:
27 friend class FFileHandleRegistry;
28
32};
33
34
36{
37public:
38 FFileHandleRegistry(int32 InMaxOpenHandles)
39 : MaxOpenHandles(InMaxOpenHandles)
42 {
43 }
44
45 virtual ~FFileHandleRegistry() = default;
46
47 FRegisteredFileHandle* InitialOpenFile(const TCHAR* Filename)
48 {
50 {
52 }
53
55 if (Handle != nullptr)
56 {
58 LinkToTail(Handle);
59 }
60 else
61 {
63 }
64
65 return Handle;
66 }
67
69 {
70 bool bWasOpen = false;
71 {
74 {
75 UnLink(Handle);
76 bWasOpen = true;
77 }
78 }
79 if (bWasOpen)
80 {
83 }
84 }
85
87 {
88 {
90
92 {
93 UnLink(Handle);
94 return;
95 }
96 }
97
99 {
101 }
102 // can do this out of the lock, in case it's slow
104 }
105
107 {
108 {
110 LinkToTail(Handle);
111 }
112 }
113
114protected:
115 virtual FRegisteredFileHandle* PlatformInitialOpenFile(const TCHAR* Filename) = 0;
118
119private:
120
122 {
123 // do we need to make room for a file handle? this assumes that the critical section is already locked
125 {
126 FRegisteredFileHandle* ToBeClosed = nullptr;
127 {
129 ToBeClosed = PopFromHead();
130 }
131 if (ToBeClosed)
132 {
133 // close it, freeing up space for new file to open
134 PlatformCloseFile(ToBeClosed);
136 }
137 else
138 {
139 FPlatformMisc::LowLevelOutputDebugString(TEXT("Spinning because we are actively reading from more file handles than we have possible handles.\r\n"));
140 FPlatformProcess::SleepNoStats(.1f);
141 }
142 }
143 }
144
145
147 {
148 check(!Handle->PreviousLink && !Handle->NextLink && !Handle->bIsOpenAndAvailableForClosing);
151 {
153 check(!OpenAndAvailableForClosingTail->NextLink);
155 }
156 else
157 {
158 check(!OpenAndAvailableForClosingHead);
160 }
162 }
163
165 {
166 if (OpenAndAvailableForClosingHead == Handle)
167 {
168 verify(PopFromHead() == Handle);
169 return;
170 }
171 check(Handle->bIsOpenAndAvailableForClosing);
173 if (OpenAndAvailableForClosingTail == Handle)
174 {
175 check(OpenAndAvailableForClosingHead && OpenAndAvailableForClosingHead != Handle && Handle->PreviousLink);
178 Handle->NextLink = nullptr;
179 Handle->PreviousLink = nullptr;
180 return;
181 }
182 check(Handle->NextLink && Handle->PreviousLink);
185 Handle->NextLink = nullptr;
186 Handle->PreviousLink = nullptr;
187
188 }
189
191 {
193 if (Result)
194 {
195 check(!Result->PreviousLink);
196 check(Result->bIsOpenAndAvailableForClosing);
200 {
201 check(OpenAndAvailableForClosingTail == Result);
203 }
204 else
205 {
206 check(OpenAndAvailableForClosingHead->PreviousLink == Result);
208 }
209 Result->NextLink = nullptr;
210 Result->PreviousLink = nullptr;
211 }
212 return Result;
213 }
214
215 // critical section to protect the below arrays
217
219
222
224};
#define check(expr)
#define TEXT(x)
Definition Platform.h:1108
FWindowsCriticalSection FCriticalSection
void UnLink(FRegisteredFileHandle *Handle)
void LinkToTail(FRegisteredFileHandle *Handle)
virtual void PlatformCloseFile(FRegisteredFileHandle *)=0
virtual bool PlatformReopenFile(FRegisteredFileHandle *)=0
FCriticalSection LockSection
FRegisteredFileHandle * InitialOpenFile(const TCHAR *Filename)
FFileHandleRegistry(int32 InMaxOpenHandles)
FThreadSafeCounter HandlesCurrentlyInUse
virtual ~FFileHandleRegistry()=default
FRegisteredFileHandle * OpenAndAvailableForClosingTail
void UnTrackAndCloseFile(FRegisteredFileHandle *Handle)
virtual FRegisteredFileHandle * PlatformInitialOpenFile(const TCHAR *Filename)=0
FRegisteredFileHandle * PopFromHead()
void TrackEndRead(FRegisteredFileHandle *Handle)
void TrackStartRead(FRegisteredFileHandle *Handle)
FRegisteredFileHandle * OpenAndAvailableForClosingHead
FRegisteredFileHandle * PreviousLink
FRegisteredFileHandle * NextLink
UE_NODISCARD_CTOR FScopeLock(FCriticalSection *InSynchObject)
Definition ScopeLock.h:35
static void SleepNoStats(float Seconds)