Amb.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.Threading;
  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 Error.ArgumentNull(nameof(first));
  15. if (second == null)
  16. throw Error.ArgumentNull(nameof(second));
  17. return AsyncEnumerable.Create(Core);
  18. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  19. {
  20. IAsyncEnumerator<TSource>? firstEnumerator = null;
  21. IAsyncEnumerator<TSource>? secondEnumerator = null;
  22. Task<bool>? firstMoveNext = null;
  23. Task<bool>? secondMoveNext = null;
  24. try
  25. {
  26. //
  27. // REVIEW: We start both sequences unconditionally. An alternative implementation could be to just stick
  28. // to the first sequence if we notice it already has a value (or exception) available. This would
  29. // be similar to Task.WhenAny behavior (see CommonCWAnyLogic in dotnet/coreclr). We could consider
  30. // adding a WhenAny combinator that does exactly that. We can even avoid calling AsTask.
  31. //
  32. firstEnumerator = first.GetAsyncEnumerator(cancellationToken);
  33. firstMoveNext = firstEnumerator.MoveNextAsync().AsTask();
  34. //
  35. // REVIEW: Order of operations has changed here compared to the original, but is now in sync with the N-ary
  36. // overload which performs GetAsyncEnumerator/MoveNextAsync in pairs, rather than phased.
  37. //
  38. secondEnumerator = second.GetAsyncEnumerator(cancellationToken);
  39. secondMoveNext = secondEnumerator.MoveNextAsync().AsTask();
  40. }
  41. catch
  42. {
  43. // NB: AwaitMoveNextAsyncAndDispose checks for null for both arguments, reducing the need for many null
  44. // checks over here.
  45. var cleanup = new[]
  46. {
  47. AwaitMoveNextAsyncAndDispose(secondMoveNext, secondEnumerator),
  48. AwaitMoveNextAsyncAndDispose(firstMoveNext, firstEnumerator)
  49. };
  50. await Task.WhenAll(cleanup).ConfigureAwait(false);
  51. throw;
  52. }
  53. //
  54. // REVIEW: Consider using the WhenAny combinator defined for Merge in TaskExt, which would avoid the need
  55. // to convert to Task<bool> prior to calling Task.WhenAny.
  56. //
  57. var moveNextWinner = await Task.WhenAny(firstMoveNext, secondMoveNext).ConfigureAwait(false);
  58. //
  59. // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks:
  60. //
  61. // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight.
  62. // 2. The winner elected by Amb is blocked to yield results until the loser unblocks.
  63. //
  64. IAsyncEnumerator<TSource> winner;
  65. Task disposeLoser;
  66. if (moveNextWinner == firstMoveNext)
  67. {
  68. winner = firstEnumerator;
  69. disposeLoser = AwaitMoveNextAsyncAndDispose(secondMoveNext, secondEnumerator);
  70. }
  71. else
  72. {
  73. winner = secondEnumerator;
  74. disposeLoser = AwaitMoveNextAsyncAndDispose(firstMoveNext, firstEnumerator);
  75. }
  76. try
  77. {
  78. await using (winner.ConfigureAwait(false))
  79. {
  80. if (!await moveNextWinner.ConfigureAwait(false))
  81. {
  82. yield break;
  83. }
  84. yield return winner.Current;
  85. while (await winner.MoveNextAsync().ConfigureAwait(false))
  86. {
  87. yield return winner.Current;
  88. }
  89. }
  90. }
  91. finally
  92. {
  93. //
  94. // REVIEW: This behavior differs from the original implementation in that we never discard any in flight
  95. // asynchronous operations. If an exception occurs while enumerating the winner, it can be
  96. // subsumed by an exception thrown due to cleanup of the loser. Also, if Amb is used to deal with
  97. // a potentially long-blocking sequence, this implementation would transfer this blocking behavior
  98. // to the resulting sequence. However, it seems never discarding a non-completed task should be a
  99. // general design tenet, and fire-and-forget dispose behavior could be added as another "unsafe"
  100. // operator, so all such sins are made explicit by the user. Nonetheless, this change is a breaking
  101. // change for Ix Async.
  102. //
  103. await disposeLoser.ConfigureAwait(false);
  104. }
  105. }
  106. }
  107. public static IAsyncEnumerable<TSource> Amb<TSource>(params IAsyncEnumerable<TSource>[] sources)
  108. {
  109. if (sources == null)
  110. throw Error.ArgumentNull(nameof(sources));
  111. return AsyncEnumerable.Create(Core);
  112. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  113. {
  114. //
  115. // REVIEW: See remarks on binary overload for changes compared to the original.
  116. //
  117. var n = sources.Length;
  118. var enumerators = new IAsyncEnumerator<TSource>[n];
  119. var moveNexts = new Task<bool>[n];
  120. try
  121. {
  122. for (var i = 0; i < n; i++)
  123. {
  124. var enumerator = sources[i].GetAsyncEnumerator(cancellationToken);
  125. enumerators[i] = enumerator;
  126. moveNexts[i] = enumerator.MoveNextAsync().AsTask();
  127. }
  128. }
  129. catch
  130. {
  131. var cleanup = new Task[n];
  132. for (var i = 0; i < n; i++)
  133. {
  134. cleanup[i] = AwaitMoveNextAsyncAndDispose(moveNexts[i], enumerators[i]);
  135. }
  136. await Task.WhenAll(cleanup).ConfigureAwait(false);
  137. throw;
  138. }
  139. var moveNextWinner = await Task.WhenAny(moveNexts).ConfigureAwait(false);
  140. //
  141. // NB: The use of IndexOf is fine. If task N completed by returning a ValueTask<bool>
  142. // which is equivalent to the task returned by task M (where M < N), AsTask may
  143. // return the same reference (e.g. due to caching of completed Boolean tasks). In
  144. // such a case, IndexOf will find task M rather than N in the array, but both have
  145. // an equivalent completion state (because they're reference equal). This only leads
  146. // to a left-bias in selection of sources, but given Amb's "ambiguous" nature, this
  147. // is acceptable.
  148. //
  149. var winnerIndex = Array.IndexOf(moveNexts, moveNextWinner);
  150. var winner = enumerators[winnerIndex];
  151. var loserCleanupTasks = new List<Task>(n - 1);
  152. for (var i = 0; i < n; i++)
  153. {
  154. if (i != winnerIndex)
  155. {
  156. var loserCleanupTask = AwaitMoveNextAsyncAndDispose(moveNexts[i], enumerators[i]);
  157. loserCleanupTasks.Add(loserCleanupTask);
  158. }
  159. }
  160. var cleanupLosers = Task.WhenAll(loserCleanupTasks);
  161. try
  162. {
  163. await using (winner.ConfigureAwait(false))
  164. {
  165. if (!await moveNextWinner.ConfigureAwait(false))
  166. {
  167. yield break;
  168. }
  169. yield return winner.Current;
  170. while (await winner.MoveNextAsync().ConfigureAwait(false))
  171. {
  172. yield return winner.Current;
  173. }
  174. }
  175. }
  176. finally
  177. {
  178. await cleanupLosers.ConfigureAwait(false);
  179. }
  180. }
  181. }
  182. public static IAsyncEnumerable<TSource> Amb<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  183. {
  184. if (sources == null)
  185. throw Error.ArgumentNull(nameof(sources));
  186. return Amb(sources.ToArray());
  187. }
  188. private static async Task AwaitMoveNextAsyncAndDispose<T>(Task<bool>? moveNextAsync, IAsyncEnumerator<T>? enumerator)
  189. {
  190. if (enumerator != null)
  191. {
  192. await using (enumerator.ConfigureAwait(false))
  193. {
  194. if (moveNextAsync != null)
  195. {
  196. await moveNextAsync.ConfigureAwait(false);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }