Author: miguel
Date: 2006-12-11 01:48:40 -0500 (Mon, 11 Dec 2006)
New Revision: 69296

Modified:
   trunk/mcs/class/corlib/Microsoft.Win32.SafeHandles/ChangeLog
   trunk/mcs/class/corlib/Microsoft.Win32.SafeHandles/SafeFileHandle.cs
   
trunk/mcs/class/corlib/System.Runtime.ConstrainedExecution/CriticialFinalizerObject.cs
   trunk/mcs/class/corlib/System.Runtime.InteropServices/ChangeLog
   trunk/mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs
Log:
The managed side:

2006-12-11  Miguel de Icaza  <[EMAIL PROTECTED]>

        * SafeHandle.cs: Implement finalizer.

        DangerousAddRef will now throw an exception if the object was
        disposed.

2006-12-10  Miguel de Icaza  <[EMAIL PROTECTED]>

        * SafeFileHandle.cs: Implement this one.




Modified: trunk/mcs/class/corlib/Microsoft.Win32.SafeHandles/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Microsoft.Win32.SafeHandles/ChangeLog        
2006-12-11 06:28:37 UTC (rev 69295)
+++ trunk/mcs/class/corlib/Microsoft.Win32.SafeHandles/ChangeLog        
2006-12-11 06:48:40 UTC (rev 69296)
@@ -1,3 +1,7 @@
+2006-12-10  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * SafeFileHandle.cs: Implement this one.
+
 2006-12-02  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * SafeFileHandle.cs: Remove the IsInvalid method, as that is

Modified: trunk/mcs/class/corlib/Microsoft.Win32.SafeHandles/SafeFileHandle.cs
===================================================================
--- trunk/mcs/class/corlib/Microsoft.Win32.SafeHandles/SafeFileHandle.cs        
2006-12-11 06:28:37 UTC (rev 69295)
+++ trunk/mcs/class/corlib/Microsoft.Win32.SafeHandles/SafeFileHandle.cs        
2006-12-11 06:48:40 UTC (rev 69296)
@@ -29,21 +29,23 @@
 #if NET_2_0
 
 using System;
+using System.IO;
 
 namespace Microsoft.Win32.SafeHandles {
 
-       [MonoTODO("Not implemented on Mono")]
        public sealed class SafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid {
-
                public SafeFileHandle (IntPtr preexistingHandle, bool 
ownsHandle) : base (ownsHandle)
                {
                        SetHandle (preexistingHandle);
                }
 
-               [MonoTODO("Not implemented")]
                protected override bool ReleaseHandle ()
                {
-                       throw new NotImplementedException ();
+                       MonoIOError error;
+                       
+                       MonoIO.Close (DangerousGetHandle (), out error);
+
+                       return error == MonoIOError.ERROR_SUCCESS;
                }
 
        }

Modified: 
trunk/mcs/class/corlib/System.Runtime.ConstrainedExecution/CriticialFinalizerObject.cs
===================================================================
--- 
trunk/mcs/class/corlib/System.Runtime.ConstrainedExecution/CriticialFinalizerObject.cs
      2006-12-11 06:28:37 UTC (rev 69295)
+++ 
trunk/mcs/class/corlib/System.Runtime.ConstrainedExecution/CriticialFinalizerObject.cs
      2006-12-11 06:48:40 UTC (rev 69296)
@@ -35,6 +35,11 @@
        [ComVisible (true)]
         public abstract class CriticalFinalizerObject
         {
+               //
+               // WARNING: If you add any fields here, update the definition
+               // for the runtime structures as well
+               //
+               
                [ReliabilityContract (Consistency.WillNotCorruptState, 
Cer.MayFail)]
                 protected CriticalFinalizerObject ()
                 {

Modified: trunk/mcs/class/corlib/System.Runtime.InteropServices/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Runtime.InteropServices/ChangeLog     
2006-12-11 06:28:37 UTC (rev 69295)
+++ trunk/mcs/class/corlib/System.Runtime.InteropServices/ChangeLog     
2006-12-11 06:48:40 UTC (rev 69296)
@@ -1,3 +1,10 @@
+2006-12-11  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * SafeHandle.cs: Implement finalizer.
+
+       DangerousAddRef will now throw an exception if the object was
+       disposed.
+
 2006-12-02  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * SafeHandle.cs: Implement this class.

Modified: trunk/mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs
===================================================================
--- trunk/mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs 
2006-12-11 06:28:37 UTC (rev 69295)
+++ trunk/mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs 
2006-12-11 06:48:40 UTC (rev 69296)
@@ -30,6 +30,17 @@
 //     For details, see:
 //     http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx
 //
+// On implementing SafeHandles:
+//     http://blogs.msdn.com/bclteam/archive/2005/03/15/396335.aspx
+//
+// Issues:
+//     The System.Runtime.ConstrainedExecution.ReliabilityContractAttribute has
+//     not been applied to any APIs here yet.
+//
+//     TODO: Although DangerousAddRef has been implemented, I need to
+//     find out whether the runtime performs the P/Invoke if the
+//     handle has been disposed already.
+//
 
 #if NET_2_0
 using System;
@@ -39,8 +50,13 @@
 namespace System.Runtime.InteropServices
 {
        public abstract class SafeHandle : CriticalFinalizerObject, IDisposable 
{
+               //
+               // Warning: the offset of handle is mapped inside the runtime
+               // if you move this, you must updated the runtime definition of
+               // MonoSafeHandle
+               //
+               protected IntPtr handle;
                object handle_lock = new object ();
-               protected IntPtr handle;
                IntPtr invalid_handle_value;
                int refcount = 0;
                bool owns_handle;
@@ -75,6 +91,16 @@
                public void DangerousAddRef (ref bool success)
                {
                        lock (handle_lock){
+                               if (handle == invalid_handle_value || refcount 
== 0){
+                                       //
+                                       // In MS, calling sf.Close () followed 
by a call
+                                       // to P/Invoke with SafeHandles throws 
this, but
+                                       // am left wondering: when would 
"success" be
+                                       // set to false?
+                                       //
+                                       throw new ObjectDisposedException 
("handle");
+                               }
+                               
                                refcount++;
                                success = true;
                        }
@@ -87,10 +113,13 @@
 
                public void DangerousRelease ()
                {
+                       Console.WriteLine ("DangerousRelease called");
                        lock (handle_lock){
                                refcount--;
-                               if (refcount == 0)
+                               if (refcount == 0 && owns_handle){
                                        ReleaseHandle ();
+                                       handle = invalid_handle_value;
+                               }
                        }
                }
 
@@ -139,6 +168,14 @@
                public abstract bool IsInvalid {
                        get;
                }
+
+               ~SafeHandle ()
+               {
+                       if (owns_handle){
+                               ReleaseHandle ();
+                               handle = invalid_handle_value;
+                       }
+               }
        }
 }
 #endif

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to