Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DeferredDisposableLifetime.cs
Go to the documentation of this file.
1namespace System.Threading;
2
3internal struct DeferredDisposableLifetime<T> where T : class, IDeferredDisposable
4{
5 private int _count;
6
7 public bool AddRef()
8 {
9 int num;
10 int value;
11 do
12 {
13 num = Volatile.Read(ref _count);
14 if (num < 0)
15 {
16 throw new ObjectDisposedException(typeof(T).ToString());
17 }
18 value = checked(num + 1);
19 }
20 while (Interlocked.CompareExchange(ref _count, value, num) != num);
21 return true;
22 }
23
24 public void Release(T obj)
25 {
26 int num3;
27 while (true)
28 {
29 int num = Volatile.Read(ref _count);
30 if (num > 0)
31 {
32 int num2 = num - 1;
33 if (Interlocked.CompareExchange(ref _count, num2, num) == num)
34 {
35 if (num2 == 0)
36 {
37 obj.OnFinalRelease(disposed: false);
38 }
39 return;
40 }
41 }
42 else
43 {
44 num3 = num + 1;
45 if (Interlocked.CompareExchange(ref _count, num3, num) == num)
46 {
47 break;
48 }
49 }
50 }
51 if (num3 == -1)
52 {
53 obj.OnFinalRelease(disposed: true);
54 }
55 }
56
57 public void Dispose(T obj)
58 {
59 int num;
60 int num2;
61 do
62 {
63 num = Volatile.Read(ref _count);
64 if (num < 0)
65 {
66 return;
67 }
68 num2 = -1 - num;
69 }
70 while (Interlocked.CompareExchange(ref _count, num2, num) != num);
71 if (num2 == -1)
72 {
73 obj.OnFinalRelease(disposed: true);
74 }
75 }
76}
static int CompareExchange(ref int location1, int value, int comparand)
static bool Read(ref bool location)
Definition Volatile.cs:67