semevent-r0drv-solaris.c revision 56665b35e85d3cd7d4232957d09b488ab44d81cf
86b51be433c7032578ae50e94ebe590cc428448bvboxsync/* $Id$ */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync/** @file
86b51be433c7032578ae50e94ebe590cc428448bvboxsync * innotek Portable Runtime - Semaphores, Ring-0 Driver, Solaris.
86b51be433c7032578ae50e94ebe590cc428448bvboxsync */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync/*
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2007 innotek GmbH
86b51be433c7032578ae50e94ebe590cc428448bvboxsync *
86b51be433c7032578ae50e94ebe590cc428448bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
86b51be433c7032578ae50e94ebe590cc428448bvboxsync * available from http://www.virtualbox.org. This file is free software;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License as published by the Free Software Foundation,
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync */
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync/*******************************************************************************
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync* Header Files *
86b51be433c7032578ae50e94ebe590cc428448bvboxsync*******************************************************************************/
86b51be433c7032578ae50e94ebe590cc428448bvboxsync#include "the-solaris-kernel.h"
86b51be433c7032578ae50e94ebe590cc428448bvboxsync#include <time.h>
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync#include <iprt/semaphore.h>
86b51be433c7032578ae50e94ebe590cc428448bvboxsync#include <iprt/alloc.h>
86b51be433c7032578ae50e94ebe590cc428448bvboxsync#include <iprt/assert.h>
86b51be433c7032578ae50e94ebe590cc428448bvboxsync#include <iprt/asm.h>
86b51be433c7032578ae50e94ebe590cc428448bvboxsync#include <iprt/err.h>
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync#include "internal/magics.h"
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync/*******************************************************************************
86b51be433c7032578ae50e94ebe590cc428448bvboxsync* Structures and Typedefs *
86b51be433c7032578ae50e94ebe590cc428448bvboxsync*******************************************************************************/
86b51be433c7032578ae50e94ebe590cc428448bvboxsync/**
86b51be433c7032578ae50e94ebe590cc428448bvboxsync * Solaris event semaphore.
86b51be433c7032578ae50e94ebe590cc428448bvboxsync */
86b51be433c7032578ae50e94ebe590cc428448bvboxsynctypedef struct RTSEMEVENTINTERNAL
86b51be433c7032578ae50e94ebe590cc428448bvboxsync{
86b51be433c7032578ae50e94ebe590cc428448bvboxsync /** Magic value (RTSEMEVENT_MAGIC). */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync uint32_t volatile u32Magic;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync /** The number of waiting threads. */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync uint32_t volatile cWaiters;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync /** Set if the event object is signaled. */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync uint8_t volatile fSignaled;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync /** The number of threads in the process of waking up. */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync uint32_t volatile cWaking;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync /** The Solaris mutex protecting this structure and pairing up the with the cv. */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync kmutex_t Mtx;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync /** The Solaris condition variable. */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync kcondvar_t Cnd;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
86b51be433c7032578ae50e94ebe590cc428448bvboxsync{
86b51be433c7032578ae50e94ebe590cc428448bvboxsync Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
86b51be433c7032578ae50e94ebe590cc428448bvboxsync AssertPtrReturn(pEventSem, VERR_INVALID_POINTER);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
86b51be433c7032578ae50e94ebe590cc428448bvboxsync if (pEventInt)
86b51be433c7032578ae50e94ebe590cc428448bvboxsync {
86b51be433c7032578ae50e94ebe590cc428448bvboxsync pEventInt->u32Magic = RTSEMEVENT_MAGIC;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync pEventInt->cWaiters = 0;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync pEventInt->cWaking = 0;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync pEventInt->fSignaled = 0;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync mutex_init(&pEventInt->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, NULL);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync cv_init(&pEventInt->Cnd, "IPRT CV", CV_DRIVER, NULL);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync *pEventSem = pEventInt;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync return VINF_SUCCESS;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync }
86b51be433c7032578ae50e94ebe590cc428448bvboxsync return VERR_NO_MEMORY;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync}
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsyncRTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
86b51be433c7032578ae50e94ebe590cc428448bvboxsync{
86b51be433c7032578ae50e94ebe590cc428448bvboxsync if (EventSem == NIL_RTSEMEVENT)
86b51be433c7032578ae50e94ebe590cc428448bvboxsync return VERR_INVALID_HANDLE;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
86b51be433c7032578ae50e94ebe590cc428448bvboxsync ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
86b51be433c7032578ae50e94ebe590cc428448bvboxsync VERR_INVALID_HANDLE);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync mutex_enter(&pEventInt->Mtx);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync ASMAtomicIncU32(&pEventInt->u32Magic); /* make the handle invalid */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync if (pEventInt->cWaiters > 0)
86b51be433c7032578ae50e94ebe590cc428448bvboxsync {
86b51be433c7032578ae50e94ebe590cc428448bvboxsync /* abort waiting thread, last man cleans up. */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync ASMAtomicXchgU32(&pEventInt->cWaking, pEventInt->cWaking + pEventInt->cWaiters);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync cv_broadcast(&pEventInt->Cnd);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync mutex_exit(&pEventInt->Mtx);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync }
86b51be433c7032578ae50e94ebe590cc428448bvboxsync else if (pEventInt->cWaking)
86b51be433c7032578ae50e94ebe590cc428448bvboxsync {
78edef1df590edefed7cef91b6bcbf4d84f79947vboxsync /* the last waking thread is gonna do the cleanup */
86b51be433c7032578ae50e94ebe590cc428448bvboxsync mutex_exit(&pEventInt->Mtx);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync }
78edef1df590edefed7cef91b6bcbf4d84f79947vboxsync else
86b51be433c7032578ae50e94ebe590cc428448bvboxsync {
86b51be433c7032578ae50e94ebe590cc428448bvboxsync mutex_exit(&pEventInt->Mtx);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync cv_destroy(&pEventInt->Cnd);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync mutex_destroy(&pEventInt->Mtx);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync RTMemFree(pEventInt);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync }
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync return VINF_SUCCESS;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync}
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsyncRTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
86b51be433c7032578ae50e94ebe590cc428448bvboxsync{
86b51be433c7032578ae50e94ebe590cc428448bvboxsync PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
86b51be433c7032578ae50e94ebe590cc428448bvboxsync AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
86b51be433c7032578ae50e94ebe590cc428448bvboxsync ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
86b51be433c7032578ae50e94ebe590cc428448bvboxsync VERR_INVALID_HANDLE);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync mutex_enter(&pEventInt->Mtx);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync
86b51be433c7032578ae50e94ebe590cc428448bvboxsync if (pEventInt->cWaiters > 0)
86b51be433c7032578ae50e94ebe590cc428448bvboxsync {
86b51be433c7032578ae50e94ebe590cc428448bvboxsync ASMAtomicDecU32(&pEventInt->cWaiters);
86b51be433c7032578ae50e94ebe590cc428448bvboxsync ASMAtomicIncU32(&pEventInt->cWaking);
cv_signal(&pEventInt->Cnd);
}
else
ASMAtomicXchgU8(&pEventInt->fSignaled, true);
mutex_exit(&pEventInt->Mtx);
return VINF_SUCCESS;
}
static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
{
int rc;
PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
VERR_INVALID_HANDLE);
mutex_enter(&pEventInt->Mtx);
if (pEventInt->fSignaled)
{
Assert(!pEventInt->cWaiters);
ASMAtomicXchgU8(&pEventInt->fSignaled, false);
rc = VINF_SUCCESS;
}
else
{
ASMAtomicIncU32(&pEventInt->cWaiters);
/*
* Translate milliseconds into ticks and go to sleep.
*/
if (cMillies != RT_INDEFINITE_WAIT)
{
int cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
clock_t timeout = ddi_get_lbolt();
timeout += cTicks;
if (fInterruptible)
rc = cv_timedwait_sig(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
else
rc = cv_timedwait(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
}
else
{
if (fInterruptible)
rc = cv_wait_sig(&pEventInt->Cnd, &pEventInt->Mtx);
else
{
cv_wait(&pEventInt->Cnd, &pEventInt->Mtx);
rc = 1;
}
}
if (rc > 0)
{
/* Retured due to call to cv_signal() or cv_broadcast() */
if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
{
rc = VERR_SEM_DESTROYED;
if (!ASMAtomicDecU32(&pEventInt->cWaking))
{
mutex_exit(&pEventInt->Mtx);
cv_destroy(&pEventInt->Cnd);
mutex_destroy(&pEventInt->Mtx);
RTMemFree(pEventInt);
return rc;
}
}
ASMAtomicDecU32(&pEventInt->cWaking);
rc = VINF_SUCCESS;
}
else if (rc == -1)
{
/* Returned due to timeout being reached */
if (pEventInt->cWaiters > 0)
ASMAtomicDecU32(&pEventInt->cWaiters);
rc = VERR_TIMEOUT;
}
else
{
/* Returned due to pending signal */
if (pEventInt->cWaiters > 0)
ASMAtomicDecU32(&pEventInt->cWaiters);
rc = VERR_INTERRUPTED;
}
}
mutex_exit(&pEventInt->Mtx);
return rc;
}
RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
{
return rtSemEventWait(EventSem, cMillies, false /* not interruptible */);
}
RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
{
return rtSemEventWait(EventSem, cMillies, true /* interruptible */);
}