semevent-r0drv-solaris.c revision 175770d4533d92ff4ef576d5ea97e566cc488472
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/* $Id$ */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/** @file
00fa3bc989ffe84474e828c8b90b24284dcfdf0cvboxsync * IPRT - Single Release Event Semaphores, Ring-0 Driver, Solaris.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/*
00fa3bc989ffe84474e828c8b90b24284dcfdf0cvboxsync * Copyright (C) 2006-2010 Oracle Corporation
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * available from http://www.virtualbox.org. This file is free software;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * you can redistribute it and/or modify it under the terms of the GNU
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * General Public License (GPL) as published by the Free Software
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * The contents of this file may alternatively be used under the terms
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * of the Common Development and Distribution License Version 1.0
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * VirtualBox OSE distribution, in which case the provisions of the
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * CDDL are applicable instead of those of the GPL.
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync *
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * You may elect to license modified versions of this file under the
0c437bb10c61b229407a7517efde04dfe3b1e4a1vboxsync * terms and conditions of either the GPL or the CDDL or both.
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync */
0c437bb10c61b229407a7517efde04dfe3b1e4a1vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/*******************************************************************************
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync* Header Files *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync*******************************************************************************/
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#define RTSEMEVENT_WITHOUT_REMAPPING
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include "the-solaris-kernel.h"
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include "internal/iprt.h"
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <iprt/semaphore.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <iprt/assert.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <iprt/asm.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync# include <iprt/asm-amd64-x86.h>
652d85a9390f54d4c6eca560340bf67ac1f85c9dvboxsync#endif
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <iprt/err.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <iprt/list.h>
9347f1987dfb760943aba5a9ef094c6066901be3vboxsync#include <iprt/lockvalidator.h>
9347f1987dfb760943aba5a9ef094c6066901be3vboxsync#include <iprt/mem.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include <iprt/mp.h>
9347f1987dfb760943aba5a9ef094c6066901be3vboxsync#include <iprt/thread.h>
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include "internal/magics.h"
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync#include "semeventwait-r0drv-solaris.h"
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/*******************************************************************************
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync* Structures and Typedefs *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync*******************************************************************************/
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/**
addc480d0d7650db6323467bbdab6c21836a2928vboxsync * Waiter entry. Lives on the stack.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * @remarks Unfortunately, we cannot easily use cv_signal because we cannot
9347f1987dfb760943aba5a9ef094c6066901be3vboxsync * distinguish between it and the spurious wakeups we get after fork.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * So, we keep an unprioritized FIFO with the sleeping threads.
9347f1987dfb760943aba5a9ef094c6066901be3vboxsync */
9347f1987dfb760943aba5a9ef094c6066901be3vboxsynctypedef struct RTSEMEVENTSOLENTRY
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync{
1999ae03c34840fa4d712fd2e020120b2cb7182avboxsync /** The list node. */
9347f1987dfb760943aba5a9ef094c6066901be3vboxsync RTLISTNODE Node;
1999ae03c34840fa4d712fd2e020120b2cb7182avboxsync /** The thread. */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync kthread_t *pThread;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync /** Flag set when waking up the thread by signal or destroy. */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync bool volatile fWokenUp;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync} RTSEMEVENTSOLENTRY;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/** Pointer to waiter entry. */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsynctypedef RTSEMEVENTSOLENTRY *PRTSEMEVENTSOLENTRY;
faa31dfcb46e5e2fb9c2bf224d113a0ca136ddecvboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/**
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * Solaris event semaphore.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync */
1999ae03c34840fa4d712fd2e020120b2cb7182avboxsynctypedef struct RTSEMEVENTINTERNAL
2fb42335a74fe26c4e2bccbf16f077015e42a5e1vboxsync{
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync /** Magic value (RTSEMEVENT_MAGIC). */
d89d64f261b0fecda692038bc6351dedf996c563vboxsync uint32_t volatile u32Magic;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync /** The number of threads referencing this object. */
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync uint32_t volatile cRefs;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync /** Set if the object is signalled when there are no waiters. */
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync bool fSignaled;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync /** List of waiting and woken up threads. */
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync RTLISTNODE WaitList;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync /** The Solaris mutex protecting this structure and pairing up the with the cv. */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync kmutex_t Mtx;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync /** The Solaris condition variable. */
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync kcondvar_t Cnd;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync{
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync}
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsyncRTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync{
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
addc480d0d7650db6323467bbdab6c21836a2928vboxsync AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
addc480d0d7650db6323467bbdab6c21836a2928vboxsync RT_ASSERT_PREEMPTIBLE();
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
addc480d0d7650db6323467bbdab6c21836a2928vboxsync if (!pThis)
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync return VERR_NO_MEMORY;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync pThis->u32Magic = RTSEMEVENT_MAGIC;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync pThis->cRefs = 1;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync pThis->fSignaled = false;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync RTListInit(&pThis->WaitList);
addc480d0d7650db6323467bbdab6c21836a2928vboxsync mutex_init(&pThis->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync *phEventSem = pThis;
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync return VINF_SUCCESS;
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync}
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync/**
faa31dfcb46e5e2fb9c2bf224d113a0ca136ddecvboxsync * Retain a reference to the semaphore.
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync *
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync * @param pThis The semaphore.
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync */
faa31dfcb46e5e2fb9c2bf224d113a0ca136ddecvboxsyncDECLINLINE(void) rtR0SemEventSolRetain(PRTSEMEVENTINTERNAL pThis)
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync{
8de98485a60eb742b1d720a8cb9747dc03e821a8vboxsync uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync Assert(cRefs && cRefs < 100000);
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync NOREF(cRefs);
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync}
ec21c063515d0931111f0c1f8d6f4bc8e7a6c882vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync/**
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * The destruct.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync *
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync * @param pThis The semaphore.
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync */
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsyncstatic void rtR0SemEventSolDtor(PRTSEMEVENTINTERNAL pThis)
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync{
4ace9bce00c6d792391a0e064131716659e3a922vboxsync Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
760446f710619a9daa6cedc7f0601f49e4ea3442vboxsync cv_destroy(&pThis->Cnd);
mutex_destroy(&pThis->Mtx);
RTMemFree(pThis);
}
/**
* Release a reference, destroy the thing if necessary.
*
* @param pThis The semaphore.
*/
DECLINLINE(void) rtR0SemEventSolRelease(PRTSEMEVENTINTERNAL pThis)
{
if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
rtR0SemEventSolDtor(pThis);
}
RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
{
/*
* Validate input.
*/
PRTSEMEVENTINTERNAL pThis = hEventSem;
if (pThis == NIL_RTSEMEVENT)
return VINF_SUCCESS;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
Assert(pThis->cRefs > 0);
RT_ASSERT_INTS_ON();
mutex_enter(&pThis->Mtx);
/*
* Invalidate the semaphore.
*/
ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
ASMAtomicWriteBool(&pThis->fSignaled, false);
/*
* Abort and wake up all threads.
*/
PRTSEMEVENTSOLENTRY pWaiter;
RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTSOLENTRY, Node)
{
pWaiter->fWokenUp = true;
}
cv_broadcast(&pThis->Cnd);
/*
* Release the reference from RTSemEventCreateEx.
*/
mutex_exit(&pThis->Mtx);
rtR0SemEventSolRelease(pThis);
return VINF_SUCCESS;
}
RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
{
PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
RT_ASSERT_PREEMPT_CPUID_VAR();
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
RT_ASSERT_INTS_ON();
rtR0SemEventSolRetain(pThis);
rtR0SemSolWaitEnterMutexWithUnpinningHack(&pThis->Mtx);
/*
* Wake up one thread.
*/
ASMAtomicWriteBool(&pThis->fSignaled, true);
PRTSEMEVENTSOLENTRY pWaiter;
RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTSOLENTRY, Node)
{
if (!pWaiter->fWokenUp)
{
pWaiter->fWokenUp = true;
setrun(pWaiter->pThread);
ASMAtomicWriteBool(&pThis->fSignaled, false);
break;
}
}
mutex_exit(&pThis->Mtx);
rtR0SemEventSolRelease(pThis);
RT_ASSERT_PREEMPT_CPUID();
return VINF_SUCCESS;
}
/**
* Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
*
* @returns VBox status code.
* @param pThis The event semaphore.
* @param fFlags See RTSemEventWaitEx.
* @param uTimeout See RTSemEventWaitEx.
* @param pSrcPos The source code position of the wait.
*/
static int rtR0SemEventSolWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
PCRTLOCKVALSRCPOS pSrcPos)
{
/*
* Validate the input.
*/
AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
rtR0SemEventSolRetain(pThis);
mutex_enter(&pThis->Mtx);
/*
* In the signaled state?
*/
int rc;
if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
rc = VINF_SUCCESS;
else
{
/*
* We have to wait.
*/
RTR0SEMSOLWAIT Wait;
rc = rtR0SemSolWaitInit(&Wait, fFlags, uTimeout);
if (RT_SUCCESS(rc))
{
RTSEMEVENTSOLENTRY Waiter; /* ASSUMES we won't get swapped out while waiting (TS_DONT_SWAP). */
Waiter.pThread = curthread;
Waiter.fWokenUp = false;
RTListAppend(&pThis->WaitList, &Waiter.Node);
for (;;)
{
/* The destruction test. */
if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
rc = VERR_SEM_DESTROYED;
else
{
/* Check the exit conditions. */
if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
rc = VERR_SEM_DESTROYED;
else if (Waiter.fWokenUp)
rc = VINF_SUCCESS;
else if (rtR0SemSolWaitHasTimedOut(&Wait))
rc = VERR_TIMEOUT;
else if (rtR0SemSolWaitWasInterrupted(&Wait))
rc = VERR_INTERRUPTED;
else
{
/* Do the wait and then recheck the conditions. */
rtR0SemSolWaitDoIt(&Wait, &pThis->Cnd, &pThis->Mtx);
continue;
}
}
break;
}
rtR0SemSolWaitDelete(&Wait);
RTListNodeRemove(&Waiter.Node);
}
}
mutex_exit(&pThis->Mtx);
rtR0SemEventSolRelease(pThis);
return rc;
}
RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
{
#ifndef RTSEMEVENT_STRICT
return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, NULL);
#else
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
#endif
}
RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
}
RTDECL(uint32_t) RTSemEventGetResolution(void)
{
return rtR0SemSolWaitGetResolution();
}