Index: trunk/src/VBox/Runtime/r0drv/nt/semmutex-r0drv-nt.cpp
===================================================================
--- trunk/src/VBox/Runtime/r0drv/nt/semmutex-r0drv-nt.cpp	(revision 25378)
+++ trunk/src/VBox/Runtime/r0drv/nt/semmutex-r0drv-nt.cpp	(revision 25433)
@@ -69,5 +69,5 @@
 
 
-RTDECL(int)  RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
+RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
 {
     Assert(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
@@ -88,5 +88,5 @@
 
 
-RTDECL(int)  RTSemMutexDestroy(RTSEMMUTEX MutexSem)
+RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
 {
     /*
@@ -96,14 +96,10 @@
     if (!pMutexInt)
         return VERR_INVALID_PARAMETER;
-    if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
-    {
-        AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
-        return VERR_INVALID_PARAMETER;
-    }
+    AssertReturn(pMutexInt->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
 
     /*
      * Invalidate it and signal the object just in case.
      */
-    ASMAtomicIncU32(&pMutexInt->u32Magic);
+    AssertReturn(ASMAtomicCmpXchgU32(&pMutexInt->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
     RTMemFree(pMutexInt);
     return VINF_SUCCESS;
@@ -111,5 +107,15 @@
 
 
-RTDECL(int)  RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
+/**
+ * Internal worker for RTSemMutexRequest and RTSemMutexRequestNoResume
+ *
+ * @returns IPRT status code.
+ * @param   MutexSem            The mutex handle.
+ * @param   cMillies            The timeout.
+ * @param   fInterruptible      Whether it's interruptible
+ *                              (RTSemMutexRequestNoResume) or not
+ *                              (RTSemMutexRequest).
+ */
+static int rtSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies, BOOLEAN fInterruptible)
 {
     /*
@@ -119,10 +125,5 @@
     if (!pMutexInt)
         return VERR_INVALID_PARAMETER;
-    if (    !pMutexInt
-        ||  pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
-    {
-        AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
-        return VERR_INVALID_PARAMETER;
-    }
+    AssertReturn(pMutexInt->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
 
     /*
@@ -132,13 +133,13 @@
     AssertMsg(cMillies == RT_INDEFINITE_WAIT, ("timeouts are not supported when using fast mutexes!\n"));
     ExAcquireFastMutex(&pMutexInt->Mutex);
-#else
+#else  /* !RT_USE_FAST_MUTEX */
     NTSTATUS rcNt;
     if (cMillies == RT_INDEFINITE_WAIT)
-        rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, NULL);
+        rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, fInterruptible, NULL);
     else
     {
         LARGE_INTEGER Timeout;
         Timeout.QuadPart = -(int64_t)cMillies * 10000;
-        rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, &Timeout);
+        rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, fInterruptible, &Timeout);
     }
     switch (rcNt)
@@ -148,10 +149,13 @@
                 return VINF_SUCCESS;
             return VERR_SEM_DESTROYED;
+
         case STATUS_ALERTED:
-            return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
         case STATUS_USER_APC:
-            return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
+            Assert(fInterruptible);
+            return VERR_INTERRUPTED;
+
         case STATUS_TIMEOUT:
             return VERR_TIMEOUT;
+
         default:
             AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p: wait returned %lx!\n",
@@ -159,17 +163,34 @@
             return VERR_INTERNAL_ERROR;
     }
-#endif
+#endif /* !RT_USE_FAST_MUTEX */
     return VINF_SUCCESS;
 }
 
 
-RTDECL(int)  RTSemMutexRequestDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
+RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
+{
+    return rtSemMutexRequest(MutexSem, cMillies, FALSE /*fInterruptible*/);
+}
+
+
+RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
 {
     return RTSemMutexRequest(MutexSem, cMillies);
 }
 
-/** @todo implement the NoResume versions */
-
-RTDECL(int)  RTSemMutexRelease(RTSEMMUTEX MutexSem)
+
+RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
+{
+    return rtSemMutexRequest(MutexSem, cMillies, TRUE /*fInterruptible*/);
+}
+
+
+RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
+{
+    return RTSemMutexRequestNoResume(MutexSem, cMillies);
+}
+
+
+RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
 {
     /*
@@ -179,10 +200,5 @@
     if (!pMutexInt)
         return VERR_INVALID_PARAMETER;
-    if (    !pMutexInt
-        ||  pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
-    {
-        AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
-        return VERR_INVALID_PARAMETER;
-    }
+    AssertReturn(pMutexInt->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
 
     /*
@@ -192,5 +208,5 @@
     ExReleaseFastMutex(&pMutexInt->Mutex);
 #else
-    KeReleaseMutex(&pMutexInt->Mutex, FALSE);
+    KeReleaseMutex(&pMutexInt->Mutex, FALSE /*Wait*/);
 #endif
     return VINF_SUCCESS;
