AsyncIterator.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 ? this : 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.IsCancellationRequested)
  43. {
  44. cancellationTokenSource.Cancel();
  45. }
  46. cancellationTokenSource.Dispose();
  47. current = default(TSource);
  48. state = AsyncIteratorState.Disposed;
  49. }
  50. public TSource Current
  51. {
  52. get
  53. {
  54. if (currentIsInvalid)
  55. throw new InvalidOperationException("Enumerator is in an invalid state");
  56. return current;
  57. }
  58. }
  59. public async Task<bool> MoveNext(CancellationToken cancellationToken)
  60. {
  61. if (state == AsyncIteratorState.Disposed)
  62. {
  63. return false;
  64. }
  65. using (cancellationToken.Register(Dispose))
  66. using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancellationTokenSource.Token))
  67. {
  68. try
  69. {
  70. // Short circuit and don't even call MoveNexCore
  71. cancellationToken.ThrowIfCancellationRequested();
  72. var result = await MoveNextCore(cts.Token)
  73. .ConfigureAwait(false);
  74. currentIsInvalid = !result; // if move next is false, invalid otherwise valid
  75. return result;
  76. }
  77. catch
  78. {
  79. currentIsInvalid = true;
  80. Dispose();
  81. throw;
  82. }
  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(CancellationToken cancellationToken);
  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. }