[+] IFileStream::IsFlushOnClose

[+] IFileStream::SetFlushOnClose
[+] IFileStream::IsWriteEoSOnClose
[+] IFileStream::SetWriteEoSOnClose
[+] IIOHandle::IsFlushOnClose
[+] IIOHandle::SetFlushOnClose
This commit is contained in:
Reece Wilson 2024-01-02 00:00:47 +00:00
parent c16a0b1600
commit 63050b2262
12 changed files with 159 additions and 15 deletions

View File

@ -64,6 +64,30 @@ namespace Aurora::IO::FS
*/
virtual void MakeTemporary() = 0;
/**
* @brief
* @return
*/
virtual bool IsFlushOnClose() const = 0;
/**
* @brief
* @param bFlushOnClose
*/
virtual void SetFlushOnClose(bool bFlushOnClose) = 0;
/**
* @brief
* @return
*/
virtual bool IsWriteEoSOnClose() const = 0;
/**
* @brief
* @param bFlushOnClose
*/
virtual void SetWriteEoSOnClose(bool bShouldWriteEoS) = 0;
/**
* @brief Returns the IO handle backing the file stream, if available.
*/

View File

@ -131,6 +131,10 @@ namespace Aurora::IO
virtual AuString GetPath() const = 0;
virtual bool IsFlushOnClose() const = 0;
virtual void SetFlushOnClose(bool bFlushOnClose) = 0;
virtual bool IsFile() const = 0;
virtual bool IsTTY() const = 0;

View File

@ -35,7 +35,7 @@ namespace Aurora::Grug
static AuConditionVariable gCondVar(AuUnsafeRaiiToShared(gMutex.AsPointer())); // slow logger work queue
static AuSemaphore gArrows;
static AuMutex gOtherMutex;
static AuList<AuUInt> gHandlesToClose;
static AuList<AuPair<AuUInt, bool>> gHandlesToClose;
static AuList<AuThreadPrimitives::IEvent *> gEventsToTrigger;
static void SlowStartupTasks()
@ -210,13 +210,24 @@ namespace Aurora::Grug
toTrigger = AuMove(gEventsToTrigger);
}
for (const auto uHandle : toClose)
for (const auto [uHandle, bFlush] : toClose)
{
#if defined(AURORA_IS_MODERNNT_DERIVED)
auto pHandle = (void *)uHandle;
if (bFlush && pHandle)
{
if (AuIO::IsHandleFile(uHandle))
{
FlushFileBuffers(pHandle);
}
}
AuWin32CloseHandle(pHandle);
#elif defined(AURORA_IS_POSIX_DERIVED)
close(uHandle);
if (bFlush)
{
::fsync(uHandle);
}
::close(uHandle);
#endif
}
@ -234,12 +245,12 @@ namespace Aurora::Grug
GrugDoIoWork();
}
void CloseHandle(AuUInt64 handle)
void CloseHandle(AuUInt64 handle, bool bFlush)
{
{
AU_DEBUG_MEMCRUNCH;
AU_LOCK_GUARD(gOtherMutex);
gHandlesToClose.push_back(handle);
gHandlesToClose.push_back(AuMakePair(AuUInt(handle), bFlush));
}
NotifyGrugOfTelemetry();

View File

@ -24,6 +24,6 @@ namespace Aurora::Grug
void InitGrug();
void DeinitGrug();
void CloseHandle(AuUInt64 handle);
void CloseHandle(AuUInt64 handle, bool bFlush = false);
void WaitForGrugTick();
}

View File

@ -40,13 +40,13 @@ namespace Aurora::IO
return AuUInt64(hHandle);
}
void AFileHandle::CloseHandle(AuUInt64 uOSHandle)
void AFileHandle::CloseHandle(AuUInt64 uOSHandle, bool bFlushOnClose)
{
#if 0
HANDLE hHandle = (HANDLE)uOSHandle;
AuWin32CloseHandle(hHandle);
#else
Grug::CloseHandle(uOSHandle);
Grug::CloseHandle(uOSHandle, bFlushOnClose);
#endif
}

View File

@ -85,7 +85,7 @@ namespace Aurora::IO
return 1;
}
void AFileHandle::CloseHandle(AuUInt64 uOSHandle)
void AFileHandle::CloseHandle(AuUInt64 uOSHandle, bool bFlushOnClose)
{
int fd = (int)uOSHandle;
if (fd < 0)
@ -94,9 +94,13 @@ namespace Aurora::IO
}
#if 0
if (bFlushOnClose)
{
::fsync(uOSHandle);
}
::close(fd);
#else
Grug::CloseHandle(uOSHandle);
Grug::CloseHandle(uOSHandle, bFlushOnClose);
#endif
}
@ -181,7 +185,12 @@ namespace Aurora::IO
flags |= create.eMode == FS::EFileOpenMode::eRead ? O_RDONLY : (O_RDWR | O_CREAT);
flags |= O_CLOEXEC;
#if defined(O_DIRECT)
flags |= this->bDirectIO ? O_DIRECT : 0;
#else
// TBD
#endif
iFileDescriptor = ::open(pathex.c_str(),
flags,
@ -254,7 +263,7 @@ namespace Aurora::IO
return false;
}
}
switch (create.eMode)
{
case FS::EFileOpenMode::eRead:

View File

@ -196,6 +196,16 @@ namespace Aurora::IO
return bIsTTY;
}
bool AFileHandle::IsFlushOnClose() const
{
return this->bFlushOnClose;
}
void AFileHandle::SetFlushOnClose(bool bFlushOnClose)
{
this->bFlushOnClose = bFlushOnClose;
}
bool AFileHandle::IsPipe() const
{
bool bIsPipe {};

View File

@ -58,6 +58,9 @@ namespace Aurora::IO
bool IsTTY() const override;
bool IsPipe() const override;
bool IsFlushOnClose() const override;
void SetFlushOnClose(bool bFlushOnClose) override;
void InitStdIn(bool bSharing = false);
void InitStdOut(bool bError = false, bool bSharing = false);
@ -71,7 +74,7 @@ namespace Aurora::IO
AuOptionalEx<AuUInt64> uOSWriteHandle;
AuOptionalEx<AuUInt64> uOSReadHandle;
AuSPtr<IIOHandle> pThat;
bool bIsAsync {};
bool bIsAsync {}, bFlushOnClose {};
AuString path;
IPC::IPCPipeImpl *pIPCPipe {};
bool bDirectIO {};
@ -86,6 +89,11 @@ namespace Aurora::IO
static AuUInt64 DupHandle(AuUInt64 uOSHandle, bool bWriteAccess);
static AuUInt64 DupHandle(AuUInt64 uOSHandle, bool bWriteAccess, bool bShareAccess);
static void CloseHandle(AuUInt64 uOSHandle);
static void CloseHandle(AuUInt64 uOSHandle, bool bFlushOnClose);
inline void CloseHandle(AuUInt64 uOSHandle)
{
CloseHandle(uOSHandle, this->bFlushOnClose);
}
};
}

