spinlock-generic.cpp revision f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2
48N/A/* $Id$ */
48N/A/** @file
48N/A * IPRT - Spinlock, generic implementation.
48N/A */
48N/A
48N/A/*
48N/A * Copyright (C) 2006-2012 Oracle Corporation
48N/A *
48N/A * This file is part of VirtualBox Open Source Edition (OSE), as
48N/A * available from http://www.virtualbox.org. This file is free software;
48N/A * you can redistribute it and/or modify it under the terms of the GNU
48N/A * General Public License (GPL) as published by the Free Software
48N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
48N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
48N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
48N/A *
48N/A * The contents of this file may alternatively be used under the terms
48N/A * of the Common Development and Distribution License Version 1.0
48N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
48N/A * VirtualBox OSE distribution, in which case the provisions of the
48N/A * CDDL are applicable instead of those of the GPL.
48N/A *
48N/A * You may elect to license modified versions of this file under the
48N/A * terms and conditions of either the GPL or the CDDL or both.
48N/A */
48N/A
48N/A
48N/A/*******************************************************************************
48N/A* Defined Constants And Macros *
48N/A*******************************************************************************/
48N/A/** @def RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
48N/A * Force cpu yields after spinning the number of times indicated by the define.
70N/A * If 0 we will spin forever. */
48N/A#define RT_CFG_SPINLOCK_GENERIC_DO_SLEEP 100000
48N/A
70N/A
70N/A/*******************************************************************************
48N/A* Header Files *
48N/A*******************************************************************************/
70N/A#include <iprt/spinlock.h>
48N/A#include "internal/iprt.h"
48N/A
48N/A#include <iprt/alloc.h>
48N/A#include <iprt/asm.h>
70N/A#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
48N/A# include <iprt/asm-amd64-x86.h>
48N/A#endif
48N/A#include <iprt/err.h>
48N/A#include <iprt/assert.h>
70N/A#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
70N/A# include <iprt/thread.h>
70N/A#endif
70N/A
48N/A#include "internal/magics.h"
48N/A
70N/A
70N/A/*******************************************************************************
70N/A* Structures and Typedefs *
70N/A*******************************************************************************/
48N/A/**
70N/A * Generic spinlock structure.
70N/A */
70N/Atypedef struct RTSPINLOCKINTERNAL
70N/A{
70N/A /** Spinlock magic value (RTSPINLOCK_MAGIC). */
70N/A uint32_t u32Magic;
70N/A /** The spinlock creation flags. */
70N/A uint32_t fFlags;
48N/A /** The spinlock. */
48N/A uint32_t volatile fLocked;
48N/A /** The saved CPU interrupt. */
48N/A uint32_t volatile fIntSaved;
} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
{
PRTSPINLOCKINTERNAL pThis;
AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
/*
* Allocate.
*/
pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
if (!pThis)
return VERR_NO_MEMORY;
/*
* Initialize and return.
*/
pThis->u32Magic = RTSPINLOCK_MAGIC;
pThis->fFlags = fFlags;
pThis->fIntSaved = 0;
ASMAtomicWriteU32(&pThis->fLocked, 0);
*pSpinlock = pThis;
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTSpinlockCreate);
RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
{
/*
* Validate input.
*/
PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
if (!pThis)
return VERR_INVALID_PARAMETER;
if (pThis->u32Magic != RTSPINLOCK_MAGIC)
{
AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
return VERR_INVALID_PARAMETER;
}
ASMAtomicIncU32(&pThis->u32Magic);
RTMemFree(pThis);
return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTSpinlockDestroy);
RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
{
PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
{
#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
uint32_t fIntSaved = ASMGetFlags();
#endif
#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
for (;;)
{
ASMIntDisable();
for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
{
if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
{
# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
pThis->fIntSaved = fIntSaved;
# endif
return;
}
ASMNopPause();
}
ASMSetFlags(fIntSaved);
RTThreadYield();
}
#else
for (;;)
{
ASMIntDisable();
if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
{
# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
pThis->fIntSaved = fIntSaved;
# endif
return;
}
ASMSetFlags(fIntSaved);
ASMNopPause();
}
#endif
}
else
{
#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
for (;;)
{
for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
{
if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
return;
ASMNopPause();
}
RTThreadYield();
}
#else
while (!ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
ASMNopPause();
#endif
}
}
RT_EXPORT_SYMBOL(RTSpinlockAcquire);
RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
{
PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
{
#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
uint32_t fIntSaved = pThis->fIntSaved;
pThis->fIntSaved = 0;
#endif
if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
ASMSetFlags(fIntSaved);
#endif
}
else
{
if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
}
}
RT_EXPORT_SYMBOL(RTSpinlockRelease);
RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock)
{
#if 1
if (RT_UNLIKELY(!(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)))
RTAssertMsg2("RTSpinlockReleaseNoInts: %p (magic=%#x)\n", Spinlock, Spinlock->u32Magic);
#else
AssertRelease(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE);
#endif
RTSpinlockRelease(Spinlock);
}
RT_EXPORT_SYMBOL(RTSpinlockReleaseNoInts);