From 1e262b69cc6efd0235c6e8f28592f07b6e0835f6 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 3 Jan 2025 01:05:56 +0100 Subject: [PATCH] Add `Task::WaitAll` with a span of tasks and wrap around profiler macro --- Source/Engine/Threading/Task.cpp | 12 ++++++++++++ Source/Engine/Threading/Task.h | 17 ++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Threading/Task.cpp b/Source/Engine/Threading/Task.cpp index a0b3ddb6d..a126bdef4 100644 --- a/Source/Engine/Threading/Task.cpp +++ b/Source/Engine/Threading/Task.cpp @@ -7,6 +7,7 @@ #include "Engine/Core/Types/DateTime.h" #include "Engine/Core/Collections/Array.h" #include "Engine/Core/Math/Math.h" +#include "Engine/Core/Types/Span.h" #include "Engine/Profiler/ProfilerCPU.h" void Task::Start() @@ -72,6 +73,17 @@ bool Task::Wait(double timeoutMilliseconds) const return true; } +bool Task::WaitAll(const Span& tasks, double timeoutMilliseconds) +{ + PROFILE_CPU(); + for (int32 i = 0; i < tasks.Length(); i++) + { + if (tasks[i]->Wait()) + return true; + } + return false; +} + Task* Task::ContinueWith(Task* task) { ASSERT(task != nullptr && task != this); diff --git a/Source/Engine/Threading/Task.h b/Source/Engine/Threading/Task.h index c741da73d..11326fa3a 100644 --- a/Source/Engine/Threading/Task.h +++ b/Source/Engine/Threading/Task.h @@ -181,6 +181,14 @@ public: /// True if task failed or has been canceled or has timeout, otherwise false. bool Wait(double timeoutMilliseconds = -1) const; + /// + /// Waits for all the tasks from the list. + /// + /// The tasks list to wait for. + /// The maximum amount of milliseconds to wait for the task to finish it's job. Timeout smaller/equal 0 will result in infinite waiting. + /// True if any task failed or has been canceled or has timeout, otherwise false. + static bool WaitAll(const Span& tasks, double timeoutMilliseconds = -1); + /// /// Waits for all the tasks from the list. /// @@ -188,14 +196,9 @@ public: /// The maximum amount of milliseconds to wait for the task to finish it's job. Timeout smaller/equal 0 will result in infinite waiting. /// True if any task failed or has been canceled or has timeout, otherwise false. template - static bool WaitAll(Array& tasks, double timeoutMilliseconds = -1) + static bool WaitAll(const Array& tasks, double timeoutMilliseconds = -1) { - for (int32 i = 0; i < tasks.Count(); i++) - { - if (tasks[i]->Wait()) - return true; - } - return false; + return WaitAll(ToSpan(tasks), timeoutMilliseconds); } public: