AsyncIterator.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. protected AsyncIterator()
  25. {
  26. threadId = Environment.CurrentManagedThreadId;
  27. }
  28. public abstract AsyncIterator<TSource> Clone();
  29. public IAsyncEnumerator<TSource> GetEnumerator()
  30. {
  31. var enumerator = state == State.New && threadId == Environment.CurrentManagedThreadId ? this : Clone();
  32. enumerator.state = State.Allocated;
  33. enumerator.cancellationTokenSource = new CancellationTokenSource();
  34. return enumerator;
  35. }
  36. public virtual void Dispose()
  37. {
  38. if (!cancellationTokenSource.IsCancellationRequested)
  39. {
  40. cancellationTokenSource.Cancel();
  41. }
  42. cancellationTokenSource.Dispose();
  43. current = default(TSource);
  44. state = State.Disposed;
  45. }
  46. private void Cancel()
  47. {
  48. if (!cancellationTokenSource.IsCancellationRequested)
  49. {
  50. cancellationTokenSource.Cancel();
  51. }
  52. Dispose();
  53. Debug.WriteLine("Canceled");
  54. }
  55. public TSource Current => current;
  56. public async Task<bool> MoveNext(CancellationToken cancellationToken)
  57. {
  58. // using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancellationTokenSource.Token))
  59. using (cancellationToken.Register(Cancel))
  60. {
  61. try
  62. {
  63. var result = await MoveNextCore(cancellationTokenSource.Token).ConfigureAwait(false);
  64. // cts.Dispose();
  65. //if (cts.IsCancellationRequested)
  66. //{
  67. // Dispose();
  68. //}
  69. return result;
  70. }
  71. catch
  72. {
  73. Dispose();
  74. throw;
  75. }
  76. }
  77. }
  78. public abstract Task<bool> MoveNextCore(CancellationToken cancellationToken);
  79. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
  80. {
  81. return new SelectEnumerableAsyncIterator<TSource, TResult>(this, selector);
  82. }
  83. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, bool> predicate)
  84. {
  85. return new WhereEnumerableAsyncIterator<TSource>(this, predicate);
  86. }
  87. }
  88. }
  89. }