semmutex-win.cpp revision f847566a82fc74d59381a4a27553dbbf8c8061f4
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * IPRT - Mutex Semaphores, Windows.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Copyright (C) 2006-2009 Sun Microsystems, Inc.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * available from http://www.virtualbox.org. This file is free software;
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * you can redistribute it and/or modify it under the terms of the GNU
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * General Public License (GPL) as published by the Free Software
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * The contents of this file may alternatively be used under the terms
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * of the Common Development and Distribution License Version 1.0
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * VirtualBox OSE distribution, in which case the provisions of the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * CDDL are applicable instead of those of the GPL.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * You may elect to license modified versions of this file under the
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * terms and conditions of either the GPL or the CDDL or both.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * additional information or have any questions.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync/*******************************************************************************
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync* Header Files *
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync*******************************************************************************/
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync/*******************************************************************************
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync* Defined Constants And Macros *
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync*******************************************************************************/
0d282f291fba8c5597b0cc338058a7976938975cvboxsync/** Posix internal representation of a Mutex semaphore. */
0d282f291fba8c5597b0cc338058a7976938975cvboxsync /** Magic value (RTSEMMUTEX_MAGIC). */
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync /** Recursion count. */
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync /** The owner thread. */
0d282f291fba8c5597b0cc338058a7976938975cvboxsync /** The mutex handle. */
0d282f291fba8c5597b0cc338058a7976938975cvboxsync /** Lock validator record associated with this mutex. */
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync/* Undefine debug mappings. */
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Create the semaphore.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTSEMMUTEXINTERNAL *pThis = (RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(*pThis));
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync RTLockValidatorRecExclInit(&pThis->ValidatorRec, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, "RTSemMutex", pThis);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Validate.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Close semaphore handle.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync ASMAtomicWritePtr((void * volatile *)&pThis->hMtx, (void *)INVALID_HANDLE_VALUE);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertMsgFailed(("%p rc=%d lasterr=%d\n", pThis->hMtx, rc, GetLastError()));
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Internal worker for RTSemMutexRequestNoResume and it's debug companion.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * @returns Same as RTSEmMutexRequestNoResume
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync * @param MutexSem The mutex handle.
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync * @param cMillies The number of milliseconds to wait.
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync * @param pSrcPos The source position of the caller.
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsyncDECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies, PCRTLOCKVALSRCPOS pSrcPos)
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Validate.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync * Check for recursive entry.
f847566a82fc74d59381a4a27553dbbf8c8061f4vboxsync ASMAtomicReadHandle(&pThis->hNativeOwner, &hNativeOwner);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Lock mutex semaphore.
c364b6c44a252ab65b514fa8f9a665cc5e33a1cevboxsync int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true, RTTHREADSTATE_MUTEX);
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
08870164da59f90379b1ea9b5a2476296c362217vboxsync RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync ASMAtomicWriteHandle(&pThis->hNativeOwner, hNativeSelf);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d lasterr=%d\n", MutexSem, rc, GetLastError()));
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
0d282f291fba8c5597b0cc338058a7976938975cvboxsyncRTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, NULL);
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, &SrcPos);
0d282f291fba8c5597b0cc338058a7976938975cvboxsyncRTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, &SrcPos);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Validate.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync * Check ownership and recursions.
f847566a82fc74d59381a4a27553dbbf8c8061f4vboxsync ASMAtomicReadHandle(&pThis->hNativeOwner, &hNativeOwner);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync AssertMsgFailed(("Not owner of mutex %p!! hNativeSelf=%RTntrd Owner=%RTntrd cRecursions=%d\n",
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync pThis, hNativeSelf, hNativeOwner, pThis->cRecursions));
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync int rc9 = RTLockValidatorRecExclUnwind(&pThis->ValidatorRec);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Unlock mutex semaphore.
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, false);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync ASMAtomicWriteHandle(&pThis->hNativeOwner, NIL_RTNATIVETHREAD);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertMsgFailed(("%p/%p, rc=%Rrc lasterr=%d\n", pThis, pThis->hMtx, rc, GetLastError()));
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync * Validate.
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);