semmutex-win.cpp revision 549e45e906cbe16cbeb2704c3a7354aa8ab022b4
77b1a2d8b5dbe2c0b5200794914239fee3c8ee5dvboxsync * IPRT - Mutex Semaphores, Windows.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2009 Sun Microsystems, Inc.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * available from http://www.virtualbox.org. This file is free software;
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * 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.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * The contents of this file may alternatively be used under the terms
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * of the Common Development and Distribution License Version 1.0
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * VirtualBox OSE distribution, in which case the provisions of the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * CDDL are applicable instead of those of the GPL.
cd6f71bc352f550074f1ba2c830a2cf2f0b3dd46vboxsync * You may elect to license modified versions of this file under the
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * terms and conditions of either the GPL or the CDDL or both.
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
16a9adc14900ca18e6909679a579f6833425e030vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * additional information or have any questions.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync/*******************************************************************************
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync* Header Files *
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync*******************************************************************************/
efff36b306e370346025647a158689021df2e1d1vboxsync/*******************************************************************************
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync* Defined Constants And Macros *
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync*******************************************************************************/
efff36b306e370346025647a158689021df2e1d1vboxsync/** Posix internal representation of a Mutex semaphore. */
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync /** Magic value (RTSEMMUTEX_MAGIC). */
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync /** Recursion count. */
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync /** The owner thread. */
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync /** The mutex handle. */
9496f2d398b49813176939d7a339ae513d5175efvboxsync /** Lock validator record associated with this mutex. */
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsyncRTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsyncRTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * Create the semaphore.
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync RTSEMMUTEXINTERNAL *pThis = (RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(*pThis));
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync RTLockValidatorRecExclInitV(&pThis->ValidatorRec, hClass, uSubClass, pThis,
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync !(fFlags & RTSEMMUTEX_FLAGS_NO_LOCK_VAL), pszNameFmt, va);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Validate.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * 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);
9496f2d398b49813176939d7a339ae513d5175efvboxsync AssertMsgFailed(("%p rc=%d lasterr=%d\n", pThis->hMtx, rc, GetLastError()));
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTLockValidatorRecExclDelete(&pThis->ValidatorRec);
16a9adc14900ca18e6909679a579f6833425e030vboxsyncRTDECL(uint32_t) RTSemMutexSetSubClass(RTSEMMUTEX hMutexSem, uint32_t uSubClass)
16a9adc14900ca18e6909679a579f6833425e030vboxsync * Validate.
63c12491acc2b8b95c8ac454f1c48b98eec8f7d8vboxsync AssertPtrReturn(pThis, RTLOCKVAL_SUB_CLASS_INVALID);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, RTLOCKVAL_SUB_CLASS_INVALID);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync return RTLockValidatorRecExclSetSubClass(&pThis->ValidatorRec, uSubClass);
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * Internal worker for RTSemMutexRequestNoResume and it's debug companion.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * @returns Same as RTSEmMutexRequestNoResume
b5ad839a3757b305d4e98d7264da2b53c9cd27f0vboxsync * @param MutexSem The mutex handle.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync * @param cMillies The number of milliseconds to wait.
61b5982fad4660d0fe3dd6ceba9eda85eb32f7e8vboxsync * @param pSrcPos The source position of the caller.
681fd85cc7cd49e9cf66a917d6ae9ff36eb7d9e9vboxsyncDECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies, PCRTLOCKVALSRCPOS pSrcPos)
681fd85cc7cd49e9cf66a917d6ae9ff36eb7d9e9vboxsync * Validate.
e4f367251aede667a6de69baa54ef9eb5f150871vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * Check for recursive entry.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync ASMAtomicReadHandle(&pThis->hNativeOwner, &hNativeOwner);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * Lock mutex semaphore.
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true,
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX, true);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
16a9adc14900ca18e6909679a579f6833425e030vboxsync RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
16a9adc14900ca18e6909679a579f6833425e030vboxsync ASMAtomicWriteHandle(&pThis->hNativeOwner, hNativeSelf);
16a9adc14900ca18e6909679a579f6833425e030vboxsync AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d lasterr=%d\n", MutexSem, rc, GetLastError()));
16a9adc14900ca18e6909679a579f6833425e030vboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsyncRTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, NULL);
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, &SrcPos);
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsyncRTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, &SrcPos);
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * Validate.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync * Check ownership and recursions.
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync ASMAtomicReadHandle(&pThis->hNativeOwner, &hNativeOwner);
a9d49c8f2b28a72e6a4db86eee91e4569290157bvboxsync AssertMsgFailed(("Not owner of mutex %p!! hNativeSelf=%RTntrd Owner=%RTntrd cRecursions=%d\n",
1bf495e3eec00dd79cecb6b36ef2a97f422c3737vboxsync pThis, hNativeSelf, hNativeOwner, pThis->cRecursions));
9496f2d398b49813176939d7a339ae513d5175efvboxsync int rc9 = RTLockValidatorRecExclUnwind(&pThis->ValidatorRec);
aa834e89e076db44fa8fe82d177748f0a45d14c2vboxsync * Unlock mutex semaphore.
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, false);
5341459ca931b65de60b5af2a4cba6836b6b45cavboxsync ASMAtomicWriteHandle(&pThis->hNativeOwner, NIL_RTNATIVETHREAD);
cc15c3fa4bb2d3fb91e4d0cd15a73133963f86b0vboxsync AssertMsgFailed(("%p/%p, rc=%Rrc lasterr=%d\n", pThis, pThis->hMtx, rc, GetLastError()));
bbede9c189def47a9880f0ffb03c0c230c774185vboxsync * Validate.
9496f2d398b49813176939d7a339ae513d5175efvboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, false);