5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * IPRT - Mutex Semaphores, Windows.
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2010 Oracle Corporation
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/*******************************************************************************
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. */
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsyncRTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Create the semaphore.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTSEMMUTEXINTERNAL *pThis = (RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(*pThis));
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync RTLockValidatorRecExclInit(&pThis->ValidatorRec, hClass, uSubClass, pThis,
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync "RTSemMutex-%u", ASMAtomicIncU32(&s_iMutexAnon) - 1);
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync RTLockValidatorRecExclInitV(&pThis->ValidatorRec, hClass, uSubClass, pThis,
2e42e0850e182e37277fe28ba5b5d1c37018e783vboxsync !(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);
c7ff622115966b69b482bd2896662e40d823b22fvboxsync ASMAtomicWritePtr(&pThis->hMtx, INVALID_HANDLE_VALUE);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertMsgFailed(("%p rc=%d lasterr=%d\n", pThis->hMtx, rc, GetLastError()));
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsyncRTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync * Validate.
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Internal worker for RTSemMutexRequestNoResume and it's debug companion.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * @returns Same as RTSEmMutexRequestNoResume
82ded957f5370e7d4b8050fe3ac2117ddebac02avboxsync * @param hMutexSem The mutex handle.
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync * @param cMillies The number of milliseconds to wait.
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync * @param pSrcPos The source position of the caller.
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsyncDECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL 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.
0fcf82b2591711fa8980e8f5d9cad1b8f222d6d7vboxsync int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true,
0fcf82b2591711fa8980e8f5d9cad1b8f222d6d7vboxsync RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX, true);
7f22d63b8c416918f318efcd1b28b883d934e84dvboxsync cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
08870164da59f90379b1ea9b5a2476296c362217vboxsync RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync ASMAtomicWriteHandle(&pThis->hNativeOwner, hNativeSelf);
82ded957f5370e7d4b8050fe3ac2117ddebac02avboxsync 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));
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsyncRTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
82ded957f5370e7d4b8050fe3ac2117ddebac02avboxsync return rtSemMutexRequestNoResume(hMutexSem, cMillies, NULL);
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
82ded957f5370e7d4b8050fe3ac2117ddebac02avboxsync return rtSemMutexRequestNoResume(hMutexSem, cMillies, &SrcPos);
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsyncRTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
82ded957f5370e7d4b8050fe3ac2117ddebac02avboxsync return rtSemMutexRequestNoResume(hMutexSem, 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()));
82ded957f5370e7d4b8050fe3ac2117ddebac02avboxsyncRTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync * Validate.
a84b1f249b9c68d3a05455fe946d02cc9a6772b5vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);