spinlock-r0drv-freebsd.c revision 3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7cca
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Spinlocks, Ring-0 Driver, FreeBSD.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Permission is hereby granted, free of charge, to any person
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * obtaining a copy of this software and associated documentation
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * files (the "Software"), to deal in the Software without
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * restriction, including without limitation the rights to use,
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * copy, modify, merge, publish, distribute, sublicense, and/or sell
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * copies of the Software, and to permit persons to whom the
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Software is furnished to do so, subject to the following
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * conditions:
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * The above copyright notice and this permission notice shall be
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * included in all copies or substantial portions of the Software.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * OTHER DEALINGS IN THE SOFTWARE.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync/*******************************************************************************
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync* Header Files *
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync*******************************************************************************/
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync/*******************************************************************************
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync* Structures and Typedefs *
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync*******************************************************************************/
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Wrapper for the struct mtx type.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync /** Spinlock magic value (RTSPINLOCK_MAGIC). */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /** The spinlock. */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /** Reserved to satisfy compile assertion below. */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /** The idAssertCpu variable before acquring the lock for asserting after
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync * releasing the spinlock. */
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /** The CPU that owns the lock. */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Allocate.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
24b814713d57519124d491243137f551be686c66vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pSpinlockInt));
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Initialize & return.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Validate input.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync * Make the lock invalid and release the memory.
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync for (int c = 50; c > 0; c--)
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pSpinlockInt);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync /* Enable interrupts while we sleep. */
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pSpinlockInt);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync for (int c = 50; c > 0; c--)
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pSpinlockInt);
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsyncRTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
1f1986470af9f0bb750dd859b142dc2e952deb20vboxsync Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pSpinlockInt);
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
3619a23ecce9ab8cdbf52dc55d9c7ff6e8ad7ccavboxsync AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));