semeventwait-r0drv-solaris.h revision 4030b8473736055cd02947418ef4f8f9b1945323
844cd568b34737db015068632c9131c25f9b6578vboxsync * IPRT - Solaris Ring-0 Driver Helpers for Event Semaphore Waits.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * Copyright (C) 2006-2011 Oracle Corporation
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * available from http://www.virtualbox.org. This file is free software;
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * you can redistribute it and/or modify it under the terms of the GNU
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * General Public License (GPL) as published by the Free Software
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * The contents of this file may alternatively be used under the terms
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * of the Common Development and Distribution License Version 1.0
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * VirtualBox OSE distribution, in which case the provisions of the
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * CDDL are applicable instead of those of the GPL.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * You may elect to license modified versions of this file under the
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * terms and conditions of either the GPL or the CDDL or both.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync#ifndef ___r0drv_solaris_semeventwait_r0drv_solaris_h
3bad884471a4755d52abffc98cc326f153750ca1vboxsync#define ___r0drv_solaris_semeventwait_r0drv_solaris_h
0177e4f8d16e61ee5c9858c5ea30ca90e2ed41a3vboxsync/** The resolution (nanoseconds) specified when using timeout_generic. */
4030b8473736055cd02947418ef4f8f9b1945323vboxsync/** Disables the cyclic fallback code for old S10 installs - see @bugref{5342}.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * @todo Fixed by @bugref{5595}, can be reenabled after checking out
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * CY_HIGH_LEVEL. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Solaris semaphore wait structure.
0177e4f8d16e61ee5c9858c5ea30ca90e2ed41a3vboxsync /** The absolute timeout given as nanoseconds since the start of the
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * monotonic clock. */
0177e4f8d16e61ee5c9858c5ea30ca90e2ed41a3vboxsync /** The timeout in nanoseconds relative to the start of the wait. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** The native timeout value. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** The timeout (abs lbolt) when fHighRes is false. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** Set if we use high resolution timeouts. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** Set if it's an indefinite wait. */
4030b8473736055cd02947418ef4f8f9b1945323vboxsync /** Set if the waiting thread is ready to be woken up.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * Avoids false setrun() calls due to temporary mutex exits. */
4030b8473736055cd02947418ef4f8f9b1945323vboxsync bool volatile fWantWakeup;
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** Set if we've already timed out.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Set by rtR0SemSolWaitDoIt or rtR0SemSolWaitHighResTimeout, read by
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * rtR0SemSolWaitHasTimedOut. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync bool volatile fTimedOut;
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** Whether the wait was interrupted. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** Interruptible or uninterruptible wait. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** The thread to wake up. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync /** Cylic timer ID (used by the timeout callback). */
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync /** The mutex associated with the condition variable wait. */
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync void volatile *pvMtx;
3bad884471a4755d52abffc98cc326f153750ca1vboxsync/** Pointer to a solaris semaphore wait structure. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Initializes a wait.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * The caller MUST check the wait condition BEFORE calling this function or the
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * timeout logic will be flawed.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @returns VINF_SUCCESS or VERR_TIMEOUT.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pWait The wait structure.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param fFlags The wait flags.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param uTimeout The timeout.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pWaitQueue The wait queue head.
3bad884471a4755d52abffc98cc326f153750ca1vboxsyncDECLINLINE(int) rtR0SemSolWaitInit(PRTR0SEMSOLWAIT pWait, uint32_t fFlags, uint64_t uTimeout)
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Process the flags and timeout.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
81631dabcaa76e2e48221548b08c71bd7ed6f2ecvboxsync if ( ( (fFlags & (RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_ABSOLUTE))
81631dabcaa76e2e48221548b08c71bd7ed6f2ecvboxsync || pWait->cNsRelTimeout < UINT32_C(1000000000) / 100 /*Hz*/ * 4)
3bad884471a4755d52abffc98cc326f153750ca1vboxsync pWait->fInterruptible = !!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Cyclic timeout callback that sets the timeout indicator and wakes up the
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * waiting thread.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pvUser The wait structure.
3bad884471a4755d52abffc98cc326f153750ca1vboxsyncstatic void rtR0SemSolWaitHighResTimeout(void *pvUser)
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync kmutex_t *pMtx = (kmutex_t *)ASMAtomicReadPtr(&pWait->pvMtx);
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync /* Enter the mutex here to make sure the thread has gone to sleep
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync before we wake it up.
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync Note: Trying to take the cpu_lock here doesn't work. */
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * Timeout callback that sets the timeout indicator and wakes up the waiting
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * @param pvUser The wait structure.
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync kmutex_t *pMtx = (kmutex_t *)ASMAtomicReadPtr(&pWait->pvMtx);
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync /* Enter the mutex here to make sure the thread has gone to sleep
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync before we wake it up. */
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Do the actual wait.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pWait The wait structure.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pCnd The condition variable to wait on.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pMtx The mutex related to the condition variable.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * The caller has entered this.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * @param pfState The state variable to check if have changed
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * after leaving the mutex (spinlock).
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * @param fCurState The current value of @a pfState. We'll return
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * without sleeping if @a pfState doesn't hold
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * this value after reacquiring the mutex.
3023311f6627a7ef580ad6d7b5f01f82c0e8f105vboxsync * @remarks This must be call with the object mutex (spinlock) held.
4030b8473736055cd02947418ef4f8f9b1945323vboxsyncDECLINLINE(void) rtR0SemSolWaitDoIt(PRTR0SEMSOLWAIT pWait, kcondvar_t *pCnd, kmutex_t *pMtx,
4030b8473736055cd02947418ef4f8f9b1945323vboxsync uint32_t volatile *pfState, uint32_t const fCurState)
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * Arm the timeout callback.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * We will have to leave the mutex (spinlock) when doing this because S10
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * (didn't check S11) will not correctly preserve PIL across calls to
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * timeout_generic() - @bugref{5595}. We do it for all timeout methods to
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * be on the safe side, the nice sideeffect of which is that it solves the
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * lock inversion problem found in @bugref{5342}.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync pWait->fWantWakeup = false; /* only want fTimedOut */
f596d8083ef5681ce0688150d1bc02b66feb0081vboxsync ASMAtomicWritePtr(&pWait->pvMtx, pMtx); /* atomic is paranoia */
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * High resolution timeout - arm a high resolution timeout callback
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * for waking up the thread at the desired time.
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync u.idCo = g_pfnrtR0Sol_timeout_generic(CALLOUT_REALTIME, rtR0SemSolWaitTimeout, pWait,
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * High resolution timeout - arm a one-shot cyclic for waking up
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * the thread at the desired time.
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync Cyh.cyh_level = CY_LOW_LEVEL; /// @todo try CY_LOCK_LEVEL and CY_HIGH_LEVEL?
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * Normal timeout.
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * We're better off with our own callback like on the timeout man page,
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * than calling cv_timedwait[_sig]().
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync u.idTom = realtime_timeout(rtR0SemSolWaitTimeout, pWait, pWait->u.lTimeout);
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * Reacquire the mutex and check if the sleep condition still holds and
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * that we didn't already time out.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync fGoToSleep = !ASMAtomicUoReadBool(&pWait->fTimedOut)
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * Do the waiting if that's still desirable.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * (rc > 0 - normal wake-up; rc == 0 - interruption; rc == -1 - timeout)
3023311f6627a7ef580ad6d7b5f01f82c0e8f105vboxsync AssertMsgFailed(("rc=%d\n", rc)); /* no timeouts, see above! */
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync * Remove the timeout callback. Drop the lock while we're doing that
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * to reduce lock contention / deadlocks. Before dropping the lock,
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * indicate that the callback shouldn't do anything.
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * (Too bad we are stuck with the cv_* API here, it's doing a little
4030b8473736055cd02947418ef4f8f9b1945323vboxsync * bit too much.)
5eb8c49f193655f19bb89b653df8643091fa987bvboxsync g_pfnrtR0Sol_untimeout_generic(u.idCo, 0 /*nowait*/);
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Checks if a solaris wait was interrupted.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @returns true / false
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pWait The wait structure.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @remarks This shall be called before the first rtR0SemSolWaitDoIt().
3bad884471a4755d52abffc98cc326f153750ca1vboxsyncDECLINLINE(bool) rtR0SemSolWaitWasInterrupted(PRTR0SEMSOLWAIT pWait)
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Checks if a solaris wait has timed out.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @returns true / false
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pWait The wait structure.
3bad884471a4755d52abffc98cc326f153750ca1vboxsyncDECLINLINE(bool) rtR0SemSolWaitHasTimedOut(PRTR0SEMSOLWAIT pWait)
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Deletes a solaris wait.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pWait The wait structure.
3bad884471a4755d52abffc98cc326f153750ca1vboxsyncDECLINLINE(void) rtR0SemSolWaitDelete(PRTR0SEMSOLWAIT pWait)
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Enters the mutex, unpinning the underlying current thread if contended and
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * we're on an interrupt thread.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * The unpinning is done to prevent a deadlock, see s this could lead to a
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * deadlock (see #4259 for the full explanation)
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * @param pMtx The mutex to enter.
3bad884471a4755d52abffc98cc326f153750ca1vboxsyncDECLINLINE(void) rtR0SemSolWaitEnterMutexWithUnpinningHack(kmutex_t *pMtx)
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * Note! This assumes nobody is using the RTThreadPreemptDisable in an
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * interrupt context and expects it to work right. The swtch will
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * result in a voluntary preemption. To fix this, we would have to
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * do our own counting in RTThreadPreemptDisable/Restore like we do
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * on systems which doesn't do preemption (OS/2, linux, ...) and
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * check whether preemption was disabled via RTThreadPreemptDisable
3bad884471a4755d52abffc98cc326f153750ca1vboxsync * or not and only call swtch if RTThreadPreemptDisable wasn't called.
3bad884471a4755d52abffc98cc326f153750ca1vboxsync RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
0177e4f8d16e61ee5c9858c5ea30ca90e2ed41a3vboxsync * Gets the max resolution of the timeout machinery.
0177e4f8d16e61ee5c9858c5ea30ca90e2ed41a3vboxsync * @returns Resolution specified in nanoseconds.