blob: 835bd685c3d41764422b84fb93d5c772fb35c6b7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Reactive.Disposables
{
/// <summary>
/// Represents a disposable resource whose underlying disposable resource can be replaced by another disposable resource, causing automatic disposal of the previous underlying disposable resource.
/// </summary>
public sealed class SerialDisposable : ICancelable
{
private readonly object _gate = new object();
private IDisposable _current;
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.SerialDisposable"/> class.
/// </summary>
public SerialDisposable()
{
}
/// <summary>
/// Gets a value that indicates whether the object is disposed.
/// </summary>
public bool IsDisposed
{
get
{
lock (_gate)
{
return _disposed;
}
}
}
/// <summary>
/// Gets or sets the underlying disposable.
/// </summary>
/// <remarks>If the SerialDisposable has already been disposed, assignment to this property causes immediate disposal of the given disposable object. Assigning this property disposes the previous disposable object.</remarks>
public IDisposable Disposable
{
get
{
return _current;
}
set
{
var shouldDispose = false;
var old = default(IDisposable);
lock (_gate)
{
shouldDispose = _disposed;
if (!shouldDispose)
{
old = _current;
_current = value;
}
}
if (old != null)
old.Dispose();
if (shouldDispose && value != null)
value.Dispose();
}
}
/// <summary>
/// Disposes the underlying disposable as well as all future replacements.
/// </summary>
public void Dispose()
{
var old = default(IDisposable);
lock (_gate)
{
if (!_disposed)
{
_disposed = true;
old = _current;
_current = null;
}
}
if (old != null)
old.Dispose();
}
}
}
|