Finally.cs 4.4 KB

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