diff --git a/src/DotNext.Threading/Threading/AsyncEventHub.cs b/src/DotNext.Threading/Threading/AsyncEventHub.cs index 944866aa8..68469466a 100644 --- a/src/DotNext.Threading/Threading/AsyncEventHub.cs +++ b/src/DotNext.Threading/Threading/AsyncEventHub.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -449,34 +450,32 @@ public Task WaitAnyAsync(ReadOnlySpan eventIndexes, CancellationToken /// The operation has timed out. /// The operation has been canceled. public Task WaitAnyAsync(TimeSpan timeout, CancellationToken token = default) + => timeout < TimeSpan.Zero ? WaitAnyAsync(token) : WaitAnyCoreAsync(timeout, token); + + private Task WaitAnyCoreAsync(TimeSpan timeout, CancellationToken token) { - return timeout < TimeSpan.Zero ? this.WaitAnyAsync(token) : WaitAnyAsync(); + Task result; - Task WaitAnyAsync() + var lockTaken = false; + var start = new Timestamp(); + try { - Task result; - - var lockTaken = false; - var start = new Timestamp(); - try - { - lockTaken = Monitor.TryEnter(sources, timeout); - result = lockTaken && (timeout -= start.Elapsed) > TimeSpan.Zero - ? Task.WhenAny(GetTasks()) - : throw new TimeoutException(); - } - catch (Exception e) - { - result = Task.FromException(e); - } - finally - { - if (lockTaken) - Monitor.Exit(sources); - } - - return result.WaitAsync(timeout, token).Convert(GetIndex); + lockTaken = Monitor.TryEnter(sources, timeout); + result = lockTaken && (timeout -= start.Elapsed) > TimeSpan.Zero + ? Task.WhenAny(GetTasks()) + : throw new TimeoutException(); } + catch (Exception e) + { + result = Task.FromException(e); + } + finally + { + if (lockTaken) + Monitor.Exit(sources); + } + + return result.WaitAsync(timeout, token).Convert(GetIndex); } /// @@ -563,7 +562,7 @@ private Task WaitAllCoreAsync(ReadOnlySpan eventIndexes, TimeSpan timeout, /// The indexes of the events. /// The time to wait for the events. /// The token that can be used to cancel the operation. - /// A task that represents the completion of all of the specified events. + /// A task that represents the completion of all the specified events. /// The operation has timed out. /// The operation has been canceled. public Task WaitAllAsync(ReadOnlySpan eventIndexes, TimeSpan timeout, CancellationToken token = default) @@ -593,34 +592,32 @@ public Task WaitAllAsync(ReadOnlySpan eventIndexes, CancellationToken token /// The operation has timed out. /// The operation has been canceled. public Task WaitAllAsync(TimeSpan timeout, CancellationToken token = default) + => timeout < TimeSpan.Zero ? WaitAllAsync(token) : WaitAllCoreAsync(timeout, token); + + private Task WaitAllCoreAsync(TimeSpan timeout, CancellationToken token) { - return timeout < TimeSpan.Zero ? this.WaitAllAsync(token) : WaitAllAsync(); + Task result; - Task WaitAllAsync() + var lockTaken = false; + var start = new Timestamp(); + try { - Task result; - - var lockTaken = false; - var start = new Timestamp(); - try - { - lockTaken = Monitor.TryEnter(sources, timeout); - result = lockTaken && (timeout -= start.Elapsed) > TimeSpan.Zero - ? Task.WhenAll(GetTasks()) - : throw new TimeoutException(); - } - catch (Exception e) - { - result = Task.FromException(e); - } - finally - { - if (lockTaken) - Monitor.Exit(sources); - } - - return result.WaitAsync(timeout, token); + lockTaken = Monitor.TryEnter(sources, timeout); + result = lockTaken && (timeout -= start.Elapsed) > TimeSpan.Zero + ? Task.WhenAll(GetTasks()) + : throw new TimeoutException(); } + catch (Exception e) + { + result = Task.FromException(e); + } + finally + { + if (lockTaken) + Monitor.Exit(sources); + } + + return result.WaitAsync(timeout, token); } ///