Skip to content

Commit

Permalink
Added support of cancellation token
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jun 23, 2024
1 parent 4c1b238 commit 53a87ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/DotNext.Tests/Threading/AsyncBridgeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public static async Task WaitForSignal()
await ev.WaitAsync(DefaultTimeout);
}

[Fact]
public static async Task CancelWaitForSignal()
{
using var ev = new ManualResetEvent(false);
using var cts = new CancellationTokenSource();
cts.CancelAfter(150);
await ThrowsAsync<OperationCanceledException>(async () => await ev.WaitAsync(cts.Token));
}

[Fact]
public static async Task AlreadySignaled()
{
Expand Down
12 changes: 8 additions & 4 deletions src/DotNext.Threading/Threading/AsyncBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ static void Complete(object? state, bool timedOut)
/// </summary>
/// <param name="handle">The handle to await.</param>
/// <param name="timeout">The timeout used to await completion.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns><see langword="true"/> if handle is signaled; otherwise, <see langword="false"/> if timeout occurred.</returns>
public static ValueTask<bool> WaitAsync(this WaitHandle handle, TimeSpan timeout)
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public static ValueTask<bool> WaitAsync(this WaitHandle handle, TimeSpan timeout, CancellationToken token = default)
{
ValueTask<bool> result;
if (handle.WaitOne(0))
Expand All @@ -106,7 +108,7 @@ public static ValueTask<bool> WaitAsync(this WaitHandle handle, TimeSpan timeout
}
else
{
result = GetCompletionSource(handle, timeout).CreateTask(InfiniteTimeSpan, CancellationToken.None);
result = GetCompletionSource(handle, timeout).CreateTask(InfiniteTimeSpan, token);
}

return result;
Expand All @@ -118,13 +120,15 @@ public static ValueTask<bool> WaitAsync(this WaitHandle handle, TimeSpan timeout
/// Obtains a task that can be used to await handle completion.
/// </summary>
/// <param name="handle">The handle to await.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns>The task that will be completed .</returns>
public static ValueTask WaitAsync(this WaitHandle handle)
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public static ValueTask WaitAsync(this WaitHandle handle, CancellationToken token = default)
=> handle.WaitOne(0)
? ValueTask.CompletedTask
: GetCompletionSource(handle, InfiniteTimeSpan)
.As<ISupplier<TimeSpan, CancellationToken, ValueTask>>()
.Invoke(InfiniteTimeSpan, CancellationToken.None);
.Invoke(InfiniteTimeSpan, token);

/// <summary>
/// Gets or sets the capacity of the internal pool used to create awaitable tasks returned
Expand Down

0 comments on commit 53a87ff

Please sign in to comment.