Index: /trunk/include/iprt/lockvalidator.h
===================================================================
--- /trunk/include/iprt/lockvalidator.h	(revision 25617)
+++ /trunk/include/iprt/lockvalidator.h	(revision 25618)
@@ -434,7 +434,8 @@
 
 /**
- * Do deadlock detection before blocking on exclusive access to a lock.
- *
- * @retval  VINF_SUCCESS
+ * Do deadlock detection before blocking on exclusive access to a lock and
+ * change the thread state.
+ *
+ * @retval  VINF_SUCCESS - thread is in the specified sleep state.
  * @retval  VERR_SEM_LV_DEADLOCK if blocking would deadlock.  Gone thru the
  *          motions.
@@ -450,7 +451,9 @@
  * @param   pSrcPos             The source position of the lock operation.
  * @param   fRecursiveOk        Whether it's ok to recurse.
+ * @param   enmSleepState       The sleep state to enter on successful return.
  */
 RTDECL(int) RTLockValidatorRecExclCheckBlocking(PRTLOCKVALRECEXCL pRec, RTTHREAD hThreadSelf,
-                                                PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk);
+                                                PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk,
+                                                RTTHREADSTATE enmSleepState);
 
 /**
@@ -463,7 +466,9 @@
  * @param   pSrcPos             The source position of the lock operation.
  * @param   fRecursiveOk        Whether it's ok to recurse.
+ * @param   enmSleepState       The sleep state to enter on successful return.
  */
 RTDECL(int) RTLockValidatorRecExclCheckOrderAndBlocking(PRTLOCKVALRECEXCL pRec, RTTHREAD hThreadSelf,
-                                                        PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk);
+                                                        PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk,
+                                                        RTTHREADSTATE enmSleepState);
 
 /**
@@ -509,7 +514,8 @@
 
 /**
- * Do deadlock detection before blocking on shared access to a lock.
- *
- * @retval  VINF_SUCCESS
+ * Do deadlock detection before blocking on shared access to a lock and change
+ * the thread state.
+ *
+ * @retval  VINF_SUCCESS - thread is in the specified sleep state.
  * @retval  VERR_SEM_LV_DEADLOCK if blocking would deadlock.  Gone thru the
  *          motions.
@@ -525,7 +531,9 @@
  * @param   pSrcPos             The source position of the lock operation.
  * @param   fRecursiveOk        Whether it's ok to recurse.
+ * @param   enmSleepState       The sleep state to enter on successful return.
  */
 RTDECL(int) RTLockValidatorRecSharedCheckBlocking(PRTLOCKVALRECSHRD pRec, RTTHREAD hThreadSelf,
-                                                  PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk);
+                                                  PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk,
+                                                  RTTHREADSTATE enmSleepState);
 
 /**
@@ -540,5 +548,6 @@
  */
 RTDECL(int) RTLockValidatorRecSharedCheckOrderAndBlocking(PRTLOCKVALRECSHRD pRec, RTTHREAD hThreadSelf,
-                                                          PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk);
+                                                          PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk,
+                                                          RTTHREADSTATE enmSleepState);
 
 /**
Index: /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp
===================================================================
--- /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp	(revision 25618)
@@ -1133,5 +1133,6 @@
 
 RTDECL(int) RTLockValidatorRecExclCheckBlocking(PRTLOCKVALRECEXCL pRec, RTTHREAD hThreadSelf,
-                                                PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk)
+                                                PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk,
+                                                RTTHREADSTATE enmSleepState)
 {
     /*
@@ -1149,9 +1150,14 @@
     Assert(pThreadSelf == RTThreadSelf());
 
+    AssertReturn(RTTHREAD_IS_SLEEPING(enmSleepState), VERR_SEM_LV_INVALID_PARAMETER);
+
     RTTHREADSTATE enmThreadState = rtThreadGetState(pThreadSelf);
-    AssertReturn(   enmThreadState == RTTHREADSTATE_RUNNING
-                 || enmThreadState == RTTHREADSTATE_TERMINATED   /* rtThreadRemove uses locks too */
-                 || enmThreadState == RTTHREADSTATE_INITIALIZING /* rtThreadInsert uses locks too */
-                 , VERR_SEM_LV_INVALID_PARAMETER);
+    if (RT_UNLIKELY(enmThreadState != RTTHREADSTATE_RUNNING))
+    {
+        AssertReturn(   enmThreadState == RTTHREADSTATE_TERMINATED   /* rtThreadRemove uses locks too */
+                     || enmThreadState == RTTHREADSTATE_INITIALIZING /* rtThreadInsert uses locks too */
+                     , VERR_SEM_LV_INVALID_PARAMETER);
+        enmSleepState = enmThreadState;
+    }
 
     /*
@@ -1160,4 +1166,5 @@
     rtLockValidatorWriteRecUnionPtr(&pThreadSelf->LockValidator.pRec, pRecU);
     rtLockValidatorCopySrcPos(&pThreadSelf->LockValidator.SrcPos, pSrcPos);
+    rtThreadSetState(pThreadSelf, enmSleepState);
 
     /*
@@ -1173,4 +1180,5 @@
         rtLockValidatorComplainFirst("Recursion not allowed", pSrcPos, pThreadSelf, pRecU);
         rtLockValidatorComplainPanic();
+        rtThreadSetState(pThreadSelf, enmThreadState);
         return VERR_SEM_LV_NESTED;
     }
@@ -1179,7 +1187,12 @@
      * Perform deadlock detection.
      */
