semmutex-win.cpp revision 08870164da59f90379b1ea9b5a2476296c362217
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * IPRT - Mutex Semaphores, Windows.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Copyright (C) 2006-2009 Sun Microsystems, Inc.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * available from http://www.virtualbox.org. This file is free software;
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * you can redistribute it and/or modify it under the terms of the GNU
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * General Public License (GPL) as published by the Free Software
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * The contents of this file may alternatively be used under the terms
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * of the Common Development and Distribution License Version 1.0
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * VirtualBox OSE distribution, in which case the provisions of the
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * CDDL are applicable instead of those of the GPL.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * You may elect to license modified versions of this file under the
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * terms and conditions of either the GPL or the CDDL or both.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * additional information or have any questions.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync/*******************************************************************************
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync* Header Files *
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync*******************************************************************************/
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync/*******************************************************************************
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync* Defined Constants And Macros *
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync*******************************************************************************/
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync/** Posix internal representation of a Mutex semaphore. */
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync /** Magic value (RTSEMMUTEX_MAGIC). */
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync /** The mutex handle. */
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync /** Lock validator record associated with this mutex. */
e170d0511a1456f1677c93e03a78d7162ae82472vboxsync/* Undefine debug mappings. */
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Create the semaphore.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync RTSEMMUTEXINTERNAL *pThis = (RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(*pThis));
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync RTLockValidatorInit(&pThis->ValidatorRec, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, NULL, pThis);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Validate.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Close semaphore handle.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync ASMAtomicWritePtr((void * volatile *)&pThis->hMtx, (void *)INVALID_HANDLE_VALUE);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertMsgFailed(("%p rc=%d lasterr=%d\n", pThis->hMtx, rc, GetLastError()));
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Internal worker for RTSemMutexRequestNoResume and it's debug companion.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * @returns Same as RTSEmMutexRequestNoResume
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * @param MutexSem The mutex handle.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * @param cMillies The number of milliseconds to wait.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * @param TSEMMUTEX_STRICT_POS_DECL The source position of the caller.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsyncDECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies, RTSEMMUTEX_STRICT_POS_DECL)
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Validate.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync RTLockValidatorCheckOrder(&pThis->ValidatorRec, hThreadSelf, RTSEMMUTEX_STRICT_POS_ARGS);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Lock mutex semaphore.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync int rc9 = RTLockValidatorCheckBlocking(&pThis->ValidatorRec, hThreadSelf,
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync RTLockValidatorSetOwner(&pThis->ValidatorRec, hThreadSelf, RTSEMMUTEX_STRICT_POS_ARGS);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d lasterr=%d\n", MutexSem, rc, GetLastError()));
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
e4f367251aede667a6de69baa54ef9eb5f150871vboxsyncRTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, RTSEMMUTEX_STRICT_POS_ARGS);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync return RTSemMutexRequestNoResumeDebug(MutexSem, cMillies, (uintptr_t)ASMReturnAddress(), RT_SRC_POS);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsyncRTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, RTSEMMUTEX_STRICT_POS_ARGS);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync return RTSemMutexRequestNoResume(MutexSem, cMillies);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Validate.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Unlock mutex semaphore.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertMsgFailed(("%p hThread=%RTthrd\n", pThis, pThis->ValidatorRec.hThread));
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertMsgFailed(("%p/%p, rc=%Rrc lasterr=%d\n", pThis, pThis->hMtx, rc, GetLastError()));