semmutex-r0drv-linux.c revision 9df048d43d767b5806f47a07c6914364760ead96
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;
9df048d43d767b5806f47a07c6914364760ead96vboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
9df048d43d767b5806f47a07c6914364760ead96vboxsync return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
9df048d43d767b5806f47a07c6914364760ead96vboxsyncRTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
9df048d43d767b5806f47a07c6914364760ead96vboxsync RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
9df048d43d767b5806f47a07c6914364760ead96vboxsync AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
9df048d43d767b5806f47a07c6914364760ead96vboxsync pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsyncAssertReleaseMsgFailed(("This mutex implementation is buggy, fix it!\n"));
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Validate input.
9df048d43d767b5806f47a07c6914364760ead96vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
9df048d43d767b5806f47a07c6914364760ead96vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Invalidate it and signal the object just in case.
9df048d43d767b5806f47a07c6914364760ead96vboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsyncRTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
9df048d43d767b5806f47a07c6914364760ead96vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Validate input.
9df048d43d767b5806f47a07c6914364760ead96vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Check for recursive request.
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Try aquire it.
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. */
9df048d43d767b5806f47a07c6914364760ead96vboxsync prepare_to_wait(&pThis->Head, &Wait, TASK_INTERRUPTIBLE);
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync /* check the condition. */
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.
9df048d43d767b5806f47a07c6914364760ead96vboxsync PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
9df048d43d767b5806f47a07c6914364760ead96vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
9df048d43d767b5806f47a07c6914364760ead96vboxsync AssertReturn(pThis->pOwner == current, VERR_NOT_OWNER);
a8e0cb7ddfd05bcabf5e18959cf09e723f254663vboxsync * Release the mutex.