-    if (rtLockValidatorIsSimpleNoDeadlockCase(pRecU))
-        return VINF_SUCCESS;
-    return rtLockValidatorDeadlockDetection(pRecU, pThreadSelf, pSrcPos);
+    int rc = VINF_SUCCESS;
+    if (!rtLockValidatorIsSimpleNoDeadlockCase(pRecU))
+    {
+        rc = rtLockValidatorDeadlockDetection(pRecU, pThreadSelf, pSrcPos);
+        if (RT_FAILURE(rc))
+            rtThreadSetState(pThreadSelf, enmThreadState);
+    }
+    return rc;
 }
 RT_EXPORT_SYMBOL(RTLockValidatorRecExclCheckBlocking);
@@ -1187,9 +1200,10 @@
 
 RTDECL(int) RTLockValidatorRecExclCheckOrderAndBlocking(PRTLOCKVALRECEXCL pRec, RTTHREAD hThreadSelf,
-                                                        PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk)
+                                                        PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk,
+                                                        RTTHREADSTATE enmSleepState)
 {
     int rc = RTLockValidatorRecExclCheckOrder(pRec, hThreadSelf, pSrcPos);
     if (RT_SUCCESS(rc))
-        rc = RTLockValidatorRecExclCheckBlocking(pRec, hThreadSelf, pSrcPos, fRecursiveOk);
+        rc = RTLockValidatorRecExclCheckBlocking(pRec, hThreadSelf, pSrcPos, fRecursiveOk, enmSleepState);
     return rc;
 }
@@ -1318,5 +1332,6 @@
 
 RTDECL(int) RTLockValidatorRecSharedCheckBlocking(PRTLOCKVALRECSHRD pRec, RTTHREAD hThreadSelf,
-                                                  PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk)
+                                                  PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk,
+                                                  RTTHREADSTATE enmSleepState)
 {
     /*
@@ -1334,9 +1349,14 @@
     Assert(pThreadSelf == RTThreadSelf());
 
+    AssertReturn(RTTHREAD_IS_SLEEPING(enmSleepState), VERR_SEM_LV_INVALID_PARAMETER);
+
     RTTHREADSTATE enmThreadState = rtThreadGetState(pThreadSelf);
-    AssertReturn(   enmThreadState == RTTHREADSTATE_RUNNING
-                 || enmThreadState == RTTHREADSTATE_TERMINATED   /* rtThreadRemove uses locks too */
-                 || enmThreadState == RTTHREADSTATE_INITIALIZING /* rtThreadInsert uses locks too */
-                 , VERR_SEM_LV_INVALID_PARAMETER);
+    if (RT_UNLIKELY(enmThreadState != RTTHREADSTATE_RUNNING))
+    {
+        AssertReturn(   enmThreadState == RTTHREADSTATE_TERMINATED   /* rtThreadRemove uses locks too */
+                     || enmThreadState == RTTHREADSTATE_INITIALIZING /* rtThreadInsert uses locks too */
+                     , VERR_SEM_LV_INVALID_PARAMETER);
+        enmSleepState = enmThreadState;
+    }
 
     /*
@@ -1345,4 +1365,5 @@
     rtLockValidatorWriteRecUnionPtr(&pThreadSelf->LockValidator.pRec, pRecU);
     rtLockValidatorCopySrcPos(&pThreadSelf->LockValidator.SrcPos, pSrcPos);
+    rtThreadSetState(pThreadSelf, enmSleepState);
 
     /*
@@ -1356,4 +1377,5 @@
         rtLockValidatorComplainFirst("Recursion not allowed", pSrcPos, pThreadSelf, pRecU);
         rtLockValidatorComplainPanic();
+        rtThreadSetState(pThreadSelf, enmThreadState);
         return VERR_SEM_LV_NESTED;
     }
@@ -1362,16 +1384,23 @@
      * Perform deadlock detection.
      */
