05afe08870681beb0792f384475077c988916762vboxsync/* $Id$ */
05afe08870681beb0792f384475077c988916762vboxsync/** @file
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * IPRT - Single Release Event Semaphores, Ring-0 Driver, Darwin.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2011 Oracle Corporation
05afe08870681beb0792f384475077c988916762vboxsync *
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 *
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 *
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
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Header Files *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
715e49c31b15c23c17a9ce3be42a75e7c48d4b78vboxsync#define RTSEMEVENT_WITHOUT_REMAPPING
05afe08870681beb0792f384475077c988916762vboxsync#include "the-darwin-kernel.h"
05afe08870681beb0792f384475077c988916762vboxsync#include "internal/iprt.h"
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/semaphore.h>
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/assert.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/asm.h>
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync# include <iprt/asm-amd64-x86.h>
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync#endif
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/err.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <iprt/list.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <iprt/lockvalidator.h>
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync#include <iprt/mem.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/mp.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/thread.h>
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#include <iprt/time.h>
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync#include "internal/magics.h"
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Structures and Typedefs *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync/**
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Waiter entry. Lives on the stack.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsynctypedef struct RTSEMEVENTDARWINENTRY
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /** The list node. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTLISTNODE Node;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /** Flag set when waking up the thread by signal or destroy. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync bool volatile fWokenUp;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync} RTSEMEVENTDARWINENTRY;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync/** Pointer to waiter entry. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsynctypedef RTSEMEVENTDARWINENTRY *PRTSEMEVENTDARWINENTRY;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync/**
05afe08870681beb0792f384475077c988916762vboxsync * Darwin event semaphore.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsynctypedef struct RTSEMEVENTINTERNAL
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /** Magic value (RTSEMEVENT_MAGIC). */
05afe08870681beb0792f384475077c988916762vboxsync uint32_t volatile u32Magic;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /** Reference counter. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uint32_t volatile cRefs;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /** Set if there are blocked threads. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync bool volatile fHaveBlockedThreads;
05afe08870681beb0792f384475077c988916762vboxsync /** Set if the event object is signaled. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync bool volatile fSignaled;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /** List of waiting and woken up threads. */
e961f5bfe1727c6816d3dad3805ebe21b6ba1c64vboxsync RTLISTANCHOR WaitList;
05afe08870681beb0792f384475077c988916762vboxsync /** The spinlock protecting us. */
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_t *pSpinlock;
05afe08870681beb0792f384475077c988916762vboxsync} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
418b9db49fbc652ef9c3f030fdc0f1a322403d95vboxsync Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPTIBLE();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
05afe08870681beb0792f384475077c988916762vboxsync if (pThis)
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync pThis->u32Magic = RTSEMEVENT_MAGIC;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pThis->cRefs = 1;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pThis->fHaveBlockedThreads = false;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pThis->fSignaled = false;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTListInit(&pThis->WaitList);
05afe08870681beb0792f384475077c988916762vboxsync Assert(g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsync pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
05afe08870681beb0792f384475077c988916762vboxsync if (pThis->pSpinlock)
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync *phEventSem = pThis;
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync pThis->u32Magic = 0;
05afe08870681beb0792f384475077c988916762vboxsync RTMemFree(pThis);
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync return VERR_NO_MEMORY;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync/**
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Retain a reference to the semaphore.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync *
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * @param pThis The semaphore.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncDECLINLINE(void) rtR0SemEventDarwinRetain(PRTSEMEVENTINTERNAL pThis)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync Assert(cRefs && cRefs < 100000);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync}
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync/**
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Release a reference, destroy the thing if necessary.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync *
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * @param pThis The semaphore.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncDECLINLINE(void) rtR0SemEventDarwinRelease(PRTSEMEVENTINTERNAL pThis)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTMemFree(pThis);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync}
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMEVENTINTERNAL pThis = hEventSem;
05afe08870681beb0792f384475077c988916762vboxsync if (pThis == NIL_RTSEMEVENT)
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_INTS_ON();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_lock(pThis->pSpinlock);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC); /* make the handle invalid */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicWriteBool(&pThis->fSignaled, false);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /* abort waiting threads. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync PRTSEMEVENTDARWINENTRY pWaiter;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pWaiter->fWokenUp = true;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_RESTART);
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync lck_spin_unlock(pThis->pSpinlock);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rtR0SemEventDarwinRelease(pThis);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
05afe08870681beb0792f384475077c988916762vboxsync ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
05afe08870681beb0792f384475077c988916762vboxsync VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPT_CPUID_VAR();
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_INTS_ON();
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rtR0SemEventDarwinRetain(pThis);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
05afe08870681beb0792f384475077c988916762vboxsync /** @todo should probably disable interrupts here... update
05afe08870681beb0792f384475077c988916762vboxsync * semspinmutex-r0drv-generic.c when done. */
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_lock(pThis->pSpinlock);
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Wake up one thread.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicWriteBool(&pThis->fSignaled, true);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync PRTSEMEVENTDARWINENTRY pWaiter;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (!pWaiter->fWokenUp)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync pWaiter->fWokenUp = true;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_AWAKENED);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicWriteBool(&pThis->fSignaled, false);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_unlock(pThis->pSpinlock);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rtR0SemEventDarwinRelease(pThis);
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPT_CPUID();
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync/**
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync *
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * @returns VBox status code.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * @param pThis The event semaphore.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * @param fFlags See RTSemEventWaitEx.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * @param uTimeout See RTSemEventWaitEx.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * @param pSrcPos The source code position of the wait.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncstatic int rtR0SemEventDarwinWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync PCRTLOCKVALSRCPOS pSrcPos)
05afe08870681beb0792f384475077c988916762vboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Validate the input.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rtR0SemEventDarwinRetain(pThis);
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_lock(pThis->pSpinlock);
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * In the signaled state?
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
05afe08870681beb0792f384475077c988916762vboxsync int rc;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
05afe08870681beb0792f384475077c988916762vboxsync rc = VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync else
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * We have to wait. So, we'll need to convert the timeout and figure
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * out if it's indefinite or not.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uint64_t uNsAbsTimeout = 1;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ? uTimeout * UINT32_C(1000000)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync : UINT64_MAX;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (uTimeout == UINT64_MAX)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uint64_t u64Now;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (uTimeout != 0)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync u64Now = RTTimeSystemNanoTS();
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uNsAbsTimeout = u64Now + uTimeout;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (uNsAbsTimeout < u64Now) /* overflow */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uNsAbsTimeout = uTimeout;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync u64Now = RTTimeSystemNanoTS();
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if ( !(fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync && uTimeout == 0)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Poll call, we already checked the condition above so no need to
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * wait for anything.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_TIMEOUT;
05afe08870681beb0792f384475077c988916762vboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTSEMEVENTDARWINENTRY Waiter;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync Waiter.fWokenUp = false;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTListAppend(&pThis->WaitList, &Waiter.Node);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync for (;;)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Do the actual waiting.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, true);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync wait_interrupt_t fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE ? THREAD_ABORTSAFE : THREAD_UNINT;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync wait_result_t rcWait;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)&Waiter, fInterruptible);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync uint64_t u64AbsTime;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync nanoseconds_to_absolutetime(uNsAbsTimeout, &u64AbsTime);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync (event_t)&Waiter, fInterruptible, u64AbsTime);
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync /*
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync * Deal with the wait result.
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_LIKELY(pThis->u32Magic == RTSEMEVENT_MAGIC))
05afe08870681beb0792f384475077c988916762vboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync switch (rcWait)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync {
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case THREAD_AWAKENED:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync if (RT_LIKELY(Waiter.fWokenUp))
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_INTERRUPTED;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync continue; /* Seen this happen after fork/exec/something. */
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case THREAD_TIMED_OUT:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync Assert(!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = !Waiter.fWokenUp ? VERR_TIMEOUT : VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case THREAD_INTERRUPTED:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync Assert(fInterruptible != THREAD_UNINT);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = !Waiter.fWokenUp ? VERR_INTERRUPTED : VINF_SUCCESS;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync case THREAD_RESTART:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertMsg(pThis->u32Magic == ~RTSEMEVENT_MAGIC, ("%#x\n", pThis->u32Magic));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_SEM_DESTROYED;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync default:
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync AssertMsgFailed(("rcWait=%d\n", rcWait));
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_INTERNAL_ERROR_3;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
05afe08870681beb0792f384475077c988916762vboxsync }
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rc = VERR_SEM_DESTROYED;
05afe08870681beb0792f384475077c988916762vboxsync break;
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTListNodeRemove(&Waiter.Node);
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_unlock(pThis->pSpinlock);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync rtR0SemEventDarwinRelease(pThis);
05afe08870681beb0792f384475077c988916762vboxsync return rc;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncRTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
05afe08870681beb0792f384475077c988916762vboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#ifndef RTSEMEVENT_STRICT
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, NULL);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#else
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync#endif
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
09776500e6fe37b0613ab81ad127c6c14639386bvboxsyncRTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTHCUINTPTR uId, RT_SRC_POS_DECL)
05afe08870681beb0792f384475077c988916762vboxsync{
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
09776500e6fe37b0613ab81ad127c6c14639386bvboxsync return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsyncRTDECL(uint32_t) RTSemEventGetResolution(void)
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync{
8f9193a0c6db641a38d56ceba5747557f1e9927dvboxsync uint64_t cNs;
8f9193a0c6db641a38d56ceba5747557f1e9927dvboxsync absolutetime_to_nanoseconds(1, &cNs);
8f9193a0c6db641a38d56ceba5747557f1e9927dvboxsync return (uint32_t)cNs ? (uint32_t)cNs : 0;
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync}
e0e7da0420be1398d23ffa9953686d3a43619abdvboxsync