private void MutexCleanupCode(Object userData, bool exceptionThrown) { MutexCleanupInfo cleanupInfo = (MutexCleanupInfo)userData; // If hasThreadAffinity isn't true, we've thrown an exception in the above try, and we must free the mutex // on this OS thread before ending our thread affninity. if (!hasThreadAffinity) { if (cleanupInfo.mutexHandle != null && !cleanupInfo.mutexHandle.IsInvalid) { if (cleanupInfo.inCriticalRegion) { Win32Native.ReleaseMutex(cleanupInfo.mutexHandle); } cleanupInfo.mutexHandle.Dispose(); } if (cleanupInfo.inCriticalRegion) { #if !FEATURE_CORECLR Thread.EndCriticalRegion(); Thread.EndThreadAffinity(); #endif } } }
public static bool SignalAndWait(WaitHandle toSignal, WaitHandle toWaitOn, int millisecondsTimeout, bool exitContext) { if (toSignal == null) { throw new ArgumentNullException("toSignal"); } if (toWaitOn == null) { throw new ArgumentNullException("toWaitOn"); } if (-1 > millisecondsTimeout) { throw new ArgumentOutOfRangeException("millisecondsTimeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1")); } int num = SignalAndWaitOne(toSignal.safeWaitHandle, toWaitOn.safeWaitHandle, millisecondsTimeout, toWaitOn.hasThreadAffinity, exitContext); if ((0x7fffffff != num) && toSignal.hasThreadAffinity) { Thread.EndCriticalRegion(); Thread.EndThreadAffinity(); } if (0x80 == num) { ThrowAbandonedMutexException(); } if (0x12a == num) { throw new InvalidOperationException(Environment.GetResourceString("Threading.WaitHandleTooManyPosts")); } return(num == 0); }
public void Dispose() { if (_signal == null) { return; } BCLDebug.Assert(Thread.CurrentThread == _signal.Thread, "You called Dispose on the wrong thread, or reused your cancellation signal?"); List <CancellationSignal> signals = _signal.Thread.CancellationSignals; bool tookLock = false; RuntimeHelpers.PrepareConstrainedRegions(); try { Monitor.ReliableEnter(signals, out tookLock); CancellationSignal s = signals[signals.Count - 1]; signals.RemoveAt(signals.Count - 1); if (!Object.ReferenceEquals(s, _signal)) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CancellationRegionLeak")); } _signal.Thread = null; _signal = null; } finally { if (tookLock) { Monitor.Exit(signals); } Thread.EndThreadAffinity(); } }
public void ReleaseMutex() { if (!Win32Native.ReleaseMutex(this.safeWaitHandle)) { throw new ApplicationException(Environment.GetResourceString("Arg_SynchronizationLockException")); } Thread.EndCriticalRegion(); Thread.EndThreadAffinity(); }
public static bool SignalAndWait( WaitHandle toSignal, WaitHandle toWaitOn, int millisecondsTimeout, bool exitContext) { #if PLATFORM_UNIX throw new PlatformNotSupportedException(); #else if (null == toSignal) { throw new ArgumentNullException("toSignal"); } if (null == toWaitOn) { throw new ArgumentNullException("toWaitOn"); } if (-1 > millisecondsTimeout) { throw new ArgumentOutOfRangeException("millisecondsTimeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1")); } Contract.EndContractBlock(); //NOTE: This API is not supporting Pause/Resume as it's not exposed in CoreCLR (not in WP or SL) int ret = SignalAndWaitOne(toSignal.safeWaitHandle, toWaitOn.safeWaitHandle, millisecondsTimeout, toWaitOn.hasThreadAffinity, exitContext); #if !FEATURE_CORECLR if (WAIT_FAILED != ret && toSignal.hasThreadAffinity) { Thread.EndCriticalRegion(); Thread.EndThreadAffinity(); } #endif if (WAIT_ABANDONED == ret) { ThrowAbandonedMutexException(); } if (ERROR_TOO_MANY_POSTS == ret) { throw new InvalidOperationException(Environment.GetResourceString("Threading.WaitHandleTooManyPosts")); } //Object was signaled if (WAIT_OBJECT_0 == ret) { return(true); } //Timeout return(false); #endif }
private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { bool bHandleObtained = false; bool flag2 = false; Label_0004: mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); int num = Marshal.GetLastWin32Error(); if (mutexHandle.IsInvalid && (num == 5)) { RuntimeHelpers.PrepareConstrainedRegions(); try { try { } finally { Thread.BeginThreadAffinity(); flag2 = true; } AcquireReservedMutex(ref bHandleObtained); mutexHandle = Win32Native.OpenMutex(0x100001, false, name); if (!mutexHandle.IsInvalid) { num = 0xb7; } else { num = Marshal.GetLastWin32Error(); } } finally { if (bHandleObtained) { ReleaseReservedMutex(); } if (flag2) { Thread.EndThreadAffinity(); } } switch (num) { case 2: goto Label_0004; case 0: return(0xb7); } } return(num); }
public void Dispose() { if (this._isDisposed) { return; } this._isDisposed = true; GC.SuppressFinalize(this); NativeMethods.LeaveBackgroundMode(); Thread.EndThreadAffinity(); }
private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { bool flag = false; int num; do { mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); num = Marshal.GetLastWin32Error(); if (!mutexHandle.IsInvalid || num != 5) { return(num); } RuntimeHelpers.PrepareConstrainedRegions(); try { try { } finally { Thread.BeginThreadAffinity(); flag = true; } mutexHandle = Win32Native.OpenMutex(1048577, false, name); if (!mutexHandle.IsInvalid) { num = 183; } else { num = Marshal.GetLastWin32Error(); } } finally { if (flag) { Thread.EndThreadAffinity(); } } }while (num == 2); if (num == 0) { num = 183; } return(num); }
public void ReleaseMutex() { if (Win32Native.ReleaseMutex(safeWaitHandle)) { #if !FEATURE_CORECLR Thread.EndCriticalRegion(); Thread.EndThreadAffinity(); #endif } else { #if FEATURE_CORECLR throw new Exception(Environment.GetResourceString("Arg_SynchronizationLockException")); #else throw new ApplicationException(Environment.GetResourceString("Arg_SynchronizationLockException")); #endif // FEATURE_CORECLR } }
private void MutexCleanupCode(object userData, bool exceptionThrown) { Mutex.MutexCleanupInfo mutexCleanupInfo = (Mutex.MutexCleanupInfo)userData; if (!this.hasThreadAffinity) { if (mutexCleanupInfo.mutexHandle != null && !mutexCleanupInfo.mutexHandle.IsInvalid) { if (mutexCleanupInfo.inCriticalRegion) { Win32Native.ReleaseMutex(mutexCleanupInfo.mutexHandle); } mutexCleanupInfo.mutexHandle.Dispose(); } if (mutexCleanupInfo.inCriticalRegion) { Thread.EndCriticalRegion(); Thread.EndThreadAffinity(); } } }
static int CreateMutexHandle(bool initiallyOwned, String name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { int errorCode; bool fAffinity = false; while (true) { mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); errorCode = Marshal.GetLastWin32Error(); if (!mutexHandle.IsInvalid) { break; } if (errorCode == Win32Native.ERROR_ACCESS_DENIED) { // If a mutex with the name already exists, OS will try to open it with FullAccess. // It might fail if we don't have enough access. In that case, we try to open the mutex will modify and synchronize access. // RuntimeHelpers.PrepareConstrainedRegions(); try { try { } finally { #if !FEATURE_CORECLR Thread.BeginThreadAffinity(); #endif fAffinity = true; } mutexHandle = Win32Native.OpenMutex(Win32Native.MUTEX_MODIFY_STATE | Win32Native.SYNCHRONIZE, false, name); if (!mutexHandle.IsInvalid) { errorCode = Win32Native.ERROR_ALREADY_EXISTS; } else { errorCode = Marshal.GetLastWin32Error(); } } finally { if (fAffinity) { #if !FEATURE_CORECLR Thread.EndThreadAffinity(); #endif } } // There could be a ---- here, the other owner of the mutex can free the mutex, // We need to retry creation in that case. if (errorCode != Win32Native.ERROR_FILE_NOT_FOUND) { if (errorCode == Win32Native.ERROR_SUCCESS) { errorCode = Win32Native.ERROR_ALREADY_EXISTS; } break; } } else { break; } } return(errorCode); }
private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { bool flag = false; bool flag2 = false; bool flag3 = false; Label_0006: flag2 = false; flag3 = false; mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); int num = Marshal.GetLastWin32Error(); if (!mutexHandle.IsInvalid || (num != 5)) { return(num); } RuntimeHelpers.PrepareConstrainedRegions(); try { RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { Thread.BeginThreadAffinity(); flag = true; } mutexHandle = Win32Native.OpenMutex(0x100001, false, name); if (!mutexHandle.IsInvalid) { num = 0xb7; if (Environment.IsW2k3) { SafeWaitHandle handle = Win32Native.OpenMutex(0x100001, false, name); if (!handle.IsInvalid) { RuntimeHelpers.PrepareConstrainedRegions(); try { uint num2 = 0; IntPtr ptr = mutexHandle.DangerousGetHandle(); IntPtr ptr2 = handle.DangerousGetHandle(); IntPtr[] handles = new IntPtr[] { ptr, ptr2 }; num2 = Win32Native.WaitForMultipleObjects(2, handles, true, 0); GC.KeepAlive(handles); if (num2 == uint.MaxValue) { if (Marshal.GetLastWin32Error() != 0x57) { mutexHandle.Dispose(); flag3 = true; } } else { flag2 = true; if ((num2 >= 0) && (num2 < 2)) { Win32Native.ReleaseMutex(mutexHandle); Win32Native.ReleaseMutex(handle); } else if ((num2 >= 0x80) && (num2 < 130)) { Win32Native.ReleaseMutex(mutexHandle); Win32Native.ReleaseMutex(handle); } mutexHandle.Dispose(); } goto Label_0166; } finally { handle.Dispose(); } } mutexHandle.Dispose(); flag3 = true; } } else { num = Marshal.GetLastWin32Error(); } } finally { if (flag) { Thread.EndThreadAffinity(); } } Label_0166: if ((flag2 || flag3) || (num == 2)) { goto Label_0006; } if (num == 0) { num = 0xb7; } return(num); }