// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable enable using System; using System.Threading; using System.Threading.Tasks; namespace osu.Game.Utils { /// /// A chain of s that run sequentially. /// public class TaskChain { private readonly object finalTaskLock = new object(); private Task? finalTask; /// /// Adds a new task to the end of this . /// /// 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) { Task? previousTask; Task currentTask; lock (finalTaskLock) { previousTask = finalTask; finalTask = currentTask = new Task(action, cancellationToken); } if (previousTask != null) await previousTask; currentTask.Start(); await currentTask; } } }