RefCountDisposable.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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;
  5. using System.Threading;
  6. namespace System.Reactive.Disposables
  7. {
  8. /// <summary>
  9. /// Represents a disposable resource that only disposes its underlying disposable resource when all <see cref="GetDisposable">dependent disposable objects</see> have been disposed.
  10. /// </summary>
  11. public sealed class RefCountDisposable : ICancelable
  12. {
  13. private readonly bool _throwWhenDisposed;
  14. private readonly object _gate = new object();
  15. private IDisposable _disposable;
  16. private bool _isPrimaryDisposed;
  17. private int _count;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.RefCountDisposable"/> class with the specified disposable.
  20. /// </summary>
  21. /// <param name="disposable">Underlying disposable.</param>
  22. /// <exception cref="ArgumentNullException"><paramref name="disposable"/> is null.</exception>
  23. public RefCountDisposable(IDisposable disposable) : this(disposable, false)
  24. {
  25. }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.RefCountDisposable"/> class with the specified disposable.
  28. /// </summary>
  29. /// <param name="disposable">Underlying disposable.</param>
  30. /// <param name="throwWhenDisposed">Indicates whether subsequent calls to <see cref="GetDisposable"/> should throw when this instance is disposed.</param>
  31. /// <exception cref="ArgumentNullException"><paramref name="disposable"/> is null.</exception>
  32. public RefCountDisposable(IDisposable disposable, bool throwWhenDisposed)
  33. {
  34. if (disposable == null)
  35. throw new ArgumentNullException(nameof(disposable));
  36. _disposable = disposable;
  37. _isPrimaryDisposed = false;
  38. _count = 0;
  39. _throwWhenDisposed = throwWhenDisposed;
  40. }
  41. /// <summary>
  42. /// Gets a value that indicates whether the object is disposed.
  43. /// </summary>
  44. public bool IsDisposed
  45. {
  46. get { return _disposable == null; }
  47. }
  48. /// <summary>
  49. /// Returns a dependent disposable that when disposed decreases the refcount on the underlying disposable.
  50. /// </summary>
  51. /// <returns>A dependent disposable contributing to the reference count that manages the underlying disposable's lifetime.</returns>
  52. /// <exception cref="ObjectDisposedException">This instance has been disposed and is configured to throw in this case by <see cref="RefCountDisposable(IDisposable, bool)"/>.</exception>
  53. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Backward compat + non-trivial work for a property getter.")]
  54. public IDisposable GetDisposable()
  55. {
  56. lock (_gate)
  57. {
  58. if (_disposable == null)
  59. {
  60. if (_throwWhenDisposed)
  61. throw new ObjectDisposedException("RefCountDisposable");
  62. return Disposable.Empty;
  63. }
  64. else
  65. {
  66. _count++;
  67. return new InnerDisposable(this);
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Disposes the underlying disposable only when all dependent disposables have been disposed.
  73. /// </summary>
  74. public void Dispose()
  75. {
  76. var disposable = default(IDisposable);
  77. lock (_gate)
  78. {
  79. if (_disposable != null)
  80. {
  81. if (!_isPrimaryDisposed)
  82. {
  83. _isPrimaryDisposed = true;
  84. if (_count == 0)
  85. {
  86. disposable = _disposable;
  87. _disposable = null;
  88. }
  89. }
  90. }
  91. }
  92. if (disposable != null)
  93. disposable.Dispose();
  94. }
  95. private void Release()
  96. {
  97. var disposable = default(IDisposable);
  98. lock (_gate)
  99. {
  100. if (_disposable != null)
  101. {
  102. _count--;
  103. System.Diagnostics.Debug.Assert(_count >= 0);
  104. if (_isPrimaryDisposed)
  105. {
  106. if (_count == 0)
  107. {
  108. disposable = _disposable;
  109. _disposable = null;
  110. }
  111. }
  112. }
  113. }
  114. if (disposable != null)
  115. disposable.Dispose();
  116. }
  117. sealed class InnerDisposable : IDisposable
  118. {
  119. private RefCountDisposable _parent;
  120. public InnerDisposable(RefCountDisposable parent)
  121. {
  122. _parent = parent;
  123. }
  124. public void Dispose()
  125. {
  126. var parent = Interlocked.Exchange(ref _parent, null);
  127. if (parent != null)
  128. parent.Release();
  129. }
  130. }
  131. }
  132. }