RefCountDisposable.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // This code is borrwed from Rx Official and some modified.
  2. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Threading;
  7. namespace UniRx
  8. {
  9. /// <summary>
  10. /// Represents a disposable resource that only disposes its underlying disposable resource when all <see cref="GetDisposable">dependent disposable objects</see> have been disposed.
  11. /// </summary>
  12. public sealed class RefCountDisposable : ICancelable
  13. {
  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)
  24. {
  25. if (disposable == null)
  26. throw new ArgumentNullException("disposable");
  27. _disposable = disposable;
  28. _isPrimaryDisposed = false;
  29. _count = 0;
  30. }
  31. /// <summary>
  32. /// Gets a value that indicates whether the object is disposed.
  33. /// </summary>
  34. public bool IsDisposed
  35. {
  36. get { return _disposable == null; }
  37. }
  38. /// <summary>
  39. /// Returns a dependent disposable that when disposed decreases the refcount on the underlying disposable.
  40. /// </summary>
  41. /// <returns>A dependent disposable contributing to the reference count that manages the underlying disposable's lifetime.</returns>
  42. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Backward compat + non-trivial work for a property getter.")]
  43. public IDisposable GetDisposable()
  44. {
  45. lock (_gate)
  46. {
  47. if (_disposable == null)
  48. {
  49. return Disposable.Empty;
  50. }
  51. else
  52. {
  53. _count++;
  54. return new InnerDisposable(this);
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// Disposes the underlying disposable only when all dependent disposables have been disposed.
  60. /// </summary>
  61. public void Dispose()
  62. {
  63. var disposable = default(IDisposable);
  64. lock (_gate)
  65. {
  66. if (_disposable != null)
  67. {
  68. if (!_isPrimaryDisposed)
  69. {
  70. _isPrimaryDisposed = true;
  71. if (_count == 0)
  72. {
  73. disposable = _disposable;
  74. _disposable = null;
  75. }
  76. }
  77. }
  78. }
  79. if (disposable != null)
  80. disposable.Dispose();
  81. }
  82. private void Release()
  83. {
  84. var disposable = default(IDisposable);
  85. lock (_gate)
  86. {
  87. if (_disposable != null)
  88. {
  89. _count--;
  90. if (_isPrimaryDisposed)
  91. {
  92. if (_count == 0)
  93. {
  94. disposable = _disposable;
  95. _disposable = null;
  96. }
  97. }
  98. }
  99. }
  100. if (disposable != null)
  101. disposable.Dispose();
  102. }
  103. sealed class InnerDisposable : IDisposable
  104. {
  105. private RefCountDisposable _parent;
  106. object parentLock = new object();
  107. public InnerDisposable(RefCountDisposable parent)
  108. {
  109. _parent = parent;
  110. }
  111. public void Dispose()
  112. {
  113. RefCountDisposable parent;
  114. lock (parentLock)
  115. {
  116. parent = _parent;
  117. _parent = null;
  118. }
  119. if (parent != null)
  120. parent.Release();
  121. }
  122. }
  123. }
  124. public partial class Observable
  125. {
  126. static IObservable<T> AddRef<T>(IObservable<T> xs, RefCountDisposable r)
  127. {
  128. return Observable.Create<T>((IObserver<T> observer) => new CompositeDisposable(new IDisposable[]
  129. {
  130. r.GetDisposable(),
  131. xs.Subscribe(observer)
  132. }));
  133. }
  134. }
  135. }