#if MONO
using System;
using System.Diagnostics;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace System.Threading
{
/// Provides a slimmed down version of .
[__DynamicallyInvokable, DebuggerDisplay("Set = {IsSet}"), ComVisible(false)]
[HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
public class ManualResetEventSlim : IDisposable
{
private volatile object m_lock;
private volatile ManualResetEvent m_eventObj;
private volatile int m_combinedState;
private static Action s_cancellationTokenCallback = new Action(ManualResetEventSlim.CancellationTokenCallback);
private const int DEFAULT_SPIN_SP = 1;
private const int DEFAULT_SPIN_MP = 10;
private const int SignalledState_BitMask = -2147483648;
private const int SignalledState_ShiftCount = 31;
private const int Dispose_BitMask = 1073741824;
private const int SpinCountState_BitMask = 1073217536;
private const int SpinCountState_ShiftCount = 19;
private const int SpinCountState_MaxValue = 2047;
private const int NumWaitersState_BitMask = 524287;
private const int NumWaitersState_ShiftCount = 0;
private const int NumWaitersState_MaxValue = 524287;
/// Gets the underlying object for this .
/// The underlying event object fore this .
[__DynamicallyInvokable]
public WaitHandle WaitHandle
{
[__DynamicallyInvokable]
get
{
this.ThrowIfDisposed();
if (this.m_eventObj == null)
{
this.LazyInitializeEvent();
}
return this.m_eventObj;
}
}
/// Gets whether the event is set.
/// true if the event has is set; otherwise, false.
[__DynamicallyInvokable]
public bool IsSet
{
[__DynamicallyInvokable]
get
{
return 0 != ManualResetEventSlim.ExtractStatePortion(this.m_combinedState, -2147483648);
}
private set
{
this.UpdateStateAtomically((value ? 1 : 0) << 31, -2147483648);
}
}
/// Gets the number of spin waits that will be occur before falling back to a kernel-based wait operation.
/// Returns the number of spin waits that will be occur before falling back to a kernel-based wait operation.
[__DynamicallyInvokable]
public int SpinCount
{
[__DynamicallyInvokable]
get
{
return ManualResetEventSlim.ExtractStatePortionAndShiftRight(this.m_combinedState, 1073217536, 19);
}
private set
{
this.m_combinedState = ((this.m_combinedState & -1073217537) | value << 19);
}
}
private int Waiters
{
get
{
return ManualResetEventSlim.ExtractStatePortionAndShiftRight(this.m_combinedState, 524287, 0);
}
set
{
if (value >= 524287)
{
throw new InvalidOperationException("ManualResetEventSlim_ctor_TooManyWaiters");
}
this.UpdateStateAtomically(value, 524287);
}
}
/// Initializes a new instance of the class with an initial state of nonsignaled.
[__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public ManualResetEventSlim()
: this(false)
{
}
/// Initializes a new instance of the class with a Boolean value indicating whether to set the intial state to signaled.
/// true to set the initial state signaled; false to set the initial state to nonsignaled.
[__DynamicallyInvokable]
public ManualResetEventSlim(bool initialState)
{
this.Initialize(initialState, 10);
}
/// Initializes a new instance of the class with a Boolean value indicating whether to set the intial state to signaled and a specified spin count.
/// true to set the initial state to signaled; false to set the initial state to nonsignaled.
/// The number of spin waits that will occur before falling back to a kernel-based wait operation.
///
/// is less than 0 or greater than the maximum allowed value.
[__DynamicallyInvokable]
public ManualResetEventSlim(bool initialState, int spinCount)
{
if (spinCount < 0)
{
throw new ArgumentOutOfRangeException("spinCount");
}
if (spinCount > 2047)
{
throw new ArgumentOutOfRangeException("spinCount", "ManualResetEventSlim_ctor_SpinCountOutOfRange");
}
this.Initialize(initialState, spinCount);
}
/// Sets the state of the event to signaled, which allows one or more threads waiting on the event to proceed.
[__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Set()
{
this.Set(false);
}
private void Set(bool duringCancellation)
{
this.IsSet = true;
if (this.Waiters > 0)
{
lock (this.m_lock)
{
Monitor.PulseAll(this.m_lock);
}
}
ManualResetEvent eventObj = this.m_eventObj;
if (eventObj != null && !duringCancellation)
{
lock (eventObj)
{
if (this.m_eventObj != null)
{
this.m_eventObj.Set();
}
}
}
}
/// Sets the state of the event to nonsignaled, which causes threads to block.
/// The object has already been disposed.
[__DynamicallyInvokable]
public void Reset()
{
this.ThrowIfDisposed();
if (this.m_eventObj != null)
{
this.m_eventObj.Reset();
}
this.IsSet = false;
}
/// Blocks the current thread until the current is set.
/// The maximum number of waiters has been exceeded.
/// The object has already been disposed.
[__DynamicallyInvokable]
public void Wait()
{
this.Wait(-1, default(CancellationToken));
}
/// Blocks the current thread until the current receives a signal, while observing a .
/// The to observe.
/// The maximum number of waiters has been exceeded.
///
/// was canceled.
/// The object has already been disposed or the that created has been disposed.
[__DynamicallyInvokable]
public void Wait(CancellationToken cancellationToken)
{
this.Wait(-1, cancellationToken);
}
/// Blocks the current thread until the current is set, using a to measure the time interval.
/// true if the was set; otherwise, false.
/// A that represents the number of milliseconds to wait, or a that represents -1 milliseconds to wait indefinitely.
///
/// is a negative number other than -1 milliseconds, which represents an infinite time-out -or- timeout is greater than .
/// The maximum number of waiters has been exceeded.
/// The object has already been disposed.
[__DynamicallyInvokable]
public bool Wait(TimeSpan timeout)
{
long num = (long)timeout.TotalMilliseconds;
if (num < -1L || num > 2147483647L)
{
throw new ArgumentOutOfRangeException("timeout");
}
return this.Wait((int)num, default(CancellationToken));
}
/// Blocks the current thread until the current is set, using a to measure the time interval, while observing a .
/// true if the was set; otherwise, false.
/// A that represents the number of milliseconds to wait, or a that represents -1 milliseconds to wait indefinitely.
/// The to observe.
///
/// was canceled.
///
/// is a negative number other than -1 milliseconds, which represents an infinite time-out -or- timeout is greater than .
/// The maximum number of waiters has been exceeded.
/// The object has already been disposed or the that created has been disposed.
[__DynamicallyInvokable]
public bool Wait(TimeSpan timeout, CancellationToken cancellationToken)
{
long num = (long)timeout.TotalMilliseconds;
if (num < -1L || num > 2147483647L)
{
throw new ArgumentOutOfRangeException("timeout");
}
return this.Wait((int)num, cancellationToken);
}
/// Blocks the current thread until the current is set, using a 32-bit signed integer to measure the time interval.
/// true if the was set; otherwise, false.
/// The number of milliseconds to wait, or (-1) to wait indefinitely.
///
/// is a negative number other than -1, which represents an infinite time-out.
/// The maximum number of waiters has been exceeded.
/// The object has already been disposed.
[__DynamicallyInvokable]
public bool Wait(int millisecondsTimeout)
{
return this.Wait(millisecondsTimeout, default(CancellationToken));
}
/// Blocks the current thread until the current is set, using a 32-bit signed integer to measure the time interval, while observing a .
/// true if the was set; otherwise, false.
/// The number of milliseconds to wait, or (-1) to wait indefinitely.
/// The to observe.
///
/// was canceled.
///
/// is a negative number other than -1, which represents an infinite time-out.
/// The maximum number of waiters has been exceeded.
/// The object has already been disposed or the that created has been disposed.
[__DynamicallyInvokable]
public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
{
this.ThrowIfDisposed();
cancellationToken.ThrowIfCancellationRequested();
if (millisecondsTimeout < -1)
{
throw new ArgumentOutOfRangeException("millisecondsTimeout");
}
if (!this.IsSet)
{
if (millisecondsTimeout == 0)
{
return false;
}
uint startTime = 0u;
bool flag = false;
int num = millisecondsTimeout;
if (millisecondsTimeout != -1)
{
startTime = TimeoutHelper.GetTime();
flag = true;
}
int num2 = 10;
int num3 = 5;
int num4 = 20;
int spinCount = this.SpinCount;
for (int i = 0; i < spinCount; i++)
{
if (this.IsSet)
{
return true;
}
if (i < num2)
{
if (i == num2 / 2)
{
Thread.Sleep(0);
}
else
{
Thread.SpinWait(PlatformHelper.ProcessorCount * (4 << i));
}
}
else
{
if (i % num4 == 0)
{
Thread.Sleep(1);
}
else
{
Thread.Sleep(0);
//if (i % num3 == 0)
//{
// Thread.Sleep(0);
//}
//else
//{
// Thread.Sleep(0);
//}
}
}
if (i >= 100 && i % 10 == 0)
{
cancellationToken.ThrowIfCancellationRequested();
}
}
this.EnsureLockObjectCreated();
using (cancellationToken.InternalRegisterWithoutEC(ManualResetEventSlim.s_cancellationTokenCallback, this))
{
lock (this.m_lock)
{
while (!this.IsSet)
{
cancellationToken.ThrowIfCancellationRequested();
if (flag)
{
num = TimeoutHelper.UpdateTimeOut(startTime, millisecondsTimeout);
if (num <= 0)
{
bool result = false;
return result;
}
}
this.Waiters++;
if (this.IsSet)
{
this.Waiters--;
bool result = true;
return result;
}
try
{
if (!Monitor.Wait(this.m_lock, num))
{
bool result = false;
return result;
}
}
finally
{
this.Waiters--;
}
}
}
}
return true;
}
return true;
}
/// Releases all resources used by the current instance of the class.
[__DynamicallyInvokable]
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// When overridden in a derived class, releases the unmanaged resources used by the , and optionally releases the managed resources.
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
[__DynamicallyInvokable]
protected virtual void Dispose(bool disposing)
{
if ((this.m_combinedState & 1073741824) != 0)
{
return;
}
this.m_combinedState |= 1073741824;
if (disposing)
{
ManualResetEvent eventObj = this.m_eventObj;
if (eventObj != null)
{
lock (eventObj)
{
eventObj.Close();
this.m_eventObj = null;
}
}
}
}
private void Initialize(bool initialState, int spinCount)
{
this.m_combinedState = (initialState ? -2147483648 : 0);
this.SpinCount = (PlatformHelper.IsSingleProcessor ? 1 : spinCount);
}
private void EnsureLockObjectCreated()
{
if (this.m_lock != null)
{
return;
}
object value = new object();
Interlocked.CompareExchange(ref this.m_lock, value, null);
}
private bool LazyInitializeEvent()
{
bool isSet = this.IsSet;
ManualResetEvent manualResetEvent = new ManualResetEvent(isSet);
if (Interlocked.CompareExchange(ref this.m_eventObj, manualResetEvent, null) != null)
{
manualResetEvent.Close();
return false;
}
bool isSet2 = this.IsSet;
if (isSet2 != isSet)
{
lock (manualResetEvent)
{
if (this.m_eventObj == manualResetEvent)
{
manualResetEvent.Set();
}
}
}
return true;
}
private void ThrowIfDisposed()
{
if ((this.m_combinedState & 1073741824) != 0)
{
throw new ObjectDisposedException("ManualResetEventSlim_Disposed");
}
}
private static void CancellationTokenCallback(object obj)
{
ManualResetEventSlim manualResetEventSlim = obj as ManualResetEventSlim;
lock (manualResetEventSlim.m_lock)
{
Monitor.PulseAll(manualResetEventSlim.m_lock);
}
}
private void UpdateStateAtomically(int newBits, int updateBitsMask)
{
SpinWait spinWait = default(SpinWait);
while (true)
{
int combinedState = this.m_combinedState;
int value = (combinedState & ~updateBitsMask) | newBits;
if (Interlocked.CompareExchange(ref this.m_combinedState, value, combinedState) == combinedState)
{
break;
}
spinWait.SpinOnce();
}
}
private static int ExtractStatePortionAndShiftRight(int state, int mask, int rightBitShiftCount)
{
return (int)((uint)(state & mask) >> rightBitShiftCount);
}
private static int ExtractStatePortion(int state, int mask)
{
return state & mask;
}
}
}
#endif