-    if (rtLockValidatorIsSimpleNoDeadlockCase(pRecU))
-        return VINF_SUCCESS;
-    return rtLockValidatorDeadlockDetection(pRecU, pThreadSelf, pSrcPos);
+    int rc = VINF_SUCCESS;
+    if (!rtLockValidatorIsSimpleNoDeadlockCase(pRecU))
+    {
+        rc = rtLockValidatorDeadlockDetection(pRecU, pThreadSelf, pSrcPos);
+        if (RT_FAILURE(rc))
+            rtThreadSetState(pThreadSelf, enmThreadState);
+    }
+    return rc;
 }
 RT_EXPORT_SYMBOL(RTLockValidatorRecSharedCheckBlocking);
 
 
-RTDECL(int) RTLockValidatorRecSharedCheckOrderAndBlocking(PRTLOCKVALRECSHRD pRec, RTTHREAD hThreadSelf, PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk)
+RTDECL(int) RTLockValidatorRecSharedCheckOrderAndBlocking(PRTLOCKVALRECSHRD pRec, RTTHREAD hThreadSelf,
+                                                          PCRTLOCKVALSRCPOS pSrcPos, bool fRecursiveOk,
+                                                          RTTHREADSTATE enmSleepState)
 {
     int rc = RTLockValidatorRecSharedCheckOrder(pRec, hThreadSelf, pSrcPos);
     if (RT_SUCCESS(rc))
-        rc = RTLockValidatorRecSharedCheckBlocking(pRec, hThreadSelf, pSrcPos, fRecursiveOk);
+        rc = RTLockValidatorRecSharedCheckBlocking(pRec, hThreadSelf, pSrcPos, fRecursiveOk, enmSleepState);
     return rc;
 }
Index: /trunk/src/VBox/Runtime/common/misc/thread.cpp
===================================================================
--- /trunk/src/VBox/Runtime/common/misc/thread.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/common/misc/thread.cpp	(revision 25618)
@@ -196,16 +196,4 @@
 }
 
-
-/**
- * Sets the thread state.
- *
- * @param   pThread             The thread.
- * @param   enmNewState         The new thread state.
- */
-DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread, RTTHREADSTATE enmNewState)
-{
-    AssertCompile(sizeof(pThread->enmState) == sizeof(uint32_t));
-    ASMAtomicWriteU32((uint32_t volatile *)&pThread->enmState, enmNewState);
-}
 
 #ifdef IN_RING3
Index: /trunk/src/VBox/Runtime/generic/critsect-generic.cpp
===================================================================
--- /trunk/src/VBox/Runtime/generic/critsect-generic.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/generic/critsect-generic.cpp	(revision 25618)
@@ -209,5 +209,6 @@
 #ifdef RTCRITSECT_STRICT
             int rc9 = RTLockValidatorRecExclCheckBlocking(pCritSect->pValidatorRec, hThreadSelf, pSrcPos,
-                                                          !(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING));
+                                                          !(pCritSect->fFlags & RTCRITSECT_FLAGS_NO_NESTING),
+                                                          RTTHREADSTATE_CRITSECT);
             if (RT_FAILURE(rc9))
             {
@@ -215,7 +216,7 @@
                 return rc9;
             }
-#endif
-
+#else
             RTThreadBlocking(hThreadSelf, RTTHREADSTATE_CRITSECT);
+#endif
             int rc = RTSemEventWait(pCritSect->EventSem, RT_INDEFINITE_WAIT);
             RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_CRITSECT);
