sems-posix.cpp revision 26ce84897df60fed28f68a98b91ce82aed9f0ee7
/* $Id$ */
/** @file
* innotek Portable Runtime - Semaphores, 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#ifdef RT_OS_DARWIN
# define pthread_yield() pthread_yield_np()
#endif
#ifdef RT_OS_SOLARIS
# include <sched.h>
# define pthread_yield() sched_yield()
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Internal representation of the POSIX implementation of an Event semaphore.
* The POSIX implementation uses a mutex and a condition variable to implement
* the automatic reset event semaphore semantics.
*
* This must be identical to RTSEMEVENTMULTIINTERNAL!
*/
struct RTSEMEVENTINTERNAL
{
/** pthread condition. */
/** pthread mutex which protects the condition and the event state. */
/** The state of the semaphore.
* This is operated while owning mutex and using atomic updating. */
/** Number of waiters. */
};
/** Posix internal representation of a Mutex Multi semaphore.
* This must be identical to RTSEMEVENTINTERNAL! */
struct RTSEMEVENTMULTIINTERNAL
{
/** pthread condition. */
/** pthread mutex which protects the condition and the event state. */
/** The state of the semaphore.
* This is operated while owning mutex and using atomic updating. */
/** Number of waiters. */
};
/** The valus of the u32State variable in a RTSEMEVENTINTERNAL and RTSEMEVENTMULTIINTERNAL.
* @{ */
/** The object isn't initialized. */
#define EVENT_STATE_UNINITIALIZED 0
/** The semaphore is is signaled. */
#define EVENT_STATE_SIGNALED 0xff00ff00
/** The semaphore is not signaled. */
#define EVENT_STATE_NOT_SIGNALED 0x00ff00ff
/** @} */
/** Posix internal representation of a Mutex semaphore. */
struct RTSEMMUTEXINTERNAL
{
/** pthread mutex. */
/** The owner of the mutex. */
/** Nesting count. */
};
/** 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 an Event semaphore handle passed to one of the interface.
*
* @returns true if valid.
* @returns false if invalid.
* @param pIntEventSem Pointer to the event semaphore to validate.
*/
{
return false;
uint32_t u32 = pIntEventSem->u32State; /* this is volatile, so a explicit read like this is needed. */
if ( u32 != EVENT_STATE_NOT_SIGNALED
&& u32 != EVENT_STATE_SIGNALED)
return false;
return true;
}
{
int rc;
/*
* Allocate semaphore handle.
*/
struct RTSEMEVENTINTERNAL *pIntEventSem = (struct RTSEMEVENTINTERNAL *)RTMemAlloc(sizeof(struct RTSEMEVENTINTERNAL));
if (pIntEventSem)
{
/*
* Create the condition variable.
*/
if (!rc)
{
if (!rc)
{
/*
* Create the semaphore.
*/
if (!rc)
{
if (!rc)
{
return VINF_SUCCESS;
}
}
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
{
/*
* Validate handle.
*/
if (!rtsemEventValid(EventSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Abort all waiters forcing them to return failure.
*
*/
int rc;
for (int i = 30; i > 0; i--)
{
break;
usleep(1000);
if (rc)
{
return RTErrConvertFromErrno(rc);
}
/*
* Destroy the semaphore
* If it's busy we'll wait a bit to give the threads a chance to be scheduled.
*/
for (int i = 30; i > 0; i--)
{
break;
usleep(1000);
}
if (rc)
{
return RTErrConvertFromErrno(rc);
}
/*
* Free the semaphore memory and be gone.
*/
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
if (!rtsemEventValid(EventSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Lock the mutex semaphore.
*/
if (rc)
{
return RTErrConvertFromErrno(rc);
}
/*
* Check the state.
*/
{
}
{
}
else
/*
* Release the mutex and return.
*/
if (rc)
return RTErrConvertFromErrno(rc);
if (rc2)
return RTErrConvertFromErrno(rc2);
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
if (!rtsemEventValid(EventSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Timed or indefinite wait?
*/
if (cMillies == RT_INDEFINITE_WAIT)
{
/* for fairness, yield before going to sleep. */
/* take mutex */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
for (;;)
{
/* check state. */
{
return VINF_SUCCESS;
}
{
return VERR_SEM_DESTROYED;
}
/* wait */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
}
}
else
{
/*
* Get current time and calc end of wait time.
*/
#ifdef RT_OS_DARWIN
#else
#endif
if (cMillies != 0)
{
{
}
}
/* for fairness, yield before going to sleep. */
/* take mutex */
#ifdef RT_OS_DARWIN
#else
#endif
if (rc)
{
return RTErrConvertFromErrno(rc);
}
for (;;)
{
/* check state. */
{
return VINF_SUCCESS;
}
{
return VERR_SEM_DESTROYED;
}
/* wait */
if (rc && (rc != EINTR || !fAutoResume)) /* according to SuS this function shall not return EINTR, but linux man page says differently. */
{
return RTErrConvertFromErrno(rc);
}
} /* for (;;) */
}
}
{
return rc;
}
{
}
/**
* Validate an event multi semaphore handle passed to one of the interface.
*
* @returns true if valid.
* @returns false if invalid.
* @param pIntEventMultiSem Pointer to the event semaphore to validate.
*/
{
return false;
uint32_t u32 = pIntEventMultiSem->u32State; /* this is volatile, so a explicit read like this is needed. */
if ( u32 != EVENT_STATE_NOT_SIGNALED
&& u32 != EVENT_STATE_SIGNALED)
return false;
return true;
}
{
/* the code and the structure is identical with other type for this function. */
}
{
/* the code and the structure is identical with other type for this function. */
}
{
/* the code and the structure is identical with other type for this function. */
}
{
/*
* Validate input.
*/
{
return VERR_INVALID_HANDLE;
}
/*
* Lock the mutex semaphore.
*/
if (rc)
{
return RTErrConvertFromErrno(rc);
}
/*
* Check the state.
*/
/*
* Release the mutex and return.
*/
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
{
return VERR_INVALID_HANDLE;
}
/*
* Timed or indefinite wait?
*/
if (cMillies == RT_INDEFINITE_WAIT)
{
/* take mutex */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
for (;;)
{
/* check state. */
{
return VINF_SUCCESS;
}
{
return VERR_SEM_DESTROYED;
}
/* wait */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
}
}
else
{
/*
* Get current time and calc end of wait time.
*/
#ifdef RT_OS_DARWIN
#else
#endif
if (cMillies != 0)
{
{
}
}
/* take mutex */
#ifdef RT_OS_DARWIN
#else
#endif
if (rc)
{
return RTErrConvertFromErrno(rc);
}
for (;;)
{
/* check state. */
{
return VINF_SUCCESS;
}
{
return VERR_SEM_DESTROYED;
}
/* wait */
if (rc && (rc != EINTR || !fAutoResume)) /* according to SuS this function shall not return EINTR, but linux man page says differently. */
{
return RTErrConvertFromErrno(rc);
}
}
}
}
{
return rc;
}
{
}
/**
* Validate a Mutex semaphore handle passed to one of the interface.
*
* @returns true if valid.
* @returns false if invalid.
* @param pIntMutexSem Pointer to the mutex semaphore to validate.
*/
{
return false;
return false;
return true;
}
{
int rc;
/*
* Allocate semaphore handle.
*/
struct RTSEMMUTEXINTERNAL *pIntMutexSem = (struct RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(struct RTSEMMUTEXINTERNAL));
if (pIntMutexSem)
{
/*
* Create the semaphore.
*/
if (!rc)
{
if (!rc)
{
pIntMutexSem->cNesting = 0;
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
{
/*
* Validate input.
*/
if (!rtsemMutexValid(MutexSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Try destroy it.
*/
if (rc)
{
return RTErrConvertFromErrno(rc);
}
/*
* Free the memory and be gone.
*/
pIntMutexSem->cNesting = ~0;
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
if (!rtsemMutexValid(MutexSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Check if nested request.
*/
&& pIntMutexSem->cNesting > 0)
{
pIntMutexSem->cNesting++;
return VINF_SUCCESS;
}
/*
* Lock it.
*/
if (cMillies == RT_INDEFINITE_WAIT)
{
/* take mutex */
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 mutex */
if (rc)
{
return RTErrConvertFromErrno(rc);
}
#endif /* !RT_OS_DARWIN */
}
/*
* Set the owner and nesting.
*/
return VINF_SUCCESS;
}
{
/* EINTR isn't returned by the wait functions we're using. */
}
{
/*
* Validate input.
*/
if (!rtsemMutexValid(MutexSem))
{
return VERR_INVALID_HANDLE;
}
/*
* Check if nested.
*/
{
AssertMsgFailed(("Not owner of mutex %p!! Self=%08x Owner=%08x cNesting=%d\n",
return VERR_NOT_OWNER;
}
/*
* If nested we'll just pop a nesting.
*/
{
pIntMutexSem->cNesting--;
return VINF_SUCCESS;
}
/*
* Clear the state. (cNesting == 1)
*/
/*
* Unlock mutex semaphore.
*/
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}
/**
* 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 */
}
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.
*/
AssertMsg(sizeof(pthread_t) == sizeof(void *), ("pthread_t is not the size of a pointer but %d bytes\n", sizeof(pthread_t)));
if (rc)
{
return RTErrConvertFromErrno(rc);
}
return VINF_SUCCESS;
}