AsyncIterator.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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> GetEnumerator()
  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> MoveNext(CancellationToken cancellationToken)
  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. using (cancellationToken.Register(Dispose))
  72. using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancellationTokenSource.Token))
  73. {
  74. try
  75. {
  76. // Short circuit and don't even call MoveNexCore
  77. cancellationToken.ThrowIfCancellationRequested();
  78. var result = await MoveNextCore(cts.Token)
  79. .ConfigureAwait(false);
  80. currentIsInvalid = !result; // if move next is false, invalid otherwise valid
  81. return result;
  82. }
  83. catch
  84. {
  85. currentIsInvalid = true;
  86. Dispose();
  87. throw;
  88. }
  89. }
  90. }
  91. public abstract AsyncIterator<TSource> Clone();
  92. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
  93. {
  94. return new SelectEnumerableAsyncIterator<TSource, TResult>(this, selector);
  95. }
  96. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, bool> predicate)
  97. {
  98. return new WhereEnumerableAsyncIterator<TSource>(this, predicate);
  99. }
  100. protected abstract Task<bool> MoveNextCore(CancellationToken cancellationToken);
  101. protected virtual void OnGetEnumerator()
  102. {
  103. }
  104. }
  105. internal enum AsyncIteratorState
  106. {
  107. New = 0,
  108. Allocated = 1,
  109. Iterating = 2,
  110. Disposed = -1
  111. }
  112. }
  113. }