Index: /trunk/src/VBox/Runtime/generic/semrw-generic.cpp
===================================================================
--- /trunk/src/VBox/Runtime/generic/semrw-generic.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/generic/semrw-generic.cpp	(revision 25618)
@@ -320,10 +320,10 @@
         }
 #ifdef RTSEMRW_STRICT
-        rc = RTLockValidatorRecSharedCheckBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true);
+        rc = RTLockValidatorRecSharedCheckBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true, RTTHREADSTATE_RW_READ);
         if (RT_FAILURE(rc))
             break;
-#endif
+#else
         RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_READ);
-
+#endif
         int rcWait = rc = RTSemEventMultiWait(pThis->ReadEvent, cMillies);
         RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_READ);
@@ -572,10 +572,12 @@
             }
         }
-#ifdef RTSEMRW_STRICT
-        rc = RTLockValidatorRecExclCheckBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true);
+
+#ifdef RTSEMRW_STRICT
+        rc = RTLockValidatorRecExclCheckBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true, RTTHREADSTATE_RW_WRITE);
         if (RT_FAILURE(rc))
             break;
-#endif
+#else
         RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_WRITE);
+#endif
         int rcWait = rc = RTSemEventWait(pThis->WriteEvent, cMillies);
         RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_RW_WRITE);
Index: /trunk/src/VBox/Runtime/include/internal/thread.h
===================================================================
--- /trunk/src/VBox/Runtime/include/internal/thread.h	(revision 25617)
+++ /trunk/src/VBox/Runtime/include/internal/thread.h	(revision 25618)
@@ -196,4 +196,6 @@
 #endif
 
+#ifdef ___iprt_asm_h
+
 /**
  * Gets the thread state.
@@ -207,4 +209,18 @@
 }
 
+/**
+ * Sets the thread state.
+ *
+ * @param   pThread             The thread.
+ * @param   enmNewState         The new thread state.
+ */
+DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread, RTTHREADSTATE enmNewState)
+{
+    AssertCompile(sizeof(pThread->enmState) == sizeof(uint32_t));
+    ASMAtomicWriteU32((uint32_t volatile *)&pThread->enmState, enmNewState);
+}
+
+#endif
+
 RT_C_DECLS_END
 
Index: /trunk/src/VBox/Runtime/r3/linux/semmutex-linux.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/linux/semmutex-linux.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/r3/linux/semmutex-linux.cpp	(revision 25618)
@@ -240,9 +240,10 @@
             {
 #ifdef RTSEMMUTEX_STRICT
-                int rc9 = RTLockValidatorRecExclCheckBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
+                int rc9 = RTLockValidatorRecExclCheckBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true, RTTHREADSTATE_MUTEX);
                 if (RT_FAILURE(rc9))
                     return rc9;
-#endif
+#else
                 RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX);
+#endif
             }
 
Index: /trunk/src/VBox/Runtime/r3/posix/semmutex-posix.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/posix/semmutex-posix.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/r3/posix/semmutex-posix.cpp	(revision 25618)
@@ -188,11 +188,11 @@
 #ifdef RTSEMMUTEX_STRICT
         hThreadSelf = RTThreadSelfAutoAdopt();
-        int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
+        int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true, RTTHREADSTATE_MUTEX);
         if (RT_FAILURE(rc9))
             return rc9;
 #else
         hThreadSelf = RTThreadSelf();
-#endif
         RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX);
+#endif
     }
 
Index: /trunk/src/VBox/Runtime/r3/posix/semrw-posix.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/posix/semrw-posix.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/r3/posix/semrw-posix.cpp	(revision 25618)
@@ -219,11 +219,11 @@
 #ifdef RTSEMRW_STRICT
         hThreadSelf = RTThreadSelfAutoAdopt();
-        int rc9 = RTLockValidatorRecSharedCheckOrderAndBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true);
+        int rc9 = RTLockValidatorRecSharedCheckOrderAndBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true, RTTHREADSTATE_RW_READ);
         if (RT_FAILURE(rc9))
             return rc9;
 #else
         hThreadSelf = RTThreadSelf();
-#endif
         RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_READ);
+#endif
     }
 
@@ -381,11 +381,11 @@
 #ifdef RTSEMRW_STRICT
         hThreadSelf = RTThreadSelfAutoAdopt();
