Amb.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TSource> Amb<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  13. {
  14. if (first == null)
  15. throw Error.ArgumentNull(nameof(first));
  16. if (second == null)
  17. throw Error.ArgumentNull(nameof(second));
  18. #if USE_ASYNC_ITERATOR
  19. return AsyncEnumerable.Create(Core);
  20. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  21. {
  22. IAsyncEnumerator<TSource> firstEnumerator = null;
  23. IAsyncEnumerator<TSource> secondEnumerator = null;
  24. Task<bool> firstMoveNext = null;
  25. Task<bool> secondMoveNext = null;
  26. try
  27. {
  28. //
  29. // REVIEW: We start both sequences unconditionally. An alternative implementation could be to just stick
  30. // to the first sequence if we notice it already has a value (or exception) available. This would
  31. // be similar to Task.WhenAny behavior (see CommonCWAnyLogic in dotnet/coreclr). We could consider
  32. // adding a WhenAny combinator that does exactly that. We can even avoid calling AsTask.
  33. //
  34. firstEnumerator = first.GetAsyncEnumerator(cancellationToken);
  35. firstMoveNext = firstEnumerator.MoveNextAsync().AsTask();
  36. //
  37. // REVIEW: Order of operations has changed here compared to the original, but is now in sync with the N-ary
  38. // overload which performs GetAsyncEnumerator/MoveNextAsync in pairs, rather than phased.
  39. //
  40. secondEnumerator = second.GetAsyncEnumerator(cancellationToken);
  41. secondMoveNext = secondEnumerator.MoveNextAsync().AsTask();
  42. }
  43. catch
  44. {
  45. // NB: AwaitMoveNextAsyncAndDispose checks for null for both arguments, reducing the need for many null
  46. // checks over here.
  47. var cleanup = new[]
  48. {
  49. AwaitMoveNextAsyncAndDispose(secondMoveNext, secondEnumerator),
  50. AwaitMoveNextAsyncAndDispose(firstMoveNext, firstEnumerator)
  51. };
  52. await Task.WhenAll(cleanup).ConfigureAwait(false);
  53. throw;
  54. }
  55. //
  56. // REVIEW: Consider using the WhenAny combinator defined for Merge in TaskExt, which would avoid the need
  57. // to convert to Task<bool> prior to calling Task.WhenAny.
  58. //
  59. var moveNextWinner = await Task.WhenAny(firstMoveNext, secondMoveNext).ConfigureAwait(false);
  60. //
  61. // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks:
  62. //
  63. // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight.
  64. // 2. The winner elected by Amb is blocked to yield results until the loser unblocks.
  65. //
  66. IAsyncEnumerator<TSource> winner;
  67. Task disposeLoser;
  68. if (moveNextWinner == firstMoveNext)
  69. {
  70. winner = firstEnumerator;
  71. disposeLoser = AwaitMoveNextAsyncAndDispose(secondMoveNext, secondEnumerator);
  72. }
  73. else
  74. {
  75. winner = secondEnumerator;
  76. disposeLoser = AwaitMoveNextAsyncAndDispose(firstMoveNext, firstEnumerator);
  77. }
  78. try
  79. {
  80. await using (winner.ConfigureAwait(false))
  81. {
  82. if (!await moveNextWinner.ConfigureAwait(false))
  83. {
  84. yield break;
  85. }
  86. yield return winner.Current;
  87. while (await winner.MoveNextAsync().ConfigureAwait(false))
  88. {
  89. yield return winner.Current;
  90. }
  91. }
  92. }
  93. finally
  94. {
  95. //
  96. // REVIEW: This behavior differs from the original implementation in that we never discard any in flight
  97. // asynchronous operations. If an exception occurs while enumerating the winner, it can be
  98. // subsumed by an exception thrown due to cleanup of the loser. Also, if Amb is used to deal with
  99. // a potentially long-blocking sequence, this implementation would transfer this blocking behavior
  100. // to the resulting sequence. However, it seems never discarding a non-completed task should be a
  101. // general design tenet, and fire-and-forget dispose behavior could be added as another "unsafe"
  102. // operator, so all such sins are made explicit by the user. Nonetheless, this change is a breaking
  103. // change for Ix Async.
  104. //
  105. await disposeLoser.ConfigureAwait(false);
  106. }
  107. }
  108. #else
  109. return new AmbAsyncIterator<TSource>(first, second);
  110. #endif
  111. }
  112. public static IAsyncEnumerable<TSource> Amb<TSource>(params IAsyncEnumerable<TSource>[] sources)
  113. {
  114. if (sources == null)
  115. throw Error.ArgumentNull(nameof(sources));
  116. #if USE_ASYNC_ITERATOR
  117. return AsyncEnumerable.Create(Core);
  118. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  119. {
  120. //
  121. // REVIEW: See remarks on binary overload for changes compared to the original.
  122. //
  123. var n = sources.Length;
  124. var enumerators = new IAsyncEnumerator<TSource>[n];
  125. var moveNexts = new Task<bool>[n];
  126. try
  127. {
  128. for (var i = 0; i < n; i++)
  129. {
  130. var enumerator = sources[i].GetAsyncEnumerator(cancellationToken);
  131. enumerators[i] = enumerator;
  132. moveNexts[i] = enumerator.MoveNextAsync().AsTask();
  133. }
  134. }
  135. catch
  136. {
  137. var cleanup = new Task[n];
  138. for (var i = 0; i < n; i++)
  139. {
  140. cleanup[i] = AwaitMoveNextAsyncAndDispose(moveNexts[i], enumerators[i]);
  141. }
  142. await Task.WhenAll(cleanup).ConfigureAwait(false);
  143. throw;
  144. }
  145. var moveNextWinner = await Task.WhenAny(moveNexts).ConfigureAwait(false);
  146. //
  147. // NB: The use of IndexOf is fine. If task N completed by returning a ValueTask<bool>
  148. // which is equivalent to the task returned by task M (where M < N), AsTask may
  149. // return the same reference (e.g. due to caching of completed Boolean tasks). In
  150. // such a case, IndexOf will find task M rather than N in the array, but both have
  151. // an equivalent completion state (because they're reference equal). This only leads
  152. // to a left-bias in selection of sources, but given Amb's "ambiguous" nature, this
  153. // is acceptable.
  154. //
  155. var winnerIndex = Array.IndexOf(moveNexts, moveNextWinner);
  156. var winner = enumerators[winnerIndex];
  157. var loserCleanupTasks = new List<Task>(n - 1);
  158. for (var i = 0; i < n; i++)
  159. {
  160. if (i != winnerIndex)
  161. {
  162. var loserCleanupTask = AwaitMoveNextAsyncAndDispose(moveNexts[i], enumerators[i]);
  163. loserCleanupTasks.Add(loserCleanupTask);
  164. }
  165. }
  166. var cleanupLosers = Task.WhenAll(loserCleanupTasks);
  167. try
  168. {
  169. await using (winner.ConfigureAwait(false))
  170. {
  171. if (!await moveNextWinner.ConfigureAwait(false))
  172. {
  173. yield break;
  174. }
  175. yield return winner.Current;
  176. while (await winner.MoveNextAsync().ConfigureAwait(false))
  177. {
  178. yield return winner.Current;
  179. }
  180. }
  181. }
  182. finally
  183. {
  184. await cleanupLosers.ConfigureAwait(false);
  185. }
  186. }
  187. #else
  188. return new AmbAsyncIteratorN<TSource>(sources);
  189. #endif
  190. }
  191. public static IAsyncEnumerable<TSource> Amb<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  192. {
  193. if (sources == null)
  194. throw Error.ArgumentNull(nameof(sources));
  195. #if USE_ASYNC_ITERATOR
  196. return Amb(sources.ToArray());
  197. #else
  198. return new AmbAsyncIteratorN<TSource>(sources.ToArray());
  199. #endif
  200. }
  201. #if USE_ASYNC_ITERATOR
  202. private static async Task AwaitMoveNextAsyncAndDispose<T>(Task<bool> moveNextAsync, IAsyncEnumerator<T> enumerator)
  203. {
  204. if (enumerator != null)
  205. {
  206. await using (enumerator.ConfigureAwait(false))
  207. {
  208. if (moveNextAsync != null)
  209. {
  210. await moveNextAsync.ConfigureAwait(false);
  211. }
  212. }
  213. }
  214. }
  215. #endif
  216. #if !USE_ASYNC_ITERATOR
  217. private sealed class AmbAsyncIterator<TSource> : AsyncIterator<TSource>
  218. {
  219. private readonly IAsyncEnumerable<TSource> _first;
  220. private readonly IAsyncEnumerable<TSource> _second;
  221. private IAsyncEnumerator<TSource> _enumerator;
  222. public AmbAsyncIterator(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  223. {
  224. Debug.Assert(first != null);
  225. Debug.Assert(second != null);
  226. _first = first;
  227. _second = second;
  228. }
  229. public override AsyncIteratorBase<TSource> Clone()
  230. {
  231. return new AmbAsyncIterator<TSource>(_first, _second);
  232. }
  233. public override async ValueTask DisposeAsync()
  234. {
  235. if (_enumerator != null)
  236. {
  237. await _enumerator.DisposeAsync().ConfigureAwait(false);
  238. _enumerator = null;
  239. }
  240. await base.DisposeAsync().ConfigureAwait(false);
  241. }
  242. protected override async ValueTask<bool> MoveNextCore()
  243. {
  244. switch (_state)
  245. {
  246. case AsyncIteratorState.Allocated:
  247. //
  248. // REVIEW: Exceptions in any of these steps don't cause cleanup. This has been fixed in the new implementation.
  249. //
  250. var firstEnumerator = _first.GetAsyncEnumerator(_cancellationToken);
  251. var secondEnumerator = _second.GetAsyncEnumerator(_cancellationToken);
  252. var firstMoveNext = firstEnumerator.MoveNextAsync().AsTask();
  253. var secondMoveNext = secondEnumerator.MoveNextAsync().AsTask();
  254. var winner = await Task.WhenAny(firstMoveNext, secondMoveNext).ConfigureAwait(false);
  255. //
  256. // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks:
  257. //
  258. // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight.
  259. // 2. The winner elected by Amb is blocked to yield results until the loser unblocks.
  260. //
  261. // The approach below has one drawback, namely that exceptions raised by loser are dropped on the floor.
  262. //
  263. if (winner == firstMoveNext)
  264. {
  265. _enumerator = firstEnumerator;
  266. _ = secondMoveNext.ContinueWith(_ =>
  267. {
  268. secondEnumerator.DisposeAsync();
  269. });
  270. }
  271. else
  272. {
  273. _enumerator = secondEnumerator;
  274. _ = firstMoveNext.ContinueWith(_ =>
  275. {
  276. firstEnumerator.DisposeAsync();
  277. });
  278. }
  279. _state = AsyncIteratorState.Iterating;
  280. if (await winner.ConfigureAwait(false))
  281. {
  282. _current = _enumerator.Current;
  283. return true;
  284. }
  285. break;
  286. case AsyncIteratorState.Iterating:
  287. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  288. {
  289. _current = _enumerator.Current;
  290. return true;
  291. }
  292. break;
  293. }
  294. await DisposeAsync().ConfigureAwait(false);
  295. return false;
  296. }
  297. }
  298. private sealed class AmbAsyncIteratorN<TSource> : AsyncIterator<TSource>
  299. {
  300. private readonly IAsyncEnumerable<TSource>[] _sources;
  301. private IAsyncEnumerator<TSource> _enumerator;
  302. public AmbAsyncIteratorN(IAsyncEnumerable<TSource>[] sources)
  303. {
  304. Debug.Assert(sources != null);
  305. _sources = sources;
  306. }
  307. public override AsyncIteratorBase<TSource> Clone()
  308. {
  309. return new AmbAsyncIteratorN<TSource>(_sources);
  310. }
  311. public override async ValueTask DisposeAsync()
  312. {
  313. if (_enumerator != null)
  314. {
  315. await _enumerator.DisposeAsync().ConfigureAwait(false);
  316. _enumerator = null;
  317. }
  318. await base.DisposeAsync().ConfigureAwait(false);
  319. }
  320. protected override async ValueTask<bool> MoveNextCore()
  321. {
  322. switch (_state)
  323. {
  324. case AsyncIteratorState.Allocated:
  325. var n = _sources.Length;
  326. var enumerators = new IAsyncEnumerator<TSource>[n];
  327. var moveNexts = new Task<bool>[n];
  328. for (var i = 0; i < n; i++)
  329. {
  330. var enumerator = _sources[i].GetAsyncEnumerator(_cancellationToken);
  331. enumerators[i] = enumerator;
  332. moveNexts[i] = enumerator.MoveNextAsync().AsTask();
  333. }
  334. var winner = await Task.WhenAny(moveNexts).ConfigureAwait(false);
  335. //
  336. // REVIEW: An alternative option is to call DisposeAsync on the other and await it, but this has two drawbacks:
  337. //
  338. // 1. Concurrent DisposeAsync while a MoveNextAsync is in flight.
  339. // 2. The winner elected by Amb is blocked to yield results until all losers unblocks.
  340. //
  341. // The approach below has one drawback, namely that exceptions raised by any loser are dropped on the floor.
  342. //
  343. var winnerIndex = Array.IndexOf(moveNexts, winner);
  344. _enumerator = enumerators[winnerIndex];
  345. for (var i = 0; i < n; i++)
  346. {
  347. if (i != winnerIndex)
  348. {
  349. _ = moveNexts[i].ContinueWith(_ =>
  350. {
  351. enumerators[i].DisposeAsync();
  352. });
  353. }
  354. }
  355. _state = AsyncIteratorState.Iterating;
  356. if (await winner.ConfigureAwait(false))
  357. {
  358. _current = _enumerator.Current;
  359. return true;
  360. }
  361. break;
  362. case AsyncIteratorState.Iterating:
  363. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  364. {
  365. _current = _enumerator.Current;
  366. return true;
  367. }
  368. break;
  369. }
  370. await DisposeAsync().ConfigureAwait(false);
  371. return false;
  372. }
  373. }
  374. #endif
  375. }
  376. }