semmutex-win.cpp revision 0d282f291fba8c5597b0cc338058a7976938975c
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 /** 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));
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTLockValidatorInit(&pThis->ValidatorRec, NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, NULL, 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()));
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 TSEMMUTEX_STRICT_POS_DECL The source position of the caller.
0d282f291fba8c5597b0cc338058a7976938975cvboxsyncDECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies, RTSEMMUTEX_STRICT_POS_DECL)
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Validate.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTLockValidatorCheckOrder(&pThis->ValidatorRec, hThreadSelf, RTSEMMUTEX_STRICT_POS_ARGS);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Lock mutex semaphore.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTThreadBlocking(hThreadSelf, RTTHREADSTATE_MUTEX, RTSEMMUTEX_STRICT_BLOCK_ARGS(&pThis->ValidatorRec));
0d282f291fba8c5597b0cc338058a7976938975cvboxsync cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies,
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTThreadUnblocked(hThreadSelf, RTTHREADSTATE_MUTEX);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTThreadWriteLockInc(RTLockValidatorSetOwner(&pThis->ValidatorRec, hThreadSelf, RTSEMMUTEX_STRICT_POS_ARGS));
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)
0d282f291fba8c5597b0cc338058a7976938975cvboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, RTSEMMUTEX_STRICT_POS_ARGS);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync return RTSemMutexRequestNoResumeDebug(MutexSem, cMillies, (uintptr_t)ASMReturnAddress(), RT_SRC_POS);
0d282f291fba8c5597b0cc338058a7976938975cvboxsyncRTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX MutexSem, unsigned cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
0d282f291fba8c5597b0cc338058a7976938975cvboxsync return rtSemMutexRequestNoResume(MutexSem, cMillies, RTSEMMUTEX_STRICT_POS_ARGS);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync return RTSemMutexRequestNoResume(MutexSem, cMillies);
0d282f291fba8c5597b0cc338058a7976938975cvboxsync * Validate.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
5964f60ed7fb52a3c4becbe83c9429f9b2f119c2vboxsync * Unlock mutex semaphore.
0d282f291fba8c5597b0cc338058a7976938975cvboxsync RTThreadWriteLockDec(RTLockValidatorUnsetOwner(&pThis->ValidatorRec));
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertMsgFailed(("%p hThread=%RTthrd\n", pThis, pThis->ValidatorRec.hThread));
0d282f291fba8c5597b0cc338058a7976938975cvboxsync AssertMsgFailed(("%p/%p, rc=%Rrc lasterr=%d\n", pThis, pThis->hMtx, rc, GetLastError()));