[sgen] Exclusive write on binary protocol file
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / CriticalHandle.cs
index 2f2574b3ae321057f4190fd732bc41c651038c5f..ce50aff31b497782d36b4cbc5bc7e6b382aee63a 100644 (file)
@@ -5,7 +5,6 @@
 //   Kazuki Oikawa  (kazuki@panicode.com)
 //
 
-#if NET_2_0
 
 using System;
 using System.Runtime.ConstrainedExecution;
@@ -18,6 +17,7 @@ namespace System.Runtime.InteropServices
                protected IntPtr handle;
                bool _disposed = false;
 
+               [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
                protected CriticalHandle (IntPtr invalidHandleValue)
                {
                        handle = invalidHandleValue;
@@ -26,30 +26,35 @@ namespace System.Runtime.InteropServices
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                ~CriticalHandle ()
                {
-                       Dispose ();
+                       Dispose (false);
                }
 
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                public void Close ()
                {
-                       Dispose ();
+                       Dispose (true);
                }
 
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                public void Dispose ()
                {
-                       if (_disposed)
-                               return;
+                       Dispose (true);
+               }
 
-                       _disposed = true;
-                       if (IsInvalid)
+               [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
+               protected virtual void Dispose (bool disposing)
+               {
+                       if (_disposed)
                                return;
 
-                       if (ReleaseHandle ()) {
-                               GC.SuppressFinalize (this);
-                       } else {
-                               // Failed in release...
+                       if (!IsInvalid){
+                               if (!_disposed && !ReleaseHandle ()) {
+                                       GC.SuppressFinalize (this);
+                               } else {
+                                       // Failed in release...
+                               }
                        }
+                       _disposed = true;
                }
 
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
@@ -78,4 +83,3 @@ namespace System.Runtime.InteropServices
                }
        }
 }
-#endif