Index: /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp
===================================================================
--- /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp	(revision 25709)
+++ /trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp	(revision 25710)
@@ -3046,6 +3046,23 @@
         return VINF_SUCCESS;
     AssertReturn(pRecU->Excl.hThread != NIL_RTTHREAD, VERR_SEM_LV_INVALID_PARAMETER);
+    Assert(pRecU->Excl.hThread == RTThreadSelf());
     AssertReturn(pRecU->Excl.cRecursion > 1, VERR_SEM_LV_INVALID_PARAMETER);
 
+    /*
+     * Check the release order.
+     */
+    if (   pRecU->Excl.hClass != NIL_RTLOCKVALCLASS
+        && pRecU->Excl.hClass->fStrictReleaseOrder
+        && pRecU->Excl.hClass->cMsMinOrder != RT_INDEFINITE_WAIT
+       )
+    {
+        int rc = rtLockValidatorStackCheckReleaseOrder(pRecU->Excl.hThread, pRecU);
+        if (RT_FAILURE(rc))
+            return rc;
+    }
+
+    /*
+     * Perform the unwind.
+     */
     pRecU->Excl.cRecursion--;
     rtLockValidatorStackPopRecursion(pRecU->Excl.hThread, pRecU);
@@ -3064,4 +3081,5 @@
     if (!pRecU->Excl.fEnabled)
         return VINF_SUCCESS;
+    Assert(pRecU->Excl.hThread == RTThreadSelf());
     AssertReturn(pRecU->Excl.hThread != NIL_RTTHREAD, VERR_SEM_LV_INVALID_PARAMETER);
     AssertReturn(pRecU->Excl.cRecursion > 0, VERR_SEM_LV_INVALID_PARAMETER);
@@ -3094,7 +3112,24 @@
     if (!pRecU->Excl.fEnabled)
         return VINF_SUCCESS;
+    Assert(pRecU->Excl.hThread == RTThreadSelf());
     AssertReturn(pRecU->Excl.hThread != NIL_RTTHREAD, VERR_SEM_LV_INVALID_PARAMETER);
     AssertReturn(pRecU->Excl.cRecursion > 1, VERR_SEM_LV_INVALID_PARAMETER);
 
+    /*
+     * Check the release order.
+     */
+    if (   pRecU->Excl.hClass != NIL_RTLOCKVALCLASS
+        && pRecU->Excl.hClass->fStrictReleaseOrder
+        && pRecU->Excl.hClass->cMsMinOrder != RT_INDEFINITE_WAIT
+       )
+    {
+        int rc = rtLockValidatorStackCheckReleaseOrder(pRecU->Excl.hThread, pRecU);
+        if (RT_FAILURE(rc))
+            return rc;
+    }
+
+    /*
+     * Perform the unwind.
+     */
     pRecU->Excl.cRecursion--;
     rtLockValidatorStackPopRecursion(pRecU->Excl.hThread, pRecU);
Index: /trunk/src/VBox/Runtime/generic/semrw-generic.cpp
===================================================================
--- /trunk/src/VBox/Runtime/generic/semrw-generic.cpp	(revision 25709)
+++ /trunk/src/VBox/Runtime/generic/semrw-generic.cpp	(revision 25710)
@@ -121,5 +121,5 @@
             if (RT_SUCCESS(rc))
             {
-                rc = RTCritSectInit(&pThis->CritSect);
+                rc = RTCritSectInitEx(&pThis->CritSect, RTCRITSECT_FLAGS_NO_LOCK_VAL, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
                 if (RT_SUCCESS(rc))
                 {
@@ -142,9 +142,9 @@
                         va_list va;
                         va_start(va, pszNameFmt);
-                        RTLockValidatorRecExclInit(&pThis->ValidatorWrite, hClass, uSubClass, pThis, fLVEnabled, pszNameFmt);
+                        RTLockValidatorRecExclInitV(&pThis->ValidatorWrite, hClass, uSubClass, pThis, fLVEnabled, pszNameFmt, va);
                         va_end(va);
                         va_start(va, pszNameFmt);
-                        RTLockValidatorRecSharedInit(&pThis->ValidatorRead, hClass, uSubClass, pThis, false /*fSignaller*/,
-                                                     fLVEnabled, pszNameFmt);
+                        RTLockValidatorRecSharedInitV(&pThis->ValidatorRead, hClass, uSubClass, pThis, false /*fSignaller*/,
+                                                      fLVEnabled, pszNameFmt, va);
                         va_end(va);
                         RTLockValidatorRecMakeSiblings(&pThis->ValidatorWrite.Core, &pThis->ValidatorRead.Core);
@@ -271,5 +271,9 @@
     if (cMillies > 0)
     {
-        int rc9 = RTLockValidatorRecSharedCheckOrder(&pThis->ValidatorRead, hThreadSelf, pSrcPos, cMillies);
+        int rc9;
+        if (pThis->hWriter != NIL_RTTHREAD && pThis->hWriter == RTThreadNativeSelf())
+            rc9 = RTLockValidatorRecExclCheckOrder(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, cMillies);
+        else
+            rc9 = RTLockValidatorRecSharedCheckOrder(&pThis->ValidatorRead, hThreadSelf, pSrcPos, cMillies);
         if (RT_FAILURE(rc9))
             return rc9;
Index: /trunk/src/VBox/Runtime/generic/semrw-lockless-generic.cpp
===================================================================
--- /trunk/src/VBox/Runtime/generic/semrw-lockless-generic.cpp	(revision 25709)
+++ /trunk/src/VBox/Runtime/generic/semrw-lockless-generic.cpp	(revision 25710)
@@ -147,9 +147,9 @@
             va_list va;
             va_start(va, pszNameFmt);
-            RTLockValidatorRecExclInit(&pThis->ValidatorWrite, hClass, uSubClass, pThis, fLVEnabled, pszNameFmt);
+            RTLockValidatorRecExclInitV(&pThis->ValidatorWrite, hClass, uSubClass, pThis, fLVEnabled, pszNameFmt, va);
             va_end(va);
             va_start(va, pszNameFmt);
-            RTLockValidatorRecSharedInit(&pThis->ValidatorRead, hClass, uSubClass, pThis, false /*fSignaller*/,
-                                         fLVEnabled, pszNameFmt);
+            RTLockValidatorRecSharedInitV(&pThis->ValidatorRead, hClass, uSubClass, pThis, false /*fSignaller*/,
+                                          fLVEnabled, pszNameFmt, va);
             va_end(va);
             RTLockValidatorRecMakeSiblings(&pThis->ValidatorWrite.Core, &pThis->ValidatorRead.Core);
@@ -237,5 +237,11 @@
     if (cMillies > 0)
     {
-        int rc9 = RTLockValidatorRecSharedCheckOrder(&pThis->ValidatorRead, hThreadSelf, pSrcPos, cMillies);
+        int            rc9;
+        RTNATIVETHREAD hNativeWriter;
+        ASMAtomicUoReadHandle(&pThis->hNativeWriter, &hNativeWriter);
+        if (hNativeWriter != NIL_RTTHREAD && hNativeWriter == RTThreadNativeSelf())
+            rc9 = RTLockValidatorRecExclCheckOrder(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, cMillies);
+        else
+            rc9 = RTLockValidatorRecSharedCheckOrder(&pThis->ValidatorRead, hThreadSelf, pSrcPos, cMillies);
         if (RT_FAILURE(rc9))
             return rc9;
Index: /trunk/src/VBox/Runtime/r3/posix/semrw-posix.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/posix/semrw-posix.cpp	(revision 25709)
+++ /trunk/src/VBox/Runtime/r3/posix/semrw-posix.cpp	(revision 25710)
@@ -132,9 +132,9 @@
                 va_list va;
                 va_start(va, pszNameFmt);
-                RTLockValidatorRecExclInit(&pThis->ValidatorWrite, hClass, uSubClass, pThis, fLVEnabled, pszNameFmt);
+                RTLockValidatorRecExclInitV(&pThis->ValidatorWrite, hClass, uSubClass, pThis, fLVEnabled, pszNameFmt, va);
                 va_end(va);
                 va_start(va, pszNameFmt);
-                RTLockValidatorRecSharedInit(&pThis->ValidatorRead, hClass, uSubClass, pThis, false /*fSignaller*/,
-                                             fLVEnabled, pszNameFmt);
+                RTLockValidatorRecSharedInitV(&pThis->ValidatorRead, hClass, uSubClass, pThis, false /*fSignaller*/,
+                                              fLVEnabled, pszNameFmt, va);
                 va_end(va);
                 RTLockValidatorRecMakeSiblings(&pThis->ValidatorWrite.Core, &pThis->ValidatorRead.Core);
@@ -581,5 +581,4 @@
         return VINF_SUCCESS;
     }
-    pThis->cWrites--;
 
     /*
@@ -592,4 +591,5 @@
 #endif
 
+    pThis->cWrites--;
     ATOMIC_SET_PTHREAD_T(&pThis->Writer, (pthread_t)-1);
     int rc = pthread_rwlock_unlock(&pThis->RWLock);
Index: /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp
===================================================================
--- /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp	(revision 25709)
+++ /trunk/src/VBox/Runtime/testcase/tstRTLockValidator.cpp	(revision 25710)
@@ -1045,5 +1045,5 @@
        this as a valid lock order.  */
     RTTEST_CHECK_RC(g_hTest, RTSemRWRequestWrite(g_ahSemRWs[0], RT_INDEFINITE_WAIT), VINF_SUCCESS);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWRequestWrite(g_ahSemRWs[1], RT_INDEFINITE_WAIT), VINF_SUCCESS);
+    RTTEST_CHECK_RC(g_hTest, RTSemRWRequestRead( g_ahSemRWs[1], RT_INDEFINITE_WAIT), VINF_SUCCESS);
     RTTEST_CHECK_RC(g_hTest, RTSemRWRequestRead( g_ahSemRWs[2], RT_INDEFINITE_WAIT), VINF_SUCCESS);
     RTTEST_CHECK_RC(g_hTest, RTSemRWRequestWrite(g_ahSemRWs[3], RT_INDEFINITE_WAIT), VINF_SUCCESS);
@@ -1055,12 +1055,23 @@
     int rc;
     RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[0]), VINF_SUCCESS);
-    RTTEST_CHECK_RC(g_hTest, rc = RTSemRWReleaseWrite(g_ahSemRWs[0]), VERR_SEM_LV_WRONG_ORDER);
+    RTTEST_CHECK_RC(g_hTest, rc = RTSemRWRequestWrite(g_ahSemRWs[0], RT_INDEFINITE_WAIT), VERR_SEM_LV_WRONG_ORDER);
     if (RT_SUCCESS(rc))
         RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[0]), VINF_SUCCESS);
 
+    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead(g_ahSemRWs[1]), VINF_SUCCESS);
+    RTTEST_CHECK_RC(g_hTest, rc = RTSemRWRequestRead(g_ahSemRWs[1], RT_INDEFINITE_WAIT), VERR_SEM_LV_WRONG_ORDER);
+    if (RT_SUCCESS(rc))
+        RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead(g_ahSemRWs[1]), VINF_SUCCESS);
+
     /* Check that recursion isn't subject to order checks. */
-    RTTEST_CHECK_RC(g_hTest, rc = RTSemRWReleaseWrite(g_ahSemRWs[1]), VINF_SUCCESS);
-    if (RT_SUCCESS(rc))
-        RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[1]), VINF_SUCCESS);
+    RTTEST_CHECK_RC(g_hTest, rc = RTSemRWRequestRead(g_ahSemRWs[2], RT_INDEFINITE_WAIT), VINF_SUCCESS);
+    if (RT_SUCCESS(rc))
+        RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead(g_ahSemRWs[2]), VINF_SUCCESS);
+    RTTEST_CHECK(g_hTest, RTSemRWGetReadCount(g_ahSemRWs[2]) == 1);
+
+    RTTEST_CHECK_RC(g_hTest, rc = RTSemRWRequestWrite(g_ahSemRWs[3], RT_INDEFINITE_WAIT), VINF_SUCCESS);
+    if (RT_SUCCESS(rc))
+        RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[3]), VINF_SUCCESS);
+    RTTEST_CHECK(g_hTest, RTSemRWGetWriteRecursion(g_ahSemRWs[3]) == 1);
 
     /* Enable strict release order for class 2 and 3, then check that violations
@@ -1068,18 +1079,28 @@
     RTTEST_CHECK_RC(g_hTest, RTLockValidatorClassEnforceStrictReleaseOrder(g_ahClasses[2], true), VINF_SUCCESS);
     RTTEST_CHECK_RC(g_hTest, RTLockValidatorClassEnforceStrictReleaseOrder(g_ahClasses[3], true), VINF_SUCCESS);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead(g_ahSemRWs[2]), VINF_SUCCESS);                      /* start recursion */
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[3]), VINF_SUCCESS);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[4]), VINF_SUCCESS);
+
+    RTTEST_CHECK_RC(g_hTest, RTSemRWRequestRead( g_ahSemRWs[2], RT_INDEFINITE_WAIT), VINF_SUCCESS);  /* start recursion */
+    RTTEST_CHECK(   g_hTest, RTSemRWGetReadCount(g_ahSemRWs[2]) == 2);
+    RTTEST_CHECK_RC(g_hTest, RTSemRWRequestWrite(g_ahSemRWs[3], RT_INDEFINITE_WAIT), VINF_SUCCESS);
+    RTTEST_CHECK(   g_hTest, RTSemRWGetWriteRecursion(g_ahSemRWs[3]) == 2);
+    RTTEST_CHECK_RC(g_hTest, RTSemRWRequestRead( g_ahSemRWs[4], RT_INDEFINITE_WAIT), VINF_SUCCESS);  /* (mixed) */
+
     RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[2]), VERR_SEM_LV_WRONG_RELEASE_ORDER);
     RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[3]), VERR_SEM_LV_WRONG_RELEASE_ORDER);
+    RTTEST_CHECK(   g_hTest, RTSemRWGetWriteRecursion(g_ahSemRWs[3]) == 2);
+    RTTEST_CHECK(   g_hTest, RTSemRWGetReadCount(g_ahSemRWs[2]) == 2);
+    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[4]), VINF_SUCCESS);
+    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[3]), VINF_SUCCESS);
+    RTTEST_CHECK(   g_hTest, RTSemRWGetWriteRecursion(g_ahSemRWs[3]) == 1);
+    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[2]), VINF_SUCCESS);                      /* end recursion */
+    RTTEST_CHECK(   g_hTest, RTSemRWGetReadCount(g_ahSemRWs[2]) == 1);
+
+    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[2]), VERR_SEM_LV_WRONG_RELEASE_ORDER);
+    RTTEST_CHECK(g_hTest, RTSemRWGetReadCount(g_ahSemRWs[2]) == 1);
+    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[3]), VERR_SEM_LV_WRONG_RELEASE_ORDER);
+    RTTEST_CHECK(g_hTest, RTSemRWGetWriteRecursion(g_ahSemRWs[3]) == 1);
+    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[5]), VINF_SUCCESS);
     RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[4]), VINF_SUCCESS);
     RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[3]), VINF_SUCCESS);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[2]), VINF_SUCCESS);                      /* end recursion */
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[2]), VERR_SEM_LV_WRONG_RELEASE_ORDER);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[3]), VERR_SEM_LV_WRONG_RELEASE_ORDER);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[1]), VINF_SUCCESS);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[4]), VINF_SUCCESS);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseWrite(g_ahSemRWs[5]), VINF_SUCCESS);
-    RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[3]), VINF_SUCCESS);
     RTTEST_CHECK_RC(g_hTest, RTSemRWReleaseRead( g_ahSemRWs[2]), VINF_SUCCESS);
 
@@ -1187,4 +1208,5 @@
         testLo1();
         testLo2();
+        testLo3();
     }
 
