From 1ec305e10d4ba70ee06a9f21bb3087e0d2e245e9 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 29 Jan 2021 16:06:57 +0900 Subject: [PATCH] Update TaskChain to use ContinueWithSequential internally It turns out we may still want to use TaskChain for its locking behaviour, so I've made it internally use the refactored version I implemented, while keeping the general structure. --- osu.Game/Utils/TaskChain.cs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/osu.Game/Utils/TaskChain.cs b/osu.Game/Utils/TaskChain.cs index 30aea7578f..df28faf9fb 100644 --- a/osu.Game/Utils/TaskChain.cs +++ b/osu.Game/Utils/TaskChain.cs @@ -6,6 +6,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using osu.Game.Extensions; namespace osu.Game.Utils { @@ -14,8 +15,9 @@ namespace osu.Game.Utils /// public class TaskChain { - private readonly object finalTaskLock = new object(); - private Task? finalTask; + private readonly object taskLock = new object(); + + private Task lastTaskInChain = Task.CompletedTask; /// /// Adds a new task to the end of this . @@ -23,22 +25,22 @@ public class TaskChain /// The action to be executed. /// The for this task. Does not affect further tasks in the chain. /// The awaitable . - public async Task Add(Action action, CancellationToken cancellationToken = default) + public Task Add(Action action, CancellationToken cancellationToken = default) { - Task? previousTask; - Task currentTask; + lock (taskLock) + return lastTaskInChain = lastTaskInChain.ContinueWithSequential(action, cancellationToken); + } - lock (finalTaskLock) - { - previousTask = finalTask; - finalTask = currentTask = new Task(action, cancellationToken); - } - - if (previousTask != null) - await previousTask; - - currentTask.Start(); - await currentTask; + /// + /// Adds a new task to the end of this . + /// + /// The task to be executed. + /// The for this task. Does not affect further tasks in the chain. + /// The awaitable . + public Task Add(Func task, CancellationToken cancellationToken = default) + { + lock (taskLock) + return lastTaskInChain = lastTaskInChain.ContinueWithSequential(task, cancellationToken); } } }