Last active
August 4, 2024 07:07
-
-
Save NickStrupat/6e5bfb2e8461a7f4da50ce6351bf0491 to your computer and use it in GitHub Desktop.
AsyncLock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
/// <summary> | |
/// A simple FIFO async lock. It does not support recursion/reentrancy. | |
/// </summary> | |
public sealed class AsyncLock | |
{ | |
private Task task = Task.CompletedTask; | |
public async ValueTask LockAsync(Func<Task> whenLocked) | |
{ | |
ArgumentNullException.ThrowIfNull(whenLocked); | |
var next = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | |
var prev = Interlocked.Exchange(ref task, next.Task); | |
await prev.ConfigureAwait(false); | |
try { await whenLocked().ConfigureAwait(false); } | |
finally { next.SetResult(); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment