Index: trunk/src/VBox/VMM/VMMAll/PDMAllCritSect.cpp
===================================================================
--- trunk/src/VBox/VMM/VMMAll/PDMAllCritSect.cpp	(revision 62144)
+++ trunk/src/VBox/VMM/VMMAll/PDMAllCritSect.cpp	(revision 62145)
@@ -53,4 +53,9 @@
 
 
+/** Skips some of the overly paranoid atomic updates.
+ * Makes some assumptions about cache coherence, though not brave enough not to
+ * always end with an atomic update. */
+//#define PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
+
 /* Undefine the automatic VBOX_STRICT API mappings. */
 #undef PDMCritSectEnter
@@ -94,5 +99,9 @@
     Assert(!(pCritSect->s.Core.fFlags & PDMCRITSECT_FLAGS_PENDING_UNLOCK));
 
+# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
+    pCritSect->s.Core.cNestings = 1;
+# else
     ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, 1);
+# endif
     Assert(pCritSect->s.Core.cNestings == 1);
     ASMAtomicWriteHandle(&pCritSect->s.Core.NativeThreadOwner, hNativeSelf);
@@ -242,7 +251,11 @@
     if (pCritSect->s.Core.NativeThreadOwner == hNativeSelf)
     {
+        Assert(pCritSect->s.Core.cNestings >= 1);
+# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
+        pCritSect->s.Core.cNestings += 1;
+# else
+        ASMAtomicIncS32(&pCritSect->s.Core.cNestings);
+# endif
         ASMAtomicIncS32(&pCritSect->s.Core.cLockers);
-        ASMAtomicIncS32(&pCritSect->s.Core.cNestings);
-        Assert(pCritSect->s.Core.cNestings > 1);
         return VINF_SUCCESS;
     }
@@ -432,7 +445,11 @@
     if (pCritSect->s.Core.NativeThreadOwner == hNativeSelf)
     {
+        Assert(pCritSect->s.Core.cNestings >= 1);
+# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
+        pCritSect->s.Core.cNestings += 1;
+# else
+        ASMAtomicIncS32(&pCritSect->s.Core.cNestings);
+# endif
         ASMAtomicIncS32(&pCritSect->s.Core.cLockers);
-        ASMAtomicIncS32(&pCritSect->s.Core.cNestings);
-        Assert(pCritSect->s.Core.cNestings > 1);
         return VINF_SUCCESS;
     }
@@ -555,13 +572,17 @@
                             pCritSect->s.Core.cLockers, pCritSect->s.Core.cNestings),
                            VERR_NOT_OWNER);
-    Assert(pCritSect->s.Core.cNestings >= 1);
 
     /*
      * Nested leave.
      */
-    if (pCritSect->s.Core.cNestings > 1)
-    {
-        ASMAtomicDecS32(&pCritSect->s.Core.cNestings);
-        Assert(pCritSect->s.Core.cNestings >= 1);
+    int32_t const cNestings = pCritSect->s.Core.cNestings;
+    Assert(cNestings >= 1);
+    if (cNestings > 1)
+    {
+# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
+        pCritSect->s.Core.cNestings = cNestings - 1;
+# else
+        ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, cNestings - 1);
+# endif
         ASMAtomicDecS32(&pCritSect->s.Core.cLockers);
         Assert(pCritSect->s.Core.cLockers >= 0);
@@ -591,13 +612,19 @@
         Assert(!pCritSect->s.Core.pValidatorRec || pCritSect->s.Core.pValidatorRec->hThread == NIL_RTTHREAD);
 # endif
+# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
+        //pCritSect->s.Core.cNestings = 0; /* not really needed */
+        pCritSect->s.Core.NativeThreadOwner = NIL_RTNATIVETHREAD;
+# else
+        ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, 0);
+        ASMAtomicWriteHandle(&pCritSect->s.Core.NativeThreadOwner, NIL_RTNATIVETHREAD);
+# endif
         ASMAtomicAndU32(&pCritSect->s.Core.fFlags, ~PDMCRITSECT_FLAGS_PENDING_UNLOCK);
-        ASMAtomicWriteHandle(&pCritSect->s.Core.NativeThreadOwner, NIL_RTNATIVETHREAD);
-        ASMAtomicDecS32(&pCritSect->s.Core.cNestings);
-        Assert(pCritSect->s.Core.cNestings == 0);
 
         /* stop and decrement lockers. */
         STAM_PROFILE_ADV_STOP(&pCritSect->s.StatLocked, l);
         ASMCompilerBarrier();
-        if (ASMAtomicDecS32(&pCritSect->s.Core.cLockers) >= 0)
+        if (ASMAtomicDecS32(&pCritSect->s.Core.cLockers) < 0)
+        { /* hopefully likely */ }
+        else
         {
             /* Someone is waiting, wake up one of them. */
@@ -609,5 +636,7 @@
 
         /* Signal exit event. */
-        if (hEventToSignal != NIL_SUPSEMEVENT)
+        if (RT_LIKELY(hEventToSignal == NIL_SUPSEMEVENT))
+        { /* likely */ }
+        else
         {
             Log8(("Signalling %#p\n", hEventToSignal));
@@ -631,5 +660,9 @@
         if (pCritSect->s.Core.cLockers == 0)
         {
+# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
+            //pCritSect->s.Core.cNestings = 0; /* not really needed */
+# else
             ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, 0);
+# endif
             RTNATIVETHREAD hNativeThread = pCritSect->s.Core.NativeThreadOwner;
             ASMAtomicAndU32(&pCritSect->s.Core.fFlags, ~PDMCRITSECT_FLAGS_PENDING_UNLOCK);
@@ -644,5 +677,9 @@
             STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l);
             Assert(pCritSect->s.Core.cNestings == 0);
+# ifdef PDMCRITSECT_WITH_LESS_ATOMIC_STUFF
+            //pCritSect->s.Core.cNestings = 1;
+# else
             ASMAtomicWriteS32(&pCritSect->s.Core.cNestings, 1);
+# endif
         }
         ASMAtomicOrU32(&pCritSect->s.Core.fFlags, PDMCRITSECT_FLAGS_PENDING_UNLOCK);
