semrw-lockless-generic.cpp revision a9c682f4c99869f4d6837193f9713eea79822c06
af062818b47340eef15700d2f0211576ba3506eevboxsync * IPRT Testcase - RTSemXRoads, generic implementation.
af062818b47340eef15700d2f0211576ba3506eevboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
af062818b47340eef15700d2f0211576ba3506eevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
af062818b47340eef15700d2f0211576ba3506eevboxsync * available from http://www.virtualbox.org. This file is free software;
af062818b47340eef15700d2f0211576ba3506eevboxsync * you can redistribute it and/or modify it under the terms of the GNU
af062818b47340eef15700d2f0211576ba3506eevboxsync * General Public License (GPL) as published by the Free Software
af062818b47340eef15700d2f0211576ba3506eevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
af062818b47340eef15700d2f0211576ba3506eevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
af062818b47340eef15700d2f0211576ba3506eevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
af062818b47340eef15700d2f0211576ba3506eevboxsync * The contents of this file may alternatively be used under the terms
af062818b47340eef15700d2f0211576ba3506eevboxsync * of the Common Development and Distribution License Version 1.0
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync * VirtualBox OSE distribution, in which case the provisions of the
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync * CDDL are applicable instead of those of the GPL.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * You may elect to license modified versions of this file under the
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * terms and conditions of either the GPL or the CDDL or both.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
af062818b47340eef15700d2f0211576ba3506eevboxsync * additional information or have any questions.
af062818b47340eef15700d2f0211576ba3506eevboxsync/*******************************************************************************
af062818b47340eef15700d2f0211576ba3506eevboxsync* Header Files *
af062818b47340eef15700d2f0211576ba3506eevboxsync*******************************************************************************/
af062818b47340eef15700d2f0211576ba3506eevboxsync/*******************************************************************************
af062818b47340eef15700d2f0211576ba3506eevboxsync* Structures and Typedefs *
af062818b47340eef15700d2f0211576ba3506eevboxsync*******************************************************************************/
af062818b47340eef15700d2f0211576ba3506eevboxsync /** Magic value (RTSEMRW_MAGIC). */
af062818b47340eef15700d2f0211576ba3506eevboxsync /* The state variable.
af062818b47340eef15700d2f0211576ba3506eevboxsync * All accesses are atomic and it bits are defined like this:
af062818b47340eef15700d2f0211576ba3506eevboxsync * Bits 0..14 - cReads.
af062818b47340eef15700d2f0211576ba3506eevboxsync * Bit 15 - Unused.
af062818b47340eef15700d2f0211576ba3506eevboxsync * Bits 16..31 - cWrites. - doesn't make sense here
af062818b47340eef15700d2f0211576ba3506eevboxsync * Bit 31 - fDirection; 0=Read, 1=Write.
bool volatile fNeedReset;
#ifdef RTSEMRW_STRICT
#define RTSEMRW_CNT_RD_SHIFT 0
return RTSemRWCreateEx(phRWSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTSemRW");
if (!pThis)
return VERR_NO_MEMORY;
#ifdef RTSEMRW_STRICT
if (!pszNameFmt)
return VINF_SUCCESS;
return rc;
return VINF_SUCCESS;
AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMRW_MAGIC, RTSEMRW_MAGIC), VERR_INVALID_HANDLE);
#ifdef RTSEMRW_STRICT
return VINF_SUCCESS;
#ifdef RTSEMRW_STRICT
return RTLOCKVAL_SUB_CLASS_INVALID;
static int rtSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
return VINF_SUCCESS;
#ifdef RTSEMRW_STRICT
if (cMillies > 0)
int rc9;
return rc9;
#ifdef RTSEMRW_STRICT
#ifdef RTSEMRW_STRICT
#ifdef RTSEMRW_STRICT
int rc9 = RTLockValidatorRecExclRecursionMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core, pSrcPos);
return rc9;
if (!cMillies)
return VERR_TIMEOUT;
cWait++;
int rc;
#ifdef RTSEMRW_STRICT
if (fInterruptible)
return VERR_SEM_DESTROYED;
cWait--;
return rc;
cWait--;
if (cWait == 0)
#ifdef RTSEMRW_STRICT
return VERR_SEM_DESTROYED;
ASMNopPause();
Assert((ASMAtomicReadU64(&pThis->u64State) & RTSEMRW_DIR_MASK) == (RTSEMRW_DIR_READ << RTSEMRW_DIR_SHIFT));
return VINF_SUCCESS;
#ifndef RTSEMRW_STRICT
RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
#ifndef RTSEMRW_STRICT
RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
#ifdef RTSEMRW_STRICT
return rc9;
ASMNopPause();
#ifdef RTSEMRW_STRICT
return rc;
return VINF_SUCCESS;
DECL_FORCE_INLINE(int) rtSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
return VINF_SUCCESS;
#ifdef RTSEMRW_STRICT
if (cMillies)
return rc9;
Assert((ASMAtomicReadU64(&pThis->u64State) & RTSEMRW_DIR_MASK) == (RTSEMRW_DIR_WRITE << RTSEMRW_DIR_SHIFT));
#ifdef RTSEMRW_STRICT
return rc9;
return VINF_SUCCESS;
else if (!cMillies)
return VERR_TIMEOUT;
return VERR_SEM_DESTROYED;
ASMNopPause();
|| cMillies == 0);
if (fDone)
if (!fDone)
int rc;
#ifdef RTSEMRW_STRICT
if (cMillies)
if (fInterruptible)
return VERR_SEM_DESTROYED;
return rc;
if (fDone)
Assert((ASMAtomicReadU64(&pThis->u64State) & RTSEMRW_DIR_MASK) == (RTSEMRW_DIR_WRITE << RTSEMRW_DIR_SHIFT));
#ifdef RTSEMRW_STRICT
return VINF_SUCCESS;
#ifndef RTSEMRW_STRICT
RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
#ifndef RTSEMRW_STRICT
RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
AssertReturn(pThis->cWriterReads == 0, VERR_WRONG_ORDER); /* (must release all read recursions before the final write.) */
#ifdef RTSEMRW_STRICT
return rc9;
Assert(c > 0);
ASMNopPause();
return VERR_SEM_DESTROYED;
#ifdef RTSEMRW_STRICT
return rc9;
return VINF_SUCCESS;
#ifdef RTSEMRW_STRICT
return fWannaHear;