semmutex-r0drv-nt.cpp revision 67c26773eca4a576449ffa8f289fa344fc7b8176
af062818b47340eef15700d2f0211576ba3506eevboxsync/* $Id$ */
af062818b47340eef15700d2f0211576ba3506eevboxsync/** @file
af062818b47340eef15700d2f0211576ba3506eevboxsync * innotek Portable Runtime - Mutex Semaphores, Ring-0 Driver, NT.
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync/*
af062818b47340eef15700d2f0211576ba3506eevboxsync * Copyright (C) 2006-2007 innotek GmbH
af062818b47340eef15700d2f0211576ba3506eevboxsync *
af062818b47340eef15700d2f0211576ba3506eevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
af062818b47340eef15700d2f0211576ba3506eevboxsync * available from http://www.virtualbox.org. This file is free software;
af062818b47340eef15700d2f0211576ba3506eevboxsync * you can redistribute it and/or modify it under the terms of the GNU
af062818b47340eef15700d2f0211576ba3506eevboxsync * General Public License as published by the Free Software Foundation,
af062818b47340eef15700d2f0211576ba3506eevboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
af062818b47340eef15700d2f0211576ba3506eevboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
af062818b47340eef15700d2f0211576ba3506eevboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync/*******************************************************************************
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync* Header Files *
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync*******************************************************************************/
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync#include "the-nt-kernel.h"
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync#include <iprt/semaphore.h>
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync#include <iprt/alloc.h>
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync#include <iprt/assert.h>
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync#include <iprt/asm.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/err.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync#include "internal/magics.h"
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync/*******************************************************************************
af062818b47340eef15700d2f0211576ba3506eevboxsync* Structures and Typedefs *
af062818b47340eef15700d2f0211576ba3506eevboxsync*******************************************************************************/
af062818b47340eef15700d2f0211576ba3506eevboxsync/**
af062818b47340eef15700d2f0211576ba3506eevboxsync * NT mutex semaphore.
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsynctypedef struct RTSEMMUTEXINTERNAL
af062818b47340eef15700d2f0211576ba3506eevboxsync{
af062818b47340eef15700d2f0211576ba3506eevboxsync /** Magic value (RTSEMMUTEX_MAGIC). */
114410893548b9522c46fdcbd8f63385eb8bfb68vboxsync uint32_t volatile u32Magic;
af062818b47340eef15700d2f0211576ba3506eevboxsync#ifdef RT_USE_FAST_MUTEX
af062818b47340eef15700d2f0211576ba3506eevboxsync /** The fast mutex object. */
af062818b47340eef15700d2f0211576ba3506eevboxsync FAST_MUTEX Mutex;
af062818b47340eef15700d2f0211576ba3506eevboxsync#else
af062818b47340eef15700d2f0211576ba3506eevboxsync /** The NT Mutex object. */
af062818b47340eef15700d2f0211576ba3506eevboxsync KMUTEX Mutex;
af062818b47340eef15700d2f0211576ba3506eevboxsync#endif
af062818b47340eef15700d2f0211576ba3506eevboxsync} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
114410893548b9522c46fdcbd8f63385eb8bfb68vboxsync
5112e32d7072e280613921c982a6672f2c859cf3vboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
af062818b47340eef15700d2f0211576ba3506eevboxsync{
114410893548b9522c46fdcbd8f63385eb8bfb68vboxsync Assert(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
af062818b47340eef15700d2f0211576ba3506eevboxsync PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
af062818b47340eef15700d2f0211576ba3506eevboxsync if (pMutexInt)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
af062818b47340eef15700d2f0211576ba3506eevboxsync#ifdef RT_USE_FAST_MUTEX
114410893548b9522c46fdcbd8f63385eb8bfb68vboxsync ExInitializeFastMutex(&pMutexInt->Mutex);
af062818b47340eef15700d2f0211576ba3506eevboxsync#else
af062818b47340eef15700d2f0211576ba3506eevboxsync KeInitializeMutex(&pMutexInt->Mutex, 0);
af062818b47340eef15700d2f0211576ba3506eevboxsync#endif
af062818b47340eef15700d2f0211576ba3506eevboxsync *pMutexSem = pMutexInt;
af062818b47340eef15700d2f0211576ba3506eevboxsync return VINF_SUCCESS;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync return VERR_NO_MEMORY;
af062818b47340eef15700d2f0211576ba3506eevboxsync}
114410893548b9522c46fdcbd8f63385eb8bfb68vboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncRTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
af062818b47340eef15700d2f0211576ba3506eevboxsync{
af062818b47340eef15700d2f0211576ba3506eevboxsync /*
* Validate input.
*/
PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
if (!pMutexInt)
return VERR_INVALID_PARAMETER;
if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
{
AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
return VERR_INVALID_PARAMETER;
}
/*
* Invalidate it and signal the object just in case.
*/
ASMAtomicIncU32(&pMutexInt->u32Magic);
RTMemFree(pMutexInt);
return VINF_SUCCESS;
}
RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
{
/*
* Validate input.
*/
PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
if (!pMutexInt)
return VERR_INVALID_PARAMETER;
if ( !pMutexInt
|| pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
{
AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
return VERR_INVALID_PARAMETER;
}
/*
* Get the mutex.
*/
#ifdef RT_USE_FAST_MUTEX
AssertMsg(cMillies == RT_INDEFINITE_WAIT, ("timeouts are not supported when using fast mutexes!\n"));
ExAcquireFastMutex(&pMutexInt->Mutex);
#else
NTSTATUS rcNt;
if (cMillies == RT_INDEFINITE_WAIT)
rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, NULL);
else
{
LARGE_INTEGER Timeout;
Timeout.QuadPart = -(int64_t)cMillies * 10000;
rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, &Timeout);
}
switch (rcNt)
{
case STATUS_SUCCESS:
if (pMutexInt->u32Magic == RTSEMMUTEX_MAGIC)
return VINF_SUCCESS;
return VERR_SEM_DESTROYED;
case STATUS_ALERTED:
return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
case STATUS_USER_APC:
return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
case STATUS_TIMEOUT:
return VERR_TIMEOUT;
default:
AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p: wait returned %lx!\n",
pMutexInt->u32Magic, pMutexInt, (long)rcNt));
return VERR_INTERNAL_ERROR;
}
#endif
return VINF_SUCCESS;
}
RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
{
/*
* Validate input.
*/
PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
if (!pMutexInt)
return VERR_INVALID_PARAMETER;
if ( !pMutexInt
|| pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
{
AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
return VERR_INVALID_PARAMETER;
}
/*
* Release the mutex.
*/
#ifdef RT_USE_FAST_MUTEX
ExReleaseFastMutex(&pMutexInt->Mutex);
#else
KeReleaseMutex(&pMutexInt->Mutex, FALSE);
#endif
return VINF_SUCCESS;
}