-        int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true);
+        int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true, RTTHREADSTATE_RW_WRITE);
         if (RT_FAILURE(rc9))
             return rc9;
 #else
         hThreadSelf = RTThreadSelf();
-#endif
         RTThreadBlocking(hThreadSelf, RTTHREADSTATE_RW_WRITE);
+#endif
     }
 
Index: /trunk/src/VBox/Runtime/r3/win/semmutex-win.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/win/semmutex-win.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/r3/win/semmutex-win.cpp	(revision 25618)
@@ -159,11 +159,11 @@
 #ifdef RTSEMMUTEX_STRICT
         hThreadSelf = RTThreadSelfAutoAdopt();
-        int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
+        int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true, RTTHREADSTATE_MUTEX);
         if (RT_FAILURE(rc9))
             return rc9;
 #else
         hThreadSelf = RTThreadSelf();
-#endif
         RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX);
+#endif
     }
     int rc = WaitForSingleObjectEx(pThis->hMtx,
Index: /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp
===================================================================
--- /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp	(revision 25617)
+++ /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp	(revision 25618)
@@ -41,4 +41,5 @@
 #include <iprt/test.h>
 #include <iprt/thread.h>
+#include <iprt/time.h>
 
 
@@ -47,7 +48,7 @@
 *******************************************************************************/
 /** The testcase handle. */
-static RTTEST       g_hTest;
+static RTTEST               g_hTest;
 /** Flip this in the debugger to get some peace to single step wild code. */
-bool volatile       g_fDoNotSpin = false;
+bool volatile               g_fDoNotSpin = false;
 
 static uint32_t             g_cThreads;
@@ -56,4 +57,11 @@
 static RTCRITSECT           g_aCritSects[32];
 static RTSEMRW              g_ahSemRWs[32];
+
+/** When to stop testing. */
+static uint64_t             g_NanoTSStop;
+/** The number of deadlocks. */
+static uint32_t volatile    g_cDeadlocks;
+/** The number of loops. */
+static uint32_t volatile    g_cLoops;
 
 
@@ -281,5 +289,46 @@
 
 
-static void testIt(uint32_t cThreads, uint32_t cPasses, PFNRTTHREAD pfnThread, const char *pszName)
+static DECLCALLBACK(int) test3Thread(RTTHREAD ThreadSelf, void *pvUser)
+{
+    uintptr_t       i     = (uintptr_t)pvUser;
+    RTSEMRW         hMine = g_ahSemRWs[i];
+    RTSEMRW         hNext = g_ahSemRWs[(i + 1) % g_cThreads];
+    int             rc;
+
+    if (i & 1)
+        RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestWrite(hMine, RT_INDEFINITE_WAIT), VINF_SUCCESS, rcCheck);
+    else
+        RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestRead(hMine, RT_INDEFINITE_WAIT), VINF_SUCCESS, rcCheck);
+    if (testWaitForSemRWToBeOwned(hNext))
+    {
+        do
+        {
+            rc = RTSemRWRequestWrite(hNext, 60*1000);
+            if (rc != VINF_SUCCESS && rc != VERR_SEM_LV_DEADLOCK && rc != VERR_SEM_LV_ILLEGAL_UPGRADE)
+            {
+                RTTestFailed(g_hTest, "#%u: RTSemRWRequestWrite -> %Rrc\n", i, rc);
+                break;
+            }
+            if (RT_SUCCESS(rc))
+            {
+                RTTEST_CHECK_RC(g_hTest, rc = RTSemRWReleaseWrite(hNext), VINF_SUCCESS);
+                if (RT_FAILURE(rc))
+                    break;
+            }
+            else
+                ASMAtomicIncU32(&g_cDeadlocks);
+            ASMAtomicIncU32(&g_cLoops);
+        } while (RTTimeNanoTS() < g_NanoTSStop);
+    }
+    if (i & 1)
+        RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(hMine), VINF_SUCCESS);
+    else
+        RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead(hMine), VINF_SUCCESS);
+    RTTEST_CHECK(g_hTest, RTThreadGetState(RTThreadSelf()) == RTTHREADSTATE_RUNNING);
+    return VINF_SUCCESS;
+}
+
+
+static void testIt(uint32_t cThreads, uint32_t cPasses, uint64_t cNanoSecs, PFNRTTHREAD pfnThread, const char *pszName)
 {
     RTTestSubF(g_hTest, "%s, %u threads, %u passes", pszName, cThreads, cPasses);
@@ -297,5 +346,7 @@
     }
 
