semrw-posix.cpp revision f7f5cd7b1e530eb5636da51c974b48ae0c1775b3
/* $Id$ */
/** @file
* IPRT - Read-Write Semaphore, POSIX.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
#include <iprt/lockvalidator.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @todo move this to r3/posix/something.h. */
#ifdef RT_OS_SOLARIS
#else
AssertCompileSize(pthread_t, sizeof(void *));
# define ATOMIC_GET_PTHREAD_T(ppvVar, pThread) do { *(pThread) = (pthread_t)ASMAtomicReadPtr((void * volatile *)ppvVar); } while (0)
# define ATOMIC_SET_PTHREAD_T(ppvVar, pThread) ASMAtomicWritePtr((void * volatile *)ppvVar, (void *)pThread)
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Posix internal representation of a read-write semaphore. */
struct RTSEMRWINTERNAL
{
/** The usual magic. (RTSEMRW_MAGIC) */
/** The number of readers.
* (For preventing screwing up the lock on linux). */
/** Number of write recursions. */
/** Number of read recursions by the writer. */
/** The write owner of the lock. */
/** pthread rwlock. */
#ifdef RTSEMRW_STRICT
/** The validator record for the writer. */
/** The validator record for the readers. */
#endif
};
{
return RTSemRWCreateEx(phRWSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, "RTSemRW");
}
{
/*
* Allocate handle.
*/
int rc;
struct RTSEMRWINTERNAL *pThis = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
if (pThis)
{
/*
* Create the rwlock.
*/
if (!rc)
{
if (!rc)
{
pThis->cWriterReads = 0;
#ifdef RTSEMRW_STRICT
if (!pszNameFmt)
{
static uint32_t volatile s_iSemRWAnon = 0;
fLVEnabled, "RTSemRW-%u", i);
}
else
{
}
#endif
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
{
/*
* Validate input, nil handle is fine.
*/
if (pThis == NIL_RTSEMRW)
return VINF_SUCCESS;
/*
* Try destroy it.
*/
AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMRW_MAGIC, RTSEMRW_MAGIC), VERR_INVALID_HANDLE);
if (!rc)
{
#ifdef RTSEMRW_STRICT
#endif
rc = VINF_SUCCESS;
}
else
{
}
return rc;
}
{
#ifdef RTSEMRW_STRICT
/*
* Validate handle.
*/
#else
return RTLOCKVAL_SUB_CLASS_INVALID;
#endif
}
DECL_FORCE_INLINE(int) rtSemRWRequestRead(RTSEMRW hRWSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
{
/*
* Validate input.
*/
/*
* Check if it's the writer (implement write+read recursion).
*/
{
#ifdef RTSEMRW_STRICT
int rc9 = RTLockValidatorRecExclRecursionMixed(&pThis->ValidatorWrite, &pThis->ValidatorRead.Core, pSrcPos);
if (RT_FAILURE(rc9))
return rc9;
#endif
pThis->cWriterReads++;
return VINF_SUCCESS;
}
/*
* Try lock it.
*/
if (cMillies > 0)
{
#ifdef RTSEMRW_STRICT
int rc9 = RTLockValidatorRecSharedCheckOrderAndBlocking(&pThis->ValidatorRead, hThreadSelf, pSrcPos, true,
cMillies, RTTHREADSTATE_RW_READ, true);
if (RT_FAILURE(rc9))
return rc9;
#else
hThreadSelf = RTThreadSelf();
#endif
}
if (cMillies == RT_INDEFINITE_WAIT)
{
/* take rwlock */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
}
else
{
#ifdef RT_OS_DARWIN
AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
return VERR_NOT_IMPLEMENTED;
#else /* !RT_OS_DARWIN */
/*
* Get current time and calc end of wait time.
*/
if (cMillies != 0)
{
{
}
}
/* take rwlock */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
#endif /* !RT_OS_DARWIN */
}
#ifdef RTSEMRW_STRICT
#endif
return VINF_SUCCESS;
}
{
#ifndef RTSEMRW_STRICT
#else
#endif
}
RTDECL(int) RTSemRWRequestReadDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
/* EINTR isn't returned by the wait functions we're using. */
#ifndef RTSEMRW_STRICT
#else
#endif
}
RTDECL(int) RTSemRWRequestReadNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
/*
* Validate input.
*/
/*
* Check if it's the writer.
*/
{
#ifdef RTSEMRW_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
pThis->cWriterReads--;
return VINF_SUCCESS;
}
/*
* Try unlock it.
*/
#ifdef RTSEMRW_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
#ifdef RT_OS_LINUX /* glibc (at least 2.8) may screw up when unlocking a lock we don't own. */
{
return VERR_NOT_OWNER;
}
#endif
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}
DECL_FORCE_INLINE(int) rtSemRWRequestWrite(RTSEMRW hRWSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
{
/*
* Validate input.
*/
/*
* Recursion?
*/
{
#ifdef RTSEMRW_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
return VINF_SUCCESS;
}
/*
* Try lock it.
*/
if (cMillies)
{
#ifdef RTSEMRW_STRICT
int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorWrite, hThreadSelf, pSrcPos, true,
cMillies, RTTHREADSTATE_RW_WRITE, true);
if (RT_FAILURE(rc9))
return rc9;
#else
hThreadSelf = RTThreadSelf();
#endif
}
if (cMillies == RT_INDEFINITE_WAIT)
{
/* take rwlock */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
}
else
{
#ifdef RT_OS_DARWIN
AssertMsgFailed(("Not implemented on Darwin yet because of incomplete pthreads API."));
return VERR_NOT_IMPLEMENTED;
#else /* !RT_OS_DARWIN */
/*
* Get current time and calc end of wait time.
*/
if (cMillies != 0)
{
{
}
}
/* take rwlock */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
#endif /* !RT_OS_DARWIN */
}
#ifdef RTSEMRW_STRICT
#endif
return VINF_SUCCESS;
}
{
#ifndef RTSEMRW_STRICT
#else
#endif
}
RTDECL(int) RTSemRWRequestWriteDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
/* EINTR isn't returned by the wait functions we're using. */
#ifndef RTSEMRW_STRICT
#else
#endif
}
RTDECL(int) RTSemRWRequestWriteNoResumeDebug(RTSEMRW hRWSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
/* EINTR isn't returned by the wait functions we're using. */
}
{
/*
* Validate input.
*/
/*
* Verify ownership and implement recursion.
*/
{
#ifdef RTSEMRW_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
return VINF_SUCCESS;
}
/*
* Try unlock it.
*/
#ifdef RTSEMRW_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
AssertPtrReturn(pThis, false);
false);
/*
* Check ownership.
*/
}
{
/*
* Validate handle.
*/
AssertPtrReturn(pThis, false);
/*
* Check write ownership. The writer is also a valid reader.
*/
return true;
return false;
/*
* If there are no readers, we cannot be one of them, can we?
*/
return false;
#ifdef RTSEMRW_STRICT
/*
* Ask the lock validator.
*/
#else
/*
* Just tell the caller what he want to hear.
*/
return fWannaHear;
#endif
}
{
/*
* Validate input.
*/
AssertPtrReturn(pThis, 0);
0);
/*
* Return the requested data.
*/
}
{
/*
* Validate input.
*/
AssertPtrReturn(pThis, 0);
0);
/*
* Return the requested data.
*/
return pThis->cWriterReads;
}
{
/*
* Validate input.
*/
AssertPtrReturn(pThis, 0);
0);
/*
* Return the requested data.
*/
}