semrw-posix.cpp revision ad6025fdf797313667d02b6dd49b0be9b58d4a05
/* $Id$ */
/** @file
* Incredibly Portable Runtime - 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 <errno.h>
#include <pthread.h>
#include <unistd.h>
/** @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) */
/* Alignment padding. */
/** Number of write recursions. */
/** Number of read recursions by the writer. */
/** The write owner of the lock. */
/** pthread rwlock. */
};
{
int rc;
/*
* Allocate handle.
*/
struct RTSEMRWINTERNAL *pThis = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
if (pThis)
{
/*
* Create the rwlock.
*/
if (!rc)
{
if (!rc)
{
pThis->u32Padding = 0;
pThis->cWriterReads = 0;
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
{
/*
* Validate input, nil handle is fine.
*/
if (RWSem == NIL_RTSEMRW)
return VINF_SUCCESS;
/*
* Try destroy it.
*/
if (!rc)
{
rc = VINF_SUCCESS;
}
else
{
}
return rc;
}
{
/*
* Validate input.
*/
/*
* Check if it's the writer (implement write+read recursion).
*/
{
pThis->cWriterReads++;
return VINF_SUCCESS;
}
/*
* Try lock it.
*/
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 */
}
return VINF_SUCCESS;
}
{
/* EINTR isn't returned by the wait functions we're using. */
}
{
/*
* Validate input.
*/
/*
* Check if it's the writer.
*/
{
pThis->cWriterReads--;
return VINF_SUCCESS;
}
/*
* Try unlock it.
*/
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
/*
* Recursion?
*/
{
return VINF_SUCCESS;
}
/*
* Try lock it.
*/
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 */
}
return VINF_SUCCESS;
}
{
/* EINTR isn't returned by the wait functions we're using. */
}
{
/*
* Validate input.
*/
/*
* Verify ownership and implement recursion.
*/
return VINF_SUCCESS;
/*
* Try unlock it.
*/
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}