lockvalidator.cpp revision f0dd841cb99154da8ec0a31cae2f89044895a23f
/* $Id$ */
/** @file
* IPRT - Lock Validator.
*/
/*
* Copyright (C) 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/semaphore.h>
#include "internal/lockvalidator.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
{
pRec->cRecursion = 0;
}
{
if (!pRec)
return VERR_NO_MEMORY;
return VINF_SUCCESS;
}
{
}
{
if (pRec)
{
}
}
RTDECL(int) RTLockValidatorCheckOrder(PRTLOCKVALIDATORREC pRec, RTTHREAD hThread, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
/*
* Check it locks we're currently holding.
*/
/** @todo later */
/*
* If missing order rules, add them.
*/
return VINF_SUCCESS;
}
RTDECL(RTTHREAD) RTLockValidatorSetOwner(PRTLOCKVALIDATORREC pRec, RTTHREAD hThread, RTHCUINTPTR uId, RT_SRC_POS_DECL)
{
if (hThread == NIL_RTTHREAD)
pRec->cRecursion++;
else
{
/*
* Update the record.
*/
/*
* Push the lock onto the lock stack.
*/
/** @todo push it onto the per-thread lock stack. */
}
return hThread;
}
{
{
/*
* Pop (remove) the lock.
*/
/*
* Update the record.
*/
}
return hThread;
}
{
if (Thread == NIL_RTTHREAD)
return 0;
if (!pThread)
return VERR_INVALID_HANDLE;
return cWriteLocks;
}
{
}
{
}
{
if (Thread == NIL_RTTHREAD)
return 0;
if (!pThread)
return VERR_INVALID_HANDLE;
return cReadLocks;
}
{
}
{
}
/**
* Bitch about a deadlock.
*
* @param pRec The lock validator record we're going to block on.
* @param pThread This thread.
* @param pCur The thread we're deadlocking with.
* @param enmState The sleep state.
* @param RT_SRC_POS_DECL Where we are going to deadlock.
* @param uId Where we are going to deadlock.
*/
static void rtLockValidatorComplainAboutDeadlock(PRTLOCKVALIDATORREC pRec, PRTTHREADINT pThread, RTTHREADSTATE enmState,
{
AssertMsg1(pCur == pThread ? "!!Deadlock detected!!" : "!!Deadlock exists!!", iLine, pszFile, pszFunction);
/*
* Print the threads and locks involved.
*/
unsigned iSeenThread = 0;
{
/*
* Print info on pCur. Determin next while doing so.
*/
AssertMsg2(" #%u: %RTthrd/%RTnthrd %s: %s(%u) %RTptr\n",
switch (enmCurState)
{
case RTTHREADSTATE_CRITSECT:
case RTTHREADSTATE_EVENT:
case RTTHREADSTATE_FAST_MUTEX:
case RTTHREADSTATE_MUTEX:
case RTTHREADSTATE_RW_READ:
case RTTHREADSTATE_RW_WRITE:
case RTTHREADSTATE_SPIN_MUTEX:
{
if (enmCurState2 != enmCurState)
{
AssertMsg2(" Impossible!!! enmState=%s -> %s (%d)\n",
break;
}
{
AssertMsg2(" Waiting on %s %p [%s]: Entered %s(%u) %s %p\n",
}
AssertMsg2(" Waiting on %s pCurRec=%p: invalid magic number: %#x\n",
else
AssertMsg2(" Waiting on %s pCurRec=%p: invalid pointer\n",
break;
}
default:
break;
}
/*
* Check for cycle.
*/
break;
for (unsigned i = 0; i < RT_ELEMENTS(apSeenThreads); i++)
if (apSeenThreads[i] == pCur)
{
AssertMsg2(" Cycle!\n");
break;
}
/*
* Advance to the next thread.
*/
}
}
/**
* Change the thread state to blocking and do deadlock detection.
*
* @param pRec The validator record we're blocing on.
* @param hThread The current thread. Shall not be NIL_RTTHREAD!
* @param enmState The sleep state.
* @param pvBlock Pointer to a RTLOCKVALIDATORREC structure.
* @param fRecursiveOk Whether it's ok to recurse.
* @param uId Where we are blocking.
* @param RT_SRC_POS_DECL Where we are blocking.
*/
{
/*
* Fend off wild life.
*/
/*
* Record the location and everything before changing the state and
* performing deadlock detection.
*/
/** @todo This has to be serialized! The deadlock detection isn't 100% safe!!! */
/*
* Don't do deadlock detection if we're recursing and that's OK.
*
* On some hosts we don't do recursion accounting our selves and there
* isn't any other place to check for this. semmutex-win.cpp for instance.
*/
if ( !fRecursiveOk
{
/*
* Do deadlock detection.
*
* Since we're missing proper serialization, we don't declare it a
* deadlock until we've got three runs with the same list length.
* While this isn't perfect, it should avoid out the most obvious
* races on SMP boxes.
*/
unsigned cPrevLength = ~0U;
unsigned cEqualRuns = 0;
unsigned iParanoia = 256;
do
{
unsigned cLength = 0;
for (;;)
{
/*
* Get the next thread.
*/
for (;;)
{
switch (enmCurState)
{
case RTTHREADSTATE_CRITSECT:
case RTTHREADSTATE_EVENT:
case RTTHREADSTATE_FAST_MUTEX:
case RTTHREADSTATE_MUTEX:
case RTTHREADSTATE_RW_READ:
case RTTHREADSTATE_RW_WRITE:
case RTTHREADSTATE_SPIN_MUTEX:
{
continue;
continue;
break;
}
default:
break;
}
break;
}
/*
* If we arrive at the end of the list we're good.
*/
if (!pCur)
return;
/*
* If we've got back to the blocking thread id we've
* got a deadlock.
*/
break;
/*
* If we've got a chain of more than 256 items, there is some
* kind of cycle in the list, which means that there is already
* a deadlock somewhere.
*/
if (cLength >= 256)
break;
cLength++;
}
/* compare with previous list run. */
if (cLength != cPrevLength)
{
cEqualRuns = 0;
}
else
cEqualRuns++;
/*
* Ok, if we ever get here, it's most likely a genuine deadlock.
*/
}
}