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