StableCompositeDisposable.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. 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 NAryArray(disposables);
  43. }
  44. /// <summary>
  45. /// Creates a group of disposable resources that are disposed together
  46. /// and without copying or checking for nulls inside the group.
  47. /// </summary>
  48. /// <param name="disposables">The array of disposables that is trusted
  49. /// to not contain nulls and gives no need to defensively copy it.</param>
  50. /// <returns>Group of disposable resources that are disposed together.</returns>
  51. internal static ICancelable CreateTrusted(params IDisposable[] disposables)
  52. {
  53. return new NAryTrustedArray(disposables);
  54. }
  55. /// <summary>
  56. /// Creates a new group of disposable resources that are disposed together.
  57. /// </summary>
  58. /// <param name="disposables">Disposable resources to add to the group.</param>
  59. /// <returns>Group of disposable resources that are disposed together.</returns>
  60. public static ICancelable Create(IEnumerable<IDisposable> disposables)
  61. {
  62. if (disposables == null)
  63. {
  64. throw new ArgumentNullException(nameof(disposables));
  65. }
  66. return new NAryEnumerable(disposables);
  67. }
  68. /// <summary>
  69. /// Disposes all disposables in the group.
  70. /// </summary>
  71. public abstract void Dispose();
  72. /// <summary>
  73. /// Gets a value that indicates whether the object is disposed.
  74. /// </summary>
  75. public abstract bool IsDisposed
  76. {
  77. get;
  78. }
  79. private sealed class Binary : StableCompositeDisposable
  80. {
  81. private IDisposable? _disposable1;
  82. private IDisposable? _disposable2;
  83. public Binary(IDisposable disposable1, IDisposable disposable2)
  84. {
  85. Volatile.Write(ref _disposable1, disposable1);
  86. Volatile.Write(ref _disposable2, disposable2);
  87. }
  88. public override bool IsDisposed => Disposable.GetIsDisposed(ref _disposable1);
  89. public override void Dispose()
  90. {
  91. Disposable.Dispose(ref _disposable1);
  92. Disposable.Dispose(ref _disposable2);
  93. }
  94. }
  95. private sealed class NAryEnumerable : StableCompositeDisposable
  96. {
  97. private volatile List<IDisposable>? _disposables;
  98. public NAryEnumerable(IEnumerable<IDisposable> disposables)
  99. {
  100. _disposables = new List<IDisposable>(disposables);
  101. //
  102. // Doing this on the list to avoid duplicate enumeration of disposables.
  103. //
  104. if (_disposables.Contains(null!))
  105. {
  106. throw new ArgumentException(Strings_Core.DISPOSABLES_CANT_CONTAIN_NULL, nameof(disposables));
  107. }
  108. }
  109. public override bool IsDisposed => _disposables == null;
  110. public override void Dispose()
  111. {
  112. var old = Interlocked.Exchange(ref _disposables, null);
  113. if (old != null)
  114. {
  115. foreach (var d in old)
  116. {
  117. d.Dispose();
  118. }
  119. }
  120. }
  121. }
  122. private sealed class NAryArray : StableCompositeDisposable
  123. {
  124. private IDisposable[]? _disposables;
  125. public NAryArray(IDisposable[] disposables)
  126. {
  127. if (Array.IndexOf(disposables, null!) != -1)
  128. {
  129. throw new ArgumentException(Strings_Core.DISPOSABLES_CANT_CONTAIN_NULL, nameof(disposables));
  130. }
  131. var n = disposables.Length;
  132. var ds = new IDisposable[n];
  133. Array.Copy(disposables, 0, ds, 0, n);
  134. Volatile.Write(ref _disposables, ds);
  135. }
  136. public override bool IsDisposed => Volatile.Read(ref _disposables) == null;
  137. public override void Dispose()
  138. {
  139. var old = Interlocked.Exchange(ref _disposables, null);
  140. if (old != null)
  141. {
  142. foreach (var d in old)
  143. {
  144. d.Dispose();
  145. }
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// A stable composite that doesn't do defensive copy of
  151. /// the input disposable array nor checks it for null.
  152. /// </summary>
  153. private sealed class NAryTrustedArray : StableCompositeDisposable
  154. {
  155. private IDisposable[]? _disposables;
  156. public NAryTrustedArray(IDisposable[] disposables)
  157. {
  158. Volatile.Write(ref _disposables, disposables);
  159. }
  160. public override bool IsDisposed => Volatile.Read(ref _disposables) == null;
  161. public override void Dispose()
  162. {
  163. var old = Interlocked.Exchange(ref _disposables, null);
  164. if (old != null)
  165. {
  166. foreach (var d in old)
  167. {
  168. d.Dispose();
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }