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