semmutex-win.cpp revision b79e4344bf4eb8033fd06d560cd864192728bd0b
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). */
0d282f291fba8c5597b0cc338058a7976938975cvboxsync /** Recursion count. */
0d282f291fba8c5597b0cc338058a7976938975cvboxsync /** The owner thread. */
0d282f291fba8c5597b0cc338058a7976938975cvboxsync /** The mutex handle. */
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync /** Lock validator record associated with this mutex. */
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
0d282f291fba8c5597b0cc338058a7976938975cvboxsync return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
0d282f291fba8c5597b0cc338058a7976938975cvboxsyncRTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Create the semaphore.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTSEMMUTEXINTERNAL *pThis = (RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(*pThis));
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync RTLockValidatorRecExclInitV(&pThis->ValidatorRec, hClass, uSubClass, pThis,
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL), pszNameFmt, va);
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);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync ASMAtomicWritePtr((void * volatile *)&pThis->hMtx, (void *)INVALID_HANDLE_VALUE);
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync AssertMsgFailed(("%p rc=%d lasterr=%d\n", pThis->hMtx, rc, GetLastError()));
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
0d282f291fba8c5597b0cc338058a7976938975cvboxsyncRTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync * Validate.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
08870164da59f90379b1ea9b5a2476296c362217vboxsync * Internal worker for RTSemMutexRequestNoResume and it's debug companion.
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync * @returns Same as RTSEmMutexRequestNoResume
08870164da59f90379b1ea9b5a2476296c362217vboxsync * @param hMutexSem The mutex handle.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * @param cMillies The number of milliseconds to wait.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * @param pSrcPos The source position of the caller.
08870164da59f90379b1ea9b5a2476296c362217vboxsyncDECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Validate.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Check for recursive entry.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync ASMAtomicReadHandle(&pThis->hNativeOwner, &hNativeOwner);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Lock mutex semaphore.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true,
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX, true);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync ASMAtomicWriteHandle(&pThis->hNativeOwner, hNativeSelf);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertMsgFailed(("Wait on hMutexSem %p failed, rc=%d lasterr=%d\n", hMutexSem, rc, GetLastError()));
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
return VERR_INTERNAL_ERROR;
#ifndef RTSEMMUTEX_STRICT
RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
return VERR_NOT_OWNER;
#ifdef RTSEMMUTEX_STRICT
return rc9;
return VINF_SUCCESS;
#ifdef RTSEMMUTEX_STRICT
return rc9;
return VINF_SUCCESS;
return rc;