The Labs \ Source Viewer \ SSCLI \ Microsoft.Win32.SafeHandles \ SafeWaitHandle

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  SafeWaitHandle
  18. **
  19. **
  20. ** A wrapper for Win32 events (mutexes, auto reset events, and
  21. ** manual reset events).  Used by WaitHandle.
  22. **
  23. **
  24. ===========================================================*/
  25. using System;
  26. using System.Security;
  27. using System.Security.Permissions;
  28. using System.Runtime.InteropServices;
  29. using System.Runtime.CompilerServices;
  30. using System.Runtime.ConstrainedExecution;
  31. using System.Runtime.Versioning;
  32. using Microsoft.Win32;
  33. using System.Threading;
  34. namespace Microsoft.Win32.SafeHandles
  35. {
  36.    
  37.     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  38.     public sealed class SafeWaitHandle : SafeHandleZeroOrMinusOneIsInvalid
  39.     {
  40.        
  41.         // Special case flags for Mutexes enables workaround for known OS bug at
  42.         // http://support.microsoft.com/default.aspx?scid=kb;en-us;889318
  43.         // One machine-wide mutex serializes all OpenMutex and CloseHandle operations.
  44.        
  45.        
  46.         // bIsMutex: if true, we need to grab machine-wide mutex before doing any Close ops.
  47.         private bool bIsMutex = false;
  48.        
  49.         // bIsMutex: if true, we need to avoid grabbing the machine-wide mutex before Close ops,
  50.         // since that mutex is, of course, this very handle.
  51.         private bool bIsReservedMutex = false;
  52.         // Called by P/Invoke marshaler
  53.         private SafeWaitHandle() : base(true)
  54.         {
  55.         }
  56.        
  57.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  58.         public SafeWaitHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle)
  59.         {
  60.             SetHandle(existingHandle);
  61.         }
  62.        
  63.         [ResourceExposure(ResourceScope.Machine)]
  64.         [ResourceConsumption(ResourceScope.Machine)]
  65.         protected override bool ReleaseHandle()
  66.         {
  67.             if (!bIsMutex || Environment.HasShutdownStarted)
  68.                 return Win32Native.CloseHandle(handle);
  69.            
  70.             bool bReturn = false;
  71.             bool bMutexObtained = false;
  72.             try {
  73.                 if (!bIsReservedMutex) {
  74.                     Mutex.AcquireReservedMutex(ref bMutexObtained);
  75.                 }
  76.                 bReturn = Win32Native.CloseHandle(handle);
  77.             }
  78.             finally {
  79.                 if (bMutexObtained)
  80.                     Mutex.ReleaseReservedMutex();
  81.             }
  82.             return bReturn;
  83.         }
  84.        
  85.         internal void SetAsMutex()
  86.         {
  87.             bIsMutex = true;
  88.         }
  89.        
  90.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  91.         internal void SetAsReservedMutex()
  92.         {
  93.             bIsReservedMutex = true;
  94.         }
  95.     }
  96. }

Developer Fusion