semrw-posix.cpp revision 9e5c26690d45216629b5f588aced8fcfb68c23b6
/* $Id$ */
/** @file
* innotek Portable Runtime - Read-Write Semaphore, POSIX.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 <errno.h>
#include <pthread.h>
#include <unistd.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Posix internal representation of a read-write semaphore. */
struct RTSEMRWINTERNAL
{
/** pthread rwlock. */
/** Variable to check if initialized.
* 0 is uninitialized, ~0 is inititialized. */
volatile unsigned uCheck;
/** The write owner of the lock. */
};
/**
* Validate a read-write semaphore handle passed to one of the interface.
*
* @returns true if valid.
* @returns false if invalid.
* @param pIntRWSem Pointer to the read-write semaphore to validate.
*/
{
return false;
return false;
return true;
}
{
int rc;
/*
* Allocate handle.
*/
struct RTSEMRWINTERNAL *pIntRWSem = (struct RTSEMRWINTERNAL *)RTMemAlloc(sizeof(struct RTSEMRWINTERNAL));
if (pIntRWSem)
{
/*
* Create the rwlock.
*/
if (!rc)
{
if (!rc)
{
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
{
/*
* Validate input.
*/
if (!rtsemRWValid(RWSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Try destroy it.
*/
if (!rc)
{
rc = VINF_SUCCESS;
}
else
{
}
return rc;
}
{
/*
* Validate input.
*/
if (!rtsemRWValid(RWSem))
{
return VERR_INVALID_HANDLE;
}
/*
* 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.
*/
if (!rtsemRWValid(RWSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Try unlock it.
*/
{
return VERR_NOT_OWNER;
}
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
if (!rtsemRWValid(RWSem))
{
return VERR_INVALID_HANDLE;
}
/*
* 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 */
}
#ifdef RT_OS_SOLARIS
#else
#endif
return VINF_SUCCESS;
}
{
/* EINTR isn't returned by the wait functions we're using. */
}
{
/*
* Validate input.
*/
if (!rtsemRWValid(RWSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Try unlock it.
*/
{
AssertMsgFailed(("Not Write owner!\n"));
return VERR_NOT_OWNER;
}
/*
* Try unlock it.
*/
#ifdef RT_OS_SOLARIS
#else
AssertMsg(sizeof(pthread_t) == sizeof(void *), ("pthread_t is not the size of a pointer but %d bytes\n", sizeof(pthread_t)));
#endif
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}