semmutex-r0drv-darwin.cpp revision 715e49c31b15c23c17a9ce3be42a75e7c48d4b78
/* $Id$ */
/** @file
* IPRT - Mutex Semaphores, Ring-0 Driver, Darwin.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define RTSEMMULTI_WITHOUT_REMAPPING
#include "the-darwin-kernel.h"
#include <iprt/semaphore.h>
#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
# include <iprt/asm-amd64-x86.h>
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Darwin mutex semaphore.
*/
typedef struct RTSEMMUTEXINTERNAL
{
/** Magic value (RTSEMMUTEX_MAGIC). */
/** The number of waiting threads. */
/** The number of references. */
/** The number of recursions. */
/** The handle of the owner thread. */
/** The spinlock protecting us. */
{
return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
}
{
AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
if (pThis)
{
pThis->cRecursions = 0;
{
*phMutexSem = pThis;
return VINF_SUCCESS;
}
}
return VERR_NO_MEMORY;
}
/**
* Called when the refcount reaches zero.
*/
{
}
{
/*
* Validate input.
*/
if (!pThis)
return VERR_INVALID_PARAMETER;
AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
/*
* Kill it, wake up all waiting threads and release the reference.
*/
AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTSEMMUTEX_MAGIC, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
else
return VINF_SUCCESS;
}
/**
* Internal worker for the sleep scenario.
*
* Called owning the spinlock, returns without it.
*
* @returns IPRT status code.
* @param pThis The mutex instance.
* @param cMillies The timeout.
* @param fInterruptible Whether it's interruptible
* (RTSemMutexRequestNoResume) or not
* (RTSemMutexRequest).
* @param hNativeSelf The thread handle of the caller.
*/
{
/*
* Grab a reference and indicate that we're waiting.
*/
/*
*/
if (cMillies == RT_INDEFINITE_WAIT)
else
{
u64AbsTime += mach_absolute_time();
}
/*
* Translate the rc.
*/
int rc;
switch (rcWait)
{
case THREAD_AWAKENED:
{
{
rc = VINF_SUCCESS;
}
else
{
}
}
else
break;
case THREAD_TIMED_OUT:
rc = VERR_TIMEOUT;
break;
case THREAD_INTERRUPTED:
break;
case THREAD_RESTART:
break;
default:
break;
}
/*
* Dereference it and quit the lock.
*/
else
return rc;
}
/**
* Internal worker for RTSemMutexRequest and RTSemMutexRequestNoResume
*
* @returns IPRT status code.
* @param hMutexSem The mutex handle.
* @param cMillies The timeout.
* @param fInterruptible Whether it's interruptible
* (RTSemMutexRequestNoResume) or not
* (RTSemMutexRequest).
*/
DECLINLINE(int) rtR0SemMutexDarwinRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, wait_interrupt_t fInterruptible)
{
/*
* Validate input.
*/
/*
* Grab the lock and check out the state.
*/
int rc = VINF_SUCCESS;
/* Recursive call? */
{
pThis->cRecursions++;
}
/* Is it free and nobody ahead of us in the queue? */
{
}
/* Polling call? */
else if (cMillies == 0)
rc = VERR_TIMEOUT;
/* Yawn, time for a nap... */
else
return rc;
}
{
}
RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
}
RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
/*
* Validate input.
*/
/*
* Take the lock and do the job.
*/
int rc = VINF_SUCCESS;
{
if (--pThis->cRecursions == 0)
{
{
}
}
}
else
rc = VERR_NOT_OWNER;
return VINF_SUCCESS;
}
{
/*
* Validate.
*/
AssertPtrReturn(pThis, false);
/*
* Take the lock and do the check.
*/
return fRc;
}