Finally.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> Finally<TSource>(this IAsyncEnumerable<TSource> source, Action finallyAction)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (finallyAction == null)
  17. throw new ArgumentNullException(nameof(finallyAction));
  18. return new FinallyAsyncIterator<TSource>(source, finallyAction);
  19. }
  20. private sealed class FinallyAsyncIterator<TSource> : AsyncIterator<TSource>
  21. {
  22. private readonly Action finallyAction;
  23. private readonly IAsyncEnumerable<TSource> source;
  24. private IAsyncEnumerator<TSource> enumerator;
  25. CancellationTokenRegistration _tokenRegistration;
  26. int _once;
  27. public FinallyAsyncIterator(IAsyncEnumerable<TSource> source, Action finallyAction)
  28. {
  29. Debug.Assert(source != null);
  30. Debug.Assert(finallyAction != null);
  31. this.source = source;
  32. this.finallyAction = finallyAction;
  33. }
  34. public override AsyncIterator<TSource> Clone()
  35. {
  36. return new FinallyAsyncIterator<TSource>(source, finallyAction);
  37. }
  38. public override void Dispose()
  39. {
  40. // This could now be executed by either MoveNextCore
  41. // or the trigger from a CancellationToken
  42. // make sure this happens at most once.
  43. if (Interlocked.CompareExchange(ref _once, 1, 0) == 0)
  44. {
  45. if (enumerator != null)
  46. {
  47. enumerator.Dispose();
  48. // make sure the clearing of the enumerator
  49. // becomes visible to MoveNextCore
  50. Volatile.Write(ref enumerator, null);
  51. finallyAction();
  52. }
  53. base.Dispose();
  54. _tokenRegistration.Dispose();
  55. }
  56. }
  57. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  58. {
  59. switch (state)
  60. {
  61. case AsyncIteratorState.Allocated:
  62. enumerator = source.GetEnumerator();
  63. state = AsyncIteratorState.Iterating;
  64. goto case AsyncIteratorState.Iterating;
  65. case AsyncIteratorState.Iterating:
  66. // clear any previous registration
  67. _tokenRegistration.Dispose();
  68. // and setup a new registration
  69. // we can't know if the token is the same as last time
  70. // note that the registration extends the lifetime of "this"
  71. // so the current AsyncIterator better not be just abandoned
  72. _tokenRegistration = cancellationToken.Register(
  73. state => ((FinallyAsyncIterator<TSource>)state).Dispose(), this);
  74. // Now that the CancellationToken may call Dispose
  75. // from any thread while the current thread is in
  76. // MoveNextCore, we must make sure the enumerator
  77. // hasn't been cleared out in the meantime
  78. var en = Volatile.Read(ref enumerator);
  79. if (en != null)
  80. {
  81. if (await en.MoveNext(cancellationToken)
  82. .ConfigureAwait(false))
  83. {
  84. current = enumerator.Current;
  85. return true;
  86. }
  87. Dispose();
  88. }
  89. break;
  90. }
  91. return false;
  92. }
  93. }
  94. }
  95. }