From 6f2bd0e932a67b542fca3e813f00c0d74b3748a4 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 15 May 2024 11:22:07 +0200 Subject: [PATCH] Another attempt on 6a883bc7c6d5396ef8061e018d8a2ee8e0fe589d --- Source/Engine/Content/Storage/FlaxStorage.cpp | 17 ++++++++++++----- Source/Engine/Content/Storage/FlaxStorage.h | 7 ++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Source/Engine/Content/Storage/FlaxStorage.cpp b/Source/Engine/Content/Storage/FlaxStorage.cpp index 74b114f52..49cd27d66 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.cpp +++ b/Source/Engine/Content/Storage/FlaxStorage.cpp @@ -218,10 +218,7 @@ const Char* TypeId2TypeName(const uint32 typeId) } FlaxStorage::FlaxStorage(const StringView& path) - : _refCount(0) - , _chunksLock(0) - , _version(0) - , _path(path) + : _path(path) { } @@ -242,6 +239,7 @@ FlaxStorage::~FlaxStorage() if (stream) Delete(stream); } + Platform::AtomicStore(&_files, 0); #endif } @@ -1327,6 +1325,7 @@ FileReadStream* FlaxStorage::OpenFile() LOG(Error, "Cannot open Flax Storage file \'{0}\'.", _path); return nullptr; } + Platform::InterlockedIncrement(&_files); // Create file reading stream stream = New(file); @@ -1336,6 +1335,13 @@ FileReadStream* FlaxStorage::OpenFile() bool FlaxStorage::CloseFileHandles() { + if (Platform::AtomicRead(&_chunksLock) == 0 && Platform::AtomicRead(&_files) == 0) + { + Array> streams; + _file.GetValues(streams); + ASSERT(streams.Count() == 0); + return false; + } PROFILE_CPU(); // Note: this is usually called by the content manager when this file is not used or on exit @@ -1367,7 +1373,7 @@ bool FlaxStorage::CloseFileHandles() return true; // Failed, someone is still accessing the file // Close file handles (from all threads) - Array streams; + Array> streams; _file.GetValues(streams); for (FileReadStream* stream : streams) { @@ -1375,6 +1381,7 @@ bool FlaxStorage::CloseFileHandles() Delete(stream); } _file.Clear(); + Platform::AtomicStore(&_files, 0); return false; } diff --git a/Source/Engine/Content/Storage/FlaxStorage.h b/Source/Engine/Content/Storage/FlaxStorage.h index 3b982c32e..865b3e656 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.h +++ b/Source/Engine/Content/Storage/FlaxStorage.h @@ -87,8 +87,9 @@ public: protected: // State - int64 _refCount; - int64 _chunksLock; + int64 _refCount = 0; + int64 _chunksLock = 0; + int64 _files = 0; double _lastRefLostTime; CriticalSection _loadLocker; @@ -97,7 +98,7 @@ protected: Array _chunks; // Metadata - uint32 _version; + uint32 _version = 0; String _path; protected: