StableCompositeDisposable.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 resoruce to add to the group.</param>
  17. /// <param name="disposable2">The second disposable resoruce 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. throw new ArgumentNullException("disposable1");
  23. if (disposable2 == null)
  24. throw new ArgumentNullException("disposable2");
  25. return new Binary(disposable1, disposable2);
  26. }
  27. /// <summary>
  28. /// Creates a new group of disposable resources that are disposed together.
  29. /// </summary>
  30. /// <param name="disposables">Disposable resources to add to the group.</param>
  31. /// <returns>Group of disposable resources that are disposed together.</returns>
  32. public static ICancelable Create(params IDisposable[] disposables)
  33. {
  34. if (disposables == null)
  35. throw new ArgumentNullException("disposables");
  36. return new NAry(disposables);
  37. }
  38. /// <summary>
  39. /// Creates a new group of disposable resources that are disposed together.
  40. /// </summary>
  41. /// <param name="disposables">Disposable resources to add to the group.</param>
  42. /// <returns>Group of disposable resources that are disposed together.</returns>
  43. public static ICancelable Create(IEnumerable<IDisposable> disposables)
  44. {
  45. if (disposables == null)
  46. throw new ArgumentNullException("disposables");
  47. return new NAry(disposables);
  48. }
  49. /// <summary>
  50. /// Disposes all disposables in the group.
  51. /// </summary>
  52. public abstract void Dispose();
  53. /// <summary>
  54. /// Gets a value that indicates whether the object is disposed.
  55. /// </summary>
  56. public abstract bool IsDisposed
  57. {
  58. get;
  59. }
  60. class Binary : StableCompositeDisposable
  61. {
  62. private volatile IDisposable _disposable1;
  63. private volatile IDisposable _disposable2;
  64. public Binary(IDisposable disposable1, IDisposable disposable2)
  65. {
  66. _disposable1 = disposable1;
  67. _disposable2 = disposable2;
  68. }
  69. public override bool IsDisposed
  70. {
  71. get
  72. {
  73. return _disposable1 == null;
  74. }
  75. }
  76. public override void Dispose()
  77. {
  78. #pragma warning disable 0420
  79. var old1 = Interlocked.Exchange(ref _disposable1, null);
  80. #pragma warning restore 0420
  81. if (old1 != null)
  82. {
  83. old1.Dispose();
  84. }
  85. #pragma warning disable 0420
  86. var old2 = Interlocked.Exchange(ref _disposable2, null);
  87. #pragma warning restore 0420
  88. if (old2 != null)
  89. {
  90. old2.Dispose();
  91. }
  92. }
  93. }
  94. class NAry : StableCompositeDisposable
  95. {
  96. private volatile List<IDisposable> _disposables;
  97. public NAry(IDisposable[] disposables)
  98. : this((IEnumerable<IDisposable>)disposables)
  99. {
  100. }
  101. public NAry(IEnumerable<IDisposable> disposables)
  102. {
  103. _disposables = new List<IDisposable>(disposables);
  104. //
  105. // Doing this on the list to avoid duplicate enumeration of disposables.
  106. //
  107. if (_disposables.Contains(null))
  108. throw new ArgumentException(Strings_Core.DISPOSABLES_CANT_CONTAIN_NULL, "disposables");
  109. }
  110. public override bool IsDisposed
  111. {
  112. get
  113. {
  114. return _disposables == null;
  115. }
  116. }
  117. public override void Dispose()
  118. {
  119. #pragma warning disable 0420
  120. var old = Interlocked.Exchange(ref _disposables, null);
  121. #pragma warning restore 0420
  122. if (old != null)
  123. {
  124. foreach (var d in old)
  125. {
  126. d.Dispose();
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }