Index: /trunk/include/iprt/lockvalidator.h
===================================================================
--- /trunk/include/iprt/lockvalidator.h	(revision 25621)
+++ /trunk/include/iprt/lockvalidator.h	(revision 25622)
@@ -654,4 +654,12 @@
 RTDECL(void *) RTLockValidatorQueryBlocking(RTTHREAD hThread);
 
+/**
+ * Checks if the thread is running in the lock validator after it has entered a
+ * block state.
+ *
+ * @returns true if it is, false if it isn't.
+ * @param   hThread     The thread in question.
+ */
+RTDECL(bool) RTLockValidatorIsBlockedThreadInValidator(RTTHREAD hThread);
 
 
Index: /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp
===================================================================
--- /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp	(revision 25621)
+++ /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp	(revision 25622)
@@ -412,4 +412,5 @@
     Assert(pPerThread->cWriteLocks == 0);
     Assert(pPerThread->cReadLocks == 0);
+    Assert(pPerThread->fInValidator == false);
 }
 
@@ -1166,4 +1167,5 @@
     rtLockValidatorWriteRecUnionPtr(&pThreadSelf->LockValidator.pRec, pRecU);
     rtLockValidatorCopySrcPos(&pThreadSelf->LockValidator.SrcPos, pSrcPos);
+    ASMAtomicWriteBool(&pThreadSelf->LockValidator.fInValidator, true);
     rtThreadSetState(pThreadSelf, enmSleepState);
 
@@ -1174,24 +1176,23 @@
      * isn't any other place to check for this.  semmutex-win.cpp for instance.
      */
+    int rc = VINF_SUCCESS;
     if (rtLockValidatorReadThreadHandle(&pRecU->Excl.hThread) == pThreadSelf)
     {
-        if (fRecursiveOk)
-            return VINF_SUCCESS;
-        rtLockValidatorComplainFirst("Recursion not allowed", pSrcPos, pThreadSelf, pRecU);
-        rtLockValidatorComplainPanic();
+        if (!fRecursiveOk)
+        {
+            rtLockValidatorComplainFirst("Recursion not allowed", pSrcPos, pThreadSelf, pRecU);
+            rtLockValidatorComplainPanic();
+            rc = VERR_SEM_LV_NESTED;
+        }
+    }
+    /*
+     * Perform deadlock detection.
+     */
+    else if (!rtLockValidatorIsSimpleNoDeadlockCase(pRecU))
+        rc = rtLockValidatorDeadlockDetection(pRecU, pThreadSelf, pSrcPos);
+
+    if (RT_FAILURE(rc))
         rtThreadSetState(pThreadSelf, enmThreadState);
-        return VERR_SEM_LV_NESTED;
-    }
-
-    /*
-     * Perform deadlock detection.
-     */
-    int rc = VINF_SUCCESS;
-    if (!rtLockValidatorIsSimpleNoDeadlockCase(pRecU))
-    {
-        rc = rtLockValidatorDeadlockDetection(pRecU, pThreadSelf, pSrcPos);
-        if (RT_FAILURE(rc))
-            rtThreadSetState(pThreadSelf, enmThreadState);
-    }
+    ASMAtomicWriteBool(&pThreadSelf->LockValidator.fInValidator, false);
     return rc;
 }
@@ -1365,4 +1366,5 @@
     rtLockValidatorWriteRecUnionPtr(&pThreadSelf->LockValidator.pRec, pRecU);
     rtLockValidatorCopySrcPos(&pThreadSelf->LockValidator.SrcPos, pSrcPos);
+    ASMAtomicWriteBool(&pThreadSelf->LockValidator.fInValidator, true);
     rtThreadSetState(pThreadSelf, enmSleepState);
 
@@ -1370,25 +1372,24 @@
      * Don't do deadlock detection if we're recursing.
      */
+    int rc = VINF_SUCCESS;
     PRTLOCKVALRECSHRDOWN pEntry = rtLockValidatorRecSharedFindOwner(&pRecU->Shared, pThreadSelf, NULL);
     if (pEntry)
     {
-        if (fRecursiveOk)
-            return VINF_SUCCESS;
-        rtLockValidatorComplainFirst("Recursion not allowed", pSrcPos, pThreadSelf, pRecU);
-        rtLockValidatorComplainPanic();
+        if (!fRecursiveOk)
+        {
+            rtLockValidatorComplainFirst("Recursion not allowed", pSrcPos, pThreadSelf, pRecU);
+            rtLockValidatorComplainPanic();
+            rc =  VERR_SEM_LV_NESTED;
+        }
+    }
+    /*
+     * Perform deadlock detection.
+     */
+    else if (!rtLockValidatorIsSimpleNoDeadlockCase(pRecU))
+        rc = rtLockValidatorDeadlockDetection(pRecU, pThreadSelf, pSrcPos);
+
+    if (RT_FAILURE(rc))
         rtThreadSetState(pThreadSelf, enmThreadState);
-        return VERR_SEM_LV_NESTED;
-    }
-
-    /*
-     * Perform deadlock detection.
-     */
-    int rc = VINF_SUCCESS;
-    if (!rtLockValidatorIsSimpleNoDeadlockCase(pRecU))
-    {
-        rc = rtLockValidatorDeadlockDetection(pRecU, pThreadSelf, pSrcPos);
-        if (RT_FAILURE(rc))
-            rtThreadSetState(pThreadSelf, enmThreadState);
-    }
+    ASMAtomicWriteBool(&pThreadSelf->LockValidator.fInValidator, false);
     return rc;
 }
