05afe08870681beb0792f384475077c988916762vboxsync * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Darwin.
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2011 Oracle Corporation
05afe08870681beb0792f384475077c988916762vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
05afe08870681beb0792f384475077c988916762vboxsync * available from http://www.virtualbox.org. This file is free software;
05afe08870681beb0792f384475077c988916762vboxsync * you can redistribute it and/or modify it under the terms of the GNU
05afe08870681beb0792f384475077c988916762vboxsync * General Public License (GPL) as published by the Free Software
05afe08870681beb0792f384475077c988916762vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
05afe08870681beb0792f384475077c988916762vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
05afe08870681beb0792f384475077c988916762vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
05afe08870681beb0792f384475077c988916762vboxsync * The contents of this file may alternatively be used under the terms
05afe08870681beb0792f384475077c988916762vboxsync * of the Common Development and Distribution License Version 1.0
05afe08870681beb0792f384475077c988916762vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
05afe08870681beb0792f384475077c988916762vboxsync * VirtualBox OSE distribution, in which case the provisions of the
05afe08870681beb0792f384475077c988916762vboxsync * CDDL are applicable instead of those of the GPL.
05afe08870681beb0792f384475077c988916762vboxsync * You may elect to license modified versions of this file under the
05afe08870681beb0792f384475077c988916762vboxsync * terms and conditions of either the GPL or the CDDL or both.
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Header Files *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync/*******************************************************************************
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync* Defined Constants And Macros *
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync*******************************************************************************/
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync/** @name fStateAndGen values
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync/** The state bit number. */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync/** The state mask. */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync#define RTSEMEVENTMULTIDARWIN_STATE_MASK RT_BIT_32(RTSEMEVENTMULTIDARWIN_STATE_BIT)
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync/** The generation mask. */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync#define RTSEMEVENTMULTIDARWIN_GEN_MASK ~RTSEMEVENTMULTIDARWIN_STATE_MASK
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync/** The generation shift. */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync/** The initial variable value. */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync#define RTSEMEVENTMULTIDARWIN_STATE_GEN_INIT UINT32_C(0xfffffffc)
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Structures and Typedefs *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync * Darwin multiple release event semaphore.
05afe08870681beb0792f384475077c988916762vboxsync /** Magic value (RTSEMEVENTMULTI_MAGIC). */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync /** The object state bit and generation counter.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * The generation counter is incremented every time the object is
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * signalled. */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync /** Reference counter. */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync /** Set if there are blocked threads. */
05afe08870681beb0792f384475077c988916762vboxsync /** The spinlock protecting us. */
05afe08870681beb0792f384475077c988916762vboxsync} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
05afe08870681beb0792f384475077c988916762vboxsync return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
05afe08870681beb0792f384475077c988916762vboxsync const char *pszNameFmt, ...)
05afe08870681beb0792f384475077c988916762vboxsync AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
05afe08870681beb0792f384475077c988916762vboxsync AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync pThis->fStateAndGen = RTSEMEVENTMULTIDARWIN_STATE_GEN_INIT;
05afe08870681beb0792f384475077c988916762vboxsync pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * Retain a reference to the semaphore.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * @param pThis The semaphore.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsyncDECLINLINE(void) rtR0SemEventMultiDarwinRetain(PRTSEMEVENTMULTIINTERNAL pThis)
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * Release a reference, destroy the thing if necessary.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * @param pThis The semaphore.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsyncDECLINLINE(void) rtR0SemEventMultiDarwinRelease(PRTSEMEVENTMULTIINTERNAL pThis)
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENTMULTI_MAGIC); /* make the handle invalid */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync ASMAtomicAndU32(&pThis->fStateAndGen, RTSEMEVENTMULTIDARWIN_GEN_MASK);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync /* abort waiting threads. */
05afe08870681beb0792f384475077c988916762vboxsync thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_RESTART);
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Set the signal and increment the generation counter.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync uint32_t fNew = ASMAtomicUoReadU32(&pThis->fStateAndGen);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Wake up all sleeping threads.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, false);
05afe08870681beb0792f384475077c988916762vboxsync thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_AWAKENED);
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync ASMAtomicAndU32(&pThis->fStateAndGen, ~RTSEMEVENTMULTIDARWIN_STATE_MASK);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * @returns VBox status code.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * @param pThis The event semaphore.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * @param fFlags See RTSemEventMultiWaitEx.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * @param uTimeout See RTSemEventMultiWaitEx.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * @param pSrcPos The source code position of the wait.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsyncstatic int rtR0SemEventMultiDarwinWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync if (uTimeout != 0 || (fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * Is the event already signalled or do we have to wait?
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync uint32_t const fOrgStateAndGen = ASMAtomicUoReadU32(&pThis->fStateAndGen);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync if (fOrgStateAndGen & RTSEMEVENTMULTIDARWIN_STATE_MASK)
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * We have to wait. So, we'll need to convert the timeout and figure
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * out if it's indefinite or not.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * Poll call, we already checked the condition above so no need to
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * wait for anything.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * Do the actual waiting.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, true);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync wait_interrupt_t fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE ? THREAD_ABORTSAFE : THREAD_UNINT;
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync nanoseconds_to_absolutetime(uNsAbsTimeout, &u64AbsTime);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync * Deal with the wait result.
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync if (RT_LIKELY(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC))
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync if (RT_LIKELY(ASMAtomicUoReadU32(&pThis->fStateAndGen) != fOrgStateAndGen))
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync continue; /* Seen this happen after fork/exec/something. */
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync AssertMsg(pThis->u32Magic == ~RTSEMEVENTMULTI_MAGIC, ("%#x\n", pThis->u32Magic));
95e56b6db151d1f9011694dcc552a6012f305a2avboxsyncRTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync return rtR0SemEventMultiDarwinWait(hEventMultiSem, fFlags, uTimeout, NULL);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync return rtR0SemEventMultiDarwinWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
95e56b6db151d1f9011694dcc552a6012f305a2avboxsyncRTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
95e56b6db151d1f9011694dcc552a6012f305a2avboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();