AsyncIterator.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 AsyncEnumerable
  10. {
  11. private static readonly Task CompletedTask = TaskExt.True;
  12. internal abstract class AsyncIterator<TSource> : IAsyncEnumerable<TSource>, IAsyncEnumerator<TSource>
  13. {
  14. private readonly int threadId;
  15. private CancellationTokenSource cancellationTokenSource;
  16. private bool currentIsInvalid = true;
  17. internal TSource current;
  18. internal AsyncIteratorState state = AsyncIteratorState.New;
  19. protected AsyncIterator()
  20. {
  21. threadId = Environment.CurrentManagedThreadId;
  22. }
  23. public IAsyncEnumerator<TSource> GetAsyncEnumerator()
  24. {
  25. var enumerator = state == AsyncIteratorState.New && threadId == Environment.CurrentManagedThreadId ?
  26. this :
  27. Clone();
  28. enumerator.state = AsyncIteratorState.Allocated;
  29. enumerator.cancellationTokenSource = new CancellationTokenSource();
  30. try
  31. {
  32. enumerator.OnGetEnumerator();
  33. }
  34. catch
  35. {
  36. enumerator.DisposeAsync(); // REVIEW: fire-and-forget?
  37. throw;
  38. }
  39. return enumerator;
  40. }
  41. public virtual Task DisposeAsync()
  42. {
  43. if (cancellationTokenSource != null)
  44. {
  45. if (!cancellationTokenSource.IsCancellationRequested)
  46. {
  47. cancellationTokenSource.Cancel();
  48. }
  49. cancellationTokenSource.Dispose();
  50. }
  51. current = default(TSource);
  52. state = AsyncIteratorState.Disposed;
  53. return CompletedTask;
  54. }
  55. public TSource Current
  56. {
  57. get
  58. {
  59. if (currentIsInvalid)
  60. throw new InvalidOperationException("Enumerator is in an invalid state");
  61. return current;
  62. }
  63. }
  64. public async Task<bool> MoveNextAsync()
  65. {
  66. // Note: MoveNext *must* be implemented as an async method to ensure
  67. // that any exceptions thrown from the MoveNextCore call are handled
  68. // by the try/catch, whether they're sync or async
  69. if (state == AsyncIteratorState.Disposed)
  70. {
  71. return false;
  72. }
  73. try
  74. {
  75. var result = await MoveNextCore().ConfigureAwait(false);
  76. currentIsInvalid = !result; // if move next is false, invalid otherwise valid
  77. return result;
  78. }
  79. catch
  80. {
  81. currentIsInvalid = true;
  82. await DisposeAsync().ConfigureAwait(false);
  83. throw;
  84. }
  85. }
  86. public abstract AsyncIterator<TSource> Clone();
  87. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
  88. {
  89. return new SelectEnumerableAsyncIterator<TSource, TResult>(this, selector);
  90. }
  91. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, Task<TResult>> selector)
  92. {
  93. return new SelectEnumerableAsyncIteratorWithTask<TSource, TResult>(this, selector);
  94. }
  95. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, bool> predicate)
  96. {
  97. return new WhereEnumerableAsyncIterator<TSource>(this, predicate);
  98. }
  99. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, Task<bool>> predicate)
  100. {
  101. return new WhereEnumerableAsyncIteratorWithTask<TSource>(this, predicate);
  102. }
  103. protected abstract Task<bool> MoveNextCore();
  104. protected virtual void OnGetEnumerator()
  105. {
  106. }
  107. }
  108. internal enum AsyncIteratorState
  109. {
  110. New = 0,
  111. Allocated = 1,
  112. Iterating = 2,
  113. Disposed = -1
  114. }
  115. }
  116. }