semfastmutex-r0drv-freebsd.c revision 1f1986470af9f0bb750dd859b142dc2e952deb20
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/* $Id$ */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/** @file
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * innotek Portable Runtime - Fast Mutex Semaphores, Ring-0 Driver, FreeBSD.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/*
506df97a8c78dacfe69bc4d3ee5d3de832d0f19avboxsync * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync *
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * Permission is hereby granted, free of charge, to any person
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * obtaining a copy of this software and associated documentation
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * files (the "Software"), to deal in the Software without
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * restriction, including without limitation the rights to use,
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * copy, modify, merge, publish, distribute, sublicense, and/or sell
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * copies of the Software, and to permit persons to whom the
6ec4e1827eab6a424d672ef0e5a17b065e52db20vboxsync * Software is furnished to do so, subject to the following
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * conditions:
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync *
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * The above copyright notice and this permission notice shall be
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * included in all copies or substantial portions of the Software.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync *
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * OTHER DEALINGS IN THE SOFTWARE.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
506df97a8c78dacfe69bc4d3ee5d3de832d0f19avboxsync/*******************************************************************************
506df97a8c78dacfe69bc4d3ee5d3de832d0f19avboxsync* Header Files *
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync*******************************************************************************/
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync#include "the-freebsd-kernel.h"
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync#include <iprt/semaphore.h>
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync#include <iprt/err.h>
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync#include <iprt/alloc.h>
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync#include <iprt/assert.h>
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync#include <iprt/asm.h>
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync#include "internal/magics.h"
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/*******************************************************************************
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync* Structures and Typedefs *
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync*******************************************************************************/
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync/**
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync * Wrapper for the FreeBSD (sleep) mutex.
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsynctypedef struct RTSEMFASTMUTEXINTERNAL
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync{
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /** Magic value (RTSEMFASTMUTEX_MAGIC). */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync uint32_t u32Magic;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync /** The FreeBSD (sleep) mutex. */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync struct mtx Mtx;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncRTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX pMutexSem)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync{
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertPtrReturn(pMutexSem, VERR_INVALID_POINTER);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pFastInt));
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync if (pFastInt)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync {
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync pFastInt->u32Magic = RTSEMFASTMUTEX_MAGIC;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync mtx_init(&pFastInt->Mtx, "IPRT Fast Mutex Semaphore", NULL, MTX_DEF | MTX_RECURSE);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync *pMutexSem = pFastInt;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync return VINF_SUCCESS;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync }
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync return VERR_NO_MEMORY;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync}
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncRTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX MutexSem)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync{
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync if (MutexSem == NIL_RTSEMFASTMUTEX) /* don't bitch */
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync return VERR_INVALID_PARAMETER;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync VERR_INVALID_PARAMETER);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync ASMAtomicXchgU32(&pFastInt->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync mtx_destroy(&pFastInt->Mtx);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync RTMemFree(pFastInt);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync return VINF_SUCCESS;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync}
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncRTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX MutexSem)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync{
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync VERR_INVALID_PARAMETER);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync mtx_lock(&pFastInt->Mtx);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync return VINF_SUCCESS;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync}
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsyncRTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX MutexSem)
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync{
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync VERR_INVALID_PARAMETER);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync mtx_unlock(&pFastInt->Mtx);
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync return VINF_SUCCESS;
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync}
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync
a33af978add1a03aab11b2895f441af5cb2a11a6vboxsync