semmutex-r0drv-darwin.cpp revision 05afe08870681beb0792f384475077c988916762
05afe08870681beb0792f384475077c988916762vboxsync/* $Id$ */
05afe08870681beb0792f384475077c988916762vboxsync/** @file
05afe08870681beb0792f384475077c988916762vboxsync * IPRT - Mutex Semaphores, Ring-0 Driver, Darwin.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync/*
05afe08870681beb0792f384475077c988916762vboxsync * Copyright (C) 2006-2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
05afe08870681beb0792f384475077c988916762vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
05afe08870681beb0792f384475077c988916762vboxsync * additional information or have any questions.
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/alloc.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/assert.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/asm.h>
05afe08870681beb0792f384475077c988916762vboxsync#include <iprt/err.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#if 0 /** @todo */
05afe08870681beb0792f384475077c988916762vboxsync/**
05afe08870681beb0792f384475077c988916762vboxsync * Darwin mutex semaphore.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsynctypedef struct RTSEMMUTEXINTERNAL
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /** Magic value (RTSEMMUTEX_MAGIC). */
05afe08870681beb0792f384475077c988916762vboxsync uint32_t volatile u32Magic;
05afe08870681beb0792f384475077c988916762vboxsync /** The mutex. */
05afe08870681beb0792f384475077c988916762vboxsync lck_mtx_t *pMtx;
05afe08870681beb0792f384475077c988916762vboxsync} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync#endif
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync#if 0 /* need proper timeout lock function! */
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phFastMtx)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPTIBLE();
05afe08870681beb0792f384475077c988916762vboxsync AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
05afe08870681beb0792f384475077c988916762vboxsync if (pThis)
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync pThis->u32Magic = RTSEMMUTEX_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 RTMemFree(pThis);
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync return VERR_NO_MEMORY;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
05afe08870681beb0792f384475077c988916762vboxsync if (!pThis)
05afe08870681beb0792f384475077c988916762vboxsync return VERR_INVALID_PARAMETER;
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync AssertMsg(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_INTS_ON();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Invalidate it and signal the object just in case.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync ASMAtomicIncU32(&pThis->u32Magic);
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync Assert(g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsync lck_mtx_free(pThis->pMtx, g_pDarwinLockGroup);
05afe08870681beb0792f384475077c988916762vboxsync pThis->pMtx = NULL;
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync RTMemFree(pThis);
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
05afe08870681beb0792f384475077c988916762vboxsync if (!pThis)
05afe08870681beb0792f384475077c988916762vboxsync return VERR_INVALID_PARAMETER;
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync AssertMsg(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync if (cMillies)
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPTIBLE();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Get the mutex.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync wait_result_t rc = lck_mtx_lock_deadlink
05afe08870681beb0792f384475077c988916762vboxsync#if 1
05afe08870681beb0792f384475077c988916762vboxsync#else
05afe08870681beb0792f384475077c988916762vboxsync NTSTATUS rcNt;
05afe08870681beb0792f384475077c988916762vboxsync if (cMillies == RT_INDEFINITE_WAIT)
05afe08870681beb0792f384475077c988916762vboxsync rcNt = KeWaitForSingleObject(&pThis->Mutex, Executive, KernelMode, TRUE, NULL);
05afe08870681beb0792f384475077c988916762vboxsync else
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync LARGE_INTEGER Timeout;
05afe08870681beb0792f384475077c988916762vboxsync Timeout.QuadPart = -(int64_t)cMillies * 10000;
05afe08870681beb0792f384475077c988916762vboxsync rcNt = KeWaitForSingleObject(&pThis->Mutex, Executive, KernelMode, TRUE, &Timeout);
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync switch (rcNt)
05afe08870681beb0792f384475077c988916762vboxsync {
05afe08870681beb0792f384475077c988916762vboxsync case STATUS_SUCCESS:
05afe08870681beb0792f384475077c988916762vboxsync if (pThis->u32Magic == RTSEMMUTEX_MAGIC)
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync return VERR_SEM_DESTROYED;
05afe08870681beb0792f384475077c988916762vboxsync case STATUS_ALERTED:
05afe08870681beb0792f384475077c988916762vboxsync return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
05afe08870681beb0792f384475077c988916762vboxsync case STATUS_USER_APC:
05afe08870681beb0792f384475077c988916762vboxsync return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
05afe08870681beb0792f384475077c988916762vboxsync case STATUS_TIMEOUT:
05afe08870681beb0792f384475077c988916762vboxsync return VERR_TIMEOUT;
05afe08870681beb0792f384475077c988916762vboxsync default:
05afe08870681beb0792f384475077c988916762vboxsync AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
05afe08870681beb0792f384475077c988916762vboxsync pThis->u32Magic, pThis, (long)rcNt));
05afe08870681beb0792f384475077c988916762vboxsync return VERR_INTERNAL_ERROR;
05afe08870681beb0792f384475077c988916762vboxsync }
05afe08870681beb0792f384475077c988916762vboxsync#endif
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsyncRTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
05afe08870681beb0792f384475077c988916762vboxsync{
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Validate input.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)hMutexSem;
05afe08870681beb0792f384475077c988916762vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync AssertMsg(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
05afe08870681beb0792f384475077c988916762vboxsync RT_ASSERT_PREEMPTIBLE();
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync /*
05afe08870681beb0792f384475077c988916762vboxsync * Release the mutex.
05afe08870681beb0792f384475077c988916762vboxsync */
05afe08870681beb0792f384475077c988916762vboxsync#ifdef RT_USE_FAST_MUTEX
05afe08870681beb0792f384475077c988916762vboxsync ExReleaseFastMutex(&pThis->Mutex);
05afe08870681beb0792f384475077c988916762vboxsync#else
05afe08870681beb0792f384475077c988916762vboxsync KeReleaseMutex(&pThis->Mutex, FALSE);
05afe08870681beb0792f384475077c988916762vboxsync#endif
05afe08870681beb0792f384475077c988916762vboxsync return VINF_SUCCESS;
05afe08870681beb0792f384475077c988916762vboxsync}
05afe08870681beb0792f384475077c988916762vboxsync
05afe08870681beb0792f384475077c988916762vboxsync#endif /* later */
05afe08870681beb0792f384475077c988916762vboxsync