semmutex-r0drv-darwin.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
05afe08870681beb0792f384475077c988916762vboxsync * IPRT - Mutex 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 mutex semaphore.
05afe08870681beb0792f384475077c988916762vboxsync /** Magic value (RTSEMMUTEX_MAGIC). */
243cd452144a61285fc9be2bd5448698af935f13vboxsync /** The number of waiting threads. */
243cd452144a61285fc9be2bd5448698af935f13vboxsync /** The number of references. */
243cd452144a61285fc9be2bd5448698af935f13vboxsync /** The number of recursions. */
243cd452144a61285fc9be2bd5448698af935f13vboxsync /** The handle of the owner thread. */
243cd452144a61285fc9be2bd5448698af935f13vboxsync /** The spinlock protecting us. */
243cd452144a61285fc9be2bd5448698af935f13vboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
243cd452144a61285fc9be2bd5448698af935f13vboxsync return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
243cd452144a61285fc9be2bd5448698af935f13vboxsyncRTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
243cd452144a61285fc9be2bd5448698af935f13vboxsync RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
243cd452144a61285fc9be2bd5448698af935f13vboxsync AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
05afe08870681beb0792f384475077c988916762vboxsync AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
243cd452144a61285fc9be2bd5448698af935f13vboxsync pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Called when the refcount reaches zero.
243cd452144a61285fc9be2bd5448698af935f13vboxsyncstatic void rtSemMutexDarwinFree(PRTSEMMUTEXINTERNAL pThis)
243cd452144a61285fc9be2bd5448698af935f13vboxsync lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
243cd452144a61285fc9be2bd5448698af935f13vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Kill it, wake up all waiting threads and release the reference.
243cd452144a61285fc9be2bd5448698af935f13vboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMMUTEX_MAGIC, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
243cd452144a61285fc9be2bd5448698af935f13vboxsync thread_wakeup_prim((event_t)pThis, FALSE /* one_thread */, THREAD_RESTART);
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Internal worker for the sleep scenario.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Called owning the spinlock, returns without it.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @returns IPRT status code.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @param pThis The mutex instance.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @param cMillies The timeout.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @param fInterruptible Whether it's interruptible
243cd452144a61285fc9be2bd5448698af935f13vboxsync * (RTSemMutexRequestNoResume) or not
243cd452144a61285fc9be2bd5448698af935f13vboxsync * (RTSemMutexRequest).
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @param hNativeSelf The thread handle of the caller.
243cd452144a61285fc9be2bd5448698af935f13vboxsyncstatic int rtR0SemMutexDarwinRequestSleep(PRTSEMMUTEXINTERNAL pThis, RTMSINTERVAL cMillies,
243cd452144a61285fc9be2bd5448698af935f13vboxsync wait_interrupt_t fInterruptible, RTNATIVETHREAD hNativeSelf)
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Grab a reference and indicate that we're waiting.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Go to sleep, use the address of the mutex instance as sleep/blocking/event id.
243cd452144a61285fc9be2bd5448698af935f13vboxsync rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
243cd452144a61285fc9be2bd5448698af935f13vboxsync nanoseconds_to_absolutetime(cMillies * UINT64_C(1000000), &u64AbsTime);
243cd452144a61285fc9be2bd5448698af935f13vboxsync rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Translate the rc.
243cd452144a61285fc9be2bd5448698af935f13vboxsync if (RT_LIKELY(pThis->u32Magic == RTSEMMUTEX_MAGIC))
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Dereference it and quit the lock.
243cd452144a61285fc9be2bd5448698af935f13vboxsync if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Internal worker for RTSemMutexRequest and RTSemMutexRequestNoResume
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @returns IPRT status code.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @param hMutexSem The mutex handle.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @param cMillies The timeout.
243cd452144a61285fc9be2bd5448698af935f13vboxsync * @param fInterruptible Whether it's interruptible
243cd452144a61285fc9be2bd5448698af935f13vboxsync * (RTSemMutexRequestNoResume) or not
243cd452144a61285fc9be2bd5448698af935f13vboxsync * (RTSemMutexRequest).
243cd452144a61285fc9be2bd5448698af935f13vboxsyncDECLINLINE(int) rtR0SemMutexDarwinRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, wait_interrupt_t fInterruptible)
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Validate input.
243cd452144a61285fc9be2bd5448698af935f13vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
243cd452144a61285fc9be2bd5448698af935f13vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Grab the lock and check out the state.
243cd452144a61285fc9be2bd5448698af935f13vboxsync /* Recursive call? */
243cd452144a61285fc9be2bd5448698af935f13vboxsync /* Is it free and nobody ahead of us in the queue? */
243cd452144a61285fc9be2bd5448698af935f13vboxsync else if ( pThis->hNativeOwner == NIL_RTNATIVETHREAD
243cd452144a61285fc9be2bd5448698af935f13vboxsync /* Polling call? */
243cd452144a61285fc9be2bd5448698af935f13vboxsync else if (cMillies == 0)
243cd452144a61285fc9be2bd5448698af935f13vboxsync /* Yawn, time for a nap... */
243cd452144a61285fc9be2bd5448698af935f13vboxsync return rtR0SemMutexDarwinRequestSleep(pThis, cMillies, fInterruptible, hNativeSelf);
243cd452144a61285fc9be2bd5448698af935f13vboxsyncRTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
243cd452144a61285fc9be2bd5448698af935f13vboxsync return rtR0SemMutexDarwinRequest(hMutexSem, cMillies, THREAD_UNINT);
243cd452144a61285fc9be2bd5448698af935f13vboxsyncRTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
243cd452144a61285fc9be2bd5448698af935f13vboxsyncRTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
243cd452144a61285fc9be2bd5448698af935f13vboxsync return rtR0SemMutexDarwinRequest(hMutexSem, cMillies, THREAD_ABORTSAFE);
243cd452144a61285fc9be2bd5448698af935f13vboxsyncRTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
243cd452144a61285fc9be2bd5448698af935f13vboxsync return RTSemMutexRequestNoResume(hMutexSem, cMillies);
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
243cd452144a61285fc9be2bd5448698af935f13vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Take the lock and do the job.
243cd452144a61285fc9be2bd5448698af935f13vboxsync int rc2=thread_wakeup_prim((event_t)pThis, TRUE /* one_thread */, THREAD_AWAKENED);
243cd452144a61285fc9be2bd5448698af935f13vboxsyncRTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Validate.
243cd452144a61285fc9be2bd5448698af935f13vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);
243cd452144a61285fc9be2bd5448698af935f13vboxsync * Take the lock and do the check.