semrw-generic.cpp revision 47d9037afecc80d7a107338f0adfda45c1224fed
/* $Id$ */
/** @file
* IPRT - Read-Write Semaphore, Generic.
*
* This is a generic implementation for OSes which don't have
* native RW semaphores.
*/
/*
* Copyright (C) 2006-2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
#include <iprt/critsect.h>
#include <iprt/lockvalidator.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Internal representation of a Read-Write semaphore for the
* Generic implementation. */
struct RTSEMRWINTERNAL
{
/** The usual magic. (RTSEMRW_MAGIC) */
/* Alignment padding. */
/** This critical section serializes the access to and updating of the structure members. */
/** The current number of reads. (pure read recursion counts too) */
/** The current number of writes. (recursion counts too) */
/** Number of read recursions by the writer. */
/** Number of writers waiting. */
/** The write owner of the lock. */
/** The handle of the event object on which the waiting readers block. (manual reset). */
/** The handle of the event object on which the waiting writers block. (automatic reset). */
/** Need to reset ReadEvent. */
bool fNeedResetReadEvent;
#ifdef RTSEMRW_STRICT
/** The validator record for the writer. */
/** The validator record for the readers. */
#endif
};
/* No debug wrapping here. */
{
int rc;
/*
* Allocate memory.
*/
struct RTSEMRWINTERNAL *pThis = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
if (pThis)
{
/*
* Create the semaphores.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Signal the read semaphore and initialize other variables.
*/
if (RT_SUCCESS(rc))
{
pThis->cWriterReads = 0;
pThis->cWritesWaiting = 0;
pThis->fNeedResetReadEvent = true;
#ifdef RTSEMRW_STRICT
RTLockValidatorRecExclInit(&pThis->ValidatorWrite, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, pThis,
true /*fEnabled*/, "RTSemRW");
RTLockValidatorRecSharedInit(&pThis->ValidatorRead, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, pThis,
false /*fSignaller*/, true /*fEnabled*/, "RTSemEvent");
#endif
return VINF_SUCCESS;
}
}
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
{
/*
* Validate handle.
*/
if (pThis == NIL_RTSEMRW)
return VINF_SUCCESS;
/*
* Check if busy.
*/
if (RT_SUCCESS(rc))
{
{
/*
* Make it invalid and unusable.
*/
/*
* Do actual cleanup. None of these can now fail.
*/
#ifdef RTSEMRW_STRICT
#endif
rc = VINF_SUCCESS;
}
else
{
rc = VERR_SEM_BUSY;
}
}
else
{
rc = VERR_SEM_BUSY;
}
return rc;
}
DECL_FORCE_INLINE(int) rtSemRWRequestRead(RTSEMRW RWSem, unsigned cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
{
/*
* Validate handle.
*/
unsigned cMilliesInitial = cMillies;
tsStart = RTTimeNanoTS();
#ifdef RTSEMRW_STRICT
if (cMillies > 0)
{
int rc9 = RTLockValidatorRecSharedCheckOrder(&pThis->ValidatorRead, hThreadSelf, pSrcPos, cMillies);
if (RT_FAILURE(rc9))
return rc9;
}
#endif
/*
* Take critsect.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Check if the state of affairs allows read access.
* Do not block further readers if there is a writer waiting, as
*/
#if 0
&& ( !pThis->cWritesWaiting
#endif
)
{
#ifdef RTSEMRW_STRICT
#endif
return VINF_SUCCESS;
}
{
#ifdef RTSEMRW_STRICT
int rc9 = RTLockValidatorRecExclRecursionMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core, pSrcPos);
if (RT_FAILURE(rc9))
{
return rc9;
}
#endif
pThis->cWriterReads++;
return VINF_SUCCESS;
}
/*
* Wait till it's ready for reading.
*/
if (cMillies == 0)
return VERR_TIMEOUT;
#ifndef RTSEMRW_STRICT
#endif
for (;;)
{
if (cMillies != RT_INDEFINITE_WAIT)
{
if (tsDelta >= 1000000)
{
tsDelta /= 1000000;
cMilliesInitial = (unsigned)tsDelta;
else
cMilliesInitial = 1;
}
}
#ifdef RTSEMRW_STRICT
cMillies, RTTHREADSTATE_RW_READ, false);
if (RT_FAILURE(rc))
break;
#else
#endif
int rcWait;
if (fInterruptible)
else
{
break;
}
{
break;
}
/*
* Re-take critsect and repeate the check we did before the loop.
*/
if (RT_FAILURE(rc))
{
break;
}
#if 0
&& ( !pThis->cWritesWaiting
#endif
)
{
#ifdef RTSEMRW_STRICT
#endif
return VINF_SUCCESS;
}
/*
* Quit if the wait already timed out.
*/
if (rcWait == VERR_TIMEOUT)
{
rc = VERR_TIMEOUT;
break;
}
}
/* failed */
return rc;
}
{
#ifndef RTSEMRW_STRICT
#else
#endif
}
RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW RWSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
#ifndef RTSEMRW_STRICT
#else
#endif
}
RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW RWSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
/*
* Validate handle.
*/
/*
* Take critsect.
*/
if (RT_SUCCESS(rc))
{
{
#ifdef RTSEMRW_STRICT
if (RT_SUCCESS(rc))
#endif
{
{
/* Kick off a writer if appropriate. */
if ( pThis->cWritesWaiting > 0
{
}
}
else
{
AssertFailed();
rc = VERR_NOT_OWNER;
}
}
}
else
{
{
if (pThis->cWriterReads > 0)
{
#ifdef RTSEMRW_STRICT
if (RT_SUCCESS(rc))
#endif
{
pThis->cWriterReads--;
}
}
else
{
AssertFailed();
rc = VERR_NOT_OWNER;
}
}
else
{
AssertFailed();
rc = VERR_NOT_OWNER;
}
}
}
else
return rc;
}
DECL_FORCE_INLINE(int) rtSemRWRequestWrite(RTSEMRW RWSem, unsigned cMillies, bool fInterruptible, PCRTLOCKVALSRCPOS pSrcPos)
{
/*
* Validate handle.
*/
unsigned cMilliesInitial = cMillies;
tsStart = RTTimeNanoTS();
#ifdef RTSEMRW_STRICT
if (cMillies)
{
if (RT_FAILURE(rc9))
return rc9;
}
#endif
/*
* Take critsect.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Check if the state of affairs allows write access.
*/
|| !cMillies)
)
)
)
{
/*
* Reset the reader event semaphore if necessary.
*/
if (pThis->fNeedResetReadEvent)
{
pThis->fNeedResetReadEvent = false;
}
#ifdef RTSEMRW_STRICT
#endif
return VINF_SUCCESS;
}
/*
* Signal writer presence.
*/
if (cMillies != 0)
pThis->cWritesWaiting++;
/*
* Wait till it's ready for writing.
*/
if (cMillies == 0)
return VERR_TIMEOUT;
#ifndef RTSEMRW_STRICT
#endif
for (;;)
{
if (cMillies != RT_INDEFINITE_WAIT)
{
if (tsDelta >= 1000000)
{
tsDelta /= 1000000;
cMilliesInitial = (unsigned)tsDelta;
else
cMilliesInitial = 1;
}
}
#ifdef RTSEMRW_STRICT
cMillies, RTTHREADSTATE_RW_WRITE, false);
if (RT_FAILURE(rc))
break;
#else
#endif
int rcWait;
if (fInterruptible)
else
{
break;
}
{
break;
}
/*
* Re-take critsect and repeate the check we did prior to this loop.
*/
if (RT_FAILURE(rc))
{
break;
}
{
/*
* Reset the reader event semaphore if necessary.
*/
if (pThis->fNeedResetReadEvent)
{
pThis->fNeedResetReadEvent = false;
}
pThis->cWritesWaiting--;
#ifdef RTSEMRW_STRICT
#endif
return VINF_SUCCESS;
}
/*
* Quit if the wait already timed out.
*/
if (rcWait == VERR_TIMEOUT)
{
rc = VERR_TIMEOUT;
break;
}
}
/*
*/
{
/* Adjust this counter, whether we got the critsect or not. */
pThis->cWritesWaiting--;
}
return rc;
}
{
#ifndef RTSEMRW_STRICT
#else
#endif
}
RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW RWSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
#ifndef RTSEMRW_STRICT
#else
#endif
}
RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW RWSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
/*
* Validate handle.
*/
/*
* Take critsect.
*/
/*
* Check if owner.
*/
{
return VERR_NOT_OWNER;
}
#ifdef RTSEMRW_STRICT
{
if (RT_FAILURE(rc9))
{
return rc9;
}
}
#endif
/*
* Release ownership and remove ourselves from the writers count.
*/
{
{
AssertMsgFailed(("All recursive read locks need to be released prior to the final write lock! (%p)n\n", pThis));
return VERR_WRONG_ORDER;
}
}
/*
* Release the readers if no more writers waiting, otherwise the writers.
*/
if (!pThis->cWritesWaiting)
{
pThis->fNeedResetReadEvent = true;
}
else
{
}
return rc;
}
{
/*
* Validate handle.
*/
/*
* Check ownership.
*/
return hWriter == hNativeSelf;
}
{
/*
* Validate handle.
*/
/*
* Return the requested data.
*/
}
{
/*
* Validate handle.
*/
/*
* Return the requested data.
*/
return pThis->cWriterReads;
}
{
/*
* Validate input.
*/
AssertPtrReturn(pThis, 0);
0);
/*
* Return the requested data.
*/
}