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

View File

@ -88,17 +88,43 @@ namespace Aurora::IO
bool ShouldClone(); bool ShouldClone();
AuOptional<AuUInt64> uOSWriteHandle; std::shared_ptr<IIOHandle> pThat;
AuOptional<AuUInt64> uOSReadHandle; AuString path;
AuSPtr<IIOHandle> pThat; IPC::IPCPipeImpl * pIPCPipe { };
AuString path; mutable AuSPtr<AuString> pIPCString;
IPC::IPCPipeImpl *pIPCPipe {}; AuUInt uThreadId { };
mutable AuOptional<bool> optIsFile; AuOptional<AuUInt64> uOSWriteHandle;
mutable AuOptional<bool> optIsPipe; AuOptional<AuUInt64> uOSReadHandle;
mutable AuOptional<bool> optIsTTY; mutable AuOptional<bool> optIsFile;
mutable AuSPtr<AuString> pIPCString; mutable AuOptional<bool> optIsPipe;
AuUInt uThreadId {}; mutable AuOptional<bool> optIsTTY;
bool bIsAsync {}, bFlushOnClose {}, bShouldWriteEoS {}, bDirectIO {}; 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: protected:
// Implement me: // Implement me: