10 [DebuggerDisplay(
"Participant Count={ParticipantCount},Participants Remaining={ParticipantsRemaining}")]
11 [global::__DynamicallyInvokable]
12 [HostProtection(
SecurityAction.LinkDemand, Synchronization =
true, ExternalThreading =
true)]
15 private volatile int m_currentTotalCount;
17 private const int CURRENT_MASK = 2147418112;
19 private const int TOTAL_MASK = 32767;
21 private const int SENSE_MASK =
int.MinValue;
23 private const int MAX_PARTICIPANTS = 32767;
25 private long m_currentPhase;
27 private bool m_disposed;
38 private Action<Barrier> m_postPhaseAction;
42 private int m_actionCallerID;
46 [global::__DynamicallyInvokable]
49 [global::__DynamicallyInvokable]
52 int currentTotalCount = m_currentTotalCount;
53 int num = currentTotalCount & 0x7FFF;
54 int num2 = (currentTotalCount & 0x7FFF0000) >> 16;
61 [global::__DynamicallyInvokable]
64 [global::__DynamicallyInvokable]
67 return m_currentTotalCount & 0x7FFF;
73 [global::__DynamicallyInvokable]
76 [global::__DynamicallyInvokable]
91 [global::__DynamicallyInvokable]
93 : this(participantCount, null)
102 [global::__DynamicallyInvokable]
103 public Barrier(
int participantCount, Action<Barrier> postPhaseAction)
105 if (participantCount < 0 || participantCount > 32767)
109 m_currentTotalCount = participantCount;
110 m_postPhaseAction = postPhaseAction;
117 m_actionCallerID = 0;
120 private void GetCurrentTotal(
int currentTotal, out
int current, out
int total, out
bool sense)
122 total = (currentTotal & 0x7FFF);
123 current = (currentTotal & 0x7FFF0000) >> 16;
124 sense = (((currentTotal &
int.MinValue) == 0) ? true :
false);
127 private bool SetCurrentTotal(
int currentTotal,
int current,
int total,
bool sense)
129 int num = (current << 16) | total;
134 return Interlocked.CompareExchange(ref m_currentTotalCount, num, currentTotal) == currentTotal;
141 [global::__DynamicallyInvokable]
161 [global::__DynamicallyInvokable]
165 if (participantCount < 1)
167 throw new ArgumentOutOfRangeException(
"participantCount", participantCount, SR.GetString(
"Barrier_AddParticipants_NonPositive_ArgumentOutOfRange"));
169 if (participantCount > 32767)
182 int currentTotalCount = m_currentTotalCount;
183 GetCurrentTotal(currentTotalCount, out
int current, out
int total, out sense);
184 if (participantCount + total > 32767)
188 if (SetCurrentTotal(currentTotalCount, current, total + participantCount, sense))
195 num = ((sense != (currentPhaseNumber % 2 == 0)) ? (currentPhaseNumber + 1) : currentPhaseNumber);
196 if (num != currentPhaseNumber)
207 else if (sense && m_evenEvent.
IsSet)
211 else if (!sense && m_oddEvent.
IsSet)
221 [global::__DynamicallyInvokable]
234 [global::__DynamicallyInvokable]
238 if (participantCount < 1)
240 throw new ArgumentOutOfRangeException(
"participantCount", participantCount, SR.GetString(
"Barrier_RemoveParticipants_NonPositive_ArgumentOutOfRange"));
250 int currentTotalCount = m_currentTotalCount;
251 GetCurrentTotal(currentTotalCount, out
int current, out
int total, out sense);
252 if (total < participantCount)
256 if (total - participantCount < current)
260 int num = total - participantCount;
261 if (num > 0 && current == num)
263 if (SetCurrentTotal(currentTotalCount, 0, total - participantCount, !sense))
268 else if (SetCurrentTotal(currentTotalCount, current, total - participantCount, sense))
281 [global::__DynamicallyInvokable]
293 [global::__DynamicallyInvokable]
306 [global::__DynamicallyInvokable]
322 [global::__DynamicallyInvokable]
326 if (num < -1 || num >
int.MaxValue)
341 [global::__DynamicallyInvokable]
357 [global::__DynamicallyInvokable]
362 if (millisecondsTimeout < -1)
364 throw new ArgumentOutOfRangeException(
"millisecondsTimeout", millisecondsTimeout, SR.GetString(
"Barrier_SignalAndWait_ArgumentOutOfRange"));
374 long currentPhaseNumber;
377 int currentTotalCount = m_currentTotalCount;
378 GetCurrentTotal(currentTotalCount, out current, out total, out sense);
388 if (current + 1 == total)
390 if (SetCurrentTotal(currentTotalCount, 0, total, !sense))
392 if (CdsSyncEtwBCLProvider.Log.IsEnabled())
400 else if (SetCurrentTotal(currentTotalCount, current + 1, total, sense))
411 flag2 = DiscontinuousWait(currentPhaseEvent, millisecondsTimeout, cancellationToken, currentPhaseNumber);
430 int currentTotalCount = m_currentTotalCount;
431 GetCurrentTotal(currentTotalCount, out current, out total, out
bool sense2);
436 if (SetCurrentTotal(currentTotalCount, current - 1, total, sense))
446 WaitCurrentPhase(currentPhaseEvent, currentPhaseNumber);
448 if (m_exception !=
null)
455 [SecuritySafeCritical]
456 private void FinishPhase(
bool observedSense)
458 if (m_postPhaseAction !=
null)
463 if (m_ownerThreadContext !=
null)
466 m_ownerThreadContext = m_ownerThreadContext.
CreateCopy();
473 m_postPhaseAction(
this);
477 catch (Exception exception)
479 Exception ex = m_exception = exception;
483 m_actionCallerID = 0;
484 SetResetEvents(observedSense);
485 if (m_exception !=
null)
487 throw new BarrierPostPhaseException(m_exception);
493 SetResetEvents(observedSense);
498 private static void InvokePostPhaseAction(
object obj)
501 barrier.m_postPhaseAction(barrier);
504 private void SetResetEvents(
bool observedSense)
519 private void WaitCurrentPhase(ManualResetEventSlim currentPhaseEvent,
long observedPhase)
521 SpinWait spinWait =
default(SpinWait);
528 private bool DiscontinuousWait(ManualResetEventSlim currentPhaseEvent,
int totalTimeout, CancellationToken token,
long observedPhase)
534 int num3 = (totalTimeout == -1) ? num : Math.Min(num, totalTimeout);
535 if (currentPhaseEvent.Wait(num3, token))
539 if (totalTimeout != -1)
541 totalTimeout -= num3;
542 if (totalTimeout <= 0)
547 num = ((num >= num2) ? num2 : Math.Min(num << 1, num2));
549 WaitCurrentPhase(currentPhaseEvent, observedPhase);
555 [global::__DynamicallyInvokable]
568 [global::__DynamicallyInvokable]
569 protected virtual void Dispose(
bool disposing)
579 if (m_ownerThreadContext !=
null)
581 m_ownerThreadContext.
Dispose();
582 m_ownerThreadContext =
null;
588 private void ThrowIfDisposed()
static Thread CurrentThread
Gets the currently running thread.
ExecutionContext CreateCopy()
Creates a copy of the current execution context.
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.ManualResetEventSlim cl...
static void Write(ref bool location, bool value)
Writes the specified value to the specified field. On systems that require it, inserts a memory barri...
Propagates notification that operations should be canceled.
delegate void ContextCallback(object state)
Represents a method to be called within a new context.
void ThrowIfCancellationRequested()
Throws a T:System.OperationCanceledException if this token has had cancellation requested.
void Reset()
Resets the spin counter.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Threading.Barrier, and optionally releases the ...
long AddParticipant()
Notifies the T:System.Threading.Barrier that there will be an additional participant.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
void SignalAndWait()
Signals that a participant has reached the barrier and waits for all other participants to reach the ...
void SignalAndWait(CancellationToken cancellationToken)
Signals that a participant has reached the barrier and waits for all other participants to reach the ...
Provides support for spin-based waiting.
void RemoveParticipant()
Notifies the T:System.Threading.Barrier that there will be one less participant.
long AddParticipants(int participantCount)
Notifies the T:System.Threading.Barrier that there will be additional participants.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
double TotalMilliseconds
Gets the value of the current T:System.TimeSpan structure expressed in whole and fractional milliseco...
bool SignalAndWait(TimeSpan timeout, CancellationToken cancellationToken)
Signals that a participant has reached the barrier and waits for all other participants to reach the ...
bool SignalAndWait(int millisecondsTimeout)
Signals that a participant has reached the barrier and waits for all other participants to reach the ...
void Wait()
Blocks the current thread until the current T:System.Threading.ManualResetEventSlim is set.
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.Barrier class.
bool SignalAndWait(TimeSpan timeout)
Signals that a participant has reached the barrier and waits for all other participants to reach the ...
int ParticipantCount
Gets the total number of participants in the barrier.
The exception that is thrown when an operation is performed on a disposed object.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Manages the execution context for the current thread. This class cannot be inherited.
void Set()
Sets the state of the event to signaled, which allows one or more threads waiting on the event to pro...
void RemoveParticipants(int participantCount)
Notifies the T:System.Threading.Barrier that there will be fewer participants.
Provides a slimmed down version of T:System.Threading.ManualResetEvent.
Enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases.
Contains methods for performing volatile memory operations.
static void Run(ExecutionContext executionContext, ContextCallback callback, object state)
Runs a method in a specified execution context on the current thread.
bool? IsSet
Gets whether the event is set.
Barrier(int participantCount, Action< Barrier > postPhaseAction)
Initializes a new instance of the T:System.Threading.Barrier class.
static bool Read(ref bool location)
Reads the value of the specified field. On systems that require it, inserts a memory barrier that pre...
Controls the system garbage collector, a service that automatically reclaims unused memory.
bool SignalAndWait(int millisecondsTimeout, CancellationToken cancellationToken)
Signals that a participant has reached the barrier and waits for all other participants to reach the ...
static ExecutionContext Capture()
Captures the execution context from the current thread.
Barrier(int participantCount)
Initializes a new instance of the T:System.Threading.Barrier class.
static bool IsFlowSuppressed()
Indicates whether the flow of the execution context is currently suppressed.
The exception that is thrown in a thread upon cancellation of an operation that the thread was execut...
int ManagedThreadId
Gets a unique identifier for the current managed thread.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
void SpinOnce()
Performs a single spin.
int ParticipantsRemaining
Gets the number of participants in the barrier that haven’t yet signaled in the current phase.
Represents a time interval.To browse the .NET Framework source code for this type,...
The exception that is thrown when the post-phase action of a T:System.Threading.Barrier fails
The exception that is thrown when a method call is invalid for the object's current state.
long CurrentPhaseNumber
Gets the number of the barrier's current phase.
void Reset()
Sets the state of the event to nonsignaled, which causes threads to block.
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.ExecutionContext class.
Creates and controls a thread, sets its priority, and gets its status.