AsyncIterator.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  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. public enum State
  14. {
  15. New = 0,
  16. Allocated = 1,
  17. Iterating = 2,
  18. Disposed = -1,
  19. }
  20. private readonly int threadId;
  21. internal State state = State.New;
  22. internal TSource current;
  23. private CancellationTokenSource cancellationTokenSource;
  24. private List<CancellationTokenRegistration> moveNextRegistrations;
  25. private bool currentIsInvalid = true;
  26. protected AsyncIterator()
  27. {
  28. threadId = Environment.CurrentManagedThreadId;
  29. }
  30. public abstract AsyncIterator<TSource> Clone();
  31. public IAsyncEnumerator<TSource> GetEnumerator()
  32. {
  33. var enumerator = state == State.New && threadId == Environment.CurrentManagedThreadId ? this : Clone();
  34. enumerator.state = State.Allocated;
  35. enumerator.cancellationTokenSource = new CancellationTokenSource();
  36. enumerator.moveNextRegistrations = new List<CancellationTokenRegistration>();
  37. return enumerator;
  38. }
  39. public virtual void Dispose()
  40. {
  41. if (!cancellationTokenSource.IsCancellationRequested)
  42. {
  43. cancellationTokenSource.Cancel();
  44. }
  45. cancellationTokenSource.Dispose();
  46. current = default(TSource);
  47. state = State.Disposed;
  48. var toClean = moveNextRegistrations?.ToList();
  49. moveNextRegistrations = null;
  50. if (toClean != null)
  51. {
  52. foreach (var r in toClean)
  53. {
  54. r.Dispose();
  55. }
  56. toClean.Clear();
  57. }
  58. }
  59. public TSource Current
  60. {
  61. get
  62. {
  63. if (currentIsInvalid)
  64. throw new InvalidOperationException("Enumerator is in an invalid state");
  65. return current;
  66. }
  67. }
  68. public async Task<bool> MoveNext(CancellationToken cancellationToken)
  69. {
  70. if (state == State.Disposed)
  71. {
  72. return false;
  73. }
  74. // We keep these because cancelling any of these must trigger dispose of the iterator
  75. moveNextRegistrations.Add(cancellationToken.Register(Dispose));
  76. using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancellationTokenSource.Token))
  77. {
  78. try
  79. {
  80. var result = await MoveNextCore(cts.Token).ConfigureAwait(false);
  81. currentIsInvalid = !result; // if move next is false, invalid otherwise valid
  82. return result;
  83. }
  84. catch
  85. {
  86. currentIsInvalid = true;
  87. Dispose();
  88. throw;
  89. }
  90. }
  91. }
  92. protected abstract Task<bool> MoveNextCore(CancellationToken cancellationToken);
  93. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
  94. {
  95. return new SelectEnumerableAsyncIterator<TSource, TResult>(this, selector);
  96. }
  97. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, bool> predicate)
  98. {
  99. return new WhereEnumerableAsyncIterator<TSource>(this, predicate);
  100. }
  101. }
  102. }
  103. }