AsyncIterator.cs 4.2 KB

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