semfastmutex-r0drv-solaris.c revision ce2cd6bde5954faba8417441ce33c304eeb89cef
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* $Id$ */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/** @file
fa9e4066f08beec538e775443c5be79dd423fcabahrens * IPRT - Fast Mutex Semaphores, Ring-0 Driver, Solaris.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright (C) 2006-2007 Sun Microsystems, Inc.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * This file is part of VirtualBox Open Source Edition (OSE), as
fa9e4066f08beec538e775443c5be79dd423fcabahrens * available from http://www.virtualbox.org. This file is free software;
fa9e4066f08beec538e775443c5be79dd423fcabahrens * you can redistribute it and/or modify it under the terms of the GNU
fa9e4066f08beec538e775443c5be79dd423fcabahrens * General Public License (GPL) as published by the Free Software
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Foundation, in version 2 as it comes in the "COPYING" file of the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file may alternatively be used under the terms
fa9e4066f08beec538e775443c5be79dd423fcabahrens * of the Common Development and Distribution License Version 1.0
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * VirtualBox OSE distribution, in which case the provisions of the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL are applicable instead of those of the GPL.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
6b90ca488b504d3422b169269c3a86ccad80322clling * You may elect to license modified versions of this file under the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * terms and conditions of either the GPL or the CDDL or both.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Clara, CA 95054 USA or visit http://www.sun.com if you need
fa9e4066f08beec538e775443c5be79dd423fcabahrens * additional information or have any questions.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*******************************************************************************
fa9e4066f08beec538e775443c5be79dd423fcabahrens* Header Files *
fa9e4066f08beec538e775443c5be79dd423fcabahrens*******************************************************************************/
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include "the-solaris-kernel.h"
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <iprt/semaphore.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <iprt/err.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <iprt/alloc.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <iprt/assert.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <iprt/asm.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include "internal/magics.h"
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*******************************************************************************
fa9e4066f08beec538e775443c5be79dd423fcabahrens* Structures and Typedefs *
fa9e4066f08beec538e775443c5be79dd423fcabahrens*******************************************************************************/
fa9e4066f08beec538e775443c5be79dd423fcabahrens/**
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Wrapper for the Solaris mutex.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct RTSEMFASTMUTEXINTERNAL
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens /** Magic value (RTSEMFASTMUTEX_MAGIC). */
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint32_t u32Magic;
fa9e4066f08beec538e775443c5be79dd423fcabahrens /** The Solaris mutex. */
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmutex_t Mtx;
fa9e4066f08beec538e775443c5be79dd423fcabahrens} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensRTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX pMutexSem)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
fa9e4066f08beec538e775443c5be79dd423fcabahrens AssertPtrReturn(pMutexSem, VERR_INVALID_POINTER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pFastInt));
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (pFastInt)
fa9e4066f08beec538e775443c5be79dd423fcabahrens {
fa9e4066f08beec538e775443c5be79dd423fcabahrens pFastInt->u32Magic = RTSEMFASTMUTEX_MAGIC;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_init (&pFastInt->Mtx, "IPRT Fast Mutex Semaphore", MUTEX_DRIVER, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens *pMutexSem = pFastInt;
fa9e4066f08beec538e775443c5be79dd423fcabahrens return VINF_SUCCESS;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens return VERR_NO_MEMORY;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensRTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX MutexSem)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (MutexSem == NIL_RTSEMFASTMUTEX)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return VERR_INVALID_PARAMETER;
fa9e4066f08beec538e775443c5be79dd423fcabahrens PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
fa9e4066f08beec538e775443c5be79dd423fcabahrens AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
fa9e4066f08beec538e775443c5be79dd423fcabahrens ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
fa9e4066f08beec538e775443c5be79dd423fcabahrens VERR_INVALID_PARAMETER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASMAtomicXchgU32(&pFastInt->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_destroy(&pFastInt->Mtx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens RTMemFree(pFastInt);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return VINF_SUCCESS;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensRTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX MutexSem)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
fa9e4066f08beec538e775443c5be79dd423fcabahrens AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
fa9e4066f08beec538e775443c5be79dd423fcabahrens ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
fa9e4066f08beec538e775443c5be79dd423fcabahrens VERR_INVALID_PARAMETER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&pFastInt->Mtx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return VINF_SUCCESS;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensRTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX MutexSem)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
fa9e4066f08beec538e775443c5be79dd423fcabahrens AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
fa9e4066f08beec538e775443c5be79dd423fcabahrens ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
b81d61a68b235e0529ebadc18e14d9d1dd52a258lling VERR_INVALID_PARAMETER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&pFastInt->Mtx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return VINF_SUCCESS;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens