Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ SignalAndWait() [4/6]

bool System.Threading.Barrier.SignalAndWait ( int millisecondsTimeout,
CancellationToken cancellationToken )
inline

Definition at line 245 of file Barrier.cs.

246 {
248 cancellationToken.ThrowIfCancellationRequested();
249 if (millisecondsTimeout < -1)
250 {
251 throw new ArgumentOutOfRangeException("millisecondsTimeout", millisecondsTimeout, System.SR.Barrier_SignalAndWait_ArgumentOutOfRange);
252 }
253 if (_actionCallerID != 0 && Environment.CurrentManagedThreadId == _actionCallerID)
254 {
256 }
257 SpinWait spinWait = default(SpinWait);
258 int current;
259 int total;
260 bool sense;
261 long currentPhaseNumber;
262 while (true)
263 {
264 int currentTotalCount = _currentTotalCount;
265 GetCurrentTotal(currentTotalCount, out current, out total, out sense);
266 currentPhaseNumber = CurrentPhaseNumber;
267 if (total == 0)
268 {
270 }
271 if (current == 0 && sense != (CurrentPhaseNumber % 2 == 0))
272 {
274 }
275 if (current + 1 == total)
276 {
277 if (SetCurrentTotal(currentTotalCount, 0, total, !sense))
278 {
279 if (CdsSyncEtwBCLProvider.Log.IsEnabled())
280 {
281 CdsSyncEtwBCLProvider.Log.Barrier_PhaseFinished(sense, CurrentPhaseNumber);
282 }
283 FinishPhase(sense);
284 return true;
285 }
286 }
287 else if (SetCurrentTotal(currentTotalCount, current + 1, total, sense))
288 {
289 break;
290 }
291 spinWait.SpinOnce(-1);
292 }
293 ManualResetEventSlim currentPhaseEvent = (sense ? _evenEvent : _oddEvent);
294 bool flag = false;
295 bool flag2 = false;
296 try
297 {
298 flag2 = DiscontinuousWait(currentPhaseEvent, millisecondsTimeout, cancellationToken, currentPhaseNumber);
299 }
300 catch (OperationCanceledException)
301 {
302 flag = true;
303 }
304 catch (ObjectDisposedException)
305 {
306 if (currentPhaseNumber >= CurrentPhaseNumber)
307 {
308 throw;
309 }
310 flag2 = true;
311 }
312 if (!flag2)
313 {
314 spinWait.Reset();
315 while (true)
316 {
317 int currentTotalCount = _currentTotalCount;
318 GetCurrentTotal(currentTotalCount, out current, out total, out var sense2);
319 if (currentPhaseNumber < CurrentPhaseNumber || sense != sense2)
320 {
321 break;
322 }
323 if (SetCurrentTotal(currentTotalCount, current - 1, total, sense))
324 {
325 if (flag)
326 {
327 throw new OperationCanceledException(System.SR.Common_OperationCanceled, cancellationToken);
328 }
329 return false;
330 }
331 spinWait.SpinOnce(-1);
332 }
333 WaitCurrentPhase(currentPhaseEvent, currentPhaseNumber);
334 }
335 if (_exception != null)
336 {
337 throw new BarrierPostPhaseException(_exception);
338 }
339 return true;
340 }
static string Barrier_SignalAndWait_InvalidOperation_ThreadsExceeded
Definition SR.cs:38
static string Barrier_InvalidOperation_CalledFromPHA
Definition SR.cs:34
static string Barrier_SignalAndWait_ArgumentOutOfRange
Definition SR.cs:26
static string Barrier_SignalAndWait_InvalidOperation_ZeroTotal
Definition SR.cs:24
static string Common_OperationCanceled
Definition SR.cs:20
Definition SR.cs:7
void FinishPhase(bool observedSense)
Definition Barrier.cs:342
void GetCurrentTotal(int currentTotal, out int current, out int total, out bool sense)
Definition Barrier.cs:76
bool SetCurrentTotal(int currentTotal, int current, int total, bool sense)
Definition Barrier.cs:83
volatile int _currentTotalCount
Definition Barrier.cs:9
readonly ManualResetEventSlim _oddEvent
Definition Barrier.cs:15
readonly ManualResetEventSlim _evenEvent
Definition Barrier.cs:17
bool DiscontinuousWait(ManualResetEventSlim currentPhaseEvent, int totalTimeout, CancellationToken token, long observedPhase)
Definition Barrier.cs:410
void WaitCurrentPhase(ManualResetEventSlim currentPhaseEvent, long observedPhase)
Definition Barrier.cs:400

References System.Threading.Barrier._actionCallerID, System.Threading.Barrier._currentTotalCount, System.Threading.Barrier._evenEvent, System.Threading.Barrier._exception, System.Threading.Barrier._oddEvent, System.SR.Barrier_InvalidOperation_CalledFromPHA, System.SR.Barrier_SignalAndWait_ArgumentOutOfRange, System.SR.Barrier_SignalAndWait_InvalidOperation_ThreadsExceeded, System.SR.Barrier_SignalAndWait_InvalidOperation_ZeroTotal, System.cancellationToken, System.SR.Common_OperationCanceled, System.Environment.CurrentManagedThreadId, System.Threading.Barrier.CurrentPhaseNumber, System.Threading.Barrier.DiscontinuousWait(), System.Threading.Barrier.FinishPhase(), System.Threading.Barrier.GetCurrentTotal(), System.Threading.CdsSyncEtwBCLProvider.Log, System.millisecondsTimeout, System.Threading.SpinWait.Reset(), System.Threading.Barrier.SetCurrentTotal(), System.Threading.SpinWait.SpinOnce(), System.Threading.Barrier.ThrowIfDisposed(), and System.Threading.Barrier.WaitCurrentPhase().