Index: /trunk/include/iprt/spinlock.h
===================================================================
--- /trunk/include/iprt/spinlock.h	(revision 22649)
+++ /trunk/include/iprt/spinlock.h	(revision 22650)
@@ -66,10 +66,10 @@
 #  define RTSPINLOCKTMP_INITIALIZER { 0 }
 
-# elif defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
+# elif defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_SOLARIS)
     /** The saved [R|E]FLAGS. */
     RTCCUINTREG     uFlags;
 #  define RTSPINLOCKTMP_INITIALIZER { 0 }
 
-# elif defined(RT_OS_OS2) || defined(RT_OS_FREEBSD) /** @todo r=bird: FreeBSD is probably doing the wrong thing here. */
+# elif defined(RT_OS_OS2)
     /** The saved [R|E]FLAGS. (dummy) */
     RTCCUINTREG     uFlags;
Index: /trunk/src/VBox/Runtime/r0drv/freebsd/spinlock-r0drv-freebsd.c
===================================================================
--- /trunk/src/VBox/Runtime/r0drv/freebsd/spinlock-r0drv-freebsd.c	(revision 22649)
+++ /trunk/src/VBox/Runtime/r0drv/freebsd/spinlock-r0drv-freebsd.c	(revision 22650)
@@ -77,6 +77,6 @@
     RT_ASSERT_PREEMPTIBLE();
     AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
-    PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pSpinlockInt));
-    if (!pSpinlockInt)
+    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
+    if (!pThis)
         return VERR_NO_MEMORY;
 
@@ -84,7 +84,7 @@
      * Initialize & return.
      */
-    pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
-    pSpinlockInt->fLocked  = 0;
-    *pSpinlock = pSpinlockInt;
+    pThis->u32Magic = RTSPINLOCK_MAGIC;
+    pThis->fLocked  = 0;
+    *pSpinlock = pThis;
     return VINF_SUCCESS;
 }
@@ -97,9 +97,9 @@
      */
     RT_ASSERT_INTS_ON();
-    PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
-    if (!pSpinlockInt)
+    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
+    if (!pThis)
         return VERR_INVALID_PARAMETER;
-    AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
-                    ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
+    AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
+                    ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
                     VERR_INVALID_PARAMETER);
 
@@ -107,6 +107,6 @@
      * Make the lock invalid and release the memory.
      */
-    ASMAtomicIncU32(&pSpinlockInt->u32Magic);
-    RTMemFree(pSpinlockInt);
+    ASMAtomicIncU32(&pThis->u32Magic);
+    RTMemFree(pThis);
     return VINF_SUCCESS;
 }
@@ -115,23 +115,27 @@
 RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
 {
-    PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
-    AssertPtr(pSpinlockInt);
-    Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
+    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
+    AssertPtr(pThis);
+    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
     RT_ASSERT_PREEMPT_CPUID_VAR();
+    Assert(pTmp->uFlags == 0);
 
     for (;;)
     {
-        pTmp->uFlags = ASMGetFlags();
-        ASMIntDisable();
+        pTmp->uFlags = ASMIntDisableFlags();
         critical_enter();
 
-        for (int c = 50; c > 0; c--)
-            if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
+        int c = 50;
+        for (;;)
+        {
+            if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
             {
-                RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pSpinlockInt);
+                RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
                 return;
             }
-            else
-                cpu_spinwait();
+            if (--c <= 0)
+                break;
+            cpu_spinwait();
+        }
 
         /* Enable interrupts while we sleep. */
@@ -145,17 +149,17 @@
 RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
 {
-    PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
+    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
 
-    AssertPtr(pSpinlockInt);
-    Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
-    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pSpinlockInt);
-    NOREF(pTmp);
-
-    if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
-        AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
+    AssertPtr(pThis);
+    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
+    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
+
+    if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
+        AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
+
     ASMSetFlags(pTmp->uFlags);
-
     critical_exit();
+    pTmp->uFlags = 0;
 }
 
@@ -163,8 +167,12 @@
 RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
 {
-    PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
+    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     RT_ASSERT_PREEMPT_CPUID_VAR();
-    AssertPtr(pSpinlockInt);
-    Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
+    AssertPtr(pThis);
+    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
+#ifdef RT_STRICT
+    Assert(pTmp->uFlags == 0);
+    pTmp->uFlags = 0;
+#endif
 
     NOREF(pTmp);
@@ -173,12 +181,17 @@
     {
         critical_enter();
-        for (int c = 50; c > 0; c--)
-            if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
+
+        int c = 50;
+        for (;;)
+        {
+            if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
             {
-                RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pSpinlockInt);
+                RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
                 return;
             }
-            else
-                cpu_spinwait();
+            if (--c <= 0)
+                break;
+            cpu_spinwait();
+        }
 
         critical_exit();
@@ -190,15 +203,18 @@
 RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
 {
-    PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
+    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
 
-    AssertPtr(pSpinlockInt);
-    Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
-    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pSpinlockInt);
-
+    AssertPtr(pThis);
+    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
+    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
+#ifdef RT_STRICT
+    Assert(pTmp->uFlags == 42);
+    pTmp->uFlags = 0;
+#endif
     NOREF(pTmp);
 
-    if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
-        AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
+    if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
+        AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
 
     critical_exit();
