Amb.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. public static IAsyncEnumerable<TSource> Amb<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  12. {
  13. if (first == null)
  14. throw new ArgumentNullException(nameof(first));
  15. if (second == null)
  16. throw new ArgumentNullException(nameof(second));
  17. return new AmbAsyncIterator<TSource>(first, second);
  18. }
  19. private sealed class AmbAsyncIterator<TSource> : AsyncIterator<TSource>
  20. {
  21. private readonly IAsyncEnumerable<TSource> first;
  22. private readonly IAsyncEnumerable<TSource> second;
  23. private IAsyncEnumerator<TSource> enumerator;
  24. public AmbAsyncIterator(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  25. {
  26. Debug.Assert(first != null);
  27. Debug.Assert(second != null);
  28. this.first = first;
  29. this.second = second;
  30. }
  31. public override AsyncIterator<TSource> Clone()
  32. {
  33. return new AmbAsyncIterator<TSource>(first, second);
  34. }
  35. public override async Task DisposeAsync()
  36. {
  37. if (enumerator != null)
  38. {
  39. await enumerator.DisposeAsync().ConfigureAwait(false);
  40. enumerator = null;
  41. }
  42. await base.DisposeAsync().ConfigureAwait(false);
  43. }
  44. protected override async Task<bool> MoveNextCore()
  45. {
  46. switch (state)
  47. {
  48. case AsyncIteratorState.Allocated:
  49. var firstEnumerator = first.GetAsyncEnumerator();
  50. var secondEnumerator = second.GetAsyncEnumerator();
  51. var firstMoveNext = firstEnumerator.MoveNextAsync();
  52. var secondMoveNext = secondEnumerator.MoveNextAsync();
  53. var winner = await Task.WhenAny(firstMoveNext, secondMoveNext).ConfigureAwait(false);
  54. //
  55. // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks:
  56. //
  57. // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight.
  58. // 2. The winner elected by Amb is blocked to yield results until the loser unblocks.
  59. //
  60. // The approach below has one drawback, namely that exceptions raised by loser are dropped on the floor.
  61. //
  62. if (winner == firstMoveNext)
  63. {
  64. enumerator = firstEnumerator;
  65. var ignored = secondMoveNext.ContinueWith(_ =>
  66. {
  67. secondEnumerator.DisposeAsync();
  68. });
  69. }
  70. else
  71. {
  72. enumerator = secondEnumerator;
  73. var ignored = firstMoveNext.ContinueWith(_ =>
  74. {
  75. firstEnumerator.DisposeAsync();
  76. });
  77. }
  78. state = AsyncIteratorState.Iterating;
  79. if (await winner.ConfigureAwait(false))
  80. {
  81. current = enumerator.Current;
  82. return true;
  83. }
  84. break;
  85. case AsyncIteratorState.Iterating:
  86. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  87. {
  88. current = enumerator.Current;
  89. return true;
  90. }
  91. break;
  92. }
  93. await DisposeAsync().ConfigureAwait(false);
  94. return false;
  95. }
  96. }
  97. }
  98. }