[*] Harden IO handle interface

This commit is contained in:
Reece Wilson 2024-03-31 09:26:56 +01:00
parent 263ca6e646
commit 5668188945
2 changed files with 54 additions and 12 deletions

View File

@ -61,6 +61,8 @@ namespace Aurora::IO
{
SysCheckArgNotNull(pHandle, false);
AU_LOCK_GUARD(this);
auto pSrc = AuStaticCast<AFileHandle>(pHandle);
auto pDest = this;
@ -79,6 +81,8 @@ namespace Aurora::IO
bool AFileHandle::InitFromCopy(AuUInt64 uOSHandle)
{
AU_LOCK_GUARD(this);
if (this->IsValid())
{
return false;
@ -99,6 +103,8 @@ namespace Aurora::IO
bool AFileHandle::InitFromMove(AuUInt64 uOSHandle)
{
AU_LOCK_GUARD(this);
if (this->IsValid())
{
return false;
@ -113,6 +119,8 @@ namespace Aurora::IO
bool AFileHandle::InitFromPair(AuOptional<AuUInt64> optOSReadHandle,
AuOptional<AuUInt64> optOSWriteHandle)
{
AU_LOCK_GUARD(this);
if (this->IsValid())
{
return false;
@ -152,6 +160,8 @@ namespace Aurora::IO
bool AFileHandle::InitFromPairMove(AuOptional<AuUInt64> uOSReadHandle,
AuOptional<AuUInt64> uOSWriteHandle)
{
AU_LOCK_GUARD(this);
if (this->IsValid())
{
return false;
@ -166,6 +176,8 @@ namespace Aurora::IO
bool AFileHandle::InitFromStreamEnum(EStandardStream eStream)
{
AU_LOCK_GUARD(this);
switch (eStream)
{
case EStandardStream::eInputStream:
@ -349,6 +361,8 @@ namespace Aurora::IO
bool AFileHandle::InitFromSharing(const AuString &handle)
{
AU_LOCK_GUARD(this);
if (this->uOSReadHandle ||
this->uOSWriteHandle)
{
@ -393,6 +407,8 @@ namespace Aurora::IO
AuString AFileHandle::SharingGetString()
{
AU_LOCK_GUARD(this);
AuString handle;
if (this->pIPCString)

View File

@ -88,17 +88,43 @@ namespace Aurora::IO
bool ShouldClone();
AuOptional<AuUInt64> uOSWriteHandle;
AuOptional<AuUInt64> uOSReadHandle;
AuSPtr<IIOHandle> pThat;
AuString path;
IPC::IPCPipeImpl *pIPCPipe {};
mutable AuOptional<bool> optIsFile;
mutable AuOptional<bool> optIsPipe;
mutable AuOptional<bool> optIsTTY;
mutable AuSPtr<AuString> pIPCString;
AuUInt uThreadId {};
bool bIsAsync {}, bFlushOnClose {}, bShouldWriteEoS {}, bDirectIO {};
std::shared_ptr<IIOHandle> pThat;
AuString path;
IPC::IPCPipeImpl * pIPCPipe { };
mutable AuSPtr<AuString> pIPCString;
AuUInt uThreadId { };
AuOptional<AuUInt64> uOSWriteHandle;
AuOptional<AuUInt64> uOSReadHandle;
mutable AuOptional<bool> optIsFile;
mutable AuOptional<bool> optIsPipe;
mutable AuOptional<bool> optIsTTY;
AuUInt8 bShouldWriteEoS : 1 { 0 };
AuUInt8 bFlushOnClose : 1 { 0 };
AuUInt8 bDirectIO : 1 { 0 };
AuUInt8 bIsAsync : 1 { 0 };
AuAUInt32 uLock { 0 };
inline void Lock()
{
if (!AuAtomicTestAndSet(&this->uLock, 0))
{
return;
}
{
AuUInt32 uOld {};
while ((uOld = AuAtomicCompareExchange(&this->uLock, 1u, 0u)) != 0u)
{
AuThreading::WaitOnAddress((const void *)&this->uLock, &uOld, 4, 0, true);
}
}
}
inline void Unlock()
{
AuAtomicClearU8Lock(&this->uLock);
AuThreading::WakeOnAddress((const void *)&this->uLock);
}
protected:
// Implement me: