StableCompositeAsyncDisposable.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Reactive.Disposables
  8. {
  9. public abstract class StableCompositeAsyncDisposable : IAsyncDisposable
  10. {
  11. public static StableCompositeAsyncDisposable Create(IAsyncDisposable disposable1, IAsyncDisposable disposable2)
  12. {
  13. if (disposable1 == null)
  14. throw new ArgumentNullException(nameof(disposable1));
  15. if (disposable2 == null)
  16. throw new ArgumentNullException(nameof(disposable2));
  17. return new Binary(disposable1, disposable2);
  18. }
  19. public static StableCompositeAsyncDisposable Create(params IAsyncDisposable[] disposables)
  20. {
  21. if (disposables == null)
  22. throw new ArgumentNullException(nameof(disposables));
  23. return new NAry(disposables);
  24. }
  25. public static StableCompositeAsyncDisposable Create(IEnumerable<IAsyncDisposable> disposables)
  26. {
  27. if (disposables == null)
  28. throw new ArgumentNullException(nameof(disposables));
  29. return new NAry(disposables);
  30. }
  31. public abstract ValueTask DisposeAsync();
  32. private sealed class Binary : StableCompositeAsyncDisposable
  33. {
  34. private volatile IAsyncDisposable _disposable1;
  35. private volatile IAsyncDisposable _disposable2;
  36. public Binary(IAsyncDisposable disposable1, IAsyncDisposable disposable2)
  37. {
  38. _disposable1 = disposable1;
  39. _disposable2 = disposable2;
  40. }
  41. public override async ValueTask DisposeAsync()
  42. {
  43. var d1 = Interlocked.Exchange(ref _disposable1, null);
  44. if (d1 != null)
  45. {
  46. await d1.DisposeAsync().ConfigureAwait(false);
  47. }
  48. var d2 = Interlocked.Exchange(ref _disposable2, null);
  49. if (d2 != null)
  50. {
  51. await d2.DisposeAsync().ConfigureAwait(false);
  52. }
  53. }
  54. }
  55. private sealed class NAry : StableCompositeAsyncDisposable
  56. {
  57. private volatile List<IAsyncDisposable> _disposables;
  58. public NAry(IAsyncDisposable[] disposables)
  59. : this((IEnumerable<IAsyncDisposable>)disposables)
  60. {
  61. }
  62. public NAry(IEnumerable<IAsyncDisposable> disposables)
  63. {
  64. _disposables = new List<IAsyncDisposable>(disposables);
  65. }
  66. public override async ValueTask DisposeAsync()
  67. {
  68. var old = Interlocked.Exchange(ref _disposables, null);
  69. if (old != null)
  70. {
  71. foreach (var d in old)
  72. {
  73. if (d != null)
  74. {
  75. await d.DisposeAsync().ConfigureAwait(false);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }