-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathRef.cs
More file actions
189 lines (156 loc) · 6.07 KB
/
Copy pathRef.cs
File metadata and controls
189 lines (156 loc) · 6.07 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Runtime.ConstrainedExecution;
using System.Threading;
namespace Avalonia.Utilities
{
/// <summary>
/// A ref-counted wrapper for a disposable object.
/// </summary>
/// <typeparam name="T">
/// The type of the item being ref-counted.
/// Must be a reference type to avoid issues with copying value types and must be disposable
/// to ensure the item is cleaned up when the refcount reaches 0.
/// </typeparam>
internal interface IRef<out T> : IDisposable where T : class
{
/// <summary>
/// The item that is being ref-counted.
/// </summary>
T Item { get; }
/// <summary>
/// Create another reference to this object and increment the refcount.
/// </summary>
/// <returns>A new reference to this object.</returns>
IRef<T> Clone();
/// <summary>
/// Create another reference to the same object, but cast the object to a different type.
/// </summary>
/// <typeparam name="TResult">The type of the new reference.</typeparam>
/// <returns>A reference to the value as the new type but sharing the refcount.</returns>
IRef<TResult> CloneAs<TResult>() where TResult : class;
/// <summary>
/// Gets whether the reference still tracks a valid item.
/// </summary>
bool IsAlive { get; }
/// <summary>
/// The current refcount of the object tracked in this reference. For debugging/unit test use only.
/// </summary>
int RefCount { get; }
}
internal static class RefCountable
{
/// <summary>
/// Create a reference counted object wrapping the given item.
/// </summary>
/// <typeparam name="T">The type of item.</typeparam>
/// <param name="item">The item to refcount.</param>
/// <returns>The refcounted reference to the item.</returns>
public static IRef<T> Create<T>(T item) where T : class, IDisposable
{
return new Ref<T>(item, new RefCounter(item));
}
class RefCounter
{
private IDisposable? _item;
private volatile int _refs;
public RefCounter(IDisposable item)
{
_item = item ?? throw new ArgumentNullException();
_refs = 1;
}
internal bool TryAddRef()
{
var old = _refs;
while (true)
{
if (old == 0)
{
return false;
}
var current = Interlocked.CompareExchange(ref _refs, old + 1, old);
if (current == old)
{
break;
}
old = current;
}
return true;
}
public void Release()
{
var old = _refs;
while (true)
{
var current = Interlocked.CompareExchange(ref _refs, old - 1, old);
if (current == old)
{
if (old == 1)
{
_item?.Dispose();
_item = null;
}
break;
}
old = current;
}
}
internal int RefCount => _refs;
}
class Ref<T> : CriticalFinalizerObject, IRef<T> where T : class
{
private volatile T? _item;
private volatile RefCounter? _counter;
public Ref(T item, RefCounter counter)
{
_item = item;
_counter = counter;
}
public void Dispose() => Dispose(true);
void Dispose(bool disposing)
{
var item = Interlocked.Exchange(ref _item, null);
if (item != null)
{
var counter = _counter!;
_counter = null;
if (disposing)
GC.SuppressFinalize(this);
counter.Release();
}
}
~Ref()
{
Dispose(false);
}
public T Item => _item ?? throw new ObjectDisposedException("Ref<" + typeof(T) + ">");
public IRef<T> Clone()
{
// Snapshot current ref state so we don't care if it's disposed in the meantime.
var counter = _counter;
var item = _item;
// Check if ref was invalid
if (item == null || counter == null)
throw new ObjectDisposedException("Ref<" + typeof(T) + ">");
// Try to add a reference to the counter, if it fails, the item is disposed.
if (!counter.TryAddRef())
throw new ObjectDisposedException("Ref<" + typeof(T) + ">");
return new Ref<T>(item, counter);
}
public IRef<TResult> CloneAs<TResult>() where TResult : class
{
// Snapshot current ref state so we don't care if it's disposed in the meantime.
var counter = _counter;
var item = (TResult?)(object?)_item;
// Check if ref was invalid
if (item == null || counter == null)
throw new ObjectDisposedException("Ref<" + typeof(T) + ">");
// Try to add a reference to the counter, if it fails, the item is disposed.
if (!counter.TryAddRef())
throw new ObjectDisposedException("Ref<" + typeof(T) + ">");
return new Ref<TResult>(item, counter);
}
public bool IsAlive => _item is not null;
public int RefCount => _counter?.RefCount ?? throw new ObjectDisposedException("Ref<" + typeof(T) + ">");
}
}
}