67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/* $Id$ */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Fast Mutex Semaphores, Ring-0 Driver, NT.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2010 Oracle Corporation
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * available from http://www.virtualbox.org. This file is free software;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*******************************************************************************
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync* Header Files *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync*******************************************************************************/
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include "the-nt-kernel.h"
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/semaphore.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/alloc.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/assert.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/asm.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include <iprt/err.h>
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync#include "internal/magics.h"
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/*******************************************************************************
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync* Structures and Typedefs *
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync*******************************************************************************/
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync/**
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Wrapper for the linux semaphore structure.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsynctypedef struct RTSEMFASTMUTEXINTERNAL
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /** Magic value (RTSEMFASTMUTEX_MAGIC). */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync uint32_t u32Magic;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /** the NT fast mutex. */
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync FAST_MUTEX Mutex;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsyncRTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Allocate.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync PRTSEMFASTMUTEXINTERNAL pThis;
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync Assert(sizeof(*pThis) > sizeof(void *));
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync if (!pThis)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VERR_NO_MEMORY;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Initialize.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync ExInitializeFastMutex(&pThis->Mutex);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync *phFastMtx = pThis;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsyncRTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync if (pThis == NIL_RTSEMFASTMUTEX)
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync return VINF_SUCCESS;
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync Assert(pThis->Mutex.Count == 1);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync RTMemFree(pThis);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsyncRTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync#if 1
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync /*
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync * ExAcquireFastMutex will set the IRQL to APC regardless of our current
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync * level. Lowering the IRQL may screw things up, so do not allow this.
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync */
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync# if 0 /** @todo enable this when the logger has been fixed. */
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync AssertMsg(KeGetCurrentIrql() <= APC_LEVEL, ("%d\n", KeGetCurrentIrql()), VERR_INVALID_STATE);
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync# else /* the gentler approach. */
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync KIRQL Irql = KeGetCurrentIrql();
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync if (Irql > APC_LEVEL)
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync return VERR_INVALID_STATE;
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync# endif
7a99c98125959dcc22e09cf74a744ccf37c0e55bvboxsync#endif
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync ExAcquireFastMutex(&pThis->Mutex);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsyncRTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync{
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync /*
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync * Validate.
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync */
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync
47c238f82281a3771c8bfb66858da823ecf03dbcvboxsync ExReleaseFastMutex(&pThis->Mutex);
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync return VINF_SUCCESS;
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync}
67c26773eca4a576449ffa8f289fa344fc7b8176vboxsync