tstRTLockValidator.cpp revision ff3996360d63d42b188c1696d4760641a7762ce9
/* $Id$ */
/** @file
* IPRT Testcase - RTLockValidator.
*/
/*
* 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 *
*******************************************************************************/
#include <iprt/lockvalidator.h>
#include <iprt/critsect.h>
#include <iprt/semaphore.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The testcase handle. */
/** Flip this in the debugger to get some peace to single step wild code. */
bool volatile g_fDoNotSpin = false;
static uint32_t g_cThreads;
static uint32_t volatile g_iDeadlockThread;
/**
* Spin until someone else has taken ownership of the critical section.
*
* @returns true on success, false on abort.
* @param pCritSect The critical section.
*/
{
unsigned iLoop = 0;
while (!RTCritSectIsOwned(pCritSect))
{
if (!RTCritSectIsInitialized(pCritSect))
return false;
iLoop++;
}
return true;
}
/**
* Spin until someone else has taken ownership (any kind) of the read-write
* semaphore.
*
* @returns true on success, false on abort.
* @param hSemRW The read-write semaphore.
*/
{
unsigned iLoop = 0;
for (;;)
{
if (RTSemRWGetWriteRecursion(hSemRW) > 0)
return true;
if (RTSemRWGetReadCount(hSemRW) > 0)
return true;
iLoop++;
}
return true;
}
/**
* Waits for a thread to enter a sleeping state.
*
* @returns true on success, false on abort.
* @param hThread The thread.
* @param enmDesiredState The desired thread sleep state.
* @param pvLock The lock it should be sleeping on.
*/
{
{
if (RTTHREAD_IS_SLEEPING(enmState))
{
if ( enmState == enmDesiredState
&& ( !pvLock
return true;
}
else if (enmState != RTTHREADSTATE_RUNNING)
return false;
}
}
/**
* Waits for all the other threads to enter sleeping states.
*
* @returns VINF_SUCCESS on success, VERR_INTERNAL_ERROR on failure.
* @param enmDesiredState The desired thread sleep state.
* @param cWaitOn The distance to the lock they'll be waiting on,
* the lock type is derived from the desired state.
* UINT32_MAX means no special lock.
*/
{
for (uint32_t i = 0; i < g_cThreads; i++)
{
if ( hThread != NIL_RTTHREAD
&& hThread != hThreadSelf)
{
if (cWaitOn != UINT32_MAX)
{
switch (enmDesiredState)
{
case RTTHREADSTATE_RW_WRITE:
default: break;
}
}
if (!fRet)
return VERR_INTERNAL_ERROR;
}
}
return VINF_SUCCESS;
}
/**
* Worker that starts the threads.
*
* @returns Same as RTThreadCreate.
* @param cThreads The number of threads to start.
* @param pfnThread Thread function.
*/
{
uint32_t i;
for (i = 0; i < RT_ELEMENTS(g_ahThreads); i++)
g_ahThreads[i] = NIL_RTTHREAD;
for (i = 0; i < cThreads; i++)
rcCheck);
return VINF_SUCCESS;
}
/**
* Worker that waits for the threads to complete.
*
* @param cMillies How long to wait for each.
* @param fStopOnError Whether to stop on error and heed the thread
* return status.
*/
{
while (i-- > 0)
if (g_ahThreads[i] != NIL_RTTHREAD)
{
int rcThread;
int rc2;
if (RT_SUCCESS(rc2))
g_ahThreads[i] = NIL_RTTHREAD;
return;
}
}
{
{
int rc;
if (i != g_iDeadlockThread)
else
{
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
}
return VINF_SUCCESS;
}
{
int rc;
if (i & 1)
RTTEST_CHECK_RC_RET(g_hTest, RTSemRWRequestWrite(hMine, RT_INDEFINITE_WAIT), VINF_SUCCESS, rcCheck);
else
{
if (i != g_iDeadlockThread)
else
{
if (RT_SUCCESS(rc))
{
if (g_cThreads > 1)
RTTEST_CHECK_RC(g_hTest, rc = RTSemRWRequestWrite(hNext, RT_INDEFINITE_WAIT), VERR_SEM_LV_DEADLOCK);
else
RTTEST_CHECK_RC(g_hTest, rc = RTSemRWRequestWrite(hNext, RT_INDEFINITE_WAIT), VERR_SEM_LV_ILLEGAL_UPGRADE);
}
}
if (RT_SUCCESS(rc))
}
if (i & 1)
else
return VINF_SUCCESS;
}
{
{
}
{
#if 0 /** @todo figure why this ain't working for either of the two tests! */
#endif
if (RT_SUCCESS(rc))
}
{
}
}
{
}
{
}
static bool testIsLockValidationCompiledIn(void)
{
if (rc != VERR_SEM_LV_ILLEGAL_UPGRADE)
fRet = false;
return fRet;
}
int main()
{
/*
* Init.
*/
if (rc)
return rc;
RTLockValidatorSetEnabled(true);
RTLockValidatorSetMayPanic(false);
RTLockValidatorSetQuiet(true);
if (!testIsLockValidationCompiledIn())
return RTTestErrorCount(g_hTest) > 0
RTLockValidatorSetQuiet(false);
/*
* Some initial tests with verbose output.
*/
/*
* More thorough testing without noisy output.
*/
RTLockValidatorSetQuiet(true);
return RTTestSummaryAndDestroy(g_hTest);
}