View File

@ -390,6 +390,11 @@ namespace Aurora::IO::FS
bool bDeleteFailed {};
AuString path;
if (this->bShouldWriteEoS)
{
this->WriteEoS();
}
if ((hHandle != INVALID_HANDLE_VALUE) &&
(this->bShouldDelete))
{
@ -434,6 +439,36 @@ namespace Aurora::IO::FS
this->bShouldDelete = true;
}
bool WinFileStream::IsFlushOnClose() const
{
if (auto pHandle = this->pHandle_)
{
return pHandle->IsFlushOnClose();
}
else
{
return false;
}
}
void WinFileStream::SetFlushOnClose(bool bFlushOnClose)
{
if (auto pHandle = this->pHandle_)
{
pHandle->SetFlushOnClose(bFlushOnClose);
}
}
bool WinFileStream::IsWriteEoSOnClose() const
{
return this->bShouldWriteEoS;
}
void WinFileStream::SetWriteEoSOnClose(bool bShouldWriteEoS)
{
this->bShouldWriteEoS = bShouldWriteEoS;
}
AuSPtr<IIOHandle> WinFileStream::GetHandle()
{
return this->pHandle_;

View File

@ -28,6 +28,10 @@ namespace Aurora::IO::FS
void WriteEoS() override;
void MakeTemporary() override;
AuSPtr<IIOHandle> GetHandle() override;
bool IsFlushOnClose() const override;
void SetFlushOnClose(bool bFlushOnClose) override;
bool IsWriteEoSOnClose() const override;
void SetWriteEoSOnClose(bool bFlushOnClose) override;
IO::IStreamReader *ToStreamReader() override;
IO::IStreamWriter *ToStreamWriter() override;
@ -35,7 +39,7 @@ namespace Aurora::IO::FS
private:
AuSPtr<IIOHandle> pHandle_;
bool bShouldDelete {};
bool bShouldDelete {}, bShouldWriteEoS {};
HANDLE hEventHandle_ { INVALID_HANDLE_VALUE };
AuFS::FileReader reader_;
AuFS::FileWriter writer_;

View File

@ -360,6 +360,11 @@ namespace Aurora::IO::FS
auto pHandle = this->pHandle_;
AuResetMember(this->pHandle_);
if (this->bShouldWriteEoS)
{
this->WriteEoS();
}
if (AuExchange(this->bMadeTemporary, false))
{
if (pHandle)
@ -369,6 +374,36 @@ namespace Aurora::IO::FS
}
}
bool PosixFileStream::IsFlushOnClose() const
{
if (auto pHandle = this->pHandle_)
{
return pHandle->IsFlushOnClose();
}
else
{
return false;
}
}
void PosixFileStream::SetFlushOnClose(bool bFlushOnClose)
{
if (auto pHandle = this->pHandle_)
{
pHandle->SetFlushOnClose(bFlushOnClose);
}
}
bool PosixFileStream::IsWriteEoSOnClose() const
{
return this->bShouldWriteEoS;
}
void PosixFileStream::SetWriteEoSOnClose(bool bShouldWriteEoS)
{
this->bShouldWriteEoS = bShouldWriteEoS;
}
void PosixFileStream::Flush()
{
if (!this->pHandle_)

View File

@ -33,6 +33,10 @@ namespace Aurora::IO::FS
void Flush() override;
void WriteEoS() override;
void MakeTemporary() override;
bool IsFlushOnClose() const override;
void SetFlushOnClose(bool bFlushOnClose) override;
bool IsWriteEoSOnClose() const override;
void SetWriteEoSOnClose(bool bFlushOnClose) override;
AuSPtr<IIOHandle> GetHandle() override;
IO::IStreamReader *ToStreamReader() override;
IO::IStreamWriter *ToStreamWriter() override;
@ -42,7 +46,7 @@ namespace Aurora::IO::FS
AuSPtr<IIOHandle> pHandle_;
AuString path_;
AuThreadPrimitives::SpinLock spinlock_;
bool bMadeTemporary {};
bool bMadeTemporary {}, bShouldWriteEoS {};
AuFS::FileReader reader_;
AuFS::FileWriter writer_;
};