Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ManualResetEventSlim.cs
Go to the documentation of this file.
3
4namespace System.Threading;
5
6[DebuggerDisplay("Set = {IsSet}")]
8{
9 private volatile object m_lock;
10
11 private volatile ManualResetEvent m_eventObj;
12
13 private volatile int m_combinedState;
14
15 private static readonly Action<object> s_cancellationTokenCallback = CancellationTokenCallback;
16
18 {
19 get
20 {
22 if (m_eventObj == null)
23 {
25 }
26 return m_eventObj;
27 }
28 }
29
30 public bool IsSet
31 {
32 get
33 {
34 return ExtractStatePortion(m_combinedState, int.MinValue) != 0;
35 }
36 private set
37 {
38 UpdateStateAtomically((value ? 1 : 0) << 31, int.MinValue);
39 }
40 }
41
42 public int SpinCount
43 {
44 get
45 {
47 }
48 private set
49 {
50 m_combinedState = (m_combinedState & -1073217537) | (value << 19);
51 }
52 }
53
54 private int Waiters
55 {
56 get
57 {
59 }
60 set
61 {
62 if (value >= 524287)
63 {
65 }
67 }
68 }
69
71 : this(initialState: false)
72 {
73 }
74
75 public ManualResetEventSlim(bool initialState)
76 {
78 }
79
80 public ManualResetEventSlim(bool initialState, int spinCount)
81 {
82 if (spinCount < 0)
83 {
84 throw new ArgumentOutOfRangeException("spinCount");
85 }
86 if (spinCount > 2047)
87 {
89 }
90 Initialize(initialState, spinCount);
91 }
92
93 private void Initialize(bool initialState, int spinCount)
94 {
95 m_combinedState = (initialState ? int.MinValue : 0);
96 SpinCount = (Environment.IsSingleProcessor ? 1 : spinCount);
97 }
98
100 {
101 if (m_lock == null)
102 {
103 object value = new object();
105 }
106 }
107
108 private void LazyInitializeEvent()
109 {
110 bool isSet = IsSet;
111 ManualResetEvent manualResetEvent = new ManualResetEvent(isSet);
112 if (Interlocked.CompareExchange(ref m_eventObj, manualResetEvent, null) != null)
113 {
114 manualResetEvent.Dispose();
115 return;
116 }
117 bool isSet2 = IsSet;
118 if (isSet2 == isSet)
119 {
120 return;
121 }
122 lock (manualResetEvent)
123 {
124 if (m_eventObj == manualResetEvent)
125 {
126 manualResetEvent.Set();
127 }
128 }
129 }
130
131 public void Set()
132 {
133 Set(duringCancellation: false);
134 }
135
136 private void Set(bool duringCancellation)
137 {
138 IsSet = true;
139 if (Waiters > 0)
140 {
141 lock (m_lock)
142 {
144 }
145 }
146 ManualResetEvent eventObj = m_eventObj;
147 if (eventObj == null || duringCancellation)
148 {
149 return;
150 }
151 lock (eventObj)
152 {
153 if (m_eventObj != null)
154 {
155 m_eventObj.Set();
156 }
157 }
158 }
159
160 public void Reset()
161 {
163 if (m_eventObj != null)
164 {
166 }
167 IsSet = false;
168 }
169
170 [UnsupportedOSPlatform("browser")]
171 public void Wait()
172 {
174 }
175
176 [UnsupportedOSPlatform("browser")]
181
182 [UnsupportedOSPlatform("browser")]
183 public bool Wait(TimeSpan timeout)
184 {
185 long num = (long)timeout.TotalMilliseconds;
186 if (num < -1 || num > int.MaxValue)
187 {
188 throw new ArgumentOutOfRangeException("timeout");
189 }
190 return Wait((int)num, CancellationToken.None);
191 }
192
193 [UnsupportedOSPlatform("browser")]
195 {
196 long num = (long)timeout.TotalMilliseconds;
197 if (num < -1 || num > int.MaxValue)
198 {
199 throw new ArgumentOutOfRangeException("timeout");
200 }
201 return Wait((int)num, cancellationToken);
202 }
203
204 [UnsupportedOSPlatform("browser")]
205 public bool Wait(int millisecondsTimeout)
206 {
208 }
209
210 [UnsupportedOSPlatform("browser")]
212 {
214 cancellationToken.ThrowIfCancellationRequested();
215 if (millisecondsTimeout < -1)
216 {
217 throw new ArgumentOutOfRangeException("millisecondsTimeout");
218 }
219 if (!IsSet)
220 {
221 if (millisecondsTimeout == 0)
222 {
223 return false;
224 }
225 uint startTime = 0u;
226 bool flag = false;
227 int num = millisecondsTimeout;
228 if (millisecondsTimeout != -1)
229 {
230 startTime = TimeoutHelper.GetTime();
231 flag = true;
232 }
233 int spinCount = SpinCount;
234 SpinWait spinWait = default(SpinWait);
235 while (spinWait.Count < spinCount)
236 {
237 spinWait.SpinOnce(-1);
238 if (IsSet)
239 {
240 return true;
241 }
242 if (spinWait.Count >= 100 && spinWait.Count % 10 == 0)
243 {
244 cancellationToken.ThrowIfCancellationRequested();
245 }
246 }
248 using (cancellationToken.UnsafeRegister(s_cancellationTokenCallback, this))
249 {
250 lock (m_lock)
251 {
252 while (!IsSet)
253 {
254 cancellationToken.ThrowIfCancellationRequested();
255 if (flag)
256 {
258 if (num <= 0)
259 {
260 return false;
261 }
262 }
263 Waiters++;
264 if (IsSet)
265 {
266 Waiters--;
267 return true;
268 }
269 try
270 {
271 if (!Monitor.Wait(m_lock, num))
272 {
273 return false;
274 }
275 }
276 finally
277 {
278 Waiters--;
279 }
280 }
281 }
282 }
283 }
284 return true;
285 }
286
287 public void Dispose()
288 {
289 Dispose(disposing: true);
290 GC.SuppressFinalize(this);
291 }
292
293 protected virtual void Dispose(bool disposing)
294 {
295 if (((uint)m_combinedState & 0x40000000u) != 0)
296 {
297 return;
298 }
299 m_combinedState |= 1073741824;
300 if (!disposing)
301 {
302 return;
303 }
304 ManualResetEvent eventObj = m_eventObj;
305 if (eventObj == null)
306 {
307 return;
308 }
309 lock (eventObj)
310 {
311 eventObj.Dispose();
312 m_eventObj = null;
313 }
314 }
315
316 private void ThrowIfDisposed()
317 {
318 if (((uint)m_combinedState & 0x40000000u) != 0)
319 {
321 }
322 }
323
324 private static void CancellationTokenCallback(object obj)
325 {
326 ManualResetEventSlim manualResetEventSlim = (ManualResetEventSlim)obj;
327 lock (manualResetEventSlim.m_lock)
328 {
329 Monitor.PulseAll(manualResetEventSlim.m_lock);
330 }
331 }
332
333 private void UpdateStateAtomically(int newBits, int updateBitsMask)
334 {
335 SpinWait spinWait = default(SpinWait);
336 while (true)
337 {
338 int combinedState = m_combinedState;
339 int value = (combinedState & ~updateBitsMask) | newBits;
340 if (Interlocked.CompareExchange(ref m_combinedState, value, combinedState) == combinedState)
341 {
342 break;
343 }
344 spinWait.SpinOnce(-1);
345 }
346 }
347
348 private static int ExtractStatePortionAndShiftRight(int state, int mask, int rightBitShiftCount)
349 {
350 return (state & mask) >>> rightBitShiftCount;
351 }
352
353 private static int ExtractStatePortion(int state, int mask)
354 {
355 return state & mask;
356 }
357}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ManualResetEventSlim_Disposed
Definition SR.cs:1612
static string ManualResetEventSlim_ctor_SpinCountOutOfRange
Definition SR.cs:1608
static string ManualResetEventSlim_ctor_TooManyWaiters
Definition SR.cs:1610
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
bool Wait(TimeSpan timeout, CancellationToken cancellationToken)
static void CancellationTokenCallback(object obj)
static int ExtractStatePortion(int state, int mask)
ManualResetEventSlim(bool initialState, int spinCount)
void Wait(CancellationToken cancellationToken)
void Initialize(bool initialState, int spinCount)
static int ExtractStatePortionAndShiftRight(int state, int mask, int rightBitShiftCount)
bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
static readonly Action< object > s_cancellationTokenCallback
void UpdateStateAtomically(int newBits, int updateBitsMask)
static void PulseAll(object obj)
Definition Monitor.cs:115
static bool Wait(object obj, int millisecondsTimeout)
Definition Monitor.cs:87
static int UpdateTimeOut(uint startTime, int originalWaitMillisecondsTimeout)
virtual void Dispose(bool explicitDisposing)
static readonly int SpinCountforSpinBeforeWait
Definition SpinWait.cs:5