9 #include <boost/foreach.hpp>
11 #ifdef DEBUG_LOCKCONTENTION
12 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine)
14 LogPrintf(
"LOCKCONTENTION: %s\n", pszName);
15 LogPrintf(
"Locker: %s:%d\n", pszFile, nLine);
19 #ifdef DEBUG_LOCKORDER
33 CLockLocation(
const char* pszName,
const char* pszFile,
int nLine)
40 std::string ToString()
const
42 return mutexName+
" "+sourceFile+
":"+
itostr(sourceLine);
45 std::string MutexName()
const {
return mutexName; }
48 std::string mutexName;
49 std::string sourceFile;
53 typedef std::vector< std::pair<void*, CLockLocation> > LockStack;
55 static boost::mutex dd_mutex;
56 static std::map<std::pair<void*, void*>, LockStack> lockorders;
57 static boost::thread_specific_ptr<LockStack> lockstack;
60 static void potential_deadlock_detected(
const std::pair<void*, void*>& mismatch,
const LockStack& s1,
const LockStack& s2)
62 LogPrintf(
"POTENTIAL DEADLOCK DETECTED\n");
64 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)& i, s2)
66 if (i.first == mismatch.first)
LogPrintf(
" (1)");
67 if (i.first == mismatch.second)
LogPrintf(
" (2)");
71 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)& i, s1)
73 if (i.first == mismatch.first)
LogPrintf(
" (1)");
74 if (i.first == mismatch.second)
LogPrintf(
" (2)");
79 static void push_lock(
void* c,
const CLockLocation& locklocation,
bool fTry)
81 if (lockstack.get() == NULL)
82 lockstack.reset(
new LockStack);
84 LogPrint(
"lock",
"Locking: %s\n", locklocation.ToString());
87 (*lockstack).push_back(std::make_pair(c, locklocation));
90 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)& i, (*lockstack)) {
91 if (i.first == c)
break;
93 std::pair<void*, void*> p1 = std::make_pair(i.first, c);
94 if (lockorders.count(p1))
96 lockorders[p1] = (*lockstack);
98 std::pair<void*, void*> p2 = std::make_pair(c, i.first);
99 if (lockorders.count(p2))
101 potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
109 static void pop_lock()
113 const CLockLocation& locklocation = (*lockstack).rbegin()->second;
114 LogPrint(
"lock",
"Unlocked: %s\n", locklocation.ToString());
117 (*lockstack).pop_back();
121 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry)
123 push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry);
131 std::string LocksHeld()
134 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)&i, *lockstack)
135 result += i.second.ToString() +
std::
string("\n");
141 BOOST_FOREACH(
const PAIRTYPE(
void*, CLockLocation)&i, *lockstack)
142 if (i.first == cs) return;
143 fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s",
144 pszName, pszFile, nLine, LocksHeld().c_str());
static int LogPrint(const char *category, const char *format)
std::string itostr(int n)
static void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs)
static void EnterCritical(const char *pszName, const char *pszFile, int nLine, void *cs, bool fTry=false)
static void LeaveCritical()