123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerableEx
- {
- public static IAsyncEnumerable<TSource> Amb<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
- {
- if (first == null)
- throw Error.ArgumentNull(nameof(first));
- if (second == null)
- throw Error.ArgumentNull(nameof(second));
- return new AmbAsyncIterator<TSource>(first, second);
- }
- public static IAsyncEnumerable<TSource> Amb<TSource>(params IAsyncEnumerable<TSource>[] sources)
- {
- if (sources == null)
- throw Error.ArgumentNull(nameof(sources));
- return new AmbAsyncIteratorN<TSource>(sources);
- }
- public static IAsyncEnumerable<TSource> Amb<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
- {
- if (sources == null)
- throw Error.ArgumentNull(nameof(sources));
- return new AmbAsyncIteratorN<TSource>(sources.ToArray());
- }
- private sealed class AmbAsyncIterator<TSource> : AsyncIterator<TSource>
- {
- private readonly IAsyncEnumerable<TSource> _first;
- private readonly IAsyncEnumerable<TSource> _second;
- private IAsyncEnumerator<TSource> _enumerator;
- public AmbAsyncIterator(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
- {
- Debug.Assert(first != null);
- Debug.Assert(second != null);
- _first = first;
- _second = second;
- }
- public override AsyncIterator<TSource> Clone()
- {
- return new AmbAsyncIterator<TSource>(_first, _second);
- }
- public override async ValueTask DisposeAsync()
- {
- if (_enumerator != null)
- {
- await _enumerator.DisposeAsync().ConfigureAwait(false);
- _enumerator = null;
- }
- await base.DisposeAsync().ConfigureAwait(false);
- }
- protected override async ValueTask<bool> MoveNextCore()
- {
- switch (state)
- {
- case AsyncIteratorState.Allocated:
- var firstEnumerator = _first.GetAsyncEnumerator(cancellationToken);
- var secondEnumerator = _second.GetAsyncEnumerator(cancellationToken);
- var firstMoveNext = firstEnumerator.MoveNextAsync().AsTask();
- var secondMoveNext = secondEnumerator.MoveNextAsync().AsTask();
- var winner = await Task.WhenAny(firstMoveNext, secondMoveNext).ConfigureAwait(false);
- //
- // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks:
- //
- // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight.
- // 2. The winner elected by Amb is blocked to yield results until the loser unblocks.
- //
- // The approach below has one drawback, namely that exceptions raised by loser are dropped on the floor.
- //
- if (winner == firstMoveNext)
- {
- _enumerator = firstEnumerator;
- var ignored = secondMoveNext.ContinueWith(_ =>
- {
- secondEnumerator.DisposeAsync();
- });
- }
- else
- {
- _enumerator = secondEnumerator;
- var ignored = firstMoveNext.ContinueWith(_ =>
- {
- firstEnumerator.DisposeAsync();
- });
- }
- state = AsyncIteratorState.Iterating;
- if (await winner.ConfigureAwait(false))
- {
- current = _enumerator.Current;
- return true;
- }
- break;
- case AsyncIteratorState.Iterating:
- if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
- {
- current = _enumerator.Current;
- return true;
- }
- break;
- }
- await DisposeAsync().ConfigureAwait(false);
- return false;
- }
- }
- private sealed class AmbAsyncIteratorN<TSource> : AsyncIterator<TSource>
- {
- private readonly IAsyncEnumerable<TSource>[] _sources;
- private IAsyncEnumerator<TSource> _enumerator;
- public AmbAsyncIteratorN(IAsyncEnumerable<TSource>[] sources)
- {
- Debug.Assert(sources != null);
- _sources = sources;
- }
- public override AsyncIterator<TSource> Clone()
- {
- return new AmbAsyncIteratorN<TSource>(_sources);
- }
- public override async ValueTask DisposeAsync()
- {
- if (_enumerator != null)
- {
- await _enumerator.DisposeAsync().ConfigureAwait(false);
- _enumerator = null;
- }
- await base.DisposeAsync().ConfigureAwait(false);
- }
- protected override async ValueTask<bool> MoveNextCore()
- {
- switch (state)
- {
- case AsyncIteratorState.Allocated:
- var n = _sources.Length;
- var enumerators = new IAsyncEnumerator<TSource>[n];
- var moveNexts = new ValueTask<bool>[n];
- for (var i = 0; i < n; i++)
- {
- var enumerator = _sources[i].GetAsyncEnumerator(cancellationToken);
- enumerators[i] = enumerator;
- moveNexts[i] = enumerator.MoveNextAsync();
- }
- var winner = await Task.WhenAny(moveNexts.Select(t => t.AsTask())).ConfigureAwait(false);
- //
- // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks:
- //
- // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight.
- // 2. The winner elected by Amb is blocked to yield results until all losers unblocks.
- //
- // The approach below has one drawback, namely that exceptions raised by any loser are dropped on the floor.
- //
- var winnerIndex = Array.IndexOf(moveNexts, winner);
- _enumerator = enumerators[winnerIndex];
- for (var i = 0; i < n; i++)
- {
- if (i != winnerIndex)
- {
- var ignored = moveNexts[i].AsTask().ContinueWith(_ =>
- {
- enumerators[i].DisposeAsync();
- });
- }
- }
- state = AsyncIteratorState.Iterating;
- if (await winner.ConfigureAwait(false))
- {
- current = _enumerator.Current;
- return true;
- }
- break;
- case AsyncIteratorState.Iterating:
- if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
- {
- current = _enumerator.Current;
- return true;
- }
- break;
- }
- await DisposeAsync().ConfigureAwait(false);
- return false;
- }
- }
- }
- }
|