Skip to content

Commit

Permalink
Improve new BasicAsyncLock APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Feb 27, 2024
1 parent 772cde1 commit e1a3c99
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/Imazen.Common/Concurrency/BasicAsyncLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ public BasicAsyncLock()
releaser = Task.FromResult((IDisposable)new Releaser(this));
}

public Task<IDisposable> LockAsync(CancellationToken cancellationToken = default)
public Task<IDisposable> LockAsync() => LockAsyncWithTimeout(Timeout.Infinite, CancellationToken.None);
public Task<IDisposable> LockAsyncWithTimeout(int timeoutMilliseconds = Timeout.Infinite, CancellationToken cancellationToken = default)
{
var wait = semaphore.WaitAsync(cancellationToken);
return wait.IsCompleted ?
releaser :
wait.ContinueWith((_, state) => (IDisposable)state!,
releaser.Result, CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
public Task<IDisposable> LockAsync(TimeSpan timeout, CancellationToken cancellationToken = default)
{
var wait = semaphore.WaitAsync(timeout, cancellationToken);
var wait = semaphore.WaitAsync(timeoutMilliseconds, cancellationToken);
return wait.IsCompleted ?
releaser :
wait.ContinueWith((_, state) => (IDisposable)state!,
Expand Down

0 comments on commit e1a3c99

Please sign in to comment.