Minor improvements

This commit is contained in:
Wojtek Figat
2024-04-22 18:10:58 +02:00
parent e795a8b037
commit 32b15f90ab
9 changed files with 36 additions and 91 deletions

View File

@@ -236,6 +236,7 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/GrammarAndSpelling/GrammarChecking/RulesStates/=LanguageTool_002EEN_002EE_005FG/@EntryIndexedValue">DisabledByUser</s:String>
<s:Boolean x:Key="/Default/PatternsAndTemplates/Todo/TodoPatterns/=EEA05B0ED8200E4BA9D2D3F1052EBFFD/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/PatternsAndTemplates/Todo/TodoPatterns/=EEA05B0ED8200E4BA9D2D3F1052EBFFD/Color/@EntryValue">Blue</s:String>
<s:Boolean x:Key="/Default/PatternsAndTemplates/Todo/TodoPatterns/=EEA05B0ED8200E4BA9D2D3F1052EBFFD/MatchComments/@EntryValue">True</s:Boolean>

View File

@@ -587,8 +587,8 @@ bool Asset::IsInternalType() const
bool Asset::onLoad(LoadAssetTask* task)
{
// It may fail when task is cancelled and new one is created later (don't crash but just end with an error)
if (task->Asset.Get() != this || Platform::AtomicRead(&_loadingTask) == 0)
// It may fail when task is cancelled and new one was created later (don't crash but just end with an error)
return true;
Locker.Lock();
@@ -601,7 +601,6 @@ bool Asset::onLoad(LoadAssetTask* task)
}
const bool isLoaded = result == LoadResult::Ok;
const bool failed = !isLoaded;
LoadState state = LoadState::Loaded;
Platform::AtomicStore(&_loadState, (int64)(isLoaded ? LoadState::Loaded : LoadState::LoadFailed));
if (failed)
{

View File

@@ -103,7 +103,7 @@ public:
public:
/// <summary>
/// Gets the path to the asset storage file. In Editor it reflects the actual file, in cooked Game, it fakes the Editor path to be informative for developers.
/// Gets the path to the asset storage file. In Editor, it reflects the actual file, in cooked Game, it fakes the Editor path to be informative for developers.
/// </summary>
API_PROPERTY() virtual const String& GetPath() const = 0;

View File

@@ -507,8 +507,7 @@ public:
/// </summary>
/// <param name="asset">The asset.</param>
InitAssetTask(BinaryAsset* asset)
: ContentLoadTask(Type::Custom)
, _asset(asset)
: _asset(asset)
, _dataLock(asset->Storage->Lock())
{
}
@@ -527,8 +526,6 @@ protected:
AssetReference<BinaryAsset> ref = _asset.Get();
if (ref == nullptr)
return Result::MissingReferences;
// Prepare
auto storage = ref->Storage;
auto factory = (BinaryAssetFactoryBase*)Content::GetAssetFactory(ref->GetTypeName());
ASSERT(factory);
@@ -548,7 +545,6 @@ protected:
_dataLock.Release();
_asset = nullptr;
// Base
ContentLoadTask::OnEnd();
}
};

View File

@@ -15,52 +15,11 @@ class ContentLoadTask : public Task
friend LoadingThread;
public:
/// <summary>
/// Describes work type
/// </summary>
DECLARE_ENUM_3(Type, Custom, LoadAsset, LoadAssetData);
/// <summary>
/// Describes work result value
/// </summary>
DECLARE_ENUM_5(Result, Ok, AssetLoadError, MissingReferences, LoadDataError, TaskFailed);
private:
/// <summary>
/// Task type
/// </summary>
Type _type;
protected:
/// <summary>
/// Initializes a new instance of the <see cref="ContentLoadTask"/> class.
/// </summary>
/// <param name="type">The task type.</param>
ContentLoadTask(const Type type)
: _type(type)
{
}
public:
/// <summary>
/// Gets a task type.
/// </summary>
FORCE_INLINE Type GetType() const
{
return _type;
}
public:
/// <summary>
/// Checks if async task is loading given asset resource
/// </summary>
/// <param name="asset">Target asset to check</param>
/// <returns>True if is loading that asset, otherwise false</returns>
bool IsLoading(Asset* asset) const
{
return _type == Type::LoadAsset && HasReference((Object*)asset);
}
protected:
virtual Result run() = 0;

View File

@@ -4,6 +4,7 @@
#include "ContentLoadTask.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Math/Math.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Platform/CPUInfo.h"
#include "Engine/Platform/Thread.h"
#include "Engine/Platform/ConditionVariable.h"
@@ -212,7 +213,7 @@ void ContentLoadingManagerService::Dispose()
String ContentLoadTask::ToString() const
{
return String::Format(TEXT("Content Load Task {0} ({1})"), ToString(GetType()), (int32)GetState());
return String::Format(TEXT("Content Load Task ({})"), (int32)GetState());
}
void ContentLoadTask::Enqueue()

View File

