semeventmulti-r0drv-darwin.cpp revision a8f65e585466d1267633cea76b4f97a69b7f1cc0
05afe08870681beb0792f384475077c988916762vboxsync * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Darwin.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2010 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*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Structures and Typedefs *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync * Darwin multiple release event semaphore.
05afe08870681beb0792f384475077c988916762vboxsync /** Magic value (RTSEMEVENTMULTI_MAGIC). */
05afe08870681beb0792f384475077c988916762vboxsync /** The number of waiting threads. */
05afe08870681beb0792f384475077c988916762vboxsync /** Set if the event object is signaled. */
05afe08870681beb0792f384475077c988916762vboxsync /** The number of threads in the process of waking up. */
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));
05afe08870681beb0792f384475077c988916762vboxsync pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
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);
05afe08870681beb0792f384475077c988916762vboxsync ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
05afe08870681beb0792f384475077c988916762vboxsync /* abort waiting thread, last man cleans up. */
05afe08870681beb0792f384475077c988916762vboxsync ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
05afe08870681beb0792f384475077c988916762vboxsync thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_RESTART);
05afe08870681beb0792f384475077c988916762vboxsync /* the last waking thread is gonna do the cleanup */
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
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);
05afe08870681beb0792f384475077c988916762vboxsync ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
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);
05afe08870681beb0792f384475077c988916762vboxsyncstatic int rtSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies, wait_interrupt_t fInterruptible)
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
05afe08870681beb0792f384475077c988916762vboxsync nanoseconds_to_absolutetime(cMillies * UINT64_C(1000000), &u64AbsTime);
05afe08870681beb0792f384475077c988916762vboxsync rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
05afe08870681beb0792f384475077c988916762vboxsync /* the event was destroyed after we woke up, as the last thread do the cleanup. */
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsync /* Last one out does the cleanup. */
05afe08870681beb0792f384475077c988916762vboxsync lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
05afe08870681beb0792f384475077c988916762vboxsync return rtSemEventMultiWait(hEventMultiSem, cMillies, THREAD_UNINT);
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)