Fix various issues found with thread sanitizer on macOS

This commit is contained in:
Wojtek Figat
2024-04-13 13:20:17 +02:00
parent 8144db8e13
commit 9c2c02c1cf
10 changed files with 70 additions and 75 deletions

View File

@@ -11,13 +11,13 @@
void Task::Start()
{
if (_state != TaskState::Created)
if (GetState() != TaskState::Created)
return;
OnStart();
// Change state
_state = TaskState::Queued;
SetState(TaskState::Queued);
// Add task to the execution queue
Enqueue();
@@ -110,7 +110,6 @@ Task* Task::ContinueWith(const Function<bool()>& action, Object* target)
Task* Task::StartNew(Task* task)
{
ASSERT(task);
task->Start();
return task;
}
@@ -137,11 +136,10 @@ Task* Task::StartNew(Function<bool()>::Signature& action, Object* target)
void Task::Execute()
{
// Begin
if (IsCanceled())
return;
ASSERT(IsQueued());
_state = TaskState::Running;
SetState(TaskState::Running);
// Perform an operation
bool failed = Run();
@@ -149,7 +147,7 @@ void Task::Execute()
// Process result
if (IsCancelRequested())
{
_state = TaskState::Canceled;
SetState(TaskState::Canceled);
}
else if (failed)
{
@@ -167,10 +165,8 @@ void Task::OnStart()
void Task::OnFinish()
{
ASSERT(IsRunning());
ASSERT(!IsCancelRequested());
_state = TaskState::Finished;
ASSERT(IsRunning() && !IsCancelRequested());
SetState(TaskState::Finished);
// Send event further
if (_continueWith)
@@ -181,7 +177,7 @@ void Task::OnFinish()
void Task::OnFail()
{
_state = TaskState::Failed;
SetState(TaskState::Failed);
// Send event further
if (_continueWith)
@@ -209,8 +205,7 @@ void Task::OnCancel()
const auto state = GetState();
if (state != TaskState::Finished && state != TaskState::Failed)
{
_state = TaskState::Canceled;
SetState(TaskState::Canceled);
OnEnd();
}
}

View File

@@ -49,7 +49,6 @@ class FLAXENGINE_API Task : public Object, public NonCopyable
//
protected:
/// <summary>
/// The cancel flag used to indicate that there is request to cancel task operation.
/// </summary>
@@ -65,14 +64,18 @@ protected:
/// </summary>
Task* _continueWith = nullptr;
public:
void SetState(TaskState state)
{
Platform::AtomicStore((int64 volatile*)&_state, (uint64)state);
}
public:
/// <summary>
/// Gets the task state.
/// </summary>
FORCE_INLINE TaskState GetState() const
{
return static_cast<TaskState>(Platform::AtomicRead((int64 volatile*)&_state));
return (TaskState)Platform::AtomicRead((int64 const volatile*)&_state);
}
/// <summary>
@@ -94,7 +97,6 @@ public:
}
public:
/// <summary>
/// Checks if operation failed.
/// </summary>
@@ -153,7 +155,6 @@ public:
}
public:
/// <summary>
/// Starts this task execution (and will continue with all children).
/// </summary>
@@ -199,7 +200,6 @@ public:
}
public:
/// <summary>
/// Continues that task execution with a given task (will call Start on given task after finishing that one).
/// </summary>
@@ -232,7 +232,6 @@ public:
Task* ContinueWith(const Function<bool()>& action, Object* target = nullptr);
public:
/// <summary>
/// Starts the new task.
/// </summary>
@@ -312,7 +311,6 @@ public:
}
protected:
/// <summary>
/// Executes this task.
/// It should be called by the task consumer (thread pool or other executor of this task type).
@@ -328,7 +326,6 @@ protected:
virtual bool Run() = 0;
protected:
virtual void Enqueue() = 0;
virtual void OnStart();
virtual void OnFinish();