1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 07:22:54 +08:00

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.
This commit is contained in:
Dean Herbert 2021-01-29 16:06:57 +09:00
parent a30aecbafe
commit 1ec305e10d

View File

@ -6,6 +6,7 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Game.Extensions;
namespace osu.Game.Utils namespace osu.Game.Utils
{ {
@ -14,8 +15,9 @@ namespace osu.Game.Utils
/// </summary> /// </summary>
public class TaskChain public class TaskChain
{ {
private readonly object finalTaskLock = new object(); private readonly object taskLock = new object();
private Task? finalTask;
private Task lastTaskInChain = Task.CompletedTask;
/// <summary> /// <summary>
/// Adds a new task to the end of this <see cref="TaskChain"/>. /// Adds a new task to the end of this <see cref="TaskChain"/>.
@ -23,22 +25,22 @@ namespace osu.Game.Utils
/// <param name="action">The action to be executed.</param> /// <param name="action">The action to be executed.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for this task. Does not affect further tasks in the chain.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for this task. Does not affect further tasks in the chain.</param>
/// <returns>The awaitable <see cref="Task"/>.</returns> /// <returns>The awaitable <see cref="Task"/>.</returns>
public async Task Add(Action action, CancellationToken cancellationToken = default) public Task Add(Action action, CancellationToken cancellationToken = default)
{ {
Task? previousTask; lock (taskLock)
Task currentTask; return lastTaskInChain = lastTaskInChain.ContinueWithSequential(action, cancellationToken);
}
lock (finalTaskLock) /// <summary>
/// Adds a new task to the end of this <see cref="TaskChain"/>.
/// </summary>
/// <param name="task">The task to be executed.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for this task. Does not affect further tasks in the chain.</param>
/// <returns>The awaitable <see cref="Task"/>.</returns>
public Task Add(Func<Task> task, CancellationToken cancellationToken = default)
{ {
previousTask = finalTask; lock (taskLock)
finalTask = currentTask = new Task(action, cancellationToken); return lastTaskInChain = lastTaskInChain.ContinueWithSequential(task, cancellationToken);
}
if (previousTask != null)
await previousTask;
currentTask.Start();
await currentTask;
} }
} }
} }