|
@@ -2,6 +2,7 @@
|
|
|
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
|
|
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
|
|
|
// See the LICENSE file in the project root for more information.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
+using System;
|
|
|
using System.Threading;
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace System.Reactive.Disposables
|
|
namespace System.Reactive.Disposables
|
|
@@ -11,6 +12,7 @@ namespace System.Reactive.Disposables
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
public sealed class RefCountDisposable : ICancelable
|
|
public sealed class RefCountDisposable : ICancelable
|
|
|
{
|
|
{
|
|
|
|
|
+ private readonly bool _throwWhenDisposed;
|
|
|
private readonly object _gate = new object();
|
|
private readonly object _gate = new object();
|
|
|
private IDisposable _disposable;
|
|
private IDisposable _disposable;
|
|
|
private bool _isPrimaryDisposed;
|
|
private bool _isPrimaryDisposed;
|
|
@@ -21,7 +23,18 @@ namespace System.Reactive.Disposables
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <param name="disposable">Underlying disposable.</param>
|
|
/// <param name="disposable">Underlying disposable.</param>
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="disposable"/> is null.</exception>
|
|
/// <exception cref="ArgumentNullException"><paramref name="disposable"/> is null.</exception>
|
|
|
- public RefCountDisposable(IDisposable disposable)
|
|
|
|
|
|
|
+ public RefCountDisposable(IDisposable disposable) : this(disposable, false)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.RefCountDisposable"/> class with the specified disposable.
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="disposable">Underlying disposable.</param>
|
|
|
|
|
+ /// <param name="throwWhenDisposed">Indicates whether subsequent calls to <see cref="GetDisposable"/> should throw when this instance is disposed.</param>
|
|
|
|
|
+ /// <exception cref="ArgumentNullException"><paramref name="disposable"/> is null.</exception>
|
|
|
|
|
+ public RefCountDisposable(IDisposable disposable, bool throwWhenDisposed)
|
|
|
{
|
|
{
|
|
|
if (disposable == null)
|
|
if (disposable == null)
|
|
|
throw new ArgumentNullException("disposable");
|
|
throw new ArgumentNullException("disposable");
|
|
@@ -29,6 +42,7 @@ namespace System.Reactive.Disposables
|
|
|
_disposable = disposable;
|
|
_disposable = disposable;
|
|
|
_isPrimaryDisposed = false;
|
|
_isPrimaryDisposed = false;
|
|
|
_count = 0;
|
|
_count = 0;
|
|
|
|
|
+ _throwWhenDisposed = throwWhenDisposed;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -43,6 +57,7 @@ namespace System.Reactive.Disposables
|
|
|
/// Returns a dependent disposable that when disposed decreases the refcount on the underlying disposable.
|
|
/// Returns a dependent disposable that when disposed decreases the refcount on the underlying disposable.
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <returns>A dependent disposable contributing to the reference count that manages the underlying disposable's lifetime.</returns>
|
|
/// <returns>A dependent disposable contributing to the reference count that manages the underlying disposable's lifetime.</returns>
|
|
|
|
|
+ /// <exception cref="ObjectDisposedException">This instance has been disposed and is configured to throw in this case by <see cref="RefCountDisposable(IDisposable, bool)"/>.</exception>
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Backward compat + non-trivial work for a property getter.")]
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Backward compat + non-trivial work for a property getter.")]
|
|
|
public IDisposable GetDisposable()
|
|
public IDisposable GetDisposable()
|
|
|
{
|
|
{
|
|
@@ -50,6 +65,9 @@ namespace System.Reactive.Disposables
|
|
|
{
|
|
{
|
|
|
if (_disposable == null)
|
|
if (_disposable == null)
|
|
|
{
|
|
{
|
|
|
|
|
+ if (_throwWhenDisposed)
|
|
|
|
|
+ throw new ObjectDisposedException("RefCountDisposable");
|
|
|
|
|
+
|
|
|
return Disposable.Empty;
|
|
return Disposable.Empty;
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|