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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. internal abstract class AsyncIterator<TSource> : IAsyncEnumerable<TSource>, IAsyncEnumerator<TSource>
  14. {
  15. private readonly int threadId;
  16. private CancellationTokenSource cancellationTokenSource;
  17. internal TSource current;
  18. private bool currentIsInvalid = true;
  19. internal AsyncIteratorState state = AsyncIteratorState.New;
  20. protected AsyncIterator()
  21. {
  22. threadId = Environment.CurrentManagedThreadId;
  23. }
  24. public IAsyncEnumerator<TSource> GetEnumerator()
  25. {
  26. var enumerator = state == AsyncIteratorState.New && threadId == Environment.CurrentManagedThreadId ?
  27. this :
  28. Clone();
  29. enumerator.state = AsyncIteratorState.Allocated;
  30. enumerator.cancellationTokenSource = new CancellationTokenSource();
  31. try
  32. {
  33. enumerator.OnGetEnumerator();
  34. }
  35. catch
  36. {
  37. enumerator.Dispose();
  38. throw;
  39. }
  40. return enumerator;
  41. }
  42. public virtual void Dispose()
  43. {
  44. if (cancellationTokenSource != null)
  45. {
  46. if (!cancellationTokenSource.IsCancellationRequested)
  47. {
  48. cancellationTokenSource.Cancel();
  49. }
  50. cancellationTokenSource.Dispose();
  51. }
  52. current = default(TSource);
  53. state = AsyncIteratorState.Disposed;
  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> MoveNext(CancellationToken cancellationToken)
  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. using (cancellationToken.Register(Dispose))
  74. using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancellationTokenSource.Token))
  75. {
  76. try
  77. {
  78. // Short circuit and don't even call MoveNexCore
  79. cancellationToken.ThrowIfCancellationRequested();
  80. var result = await MoveNextCore(cts.Token)
  81. .ConfigureAwait(false);
  82. currentIsInvalid = !result; // if move next is false, invalid otherwise valid
  83. return result;
  84. }
  85. catch
  86. {
  87. currentIsInvalid = true;
  88. Dispose();
  89. throw;
  90. }
  91. }
  92. }
  93. public abstract AsyncIterator<TSource> Clone();
  94. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
  95. {
  96. return new SelectEnumerableAsyncIterator<TSource, TResult>(this, selector);
  97. }
  98. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, bool> predicate)
  99. {
  100. return new WhereEnumerableAsyncIterator<TSource>(this, predicate);
  101. }
  102. protected abstract Task<bool> MoveNextCore(CancellationToken cancellationToken);
  103. protected virtual void OnGetEnumerator()
  104. {
  105. }
  106. }
  107. internal enum AsyncIteratorState
  108. {
  109. New = 0,
  110. Allocated = 1,
  111. Iterating = 2,
  112. Disposed = -1
  113. }
  114. }
  115. }