Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SafeHandle.cs
Go to the documentation of this file.
3
5
7{
8 protected IntPtr handle;
9
10 private volatile int _state;
11
12 private readonly bool _ownsHandle;
13
14 private volatile bool _fullyInitialized;
15
16 internal bool OwnsHandle => _ownsHandle;
17
18 public bool IsClosed => (_state & 1) == 1;
19
20 public abstract bool IsInvalid { get; }
21
22 protected SafeHandle(IntPtr invalidHandleValue, bool ownsHandle)
23 {
24 handle = invalidHandleValue;
25 _state = 4;
26 _ownsHandle = ownsHandle;
27 if (!ownsHandle)
28 {
29 GC.SuppressFinalize(this);
30 }
31 _fullyInitialized = true;
32 }
33
35 {
37 {
38 Dispose(disposing: false);
39 }
40 }
41
42 protected internal void SetHandle(IntPtr handle)
43 {
44 this.handle = handle;
45 }
46
48 {
49 return handle;
50 }
51
52 public void Close()
53 {
54 Dispose();
55 }
56
57 public void Dispose()
58 {
59 Dispose(disposing: true);
60 GC.SuppressFinalize(this);
61 }
62
63 protected virtual void Dispose(bool disposing)
64 {
65 InternalRelease(disposeOrFinalizeOperation: true);
66 }
67
68 public void SetHandleAsInvalid()
69 {
70 Interlocked.Or(ref _state, 1);
71 GC.SuppressFinalize(this);
72 }
73
74 protected abstract bool ReleaseHandle();
75
76 public void DangerousAddRef(ref bool success)
77 {
78 int state;
79 int value;
80 do
81 {
82 state = _state;
83 if (((uint)state & (true ? 1u : 0u)) != 0)
84 {
86 }
87 value = state + 4;
88 }
90 success = true;
91 }
92
93 public void DangerousRelease()
94 {
95 InternalRelease(disposeOrFinalizeOperation: false);
96 }
97
98 private void InternalRelease(bool disposeOrFinalizeOperation)
99 {
100 bool flag = false;
101 int state;
102 int num;
103 do
104 {
105 state = _state;
106 if (disposeOrFinalizeOperation && ((uint)state & 2u) != 0)
107 {
108 return;
109 }
110 if ((state & -4) == 0)
111 {
113 }
114 flag = (state & -3) == 4 && _ownsHandle && !IsInvalid;
115 num = state - 4;
116 if ((state & -4) == 4)
117 {
118 num |= 1;
119 }
120 if (disposeOrFinalizeOperation)
121 {
122 num |= 2;
123 }
124 }
125 while (Interlocked.CompareExchange(ref _state, num, state) != state);
126 if (flag)
127 {
128 int lastPInvokeError = Marshal.GetLastPInvokeError();
130 Marshal.SetLastPInvokeError(lastPInvokeError);
131 }
132 }
133}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static void SetLastPInvokeError(int error)
void DangerousAddRef(ref bool success)
Definition SafeHandle.cs:76
virtual void Dispose(bool disposing)
Definition SafeHandle.cs:63
SafeHandle(IntPtr invalidHandleValue, bool ownsHandle)
Definition SafeHandle.cs:22
void InternalRelease(bool disposeOrFinalizeOperation)
Definition SafeHandle.cs:98
static string ObjectDisposed_SafeHandleClosed
Definition SR.cs:1756
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
static int Or(ref int location1, int value)