diff --git a/AsyncLock/AsyncLock.cs b/AsyncLock/AsyncLock.cs index b7a737d..bb537cb 100644 --- a/AsyncLock/AsyncLock.cs +++ b/AsyncLock/AsyncLock.cs @@ -68,10 +68,10 @@ internal InnerLock(AsyncLock parent, long oldId, int oldThreadId) internal async Task ObtainLockAsync(CancellationToken ct = default) { - while (!await TryEnterAsync(ct)) + while (!await TryEnterAsync(ct).ConfigureAwait(false)) { // We need to wait for someone to leave the lock before trying again. - await _parent._retry.WaitAsync(ct); + await _parent._retry.WaitAsync(ct).ConfigureAwait(false); } // Reset the owning thread id after all await calls have finished, otherwise we // could be resumed on a different thread and set an incorrect value. @@ -86,7 +86,7 @@ internal async Task ObtainLockAsync(CancellationToken ct = default) // In case of zero-timeout, don't even wait for protective lock contention if (timeout == TimeSpan.Zero) { - if (await TryEnterAsync(timeout)) + if (await TryEnterAsync(timeout).ConfigureAwait(false)) { return this; } @@ -100,7 +100,7 @@ internal async Task ObtainLockAsync(CancellationToken ct = default) // We need to wait for someone to leave the lock before trying again. while (remainder > TimeSpan.Zero) { - if (await TryEnterAsync(remainder)) + if (await TryEnterAsync(remainder).ConfigureAwait(false)) { // Reset the owning thread id after all await calls have finished, otherwise we // could be resumed on a different thread and set an incorrect value. @@ -113,7 +113,7 @@ internal async Task ObtainLockAsync(CancellationToken ct = default) now = DateTimeOffset.UtcNow; remainder -= now - last; last = now; - if (remainder < TimeSpan.Zero || !await _parent._retry.WaitAsync(remainder)) + if (remainder < TimeSpan.Zero || !await _parent._retry.WaitAsync(remainder).ConfigureAwait(false)) { return null; } @@ -130,10 +130,10 @@ internal async Task ObtainLockAsync(CancellationToken ct = default) { try { - while (!await TryEnterAsync(cancel)) + while (!await TryEnterAsync(cancel).ConfigureAwait(false)) { // We need to wait for someone to leave the lock before trying again. - await _parent._retry.WaitAsync(cancel); + await _parent._retry.WaitAsync(cancel).ConfigureAwait(false); } } catch (OperationCanceledException) @@ -201,13 +201,13 @@ internal IDisposable ObtainLock(CancellationToken cancellationToken) private async Task TryEnterAsync(CancellationToken cancel = default) { - await _parent._reentrancy.WaitAsync(cancel); + await _parent._reentrancy.WaitAsync(cancel).ConfigureAwait(false); return InnerTryEnter(); } private async Task TryEnterAsync(TimeSpan timeout) { - if (!await _parent._reentrancy.WaitAsync(timeout)) + if (!await _parent._reentrancy.WaitAsync(timeout).ConfigureAwait(false)) { return false; } @@ -296,7 +296,7 @@ public void Dispose() var oldThreadId = this._oldThreadId; Task.Run(async () => { - await @this._parent._reentrancy.WaitAsync(); + await @this._parent._reentrancy.WaitAsync().ConfigureAwait(false); try { Interlocked.Decrement(ref @this._parent._reentrances); @@ -361,7 +361,7 @@ public Task TryLockAsync(Action callback, TimeSpan timeout) disposableLock.Dispose(); } return true; - }); + }, TaskScheduler.Default); } // Make sure InnerLock.LockAsync() does not use await, because an async function triggers a snapshot of @@ -395,8 +395,8 @@ public Task TryLockAsync(Func callback, TimeSpan timeout) } return true; - }); - }).Unwrap(); + }, TaskScheduler.Default); + }, TaskScheduler.Default).Unwrap(); } // Make sure InnerLock.LockAsync() does not use await, because an async function triggers a snapshot of @@ -428,7 +428,7 @@ public Task TryLockAsync(Action callback, CancellationToken cancel) disposableLock.Dispose(); } return true; - }); + }, TaskScheduler.Default); } // Make sure InnerLock.LockAsync() does not use await, because an async function triggers a snapshot of @@ -462,8 +462,8 @@ public Task TryLockAsync(Func callback, CancellationToken cancel) } return true; - }); - }).Unwrap(); + }, TaskScheduler.Default); + }, TaskScheduler.Default).Unwrap(); } public IDisposable Lock(CancellationToken cancellationToken = default) @@ -533,7 +533,7 @@ public Task TryLockAsync(CancellationToken ct, out bool locked) var result = state.Result; *(bool*)addrLong = result is not null; return result ?? NullDisposable; - }, TaskContinuationOptions.OnlyOnRanToCompletion); + }, default(CancellationToken), TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default); } } } @@ -558,7 +558,7 @@ public Task TryLockAsync(TimeSpan timeout, out bool locked) var result = state.Result; *(bool*)addrLong = result is not null; return result ?? NullDisposable; - }, TaskContinuationOptions.OnlyOnRanToCompletion); + }, default(CancellationToken), TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default); } } }