semmutex-win.cpp revision 4b0369e0c9bcae37f2801e0f7b92509bbbaf4bec
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 /** The mutex handle. */
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync /** Lock validator record associated with this mutex. */
0d282f291fba8c5597b0cc338058a7976938975cvboxsync/* Undefine debug mappings. */
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Create the semaphore.
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync RTSEMMUTEXINTERNAL *pThis = (RTSEMMUTEXINTERNAL *)RTMemAlloc(sizeof(*pThis));
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTLockValidatorInit(&pThis->ValidatorRec, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, NULL, pThis);
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync * Validate.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Close semaphore handle.
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync ASMAtomicWritePtr((void * volatile *)&pThis->hMtx, (void *)INVALID_HANDLE_VALUE);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync AssertMsgFailed(("%p rc=%d lasterr=%d\n", pThis->hMtx, rc, GetLastError()));
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Internal worker for RTSemMutexRequestNoResume and it's debug companion.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * @returns Same as RTSEmMutexRequestNoResume
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * @param MutexSem The mutex handle.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * @param cMillies The number of milliseconds to wait.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * @param pSrcPos The source position of the caller.
eaae6c3ce81916ccc4e74af4b55972ba956cf5c8vboxsyncDECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies, PCRTLOCKVALIDATORSRCPOS pSrcPos)
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Validate.
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync RTLockValidatorCheckOrder(&pThis->ValidatorRec, hThreadSelf, pSrcPos);
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync * Lock mutex semaphore.
549e45e906cbe16cbeb2704c3a7354aa8ab022b4vboxsync int rc9 = RTLockValidatorCheckBlocking(&pThis->ValidatorRec, hThreadSelf,
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX);
4b0369e0c9bcae37f2801e0f7b92509bbbaf4becvboxsync cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
82ded957f5370e7d4b8050fe3ac2117ddebac02avboxsync RTLockValidatorSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos);
f847566a82fc74d59381a4a27553dbbf8c8061f4vboxsync AssertMsgFailed(("Wait on MutexSem %p failed, rc=%d lasterr=%d\n", MutexSem, rc, GetLastError()));
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsyncRTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, NULL);
08870164da59f90379b1ea9b5a2476296c362217vboxsync RTLOCKVALIDATORSRCPOS SrcPos = RTLOCKVALIDATORSRCPOS_INIT_NORMAL_API();
08870164da59f90379b1ea9b5a2476296c362217vboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, &SrcPos);
08870164da59f90379b1ea9b5a2476296c362217vboxsyncRTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
e75313c24325ec7bec84fa349abb39e246b6cc86vboxsync RTLOCKVALIDATORSRCPOS SrcPos = RTLOCKVALIDATORSRCPOS_INIT_DEBUG_API();
0fcf82b2591711fa8980e8f5d9cad1b8f222d6d7vboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, &SrcPos);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Validate.
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
fff1586cd63d9b86dc6e14cb958a146446a60042vboxsync * Unlock mutex semaphore.
7f22d63b8c416918f318efcd1b28b883d934e84dvboxsync AssertMsgFailed(("%p hThread=%RTthrd\n", pThis, pThis->ValidatorRec.hThread));
7f22d63b8c416918f318efcd1b28b883d934e84dvboxsync AssertMsgFailed(("%p/%p, rc=%Rrc lasterr=%d\n", pThis, pThis->hMtx, rc, GetLastError()));