semmutex-win.cpp revision 2e42e0850e182e37277fe28ba5b5d1c37018e783
/* $Id$ */
/** @file
* IPRT - Mutex Semaphores, Windows.
*/
/*
* Copyright (C) 2006-2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_SEMAPHORE
#include <Windows.h>
#include <iprt/semaphore.h>
#include <iprt/lockvalidator.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Posix internal representation of a Mutex semaphore. */
struct RTSEMMUTEXINTERNAL
{
/** Magic value (RTSEMMUTEX_MAGIC). */
/** Recursion count. */
uint32_t volatile cRecursions;
/** The owner thread. */
RTNATIVETHREAD volatile hNativeOwner;
/** The mutex handle. */
#ifdef RTSEMMUTEX_STRICT
/** Lock validator record associated with this mutex. */
#endif
};
{
return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
}
{
/*
* Create the semaphore.
*/
int rc;
if (hMtx)
{
if (pThis)
{
pThis->cRecursions = 0;
#ifdef RTSEMMUTEX_STRICT
if (!pszNameFmt)
{
static uint32_t volatile s_iMutexAnon = 0;
}
else
{
}
#endif
*phMutexSem = pThis;
return VINF_SUCCESS;
}
rc = VERR_NO_MEMORY;
}
else
return rc;
}
{
/*
* Validate.
*/
if (pThis == NIL_RTSEMMUTEX)
return VINF_SUCCESS;
/*
* Close semaphore handle.
*/
AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
int rc = VINF_SUCCESS;
if (!CloseHandle(hMtx))
{
}
#ifdef RTSEMMUTEX_STRICT
#endif
return rc;
}
{
#ifdef RTSEMMUTEX_STRICT
/*
* Validate.
*/
#else
return RTLOCKVAL_SUB_CLASS_INVALID;
#endif
}
/**
* Internal worker for RTSemMutexRequestNoResume and it's debug companion.
*
* @returns Same as RTSEmMutexRequestNoResume
* @param hMutexSem The mutex handle.
* @param cMillies The number of milliseconds to wait.
* @param pSrcPos The source position of the caller.
*/
DECL_FORCE_INLINE(int) rtSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, PCRTLOCKVALSRCPOS pSrcPos)
{
/*
* Validate.
*/
/*
* Check for recursive entry.
*/
if (hNativeOwner == hNativeSelf)
{
#ifdef RTSEMMUTEX_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
return VINF_SUCCESS;
}
/*
* Lock mutex semaphore.
*/
if (cMillies > 0)
{
#ifdef RTSEMMUTEX_STRICT
int rc9 = RTLockValidatorRecExclCheckOrderAndBlocking(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true,
cMillies, RTTHREADSTATE_MUTEX, true);
if (RT_FAILURE(rc9))
return rc9;
#else
hThreadSelf = RTThreadSelf();
#endif
}
TRUE /*fAlertable*/);
switch (rc)
{
case WAIT_OBJECT_0:
#ifdef RTSEMMUTEX_STRICT
#endif
return VINF_SUCCESS;
case WAIT_TIMEOUT: return VERR_TIMEOUT;
case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
default:
case WAIT_FAILED:
{
AssertMsgFailed(("Wait on hMutexSem %p failed, rc=%d lasterr=%d\n", hMutexSem, rc, GetLastError()));
if (rc2 != VINF_SUCCESS)
return rc2;
return VERR_INTERNAL_ERROR;
}
}
}
{
#ifndef RTSEMMUTEX_STRICT
#else
#endif
}
RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
}
{
/*
* Validate.
*/
/*
* Check ownership and recursions.
*/
{
AssertMsgFailed(("Not owner of mutex %p!! hNativeSelf=%RTntrd Owner=%RTntrd cRecursions=%d\n",
return VERR_NOT_OWNER;
}
{
#ifdef RTSEMMUTEX_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
return VINF_SUCCESS;
}
/*
* Unlock mutex semaphore.
*/
#ifdef RTSEMMUTEX_STRICT
if (RT_FAILURE(rc9))
return rc9;
#endif
return VINF_SUCCESS;
return rc;
}
{
/*
* Validate.
*/
AssertPtrReturn(pThis, false);
return hNativeOwner != NIL_RTNATIVETHREAD;
}