10 #include <boost/foreach.hpp>
12 #ifdef DEBUG_LOCKCONTENTION
13 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine)
15 LogPrintf(
"LOCKCONTENTION: %s\n", pszName);
16 LogPrintf(
"Locker: %s:%d\n", pszFile, nLine);
20 #ifdef DEBUG_LOCKORDER
34 CLockLocation(
const char* pszName,
const char* pszFile,
int nLine)
41 std::string ToString()
const
43 return mutexName+
" "+sourceFile+
":"+
itostr(sourceLine);
46 std::string MutexName()
const {
return mutexName; }
49 std::string mutexName;
50 std::string sourceFile;
54 typedef std::vector< std::pair<void*, CLockLocation> > LockStack;
56 static boost::mutex dd_mutex;
57 static std::map<std::pair<void*, void*>, LockStack> lockorders;
58 static boost::thread_specific_ptr<LockStack> lockstack;
61 static void potential_deadlock_detected(
const std::pair<void*, void*>& mismatch,
const LockStack& s1,
const LockStack& s2)
63 LogPrintf(
"POTENTIAL DEADLOCK DETECTED\n");
65 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)& i, s2)
67 if (i.first == mismatch.first)
LogPrintf(
" (1)");
68 if (i.first == mismatch.second)
LogPrintf(
" (2)");
72 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)& i, s1)
74 if (i.first == mismatch.first)
LogPrintf(
" (1)");
75 if (i.first == mismatch.second)
LogPrintf(
" (2)");
80 static void push_lock(
void* c,
const CLockLocation& locklocation,
bool fTry)
82 if (lockstack.get() == NULL)
83 lockstack.reset(
new LockStack);
85 LogPrint(
"lock",
"Locking: %s\n", locklocation.ToString());
88 (*lockstack).push_back(std::make_pair(c, locklocation));
91 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)& i, (*lockstack)) {
92 if (i.first == c)
break;
94 std::pair<void*, void*> p1 = std::make_pair(i.first, c);
95 if (lockorders.count(p1))
97 lockorders[p1] = (*lockstack);
99 std::pair<void*, void*> p2 = std::make_pair(c, i.first);
100 if (lockorders.count(p2))
102 potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
110 static void pop_lock()
114 const CLockLocation& locklocation = (*lockstack).rbegin()->second;
115 LogPrint(
"lock",
"Unlocked: %s\n", locklocation.ToString());
118 (*lockstack).pop_back();
122 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry)
124 push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry);
132 std::string LocksHeld()
135 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)&i, *lockstack)
136 result += i.second.ToString() +
std::
string("\n");
140 void AssertLockHeldInternal(const
char *pszName, const
char* pszFile,
int nLine,
void *cs)
142 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)&i, *lockstack)
143 if (i.first == cs) return;
144 fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s",
145 pszName, pszFile, nLine, LocksHeld().c_str());
std::string itostr(int n)