semrw-posix.cpp revision c364b6c44a252ab65b514fa8f9a665cc5e33a1ce
/* $Id$ */
/** @file
* IPRT - Read-Write Semaphore, POSIX.
*/
/*
* Copyright (C) 2006-2007 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/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(pvVar, pThread) do { *(pThread) = (pthread_t)ASMAtomicReadPtr((void *volatile *)pvVar); } while (0)
# define ATOMIC_SET_PTHREAD_T(pvVar, pThread) ASMAtomicWritePtr((void *volatile *)pvVar, (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
};
{
int rc;
/*
* Allocate handle.
*/
struct RTSEMRWINTERNAL *pThis = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
if (pThis)
{
/*
* Create the rwlock.
*/
if (!rc)
{
if (!rc)
{
pThis->cWriterReads = 0;
#ifdef RTSEMRW_STRICT
RTLockValidatorRecExclInit(&pThis->ValidatorWrite, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, "RTSemRW", pThis);
RTLockValidatorRecSharedInit(&pThis->ValidatorRead, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, "RTSemRW", pThis);
#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;
}
{
/*
* 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, RTTHREADSTATE_RW_READ);
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;
}
{
/* EINTR isn't returned by the wait functions we're using. */
}
{
/*
* 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 RWSem, unsigned 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, RTTHREADSTATE_RW_WRITE);
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 RWSem, unsigned 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 RWSem, unsigned 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 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.
*/
}