semmutex-r0drv-freebsd.c revision 90e8b000bdf708e8465e9c2cd7ecf1339c27c4aa
c936b605260fac01308e3f63b869f401321c52b3vboxsync/* $Id$ */
c936b605260fac01308e3f63b869f401321c52b3vboxsync/** @file
c936b605260fac01308e3f63b869f401321c52b3vboxsync * IPRT - Mutex Semaphores, Ring-0 Driver, FreeBSD.
c936b605260fac01308e3f63b869f401321c52b3vboxsync */
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync/*
c936b605260fac01308e3f63b869f401321c52b3vboxsync * Copyright (C) 2010 Sun Microsystems, Inc.
c936b605260fac01308e3f63b869f401321c52b3vboxsync *
c936b605260fac01308e3f63b869f401321c52b3vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c936b605260fac01308e3f63b869f401321c52b3vboxsync * available from http://www.virtualbox.org. This file is free software;
c936b605260fac01308e3f63b869f401321c52b3vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c936b605260fac01308e3f63b869f401321c52b3vboxsync * General Public License (GPL) as published by the Free Software
c936b605260fac01308e3f63b869f401321c52b3vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c936b605260fac01308e3f63b869f401321c52b3vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c936b605260fac01308e3f63b869f401321c52b3vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c936b605260fac01308e3f63b869f401321c52b3vboxsync *
c936b605260fac01308e3f63b869f401321c52b3vboxsync * The contents of this file may alternatively be used under the terms
c936b605260fac01308e3f63b869f401321c52b3vboxsync * of the Common Development and Distribution License Version 1.0
c936b605260fac01308e3f63b869f401321c52b3vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
c936b605260fac01308e3f63b869f401321c52b3vboxsync * VirtualBox OSE distribution, in which case the provisions of the
c936b605260fac01308e3f63b869f401321c52b3vboxsync * CDDL are applicable instead of those of the GPL.
c936b605260fac01308e3f63b869f401321c52b3vboxsync *
c936b605260fac01308e3f63b869f401321c52b3vboxsync * You may elect to license modified versions of this file under the
c936b605260fac01308e3f63b869f401321c52b3vboxsync * terms and conditions of either the GPL or the CDDL or both.
c936b605260fac01308e3f63b869f401321c52b3vboxsync *
c936b605260fac01308e3f63b869f401321c52b3vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
c936b605260fac01308e3f63b869f401321c52b3vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
c936b605260fac01308e3f63b869f401321c52b3vboxsync * additional information or have any questions.
c936b605260fac01308e3f63b869f401321c52b3vboxsync */
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync/*******************************************************************************
c936b605260fac01308e3f63b869f401321c52b3vboxsync* Header Files *
c936b605260fac01308e3f63b869f401321c52b3vboxsync*******************************************************************************/
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include "the-freebsd-kernel.h"
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include "internal/iprt.h"
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include <iprt/semaphore.h>
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include <iprt/asm.h>
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include <iprt/assert.h>
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include <iprt/err.h>
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include <iprt/mem.h>
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include <iprt/thread.h>
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include <iprt/time.h>
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync#include "internal/magics.h"
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync/*******************************************************************************
c936b605260fac01308e3f63b869f401321c52b3vboxsync* Structures and Typedefs *
c936b605260fac01308e3f63b869f401321c52b3vboxsync*******************************************************************************/
c936b605260fac01308e3f63b869f401321c52b3vboxsync/**
c936b605260fac01308e3f63b869f401321c52b3vboxsync * Wrapper for the FreeBSD (sleep) mutex.
c936b605260fac01308e3f63b869f401321c52b3vboxsync */
c936b605260fac01308e3f63b869f401321c52b3vboxsynctypedef struct RTSEMMUTEXINTERNAL
c936b605260fac01308e3f63b869f401321c52b3vboxsync{
c936b605260fac01308e3f63b869f401321c52b3vboxsync /** Magic value (RTSEMMUTEX_MAGIC). */
c936b605260fac01308e3f63b869f401321c52b3vboxsync uint32_t u32Magic;
c936b605260fac01308e3f63b869f401321c52b3vboxsync /** The FreeBSD shared/exclusive lock mutex. */
c936b605260fac01308e3f63b869f401321c52b3vboxsync struct sx SxLock;
c936b605260fac01308e3f63b869f401321c52b3vboxsync} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
c936b605260fac01308e3f63b869f401321c52b3vboxsync{
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertPtrReturn(phMutexSem, VERR_INVALID_POINTER);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (pThis)
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync pThis->u32Magic = RTSEMMUTEX_MAGIC;
5ea40b04201696e445c4eeb0d1a238543aeb9167vboxsync sx_init_flags(&pThis->SxLock, "IPRT Mutex Semaphore", SX_LOCK_RECURSED);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync *phMutexSem = pThis;
c936b605260fac01308e3f63b869f401321c52b3vboxsync return VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync return VERR_NO_MEMORY;
c936b605260fac01308e3f63b869f401321c52b3vboxsync}
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsyncRTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
c936b605260fac01308e3f63b869f401321c52b3vboxsync{
c936b605260fac01308e3f63b869f401321c52b3vboxsync PRTSEMMUTEXINTERNAL pThis = hMutexSem;
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (pThis == NIL_RTSEMMUTEX)
c936b605260fac01308e3f63b869f401321c52b3vboxsync return VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync sx_destroy(&pThis->SxLock);
c936b605260fac01308e3f63b869f401321c52b3vboxsync RTMemFree(pThis);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync return VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync}
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync#undef RTSemMutexRequest
c936b605260fac01308e3f63b869f401321c52b3vboxsyncRTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
c936b605260fac01308e3f63b869f401321c52b3vboxsync{
c936b605260fac01308e3f63b869f401321c52b3vboxsync PRTSEMMUTEXINTERNAL pThis = hMutexSem;
c936b605260fac01308e3f63b869f401321c52b3vboxsync int rc;
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (cMillies == RT_INDEFINITE_WAIT)
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync sx_xlock(&pThis->SxLock);
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync else if (!cMillies)
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (sx_try_xlock(&pThis->SxLock))
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync else
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VERR_TIMEOUT;
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync /*
c936b605260fac01308e3f63b869f401321c52b3vboxsync * GROSS HACK: poll implementation of timeout.
c936b605260fac01308e3f63b869f401321c52b3vboxsync */
c936b605260fac01308e3f63b869f401321c52b3vboxsync /** @todo Implement timeouts in RTSemMutexRequest. */
c936b605260fac01308e3f63b869f401321c52b3vboxsync else if (sx_try_xlock(&pThis->SxLock))
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync else
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync uint64_t StartTS = RTTimeSystemMilliTS();
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VERR_TIMEOUT;
c936b605260fac01308e3f63b869f401321c52b3vboxsync do
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync RTThreadSleep(1);
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (sx_try_xlock(&pThis->SxLock))
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync break;
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync } while (RTTimeSystemMilliTS() - StartTS < cMillies);
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync return VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync}
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsyncRTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync{
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync return RTSemMutexRequest(hMutexSem, cMillies);
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync}
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync#undef RTSemMutexRequestNoResume
c936b605260fac01308e3f63b869f401321c52b3vboxsyncRTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
c936b605260fac01308e3f63b869f401321c52b3vboxsync{
c936b605260fac01308e3f63b869f401321c52b3vboxsync PRTSEMMUTEXINTERNAL pThis = hMutexSem;
c936b605260fac01308e3f63b869f401321c52b3vboxsync int rc;
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (cMillies == RT_INDEFINITE_WAIT)
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (!sx_xlock_sig(&pThis->SxLock))
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync else
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VERR_INTERRUPTED;
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync else if (!cMillies)
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (sx_try_xlock(&pThis->SxLock))
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync else
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VERR_TIMEOUT;
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync /*
c936b605260fac01308e3f63b869f401321c52b3vboxsync * GROSS HACK: poll implementation of timeout.
c936b605260fac01308e3f63b869f401321c52b3vboxsync */
c936b605260fac01308e3f63b869f401321c52b3vboxsync /** @todo Implement timeouts and interrupt checks in
c936b605260fac01308e3f63b869f401321c52b3vboxsync * RTSemMutexRequestNoResume. */
c936b605260fac01308e3f63b869f401321c52b3vboxsync else if (sx_try_xlock(&pThis->SxLock))
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync else
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync uint64_t StartTS = RTTimeSystemMilliTS();
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VERR_TIMEOUT;
c936b605260fac01308e3f63b869f401321c52b3vboxsync do
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync RTThreadSleep(1);
c936b605260fac01308e3f63b869f401321c52b3vboxsync if (sx_try_xlock(&pThis->SxLock))
c936b605260fac01308e3f63b869f401321c52b3vboxsync {
c936b605260fac01308e3f63b869f401321c52b3vboxsync rc = VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync break;
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync } while (RTTimeSystemMilliTS() - StartTS < cMillies);
c936b605260fac01308e3f63b869f401321c52b3vboxsync }
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync return VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync}
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsyncRTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync{
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync return RTSemMutexRequestNoResume(hMutexSem, cMillies);
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync}
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync
90e8b000bdf708e8465e9c2cd7ecf1339c27c4aavboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsyncRTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
c936b605260fac01308e3f63b869f401321c52b3vboxsync{
c936b605260fac01308e3f63b869f401321c52b3vboxsync PRTSEMMUTEXINTERNAL pThis = hMutexSem;
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync sx_xunlock(&pThis->SxLock);
c936b605260fac01308e3f63b869f401321c52b3vboxsync return VINF_SUCCESS;
c936b605260fac01308e3f63b869f401321c52b3vboxsync}
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsyncRTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
c936b605260fac01308e3f63b869f401321c52b3vboxsync{
c936b605260fac01308e3f63b869f401321c52b3vboxsync PRTSEMMUTEXINTERNAL pThis = hMutexSem;
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertPtrReturn(pThis, false);
c936b605260fac01308e3f63b869f401321c52b3vboxsync AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), false);
c936b605260fac01308e3f63b869f401321c52b3vboxsync
c936b605260fac01308e3f63b869f401321c52b3vboxsync return sx_xlocked(&pThis->SxLock);
c936b605260fac01308e3f63b869f401321c52b3vboxsync}
c936b605260fac01308e3f63b869f401321c52b3vboxsync