diff --git a/Source/Engine/Audio/AudioClip.cpp b/Source/Engine/Audio/AudioClip.cpp index d04da7274..689f38d12 100644 --- a/Source/Engine/Audio/AudioClip.cpp +++ b/Source/Engine/Audio/AudioClip.cpp @@ -225,6 +225,7 @@ bool AudioClip::ExtractDataRaw(Array& resultData, AudioDataInfo& resultDat void AudioClip::CancelStreaming() { + Asset::CancelStreaming(); CancelStreamingTasks(); } diff --git a/Source/Engine/Content/Asset.cpp b/Source/Engine/Content/Asset.cpp index afe38f0a7..93d904d5e 100644 --- a/Source/Engine/Content/Asset.cpp +++ b/Source/Engine/Content/Asset.cpp @@ -522,6 +522,14 @@ void Asset::InitAsVirtual() void Asset::CancelStreaming() { + // Cancel loading task but go over asset locker to prevent case if other load threads still loads asset while it's reimported on other thread + Locker.Lock(); + ContentLoadTask* loadTask = _loadingTask; + Locker.Unlock(); + if (loadTask) + { + loadTask->Cancel(); + } } #if USE_EDITOR diff --git a/Source/Engine/Content/Assets/Model.cpp b/Source/Engine/Content/Assets/Model.cpp index 5a008645d..691b00a50 100644 --- a/Source/Engine/Content/Assets/Model.cpp +++ b/Source/Engine/Content/Assets/Model.cpp @@ -783,6 +783,7 @@ void Model::InitAsVirtual() void Model::CancelStreaming() { + Asset::CancelStreaming(); CancelStreamingTasks(); } diff --git a/Source/Engine/Content/Assets/SkinnedModel.cpp b/Source/Engine/Content/Assets/SkinnedModel.cpp index 5871087d9..b823db5a3 100644 --- a/Source/Engine/Content/Assets/SkinnedModel.cpp +++ b/Source/Engine/Content/Assets/SkinnedModel.cpp @@ -969,6 +969,7 @@ void SkinnedModel::InitAsVirtual() void SkinnedModel::CancelStreaming() { + Asset::CancelStreaming(); CancelStreamingTasks(); } diff --git a/Source/Engine/Content/Loading/Tasks/LoadAssetDataTask.h b/Source/Engine/Content/Loading/Tasks/LoadAssetDataTask.h index 23cf5f787..4a7bbb2bb 100644 --- a/Source/Engine/Content/Loading/Tasks/LoadAssetDataTask.h +++ b/Source/Engine/Content/Loading/Tasks/LoadAssetDataTask.h @@ -48,6 +48,8 @@ protected: // [ContentLoadTask] Result run() override { + if (IsCancelRequested()) + return Result::Ok; PROFILE_CPU(); AssetReference ref = _asset.Get(); @@ -67,8 +69,6 @@ protected: { if (IsCancelRequested()) return Result::Ok; - - // Load it #if TRACY_ENABLE ZoneScoped; ZoneName(*name, name.Length()); diff --git a/Source/Engine/Content/Storage/FlaxStorage.cpp b/Source/Engine/Content/Storage/FlaxStorage.cpp index d530e5456..a47e0bd0e 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.cpp +++ b/Source/Engine/Content/Storage/FlaxStorage.cpp @@ -1302,15 +1302,15 @@ void FlaxStorage::CloseFileHandles() // In those situations all the async tasks using this storage should be cancelled externally // Ensure that no one is using this resource - int32 waitTime = 10; + int32 waitTime = 100; while (Platform::AtomicRead(&_chunksLock) != 0 && waitTime-- > 0) - Platform::Sleep(10); + Platform::Sleep(1); if (Platform::AtomicRead(&_chunksLock) != 0) { // File can be locked by some streaming tasks (eg. AudioClip::StreamingTask or StreamModelLODTask) + Entry e; for (int32 i = 0; i < GetEntriesCount(); i++) { - Entry e; GetEntry(i, e); Asset* asset = Content::GetAsset(e.ID); if (asset) @@ -1320,8 +1320,12 @@ void FlaxStorage::CloseFileHandles() } } } + waitTime = 100; + while (Platform::AtomicRead(&_chunksLock) != 0 && waitTime-- > 0) + Platform::Sleep(1); ASSERT(_chunksLock == 0); + // Close file handles (from all threads) _file.DeleteAll(); } diff --git a/Source/Engine/Graphics/Textures/TextureBase.cpp b/Source/Engine/Graphics/Textures/TextureBase.cpp index 538b15a4a..181955fce 100644 --- a/Source/Engine/Graphics/Textures/TextureBase.cpp +++ b/Source/Engine/Graphics/Textures/TextureBase.cpp @@ -660,6 +660,7 @@ uint64 TextureBase::GetMemoryUsage() const void TextureBase::CancelStreaming() { + Asset::CancelStreaming(); _texture.CancelStreamingTasks(); }