semevent-r0drv-darwin.cpp revision 09776500e6fe37b0613ab81ad127c6c14639386b
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/* $Id$ */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/** @file
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * IPRT - Single Release Event Semaphores, Ring-0 Driver, Darwin.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Copyright (C) 2006-2010 Oracle Corporation
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * available from http://www.virtualbox.org. This file is free software;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * General Public License (GPL) as published by the Free Software
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * The contents of this file may alternatively be used under the terms
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * of the Common Development and Distribution License Version 1.0
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * CDDL are applicable instead of those of the GPL.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * You may elect to license modified versions of this file under the
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * terms and conditions of either the GPL or the CDDL or both.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/*******************************************************************************
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync* Header Files *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include "the-darwin-kernel.h"
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include "internal/iprt.h"
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/semaphore.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/assert.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/asm.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync# include <iprt/asm-amd64-x86.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#endif
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/err.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/list.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/lockvalidator.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/mem.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/mp.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/thread.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include <iprt/time.h>
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#include "internal/magics.h"
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
ecb98c0e709a5cebd8877fb39f61a821804024bcvboxsync/*******************************************************************************
ecb98c0e709a5cebd8877fb39f61a821804024bcvboxsync* Structures and Typedefs *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync*******************************************************************************/
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/**
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Waiter entry. Lives on the stack.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsynctypedef struct RTSEMEVENTDARWINENTRY
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The list node. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTLISTNODE Node;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Flag set when waking up the thread by signal or destroy. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync bool volatile fWokenUp;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync} RTSEMEVENTDARWINENTRY;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/** Pointer to waiter entry. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsynctypedef RTSEMEVENTDARWINENTRY *PRTSEMEVENTDARWINENTRY;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/**
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Darwin event semaphore.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsynctypedef struct RTSEMEVENTINTERNAL
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Magic value (RTSEMEVENT_MAGIC). */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint32_t volatile u32Magic;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Reference counter. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint32_t volatile cRefs;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Set if there are blocked threads. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync bool volatile fHaveBlockedThreads;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** Set if the event object is signaled. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync bool volatile fSignaled;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** List of waiting and woken up threads. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTLISTNODE WaitList;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** The spinlock protecting us. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync lck_spin_t *pSpinlock;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RT_ASSERT_PREEMPTIBLE();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pThis)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pThis->u32Magic = RTSEMEVENT_MAGIC;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pThis->cRefs = 1;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pThis->fHaveBlockedThreads = false;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pThis->fSignaled = false;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTListInit(&pThis->WaitList);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync Assert(g_pDarwinLockGroup);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pThis->pSpinlock)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *phEventSem = pThis;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pThis->u32Magic = 0;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTMemFree(pThis);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VERR_NO_MEMORY;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/**
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Retain a reference to the semaphore.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @param pThis The semaphore.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncDECLINLINE(void) rtR0SemEventDarwinRetain(PRTSEMEVENTINTERNAL pThis)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync Assert(cRefs && cRefs < 100000);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync/**
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Release a reference, destroy the thing if necessary.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @param pThis The semaphore.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncDECLINLINE(void) rtR0SemEventDarwinRelease(PRTSEMEVENTINTERNAL pThis)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTMemFree(pThis);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PRTSEMEVENTINTERNAL pThis = hEventSem;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (pThis == NIL_RTSEMEVENT)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RT_ASSERT_INTS_ON();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync lck_spin_lock(pThis->pSpinlock);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC); /* make the handle invalid */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicWriteBool(&pThis->fSignaled, false);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /* abort waiting threads. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PRTSEMEVENTDARWINENTRY pWaiter;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync pWaiter->fWokenUp = true;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_RESTART);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync lck_spin_unlock(pThis->pSpinlock);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rtR0SemEventDarwinRelease(pThis);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return VINF_SUCCESS;
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync}
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
83d61602c6968041692aa7203ee51c4085c7e460vboxsyncRTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
4f9276b4c85a4617d08094484cc1d983791bbb16vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
83d61602c6968041692aa7203ee51c4085c7e460vboxsync VERR_INVALID_HANDLE);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RT_ASSERT_PREEMPT_CPUID_VAR();
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync RT_ASSERT_INTS_ON();
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rtR0SemEventDarwinRetain(pThis);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /** @todo should probably disable interrupts here... update
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * semspinmutex-r0drv-generic.c when done. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync lck_spin_lock(pThis->pSpinlock);
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync /*
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync * Wake up one thread.
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync */
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync ASMAtomicWriteBool(&pThis->fSignaled, true);
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync PRTSEMEVENTDARWINENTRY pWaiter;
90ce7af4052f25f4a94d18c0ef86181971396cd3vboxsync RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!pWaiter->fWokenUp)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync pWaiter->fWokenUp = true;
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_AWAKENED);
6a0359b8230a1b91fe49967c124a75191c3dfbf9vboxsync ASMAtomicWriteBool(&pThis->fSignaled, false);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync break;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync lck_spin_unlock(pThis->pSpinlock);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rtR0SemEventDarwinRelease(pThis);
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync RT_ASSERT_PREEMPT_CPUID();
9f22c692723a5d3cb78b91896c48cf681c4fb608vboxsync return VINF_SUCCESS;
83d61602c6968041692aa7203ee51c4085c7e460vboxsync}
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
83d61602c6968041692aa7203ee51c4085c7e460vboxsync
83d61602c6968041692aa7203ee51c4085c7e460vboxsync/**
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync *
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @returns VBox status code.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @param pThis The event semaphore.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @param fFlags See RTSemEventWaitEx.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @param uTimeout See RTSemEventWaitEx.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * @param pSrcPos The source code position of the wait.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncstatic int rtR0SemEventDarwinWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync PCRTLOCKVALSRCPOS pSrcPos)
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync{
a9d98aa17ecb241bc2c79b67dc044f0af2eb7448vboxsync /*
83d61602c6968041692aa7203ee51c4085c7e460vboxsync * Validate the input.
83d61602c6968041692aa7203ee51c4085c7e460vboxsync */
83d61602c6968041692aa7203ee51c4085c7e460vboxsync AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync
1e0e13b23ace43d2fe93d45953b123f63b7e547cvboxsync rtR0SemEventDarwinRetain(pThis);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync lck_spin_lock(pThis->pSpinlock);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * In the signaled state?
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync int rc;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * We have to wait. So, we'll need to convert the timeout and figure
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * out if it's indefinite or not.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t uNsAbsTimeout = 1;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ? uTimeout * UINT32_C(1000000)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync : UINT64_MAX;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (uTimeout == UINT64_MAX)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t u64Now;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (uTimeout != 0)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync u64Now = RTTimeSystemNanoTS();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uNsAbsTimeout = u64Now + uTimeout;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (uNsAbsTimeout < u64Now) /* overflow */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uNsAbsTimeout = uTimeout;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync u64Now = RTTimeSystemNanoTS();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if ( !(fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync && uTimeout == 0)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Poll call, we already checked the condition above so no need to
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * wait for anything.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VERR_TIMEOUT;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTSEMEVENTDARWINENTRY Waiter;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync Waiter.fWokenUp = false;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTListAppend(&pThis->WaitList, &Waiter.Node);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync for (;;)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Do the actual waiting.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, true);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync wait_interrupt_t fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE ? THREAD_ABORTSAFE : THREAD_UNINT;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync wait_result_t rcWait;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)&Waiter, fInterruptible);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync uint64_t u64AbsTime;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync nanoseconds_to_absolutetime(uNsAbsTimeout, &u64AbsTime);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync (event_t)&Waiter, fInterruptible, u64AbsTime);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync /*
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync * Deal with the wait result.
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_LIKELY(pThis->u32Magic == RTSEMEVENT_MAGIC))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync switch (rcWait)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync {
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync case THREAD_AWAKENED:
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync if (RT_LIKELY(Waiter.fWokenUp))
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VERR_INTERRUPTED;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync continue; /* Seen this happen after fork/exec/something. */
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync break;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync case THREAD_TIMED_OUT:
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync Assert(!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE));
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = !Waiter.fWokenUp ? VERR_TIMEOUT : VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync break;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync case THREAD_INTERRUPTED:
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync Assert(fInterruptible != THREAD_UNINT);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = !Waiter.fWokenUp ? VERR_INTERRUPTED : VINF_SUCCESS;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync break;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync case THREAD_RESTART:
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertMsg(pThis->u32Magic == ~RTSEMEVENT_MAGIC, ("%#x\n", pThis->u32Magic));
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VERR_SEM_DESTROYED;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync break;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync default:
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync AssertMsgFailed(("rcWait=%d\n", rcWait));
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VERR_INTERNAL_ERROR_3;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync break;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rc = VERR_SEM_DESTROYED;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync break;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTListNodeRemove(&Waiter.Node);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync }
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync lck_spin_unlock(pThis->pSpinlock);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync rtR0SemEventDarwinRelease(pThis);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rc;
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#undef RTSemEventWaitEx
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#ifndef RTSEMEVENT_STRICT
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, NULL);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#else
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync#endif
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsyncRTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTHCUINTPTR uId, RT_SRC_POS_DECL)
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync{
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync}
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync
41d680dd6eb0287afc200adc5b0d61b07a32b72dvboxsync