Author: miguel
Date: 2007-01-03 14:50:44 -0500 (Wed, 03 Jan 2007)
New Revision: 70433

Modified:
   trunk/mcs/class/corlib/System.Runtime.InteropServices/ChangeLog
   trunk/mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs
Log:
2007-01-03  Miguel de Icaza  <[EMAIL PROTECTED]>

        * SafeHandle.cs: Do not use locks in SafeHandle to protect the
        access to the refcount, use Interlocked.CompareExchange.  The
        locks were the source of problems with domain finalization when
        this code was enabled.

        This should fix Matt Hargett's bug report with a full `make check'
        that reported variations of:

        ** (../../class/lib/net_2_0/nunit-console.exe:29644): WARNING **:
        Finalization of domain <domainname> timed out.

        Also, it is a lot lighther.



Modified: trunk/mcs/class/corlib/System.Runtime.InteropServices/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Runtime.InteropServices/ChangeLog     
2007-01-03 19:06:12 UTC (rev 70432)
+++ trunk/mcs/class/corlib/System.Runtime.InteropServices/ChangeLog     
2007-01-03 19:50:44 UTC (rev 70433)
@@ -1,3 +1,18 @@
+2007-01-03  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * SafeHandle.cs: Do not use locks in SafeHandle to protect the
+       access to the refcount, use Interlocked.CompareExchange.  The
+       locks were the source of problems with domain finalization when
+       this code was enabled.
+
+       This should fix Matt Hargett's bug report with a full `make check'
+       that reported variations of:
+
+       ** (../../class/lib/net_2_0/nunit-console.exe:29644): WARNING **:
+       Finalization of domain <domainname> timed out.
+
+       Also, it is a lot lighther.
+
 2006-12-21  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * CriticalHandle.cs: Add Dispose(bool disposing) method, and

Modified: trunk/mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs
===================================================================
--- trunk/mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs 
2007-01-03 19:06:12 UTC (rev 70432)
+++ trunk/mcs/class/corlib/System.Runtime.InteropServices/SafeHandle.cs 
2007-01-03 19:50:44 UTC (rev 70433)
@@ -41,11 +41,13 @@
 //     find out whether the runtime performs the P/Invoke if the
 //     handle has been disposed already.
 //
+//
 
 #if NET_2_0
 using System;
 using System.Runtime.InteropServices;
 using System.Runtime.ConstrainedExecution;
+using System.Threading;
 
 namespace System.Runtime.InteropServices
 {
@@ -56,7 +58,6 @@
                // MonoSafeHandle
                //
                protected IntPtr handle;
-               object handle_lock = new object ();
                IntPtr invalid_handle_value;
                int refcount = 0;
                bool owns_handle;
@@ -73,12 +74,15 @@
                        if (refcount == 0)
                                throw new ObjectDisposedException (GetType 
().FullName);
 
-                       lock (handle_lock){
-                               refcount--;
-                               if (refcount == 0 && owns_handle){
-                                       ReleaseHandle ();
-                                       handle = invalid_handle_value;
-                               }
+                       int newcount, current;
+                       do {
+                               current = refcount;
+                               newcount = current-1;
+                       } while (Interlocked.CompareExchange (ref refcount, 
newcount, current) != current);
+
+                       if (newcount == 0 && owns_handle){
+                               ReleaseHandle ();
+                               handle = invalid_handle_value;
                        }
                }
 
@@ -96,8 +100,12 @@
                        if (refcount == 0)
                                throw new ObjectDisposedException (GetType 
().FullName);
 
-                       lock (handle_lock){
-                               if (handle == invalid_handle_value || refcount 
== 0){
+                       int newcount, current;
+                       do {
+                               current = refcount;
+                               newcount = current + 1;
+                               
+                               if (handle == invalid_handle_value || current 
== 0){
                                        //
                                        // In MS, calling sf.Close () followed 
by a call
                                        // to P/Invoke with SafeHandles throws 
this, but
@@ -106,10 +114,8 @@
                                        //
                                        throw new ObjectDisposedException 
(GetType ().FullName);
                                }
-                               
-                               refcount++;
-                               success = true;
-                       }
+                       } while (Interlocked.CompareExchange (ref refcount, 
newcount, current) != current);
+                       success = true;
                }
 
                public IntPtr DangerousGetHandle ()
@@ -126,12 +132,15 @@
                        if (refcount == 0)
                                throw new ObjectDisposedException (GetType 
().FullName);
 
-                       lock (handle_lock){
-                               refcount--;
-                               if (refcount == 0 && owns_handle){
-                                       ReleaseHandle ();
-                                       handle = invalid_handle_value;
-                               }
+                       int newcount, current;
+                       do {
+                               current = refcount;
+                               newcount = current-1;
+                       } while (Interlocked.CompareExchange (ref refcount, 
newcount, current) != current);
+
+                       if (newcount == 0 && owns_handle){
+                               ReleaseHandle ();
+                               handle = invalid_handle_value;
                        }
                }
 

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

Reply via email to