RefCountDisposable.cs 4.3 KB

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