@@ -1860,4 +1861,18 @@
 
 
+RTDECL(bool) RTLockValidatorIsBlockedThreadInValidator(RTTHREAD hThread)
+{
+    bool            fRet    = false;
+    PRTTHREADINT    pThread = rtThreadGet(hThread);
+    if (pThread)
+    {
+        fRet = ASMAtomicReadBool(&pThread->LockValidator.fInValidator);
+        rtThreadRelease(pThread);
+    }
+    return fRet;
+}
+RT_EXPORT_SYMBOL(RTLockValidatorIsBlockedThreadInValidator);
+
+
 RTDECL(bool) RTLockValidatorSetEnabled(bool fEnabled)
 {
Index: /trunk/src/VBox/Runtime/common/misc/thread.cpp
===================================================================
--- /trunk/src/VBox/Runtime/common/misc/thread.cpp	(revision 25621)
+++ /trunk/src/VBox/Runtime/common/misc/thread.cpp	(revision 25622)
@@ -1305,5 +1305,5 @@
     Assert(RTTHREAD_IS_SLEEPING(enmState));
     PRTTHREADINT pThread = hThread;
-    if (hThread && rtThreadGetState(pThread) == RTTHREADSTATE_RUNNING)
+    if (pThread && rtThreadGetState(pThread) == RTTHREADSTATE_RUNNING)
         rtThreadSetState(pThread, enmState);
 }
Index: /trunk/src/VBox/Runtime/include/internal/lockvalidator.h
===================================================================
--- /trunk/src/VBox/Runtime/include/internal/lockvalidator.h	(revision 25621)
+++ /trunk/src/VBox/Runtime/include/internal/lockvalidator.h	(revision 25622)
@@ -65,8 +65,10 @@
     /** Number of registered read locks that this thread owns, nesting included. */
     int32_t volatile                cReadLocks;
+    /** The thread is running inside the lock validator. */
+    bool volatile                   fInValidator;
+    /** Reserved for alignment purposes. */
+    bool                            afReserved[3];
     /** Bitmap indicating which entires are free (set) and allocated (clear). */
     uint32_t                        bmFreeShrdOwners;
-    /** Reserved for alignment purposes. */
-    uint32_t                        u32Reserved;
     /** Statically allocated shared owner records */
     RTLOCKVALRECSHRDOWN             aShrdOwners[32];
Index: /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp
===================================================================
--- /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp	(revision 25621)
+++ /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp	(revision 25622)
@@ -53,5 +53,5 @@
 
 static uint32_t             g_cThreads;
-static uint32_t volatile    g_iDeadlockThread;
+static uint32_t             g_iDeadlockThread;
 static RTTHREAD             g_ahThreads[32];
 static RTCRITSECT           g_aCritSects[32];
@@ -128,8 +128,12 @@
             if (   enmState == enmDesiredState
                 && (   !pvLock
-                    || pvLock == RTLockValidatorQueryBlocking(hThread)))
+                    || (   pvLock == RTLockValidatorQueryBlocking(hThread)
+                        && !RTLockValidatorIsBlockedThreadInValidator(hThread) )
+                   )
+               )
                 return true;
         }
-        else if (enmState != RTTHREADSTATE_RUNNING)
+        else if (   enmState != RTTHREADSTATE_RUNNING
+                 && enmState != RTTHREADSTATE_INITIALIZING)
             return false;
         RTThreadSleep(g_fDoNotSpin ? 3600*1000 : iLoop > 256 ? 1 : 0);
@@ -173,4 +177,5 @@
         }
     }
+    RTThreadSleep(4);                   /* fudge factor */
     return VINF_SUCCESS;
 }
@@ -416,7 +421,5 @@
     for (uint32_t iPass = 0; iPass < cPasses && RTTestErrorCount(g_hTest) == cErrors; iPass++)
     {
-#if 0 /** @todo figure why this ain't working for either of the two tests! */
         g_iDeadlockThread = (cThreads - 1 + iPass) % cThreads;
-#endif
         g_cLoops = 0;
         g_cDeadlocks = 0;
@@ -493,4 +496,5 @@
 }
 
+
 int main()
 {
@@ -516,5 +520,4 @@
      */
     test1(3, 1);
-
     test2(1, 1);
     test2(3, 1);
@@ -525,18 +528,18 @@
     RTLockValidatorSetQuiet(true);
 
-    test1( 2, 1024);
-    test1( 3, 1024);
-    test1( 7,  896);
-    test1(10,  768);
-    test1(15,  512);
-    test1(30,  384);
-
-    test2( 1,  100);
-    test2( 2, 1024);
-    test2( 3, 1024);
-    test2( 7,  896);
-    test2(10,  768);
-    test2(15,  512);
-    test2(30,  384);
+    test1( 2, 256);                     /* 256 * 4ms = 1s (approx); 4ms == fudge factor */
+    test1( 3, 256);
+    test1( 7, 256);
+    test1(10, 256);
+    test1(15, 256);
+    test1(30, 256);
+
+    test2( 1, 256);
+    test2( 2, 256);
+    test2( 3, 256);
+    test2( 7, 256);
+    test2(10, 256);
+    test2(15, 256);
+    test2(30, 256);
 
     test3( 2,  1,  2);
