We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- using System;
- using System.Security;
- using System.Security.Permissions;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- using System.Runtime.ConstrainedExecution;
- using System.Runtime.Versioning;
- using Microsoft.Win32;
- using System.Threading;
- namespace Microsoft.Win32.SafeHandles
- {
-
- [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
- public sealed class SafeWaitHandle : SafeHandleZeroOrMinusOneIsInvalid
- {
-
-
-
-
-
-
-
- private bool bIsMutex = false;
-
-
-
- private bool bIsReservedMutex = false;
-
- private SafeWaitHandle() : base(true)
- {
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
- public SafeWaitHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle)
- {
- SetHandle(existingHandle);
- }
-
- [ResourceExposure(ResourceScope.Machine)]
- [ResourceConsumption(ResourceScope.Machine)]
- protected override bool ReleaseHandle()
- {
- if (!bIsMutex || Environment.HasShutdownStarted)
- return Win32Native.CloseHandle(handle);
-
- bool bReturn = false;
- bool bMutexObtained = false;
- try {
- if (!bIsReservedMutex) {
- Mutex.AcquireReservedMutex(ref bMutexObtained);
- }
- bReturn = Win32Native.CloseHandle(handle);
- }
- finally {
- if (bMutexObtained)
- Mutex.ReleaseReservedMutex();
- }
- return bReturn;
- }
-
- internal void SetAsMutex()
- {
- bIsMutex = true;
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- internal void SetAsReservedMutex()
- {
- bIsReservedMutex = true;
- }
- }
- }