From 50d47fe80105fcff567412d733c6ce5369c2ed04 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 16 Oct 2023 16:10:57 +0200 Subject: [PATCH] Fix asset load to trigger loading task within mutex to prevent race conditions when loading the same prefab from many threads at once --- Source/Engine/Content/Asset.cpp | 6 +----- Source/Engine/Content/Content.cpp | 8 +++++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Source/Engine/Content/Asset.cpp b/Source/Engine/Content/Asset.cpp index 309a82e04..613c7c2c2 100644 --- a/Source/Engine/Content/Asset.cpp +++ b/Source/Engine/Content/Asset.cpp @@ -538,11 +538,7 @@ ContentLoadTask* Asset::createLoadingTask() void Asset::startLoading() { - // Check if is already loaded - if (IsLoaded()) - return; - - // Start loading (using async tasks) + ASSERT(!IsLoaded()); ASSERT(_loadingTask == nullptr); _loadingTask = createLoadingTask(); ASSERT(_loadingTask != nullptr); diff --git a/Source/Engine/Content/Content.cpp b/Source/Engine/Content/Content.cpp index 236c9400b..bd27abc5a 100644 --- a/Source/Engine/Content/Content.cpp +++ b/Source/Engine/Content/Content.cpp @@ -154,7 +154,7 @@ void ContentService::LateUpdate() // Unload marked assets for (int32 i = 0; i < ToUnload.Count(); i++) { - Asset* asset = ToUnload[i]; + Asset* asset = ToUnload[i]; // Check if has no references if (asset->GetReferencesCount() <= 0) @@ -965,7 +965,7 @@ Asset* Content::load(const Guid& id, const ScriptingTypeHandle& type, AssetInfo& // Get cached asset info (from registry) if (!GetAssetInfo(id, assetInfo)) { - LOG(Warning, "Invalid or missing asset ({0}, {1}).", id.ToString(Guid::FormatType::N), type.ToString()); + LOG(Warning, "Invalid or missing asset ({0}, {1}).", id, type.ToString()); return nullptr; } @@ -1009,11 +1009,13 @@ Asset* Content::load(const Guid& id, const ScriptingTypeHandle& type, AssetInfo& ASSERT(!Assets.ContainsKey(id)); #endif Assets.Add(id, result); - AssetsLocker.Unlock(); // Start asset loading + // TODO: refactor this to create asset loading task-chain before AssetsLocker.Lock() to allow better parallelization result->startLoading(); + AssetsLocker.Unlock(); + return result; }