mirror of
https://github.com/ppy/osu.git
synced 2025-01-13 02:32:55 +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:
parent
a30aecbafe
commit
1ec305e10d
@ -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"/>.
|
||||||
previousTask = finalTask;
|
/// </summary>
|
||||||
finalTask = currentTask = new Task(action, cancellationToken);
|
/// <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>
|
||||||
if (previousTask != null)
|
public Task Add(Func<Task> task, CancellationToken cancellationToken = default)
|
||||||
await previousTask;
|
{
|
||||||
|
lock (taskLock)
|
||||||
currentTask.Start();
|
return lastTaskInChain = lastTaskInChain.ContinueWithSequential(task, cancellationToken);
|
||||||
await currentTask;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user