-    uint32_t cErrors = RTTestErrorCount(g_hTest);
+    uint32_t cLoops     = 0;
+    uint32_t cDeadlocks = 0;
+    uint32_t cErrors    = RTTestErrorCount(g_hTest);
     for (uint32_t iPass = 0; iPass < cPasses && RTTestErrorCount(g_hTest) == cErrors; iPass++)
     {
@@ -303,7 +354,16 @@
         g_iDeadlockThread = (cThreads - 1 + iPass) % cThreads;
 #endif
+        g_cLoops = 0;
+        g_cDeadlocks = 0;
+        g_NanoTSStop = cNanoSecs ? RTTimeNanoTS() + cNanoSecs : 0;
+
         int rc = testStartThreads(cThreads, pfnThread);
         if (RT_SUCCESS(rc))
-            testWaitForThreads(30*1000, true);
+            testWaitForThreads(30*1000 + cNanoSecs / 1000000, true);
+
+        RTTEST_CHECK(g_hTest, !cNanoSecs || g_cLoops > 0);
+        cLoops += g_cLoops;
+        RTTEST_CHECK(g_hTest, !cNanoSecs || g_cDeadlocks > 0);
+        cDeadlocks += g_cDeadlocks;
     }
 
@@ -314,4 +374,8 @@
     }
     testWaitForThreads(10*1000, false);
+
+    if (cNanoSecs)
+        RTTestPrintf(g_hTest,  RTTESTLVL_ALWAYS, "cLoops=%u cDeadlocks=%u (%u%%)\n",
+                     cLoops, cDeadlocks, cLoops ? cDeadlocks * 100 / cLoops : 0);
 }
 
@@ -319,5 +383,5 @@
 static void test1(uint32_t cThreads, uint32_t cPasses)
 {
-    testIt(cThreads, cPasses, test1Thread, "critsect");
+    testIt(cThreads, cPasses, 0, test1Thread, "critsect");
 }
 
@@ -325,5 +389,11 @@
 static void test2(uint32_t cThreads, uint32_t cPasses)
 {
-    testIt(cThreads, cPasses, test2Thread, "read-write");
+    testIt(cThreads, cPasses, 0, test2Thread, "read-write");
+}
+
+
+static void test3(uint32_t cThreads, uint32_t cPasses, uint64_t cNanoSecs)
+{
+    testIt(cThreads, cPasses, cNanoSecs, test3Thread, "read-write race");
 }
 
@@ -383,5 +453,5 @@
      */
     RTLockValidatorSetQuiet(true);
-
+#if 0
     test1( 2, 1024);
     test1( 3, 1024);
@@ -398,5 +468,8 @@
     test2(15,  512);
     test2(30,  384);
-
+#endif
+
+    test3( 2,  2,  5*UINT64_C(1000000000));
+    test3(10,  1,  5*UINT64_C(1000000000));
 
     return RTTestSummaryAndDestroy(g_hTest);
Index: /trunk/src/VBox/VMM/VMMAll/PDMAllCritSect.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMAll/PDMAllCritSect.cpp	(revision 25617)
+++ /trunk/src/VBox/VMM/VMMAll/PDMAllCritSect.cpp	(revision 25618)
@@ -139,10 +139,11 @@
 # ifdef PDMCRITSECT_STRICT
         int rc9 = RTLockValidatorRecExclCheckBlocking(pCritSect->s.Core.pValidatorRec, hThreadSelf, pSrcPos,
-                                                      !(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NO_NESTING));
+                                                      !(pCritSect->s.Core.fFlags & RTCRITSECT_FLAGS_NO_NESTING),
+                                                      RTTHREADSTATE_CRITSECT);
         if (RT_FAILURE(rc9))
             return rc9;
-# endif
-
+# else
         RTThreadBlocking(hThreadSelf, RTTHREADSTATE_CRITSECT);
+# endif
         int rc = SUPSemEventWaitNoResume(pSession, hEvent, RT_INDEFINITE_WAIT);
         RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_CRITSECT);
