semmutex-r0drv-linux.c revision 8f79fbb852ac192bf1b2ee23594f933662d14632
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Mutex Semaphores, Ring-0 Driver, Linux.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * available from http://www.virtualbox.org. This file is free software;
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * 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 * 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 * 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.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync/*******************************************************************************
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync* Header Files *
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync*******************************************************************************/
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync/*******************************************************************************
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync* Structures and Typedefs *
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync*******************************************************************************/
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Linux mutex semaphore.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /** Magic value (RTSEMMUTEX_MAGIC). */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /** Number of recursive locks - 0 if not owned by anyone, > 0 if owned. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /** The wait queue. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /** The current owner. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync void * volatile pOwner;
8f79fbb852ac192bf1b2ee23594f933662d14632vboxsync/* Undefine debug mappings. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsyncAssertReleaseMsgFailed(("This mutex implementation is buggy, fix it!\n"));
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Validate input.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Invalidate it and signal the object just in case.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsyncRTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Validate input.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Check for recursive request.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Try aquire it.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync if (ASMAtomicCmpXchgU32(&pMutexInt->cRecursion, 1, 0))
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Ok wait for it.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /* make everything thru schedule() atomic scheduling wise. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync prepare_to_wait(&pMutexInt->Head, &Wait, TASK_INTERRUPTIBLE);
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /* check the condition. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync if (ASMAtomicCmpXchgU32(&pMutexInt->cRecursion, 1, 0))
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /* check for pending signals. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync rc = VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /* Check if someone destroyed the semaphore while we was waiting. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /* check for timeout. */
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Validate input.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync AssertMsgFailed(("Not owner, pOwner=%p current=%p\n", (void *)pMutexInt->pOwner, (void *)current));
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Release the mutex.