[*] Improved IMemoryMappedFile API

[+] AuIO::Adapters::NewAsyncTransactionFromStreamSeekingPair
This commit is contained in:
Reece Wilson 2024-03-09 00:20:26 +00:00
parent afbe942ffd
commit 66f3250983
5 changed files with 136 additions and 21 deletions

View File

@ -13,4 +13,7 @@ namespace Aurora::IO::Adapters
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamSeekingReader(const AuSPtr<ISeekingReader> &pStreamReader, AuOptional<Aurora::Async::WorkerPId_t> workers);
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamWriter(const AuSPtr<IStreamWriter> &pStreamWriter, AuOptional<Aurora::Async::WorkerPId_t> workers);
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamSeekingWriter(const AuSPtr<ISeekingWriter> &pStreamWriter, AuOptional<Aurora::Async::WorkerPId_t> workers);
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamSeekingPair(const AuSPtr<ISeekingReader> &pStreamReader,
const AuSPtr<ISeekingWriter> &pStreamWriter,
AuOptional<Aurora::Async::WorkerPId_t> workers);
}

View File

@ -11,6 +11,7 @@ namespace Aurora::IO::FS
{
struct IMemoryMappedFile
{
virtual IAsyncFileStream * ToAsyncFile() = 0;
virtual IIOHandle * ToHandle() = 0;
virtual AuUInt GetBaseAddress() = 0;
@ -22,6 +23,7 @@ namespace Aurora::IO::FS
virtual AuSPtr<IAsyncTransaction> NewAsyncReadTransaction() = 0;
virtual AuSPtr<IAsyncTransaction> NewAsyncWriteTransaction() = 0;
virtual AuSPtr<IAsyncTransaction> NewAsyncAnyTransaction() = 0;
virtual ISeekingReader * ToStreamSeekingReader() = 0;
virtual ISeekingWriter * ToStreamSeekingWriter() = 0;
@ -32,5 +34,6 @@ namespace Aurora::IO::FS
virtual void Prefetch(AuUInt uOffset,
AuUInt uLength) = 0;
AURT_ADD_USR_DATA_EXP(this->ToAsyncFile());
};
}

View File

@ -253,4 +253,18 @@ namespace Aurora::IO::Adapters
pObject->workers = workers.ValueOr(GetAuxWorkerPool());
return pObject;
}
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamSeekingPair(const AuSPtr<ISeekingReader> &pStreamReader,
const AuSPtr<ISeekingWriter> &pStreamWriter,
AuOptional<Aurora::Async::WorkerPId_t> workers)
{
SysCheckArgNotNull(pStreamWriter, {});
SysCheckArgNotNull(pStreamReader, {});
auto pObject = AuMakeShared<AsyncReaderWriter>();
SysCheckNotNullMemory(pObject, {});
pObject->pStreamReaderEx = pStreamReader;
pObject->pStreamWriterEx = pStreamWriter;
pObject->workers = workers.ValueOr(GetAuxWorkerPool());
return pObject;
}
}

View File

@ -58,34 +58,85 @@ namespace Aurora::IO::FS
return AuMakeShared<IO::Buffered::ViewWriter>(this->ToWriteView(), this->pView);
}
AuSPtr<IAsyncTransaction> MemoryMappedFile::NewAsyncReadTransaction()
void MemoryMappedFile::InitTransactions()
{
if (AuThreading::InitOnceLocker::TryLock(&this->initOnce))
{
bool bOk { true };
if (this->eMode == EFileOpenMode::eReadWrite ||
this->eMode == EFileOpenMode::eRead)
{
auto pReader = AuMakeShared<IO::Buffered::ViewSeekableReader>(this->ToReadViewAlt(), this->pView);
SysCheckNotNullMemory(pReader, {});
if (pReader)
{
auto iBaseOffset = 0 -
AuInt64(this->uFileMapOffset) +
this->iAdjustStreamOffset;
pReader->GetBaseOffset() = iBaseOffset;
}
else
{
bOk = false;
}
auto pTransaction = Adapters::NewAsyncTransactionFromStreamSeekingReader(pReader, {});
this->pStreamReader = pReader;
}
if (this->eMode == EFileOpenMode::eReadWrite ||
this->eMode == EFileOpenMode::eWrite)
{
auto pWriter = AuMakeShared<IO::Buffered::ViewSeekableWriter>(this->ToWriteViewAlt(), this->pView);
if (pWriter)
{
auto iBaseOffset = 0 -
AuInt64(this->uFileMapOffset) +
this->iAdjustStreamOffset;
pWriter->GetBaseOffset() = iBaseOffset;
}
else
{
bOk = false;
}
this->pStreamWriter = pWriter;
}
AuThreading::InitOnceLocker::Finish(&this->initOnce, !bOk);
}
else
{
this->initOnce.Wait();
}
}
AuSPtr<IAsyncTransaction> MemoryMappedFile::NewAsyncReadTransaction()
{
this->InitTransactions();
SysCheckNotNullMemory(this->pStreamReader, {});
auto pTransaction = Adapters::NewAsyncTransactionFromStreamSeekingReader(this->pStreamReader, {});
SysCheckNotNullMemory(pTransaction, {});
return pTransaction;
}
AuSPtr<IAsyncTransaction> MemoryMappedFile::NewAsyncWriteTransaction()
{
auto pWriter = AuMakeShared<IO::Buffered::ViewSeekableWriter>(this->ToWriteViewAlt(), this->pView);
SysCheckNotNullMemory(pWriter, {});
this->InitTransactions();
SysCheckNotNullMemory(this->pStreamWriter, {});
auto pTransaction = Adapters::NewAsyncTransactionFromStreamSeekingWriter(this->pStreamWriter, {});
SysCheckNotNullMemory(pTransaction, {});
return pTransaction;
}
auto iBaseOffset = 0 -
AuInt64(this->uFileMapOffset) +
this->iAdjustStreamOffset;
pWriter->GetBaseOffset() = iBaseOffset;
auto pTransaction = Adapters::NewAsyncTransactionFromStreamSeekingWriter(pWriter, {});
AuSPtr<IAsyncTransaction> MemoryMappedFile::NewAsyncAnyTransaction()
{
this->InitTransactions();
SysCheckNotNullMemory(this->pStreamWriter, {});
SysCheckNotNullMemory(this->pStreamReader, {});
auto pTransaction = Adapters::NewAsyncTransactionFromStreamSeekingPair(this->pStreamReader, this->pStreamWriter, {});
SysCheckNotNullMemory(pTransaction, {});
return pTransaction;
}
@ -200,6 +251,35 @@ namespace Aurora::IO::FS
});
}
IAsyncFileStream *MemoryMappedFile::ToAsyncFile()
{
return this;
}
AuSPtr<IAsyncTransaction> MemoryMappedFile::NewTransaction()
{
return this->NewAsyncAnyTransaction();
}
bool MemoryMappedFile::BlockingTruncate(AuUInt64 length)
{
return false;
}
bool MemoryMappedFile::BlockingRead(AuUInt64 offset, const Memory::MemoryViewStreamWrite &parameters)
{
this->InitTransactions();
SysCheckNotNullMemory(this->pStreamReader, {});
return this->pStreamReader->ArbitraryRead(offset, parameters) == EStreamError::eErrorNone;
}
bool MemoryMappedFile::BlockingWrite(AuUInt64 offset, const Memory::MemoryViewStreamRead &parameters)
{
this->InitTransactions();
SysCheckNotNullMemory(this->pStreamWriter, {});
return this->pStreamWriter->ArbitraryWrite(offset, parameters) == EStreamError::eErrorNone;
}
bool MemoryMappedFile::Init(EFileOpenMode eMode,
AuUInt64 uFileMapOffset,
AuUInt64 uFileMapLength,
@ -213,7 +293,7 @@ namespace Aurora::IO::FS
uFileMapLength = pHandle->GetFileLength();
}
this->eMode = eMode;
this->uFileMapLength = uFileMapLength;
this->uFileMapOffset = uFileMapOffset;
this->iAdjustStreamOffset = iAdjustStreamOffset;

View File

@ -9,9 +9,10 @@
namespace Aurora::IO::FS
{
struct MemoryMappedFile : IMemoryMappedFile
struct MemoryMappedFile : IMemoryMappedFile, IAsyncFileStream
{
virtual IIOHandle * ToHandle() override;
virtual IAsyncFileStream * ToAsyncFile() override;
virtual AuUInt GetBaseAddress() override;
virtual AuUInt GetEndAddress() override;
@ -20,8 +21,11 @@ namespace Aurora::IO::FS
virtual AuSPtr<IStreamReader> NewStreamReader() override;
virtual AuSPtr<IStreamWriter> NewStreamWriter() override;
void InitTransactions();
virtual AuSPtr<IAsyncTransaction> NewAsyncReadTransaction() override;
virtual AuSPtr<IAsyncTransaction> NewAsyncWriteTransaction() override;
virtual AuSPtr<IAsyncTransaction> NewAsyncAnyTransaction() override;
virtual ISeekingReader * ToStreamSeekingReader() override;
virtual ISeekingWriter * ToStreamSeekingWriter() override;
@ -36,6 +40,13 @@ namespace Aurora::IO::FS
virtual void Prefetch(AuUInt uOffset,
AuUInt uLength) override;
AuSPtr<IAsyncTransaction> NewTransaction() override;
bool BlockingTruncate(AuUInt64 length) override;
bool BlockingRead(AuUInt64 offset, const Memory::MemoryViewStreamWrite &parameters) override;
bool BlockingWrite(AuUInt64 offset, const Memory::MemoryViewStreamRead &parameters) override;
bool Init(EFileOpenMode eMode,
AuUInt64 uFileMapOffset,
AuUInt64 uFileMapLength,
@ -46,9 +57,13 @@ namespace Aurora::IO::FS
AuSPtr<AuIO::IIOHandle> pHandleShared;
AuSPtr<AuProcess::IProcessSectionMapView> pView;
AuUInt64 uFileMapLength;
EFileOpenMode eMode;
AuUInt64 uFileMapOffset;
AuInt64 iAdjustStreamOffset;
AuOptional<IO::Buffered::ViewSeekableWriter> optSeekableWriter;
AuOptional<IO::Buffered::ViewSeekableReader> optSeekableReader;
AuInitOnce initOnce;
AuSPtr<IO::Buffered::ViewSeekableReader> pStreamReader;
AuSPtr<IO::Buffered::ViewSeekableWriter> pStreamWriter;
};
}