AsyncIterator.cs 4.0 KB

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