05afe08870681beb0792f384475077c988916762vboxsync/* $Id$ */
05afe08870681beb0792f384475077c988916762vboxsync/** @file
05afe08870681beb0792f384475077c988916762vboxsync * IPRT - Fast Mutex Semaphores, Ring-0 Driver, Darwin.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2010 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*******************************************************************************/
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>
a8f65e585466d1267633cea76b4f97a69b7f1cc0vboxsync#include <iprt/mem.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/mp.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/thread.h>
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync#include "internal/magics.h"
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*******************************************************************************
05afe08870681beb0792f384475077c988916762vboxsync* Structures and Typedefs *
05afe08870681beb0792f384475077c988916762vboxsync*******************************************************************************/
05afe08870681beb0792f384475077c988916762vboxsync/**
05afe08870681beb0792f384475077c988916762vboxsync * Wrapper for the darwin semaphore structure.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsynctypedef struct RTSEMFASTMUTEXINTERNAL
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /** Magic value (RTSEMFASTMUTEX_MAGIC). */
05afe08870681beb0792f384475077c988916762vboxsync uint32_t u32Magic;
05afe08870681beb0792f384475077c988916762vboxsync /** The mutex. */
05afe08870681beb0792f384475077c988916762vboxsync lck_mtx_t *pMtx;
05afe08870681beb0792f384475077c988916762vboxsync} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(phFastMtx, VERR_INVALID_POINTER);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPTIBLE();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMFASTMUTEXINTERNAL pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
05afe08870681beb0792f384475077c988916762vboxsync if (pThis)
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
05afe08870681beb0792f384475077c988916762vboxsync Assert(g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsync pThis->pMtx = lck_mtx_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
05afe08870681beb0792f384475077c988916762vboxsync if (pThis->pMtx)
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync *phFastMtx = pThis;
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync RTMemFree(pThis);
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync return VERR_NO_MEMORY;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
05afe08870681beb0792f384475077c988916762vboxsync if (pThis == NIL_RTSEMFASTMUTEX)
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_INTS_ON();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
05afe08870681beb0792f384475077c988916762vboxsync Assert(g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsync lck_mtx_free(pThis->pMtx, g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsync pThis->pMtx = NULL;
05afe08870681beb0792f384475077c988916762vboxsync RTMemFree(pThis);
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPTIBLE();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync lck_mtx_lock(pThis->pMtx);
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPTIBLE();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync lck_mtx_unlock(pThis->pMtx);
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync