9 [DebuggerDisplay(
"Set = {IsSet}")]
10 [__DynamicallyInvokable]
11 [HostProtection(
SecurityAction.LinkDemand, Synchronization =
true, ExternalThreading =
true)]
14 private const int DEFAULT_SPIN_SP = 1;
16 private const int DEFAULT_SPIN_MP = 10;
18 private volatile object m_lock;
22 private volatile int m_combinedState;
24 private const int SignalledState_BitMask =
int.MinValue;
26 private const int SignalledState_ShiftCount = 31;
28 private const int Dispose_BitMask = 1073741824;
30 private const int SpinCountState_BitMask = 1073217536;
32 private const int SpinCountState_ShiftCount = 19;
34 private const int SpinCountState_MaxValue = 2047;
36 private const int NumWaitersState_BitMask = 524287;
38 private const int NumWaitersState_ShiftCount = 0;
40 private const int NumWaitersState_MaxValue = 524287;
42 private static Action<object> s_cancellationTokenCallback = CancellationTokenCallback;
46 [__DynamicallyInvokable]
49 [__DynamicallyInvokable]
53 if (m_eventObj ==
null)
55 LazyInitializeEvent();
63 [__DynamicallyInvokable]
66 [__DynamicallyInvokable]
69 return ExtractStatePortion(m_combinedState,
int.MinValue) != 0;
73 UpdateStateAtomically((value ? 1 : 0) << 31,
int.MinValue);
79 [__DynamicallyInvokable]
82 [__DynamicallyInvokable]
85 return ExtractStatePortionAndShiftRight(m_combinedState, 1073217536, 19);
89 m_combinedState = ((m_combinedState & -1073217537) | (value << 19));
97 return ExtractStatePortionAndShiftRight(m_combinedState, 524287, 0);
105 UpdateStateAtomically(value, 524287);
110 [__DynamicallyInvokable]
112 : this(initialState: false)
118 [__DynamicallyInvokable]
121 Initialize(initialState, 10);
129 [__DynamicallyInvokable]
136 if (spinCount > 2047)
140 Initialize(initialState, spinCount);
143 private void Initialize(
bool initialState,
int spinCount)
145 m_combinedState = (initialState ?
int.MinValue : 0);
146 SpinCount = (PlatformHelper.IsSingleProcessor ? 1 : spinCount);
149 private void EnsureLockObjectCreated()
153 object value =
new object();
154 Interlocked.CompareExchange(ref m_lock, value,
null);
158 private bool LazyInitializeEvent()
161 ManualResetEvent manualResetEvent =
new ManualResetEvent(isSet);
162 if (Interlocked.CompareExchange(ref m_eventObj, manualResetEvent,
null) !=
null)
164 manualResetEvent.Close();
170 lock (manualResetEvent)
172 if (m_eventObj == manualResetEvent)
174 manualResetEvent.Set();
182 [__DynamicallyInvokable]
185 Set(duringCancellation:
false);
188 private void Set(
bool duringCancellation)
198 ManualResetEvent eventObj = m_eventObj;
199 if (eventObj !=
null && !duringCancellation)
203 if (m_eventObj !=
null)
213 [__DynamicallyInvokable]
217 if (m_eventObj !=
null)
227 [__DynamicallyInvokable]
239 [__DynamicallyInvokable]
242 Wait(-1, cancellationToken);
253 [__DynamicallyInvokable]
257 if (num < -1 || num >
int.MaxValue)
275 [__DynamicallyInvokable]
279 if (num < -1 || num >
int.MaxValue)
283 return Wait((
int)num, cancellationToken);
294 [__DynamicallyInvokable]
295 public bool Wait(
int millisecondsTimeout)
311 [__DynamicallyInvokable]
316 if (millisecondsTimeout < -1)
322 if (millisecondsTimeout == 0)
328 int num = millisecondsTimeout;
329 if (millisecondsTimeout != -1)
331 startTime = TimeoutHelper.GetTime();
338 for (
int i = 0; i < spinCount; i++)
355 else if (i % num4 == 0)
359 else if (i % num3 == 0)
367 if (i >= 100 && i % 10 == 0)
372 EnsureLockObjectCreated();
373 using (cancellationToken.InternalRegisterWithoutEC(s_cancellationTokenCallback,
this))
382 num = TimeoutHelper.UpdateTimeOut(startTime, millisecondsTimeout);
413 [__DynamicallyInvokable]
422 [__DynamicallyInvokable]
423 protected virtual void Dispose(
bool disposing)
425 if ((m_combinedState & 0x40000000) != 0)
429 m_combinedState |= 1073741824;
433 if (eventObj !=
null)
444 private void ThrowIfDisposed()
446 if ((m_combinedState & 0x40000000) != 0)
452 private static void CancellationTokenCallback(
object obj)
455 lock (manualResetEventSlim.m_lock)
457 Monitor.PulseAll(manualResetEventSlim.m_lock);
461 private void UpdateStateAtomically(
int newBits,
int updateBitsMask)
463 SpinWait spinWait =
default(SpinWait);
466 int combinedState = m_combinedState;
467 int value = (combinedState & ~updateBitsMask) | newBits;
468 if (Interlocked.CompareExchange(ref m_combinedState, value, combinedState) == combinedState)
476 private static int ExtractStatePortionAndShiftRight(
int state,
int mask,
int rightBitShiftCount)
478 return (
int)((uint)(state & mask) >> rightBitShiftCount);
481 private static int ExtractStatePortion(
int state,
int mask)
void Wait(CancellationToken cancellationToken)
Blocks the current thread until the current T:System.Threading.ManualResetEventSlim receives a signal...
void Dispose()
Releases all resources used by the current instance of the T:System.Threading.ManualResetEventSlim cl...
Propagates notification that operations should be canceled.
Encapsulates operating system–specific objects that wait for exclusive access to shared resources.
void ThrowIfCancellationRequested()
Throws a T:System.OperationCanceledException if this token has had cancellation requested.
static void PulseAll(object obj)
Notifies all waiting threads of a change in the object's state.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Threading.ManualResetEventSlim,...
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...
Provides a mechanism that synchronizes access to objects.
static bool Wait(object obj, int millisecondsTimeout, bool exitContext)
Releases the lock on an object and blocks the current thread until it reacquires the lock....
bool Wait(TimeSpan timeout)
Blocks the current thread until the current T:System.Threading.ManualResetEventSlim is set,...
double TotalMilliseconds
Gets the value of the current T:System.TimeSpan structure expressed in whole and fractional milliseco...
ManualResetEventSlim(bool initialState)
Initializes a new instance of the T:System.Threading.ManualResetEventSlim class with a Boolean value ...
ManualResetEventSlim()
Initializes a new instance of the T:System.Threading.ManualResetEventSlim class with an initial state...
void Wait()
Blocks the current thread until the current T:System.Threading.ManualResetEventSlim is set.
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.
Provides information about, and means to manipulate, the current environment and platform....
bool Wait(TimeSpan timeout, CancellationToken cancellationToken)
Blocks the current thread until the current T:System.Threading.ManualResetEventSlim is set,...
void Set()
Sets the state of the event to signaled, which allows one or more threads waiting on the event to pro...
bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
Blocks the current thread until the current T:System.Threading.ManualResetEventSlim is set,...
Notifies one or more waiting threads that an event has occurred. This class cannot be inherited.
Provides a slimmed down version of T:System.Threading.ManualResetEvent.
bool? IsSet
Gets whether the event is set.
virtual void Close()
Releases all resources held by the current T:System.Threading.WaitHandle.
ManualResetEventSlim(bool initialState, int spinCount)
Initializes a new instance of the T:System.Threading.ManualResetEventSlim class with a Boolean value ...
Controls the system garbage collector, a service that automatically reclaims unused memory.
Represents a time interval.To browse the .NET Framework source code for this type,...
The exception that is thrown when a method call is invalid for the object's current state.
void Reset()
Sets the state of the event to nonsignaled, which causes threads to block.
static void Sleep(int millisecondsTimeout)
Suspends the current thread for the specified number of milliseconds.
int SpinCount
Gets the number of spin waits that will occur before falling back to a kernel-based wait operation.
static bool Yield()
Causes the calling thread to yield execution to another thread that is ready to run on the current pr...
bool Wait(int millisecondsTimeout)
Blocks the current thread until the current T:System.Threading.ManualResetEventSlim is set,...
static void SpinWait(int iterations)
Causes a thread to wait the number of times defined by the iterations parameter.
Creates and controls a thread, sets its priority, and gets its status.