Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't marshal async continuations to the captured context #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions AsyncLock/AsyncLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ internal InnerLock(AsyncLock parent, long oldId, int oldThreadId)

internal async Task<IDisposable> 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.
Expand All @@ -86,7 +86,7 @@ internal async Task<IDisposable> 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;
}
Expand All @@ -100,7 +100,7 @@ internal async Task<IDisposable> 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.
Expand All @@ -113,7 +113,7 @@ internal async Task<IDisposable> 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;
}
Expand All @@ -130,10 +130,10 @@ internal async Task<IDisposable> 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)
Expand Down Expand Up @@ -201,13 +201,13 @@ internal IDisposable ObtainLock(CancellationToken cancellationToken)

private async Task<bool> TryEnterAsync(CancellationToken cancel = default)
{
await _parent._reentrancy.WaitAsync(cancel);
await _parent._reentrancy.WaitAsync(cancel).ConfigureAwait(false);
return InnerTryEnter();
}

private async Task<bool> TryEnterAsync(TimeSpan timeout)
{
if (!await _parent._reentrancy.WaitAsync(timeout))
if (!await _parent._reentrancy.WaitAsync(timeout).ConfigureAwait(false))
{
return false;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -361,7 +361,7 @@ public Task<bool> 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
Expand Down Expand Up @@ -395,8 +395,8 @@ public Task<bool> TryLockAsync(Func<Task> 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
Expand Down Expand Up @@ -428,7 +428,7 @@ public Task<bool> 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
Expand Down Expand Up @@ -462,8 +462,8 @@ public Task<bool> TryLockAsync(Func<Task> callback, CancellationToken cancel)
}

return true;
});
}).Unwrap();
}, TaskScheduler.Default);
}, TaskScheduler.Default).Unwrap();
}

public IDisposable Lock(CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -533,7 +533,7 @@ public Task<IDisposable> 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);
}
}
}
Expand All @@ -558,7 +558,7 @@ public Task<IDisposable> 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);
}
}
}
Expand Down