StableCompositeDisposable.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.Threading;
  6. namespace System.Reactive.Disposables
  7. {
  8. /// <summary>
  9. /// Represents a group of disposable resources that are disposed together.
  10. /// </summary>
  11. public abstract class StableCompositeDisposable : ICancelable
  12. {
  13. /// <summary>
  14. /// Creates a new group containing two disposable resources that are disposed together.
  15. /// </summary>
  16. /// <param name="disposable1">The first disposable resource to add to the group.</param>
  17. /// <param name="disposable2">The second disposable resource to add to the group.</param>
  18. /// <returns>Group of disposable resources that are disposed together.</returns>
  19. public static ICancelable Create(IDisposable disposable1, IDisposable disposable2)
  20. {
  21. if (disposable1 == null)
  22. {
  23. throw new ArgumentNullException(nameof(disposable1));
  24. }
  25. if (disposable2 == null)
  26. {
  27. throw new ArgumentNullException(nameof(disposable2));
  28. }
  29. return new Binary(disposable1, disposable2);
  30. }
  31. /// <summary>
  32. /// Creates a new group of disposable resources that are disposed together.
  33. /// </summary>
  34. /// <param name="disposables">Disposable resources to add to the group.</param>
  35. /// <returns>Group of disposable resources that are disposed together.</returns>
  36. public static ICancelable Create(params IDisposable[] disposables)
  37. {
  38. if (disposables == null)
  39. {
  40. throw new ArgumentNullException(nameof(disposables));
  41. }
  42. return new NAry(disposables);
  43. }
  44. /// <summary>
  45. /// Creates a new group of disposable resources that are disposed together.
  46. /// </summary>
  47. /// <param name="disposables">Disposable resources to add to the group.</param>
  48. /// <returns>Group of disposable resources that are disposed together.</returns>
  49. public static ICancelable Create(IEnumerable<IDisposable> disposables)
  50. {
  51. if (disposables == null)
  52. {
  53. throw new ArgumentNullException(nameof(disposables));
  54. }
  55. return new NAry(disposables);
  56. }
  57. /// <summary>
  58. /// Disposes all disposables in the group.
  59. /// </summary>
  60. public abstract void Dispose();
  61. /// <summary>
  62. /// Gets a value that indicates whether the object is disposed.
  63. /// </summary>
  64. public abstract bool IsDisposed
  65. {
  66. get;
  67. }
  68. private sealed class Binary : StableCompositeDisposable
  69. {
  70. private IDisposable _disposable1;
  71. private IDisposable _disposable2;
  72. public Binary(IDisposable disposable1, IDisposable disposable2)
  73. {
  74. Volatile.Write(ref _disposable1, disposable1);
  75. Volatile.Write(ref _disposable2, disposable2);
  76. }
  77. public override bool IsDisposed => Disposable.GetIsDisposed(ref _disposable1);
  78. public override void Dispose()
  79. {
  80. Disposable.TryDispose(ref _disposable1);
  81. Disposable.TryDispose(ref _disposable2);
  82. }
  83. }
  84. private sealed class NAry : StableCompositeDisposable
  85. {
  86. private volatile List<IDisposable> _disposables;
  87. public NAry(IDisposable[] disposables)
  88. : this((IEnumerable<IDisposable>)disposables)
  89. {
  90. }
  91. public NAry(IEnumerable<IDisposable> disposables)
  92. {
  93. _disposables = new List<IDisposable>(disposables);
  94. //
  95. // Doing this on the list to avoid duplicate enumeration of disposables.
  96. //
  97. if (_disposables.Contains(null))
  98. {
  99. throw new ArgumentException(Strings_Core.DISPOSABLES_CANT_CONTAIN_NULL, nameof(disposables));
  100. }
  101. }
  102. public override bool IsDisposed => _disposables == null;
  103. public override void Dispose()
  104. {
  105. var old = Interlocked.Exchange(ref _disposables, null);
  106. if (old != null)
  107. {
  108. foreach (var d in old)
  109. {
  110. d.Dispose();
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }