Skip to content

Commit

Permalink
mutex debugging routines: LocksHeld() and AssertLockHeld()
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinandresen committed Nov 29, 2013
1 parent 207cfbf commit c649637
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main.cpp
Expand Up @@ -2215,6 +2215,8 @@ void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)

bool ProcessBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
{
AssertLockHeld("cs_main");

// Check for duplicate
uint256 hash = pblock->GetHash();
if (mapBlockIndex.count(hash))
Expand Down
18 changes: 18 additions & 0 deletions src/sync.cpp
Expand Up @@ -42,6 +42,8 @@ struct CLockLocation
return mutexName+" "+sourceFile+":"+itostr(sourceLine);
}

std::string MutexName() const { return mutexName; }

private:
std::string mutexName;
std::string sourceFile;
Expand Down Expand Up @@ -126,4 +128,20 @@ void LeaveCritical()
pop_lock();
}

std::string LocksHeld()
{
std::string result;
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)&i, *lockstack)
result += i.second.ToString() + std::string("\n");
return result;
}

void AssertLockHeld(std::string strName)
{
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)&i, *lockstack)
if (i.second.MutexName() == strName) return;
LogPrintf("Lock %s not held; locks held:\n%s", strName.c_str(), LocksHeld().c_str());
assert(0);
}

#endif /* DEBUG_LOCKORDER */
3 changes: 3 additions & 0 deletions src/sync.h
Expand Up @@ -87,9 +87,12 @@ typedef AnnotatedMixin<boost::mutex> CWaitableCriticalSection;
#ifdef DEBUG_LOCKORDER
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
void LeaveCritical();
std::string LocksHeld();
void AssertLockHeld(std::string strName);
#else
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
void static inline LeaveCritical() {}
void static inline AssertLockHeld(std::string) {}
#endif

#ifdef DEBUG_LOCKCONTENTION
Expand Down

0 comments on commit c649637

Please sign in to comment.