semeventmulti-r0drv-solaris.c revision 0b7d24ee33cf513ef5a83fc9d6b9ba0a0427b39c
/* $Id$ */
/** @file
* IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Solaris.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* 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 "the-solaris-kernel.h"
#include <iprt/semaphore.h>
#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
# include <iprt/asm-amd64-x86.h>
#endif
#include <iprt/lockvalidator.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @name fStateAndGen values
* @{ */
/** The state bit number. */
#define RTSEMEVENTMULTISOL_STATE_BIT 0
/** The state mask. */
/** The generation mask. */
/** The generation shift. */
#define RTSEMEVENTMULTISOL_GEN_SHIFT 1
/** The initial variable value. */
/** @} */
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Solaris multiple release event semaphore.
*/
typedef struct RTSEMEVENTMULTIINTERNAL
{
/** Magic value (RTSEMEVENTMULTI_MAGIC). */
/** The number of references. */
/** The object state bit and generation counter.
* The generation counter is incremented every time the object is
* signalled. */
uint32_t volatile fStateAndGen;
/** The Solaris mutex protecting this structure and pairing up the with the cv. */
/** The Solaris condition variable. */
{
}
RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
const char *pszNameFmt, ...)
{
AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
if (pThis)
{
mutex_init(&pThis->Mtx, "IPRT Multiple Release Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
*phEventMultiSem = pThis;
return VINF_SUCCESS;
}
return VERR_NO_MEMORY;
}
/**
* Retain a reference to the semaphore.
*
* @param pThis The semaphore.
*/
{
}
/**
* Destructor that is called when cRefs == 0.
*
* @param pThis The instance to destroy.
*/
{
}
/**
* Release a reference, destroy the thing if necessary.
*
* @param pThis The semaphore.
*/
{
}
{
if (pThis == NIL_RTSEMEVENTMULTI)
return VINF_SUCCESS;
AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
AssertMsgReturn(pThis->cRefs > 0, ("pThis=%p cRefs=%d\n", pThis, pThis->cRefs), VERR_INVALID_HANDLE);
/* Invalidate the handle and wake up all threads that might be waiting on the semaphore. */
/* Drop the reference from RTSemEventMultiCreateEx. */
return VINF_SUCCESS;
}
{
/*
* If we're in interrupt context we need to unpin the underlying current
* thread as this could lead to a deadlock (see #4259 for the full explanation)
*
* Note! See remarks about preemption in RTSemEventSignal.
*/
if (!fAcquired)
{
{
preempt();
}
}
/*
* Do the job.
*/
return VINF_SUCCESS;
}
{
/*
* If we're in interrupt context we need to unpin the underlying current
* thread as this could lead to a deadlock (see #4259 for the full explanation)
*
* Note! See remarks about preemption in RTSemEventSignal.
*/
if (!fAcquired)
{
{
preempt();
}
}
/*
* Do the job (could be done without the lock, but play safe).
*/
return VINF_SUCCESS;
}
/* -------- Move to header ---------- */
typedef struct RTR0SEMSOLWAIT
{
/** The absolute timeout given as nano seconds since the start of the
* monotonic clock. */
/** The timeout in nano seconds relative to the start of the wait. */
/** The native timeout value. */
union
{
/** The timeout (abs lbolt) when fHighRes is false. */
} u;
/** Set if we use high resolution timeouts. */
bool fHighRes;
/** Set if it's an indefinite wait. */
bool fIndefinite;
/** Set if we've already timed out.
* Set by rtR0SemSolWaitDoIt or rtR0SemSolWaitHighResTimeout, read by
* rtR0SemSolWaitHasTimedOut. */
bool volatile fTimedOut;
/** Whether the wait was interrupted. */
bool fInterrupted;
/** Interruptible or uninterruptible wait. */
bool fInterruptible;
/** The thread to wake up. */
/** Cylic timer ID (used by the timeout callback). */
/** Pointer to a solaris semaphore wait structure. */
typedef RTR0SEMSOLWAIT *PRTR0SEMSOLWAIT;
/**
* Initializes a wait.
*
* The caller MUST check the wait condition BEFORE calling this function or the
* timeout logic will be flawed.
*
* @returns VINF_SUCCESS or VERR_TIMEOUT.
* @param pWait The wait structure.
* @param fFlags The wait flags.
* @param uTimeout The timeout.
* @param pWaitQueue The wait queue head.
*/
{
/*
* Process the flags and timeout.
*/
if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
{
if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
: UINT64_MAX;
if (uTimeout == UINT64_MAX)
else
{
if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
{
if (uTimeout == 0)
return VERR_TIMEOUT;
u64Now = RTTimeSystemNanoTS();
}
else
{
u64Now = RTTimeSystemNanoTS();
return VERR_TIMEOUT;
}
}
}
if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
{
pWait->fIndefinite = false;
else
{
#if 1
#else
#endif
else
{
}
}
}
if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
{
pWait->fIndefinite = true;
}
pWait->fInterrupted = false;
return VINF_SUCCESS;
}
/**
* Cyclic timeout callback that sets the timeout indicator and wakes up the
* waiting thread.
*
* @param pvUser The wait structure.
*/
static void rtR0SemSolWaitHighResTimeout(void *pvUser)
{
{
/* Note: Trying to take the cpu_lock here doesn't work. */
{
}
}
}
/**
* Do the actual wait.
*
* @param pWait The wait structure.
* @param pCnd The condition variable to wait on.
* @param pMtx The mutex related to the condition variable.
* The caller has entered this.
*/
{
int rc = 1;
if (pWait->fIndefinite)
{
/*
* No timeout - easy.
*/
if (pWait->fInterruptible)
else
}
{
/*
* High resolution timeout - arm a one-shot cyclic for waking up
* the thread at the desired time.
*/
if (pWait->fInterruptible)
else
{
}
}
else
{
/*
* Normal timeout.
*/
if (pWait->fInterruptible)
else
}
/* Above zero means normal wake-up. */
if (rc > 0)
return;
/* Timeout is signalled by -1. */
if (rc == -1)
/* Interruption is signalled by 0. */
else
{
pWait->fInterrupted = true;
}
}
/**
* Checks if a solaris wait was interrupted.
*
* @returns true / false
* @param pWait The wait structure.
* @remarks This shall be called before the first rtR0SemSolWaitDoIt().
*/
{
return pWait->fInterrupted;
}
/**
* Checks if a solaris wait has timed out.
*
* @returns true / false
* @param pWait The wait structure.
*/
{
}
/**
* Deletes a solaris wait.
*
* @param pWait The wait structure.
*/
{
}
/* -------- End ---------- */
/**
* Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
*
* @returns VBox status code.
* @param pThis The event semaphore.
* @param fFlags See RTSemEventMultiWaitEx.
* @param uTimeout See RTSemEventMultiWaitEx.
* @param pSrcPos The source code position of the wait.
*/
static int rtR0SemEventMultiSolWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
{
int rc;
/*
* Validate the input.
*/
AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
/*
* Is the event already signalled or do we have to wait?
*/
rc = VINF_SUCCESS;
else
{
/*
* We have to wait.
*/
if (RT_SUCCESS(rc))
{
for (;;)
{
/* The destruction test. */
else
{
/* Check the exit conditions. */
rc = VINF_SUCCESS;
else if (rtR0SemSolWaitHasTimedOut(&Wait))
rc = VERR_TIMEOUT;
else if (rtR0SemSolWaitWasInterrupted(&Wait))
else
{
/* Do the wait and then recheck the conditions. */
continue;
}
}
break;
}
}
}
return rc;
}
RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
{
#ifndef RTSEMEVENT_STRICT
#else
#endif
}
RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
{
}