@@ -15,28 +15,24 @@
class LoadAssetDataTask : public ContentLoadTask
{
private:
WeakAssetReference<BinaryAsset> _asset; // Don't keep ref to the asset (so it can be unloaded if none using it, task will fail then)
AssetChunksFlag _chunks;
FlaxStorage::LockData _dataLock;
public:
/// <summary>
/// Initializes a new instance of the <see cref="LoadAssetDataTask"/> class.
/// </summary>
/// <param name="asset">The asset to load.</param>
/// <param name="chunks">The chunks to load.</param>
LoadAssetDataTask(BinaryAsset* asset, AssetChunksFlag chunks)
: ContentLoadTask(Type::LoadAssetData)
, _asset(asset)
: _asset(asset)
, _chunks(chunks)
, _dataLock(asset->Storage->Lock())
{
}
public:
// [ContentLoadTask]
bool HasReference(Object* obj) const override
{
@@ -44,7 +40,6 @@ public:
}
protected:
// [ContentLoadTask]
Result run() override
{

View File

@@ -15,38 +15,35 @@
class LoadAssetTask : public ContentLoadTask
{
public:
/// <summary>
/// Initializes a new instance of the <see cref="LoadAssetTask"/> class.
/// </summary>
/// <param name="asset">The asset to load.</param>
LoadAssetTask(Asset* asset)
: ContentLoadTask(Type::LoadAsset)
, Asset(asset)
: Asset(asset)
{
}
~LoadAssetTask()
{
if (Asset)
auto asset = Asset.Get();
if (asset)
{
Asset->Locker.Lock();
if (Platform::AtomicRead(&Asset->_loadingTask) == (intptr)this)
asset->Locker.Lock();
if (Platform::AtomicRead(&asset->_loadingTask) == (intptr)this)
{
Platform::AtomicStore(&Asset->_loadState, (int64)Asset::LoadState::LoadFailed);
Platform::AtomicStore(&Asset->_loadingTask, 0);
Platform::AtomicStore(&asset->_loadState, (int64)Asset::LoadState::LoadFailed);
Platform::AtomicStore(&asset->_loadingTask, 0);
LOG(Error, "Loading asset \'{0}\' result: {1}.", ToString(), ToString(Result::TaskFailed));
}
Asset->Locker.Unlock();
asset->Locker.Unlock();
}
}
public:
WeakAssetReference<Asset> Asset;
public:
// [ContentLoadTask]
bool HasReference(Object* obj) const override
{
@@ -54,7 +51,6 @@ public:
}
protected:
// [ContentLoadTask]
Result run() override
{
@@ -68,32 +64,36 @@ protected:
// Call loading
if (ref->onLoad(this))
return Result::AssetLoadError;
return Result::Ok;
}
void OnFail() override
{
if (Asset)
auto asset = Asset.Get();
if (asset)
{
Asset->Locker.Lock();
if (Platform::AtomicRead(&Asset->_loadingTask) == (intptr)this)
Platform::AtomicStore(&Asset->_loadingTask, 0);
Asset->Locker.Unlock();
Asset = nullptr;
asset->Locker.Lock();
if (Platform::AtomicRead(&asset->_loadingTask) == (intptr)this)
Platform::AtomicStore(&asset->_loadingTask, 0);
asset->Locker.Unlock();
}
// Base
ContentLoadTask::OnFail();
}
void OnEnd() override
{
if (Asset)
auto asset = Asset.Get();
if (asset)
{
Asset->Locker.Lock();
if (Platform::AtomicRead(&Asset->_loadingTask) == (intptr)this)
Platform::AtomicStore(&Asset->_loadingTask, 0);
Asset->Locker.Unlock();
Asset = nullptr;
asset->Locker.Lock();
if (Platform::AtomicRead(&asset->_loadingTask) == (intptr)this)
Platform::AtomicStore(&asset->_loadingTask, 0);
asset->Locker.Unlock();
asset = nullptr;
}
// Base

View File

@@ -7,7 +7,6 @@
#include "Engine/Core/NonCopyable.h"
#include "Engine/Core/Enums.h"
#include "Engine/Core/Types/TimeSpan.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Platform/Platform.h"
/// <summary>
@@ -188,8 +187,8 @@ public:
/// <param name="tasks">The tasks list to wait for.</param>
/// <param name="timeoutMilliseconds">The maximum amount of milliseconds to wait for the task to finish it's job. Timeout smaller/equal 0 will result in infinite waiting.</param>
/// <returns>True if any task failed or has been canceled or has timeout, otherwise false.</returns>
template<class T = Task>
static bool WaitAll(Array<T*>& tasks, double timeoutMilliseconds = -1)
template<class T = Task, typename AllocationType = HeapAllocation>
static bool WaitAll(Array<T*, AllocationType>& tasks, double timeoutMilliseconds = -1)
{
for (int32 i = 0; i < tasks.Count(); i++)
{
@@ -300,27 +299,22 @@ public:
/// <summary>
/// Cancels all the tasks from the list and clears it.
/// </summary>
template<class T = Task>
static void CancelAll(Array<T*>& tasks)
template<class T = Task, typename AllocationType = HeapAllocation>
static void CancelAll(Array<T*, AllocationType>& tasks)
{
for (int32 i = 0; i < tasks.Count(); i++)
{
tasks[i]->Cancel();
}
tasks.Clear();
}
protected:
/// <summary>
/// Executes this task.
/// It should be called by the task consumer (thread pool or other executor of this task type).
/// It calls run() and handles result).
/// Executes this task. It should be called by the task consumer (thread pool or other executor of this task type). It calls run() and handles result).
/// </summary>
void Execute();
/// <summary>
/// Runs the task specified operations
/// Does not handles any task related logic, only performs the actual job.
/// Runs the task specified operations. It does not handle any task related logic, but only performs the actual job.
/// </summary>
/// <returns>The task execution result. Returns true if failed, otherwise false.</returns>
virtual bool Run() = 0;