14 #ifdef DEBUG_LOCKCONTENTION 15 #if !defined(HAVE_THREAD_LOCAL) 16 static_assert(
false,
"thread_local is not supported");
18 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine)
20 LogPrintf(
"LOCKCONTENTION: %s Locker: %s:%d\n", pszName, pszFile, nLine);
24 #ifdef DEBUG_LOCKORDER 36 struct CLockLocation {
37 CLockLocation(
const char* pszName,
const char* pszFile,
int nLine,
bool fTryIn)
45 std::string ToString()
const 47 return mutexName +
" " + sourceFile +
":" +
itostr(sourceLine) + (fTry ?
" (TRY)" :
"");
52 std::string mutexName;
53 std::string sourceFile;
57 typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
58 typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
59 typedef std::set<std::pair<void*, void*> > InvLockOrders;
67 LockData() : available(
true) {}
68 ~LockData() { available =
false; }
70 LockOrders lockorders;
71 InvLockOrders invlockorders;
75 static thread_local std::unique_ptr<LockStack> lockstack;
77 static void potential_deadlock_detected(
const std::pair<void*, void*>& mismatch,
const LockStack& s1,
const LockStack& s2)
79 std::string strOutput =
"";
80 strOutput +=
"POTENTIAL DEADLOCK DETECTED\n";
81 strOutput +=
"Previous lock order was:\n";
82 for (
const std::pair<void*, CLockLocation> & i : s2) {
83 if (i.first == mismatch.first) {
86 if (i.first == mismatch.second) {
89 strOutput +=
strprintf(
" %s\n", i.second.ToString().c_str());
91 strOutput +=
"Current lock order is:\n";
92 for (
const std::pair<void*, CLockLocation> & i : s1) {
93 if (i.first == mismatch.first) {
96 if (i.first == mismatch.second) {
99 strOutput +=
strprintf(
" %s\n", i.second.ToString().c_str());
102 printf(
"%s\n", strOutput.c_str());
108 static void push_lock(
void* c,
const CLockLocation& locklocation)
111 lockstack.reset(
new LockStack);
113 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
115 lockstack->push_back(std::make_pair(c, locklocation));
117 for (
const std::pair<void*, CLockLocation> & i : (*lockstack)) {
121 std::pair<void*, void*> p1 = std::make_pair(i.first, c);
122 if (lockdata.lockorders.count(p1))
124 lockdata.lockorders[p1] = (*lockstack);
126 std::pair<void*, void*> p2 = std::make_pair(c, i.first);
127 lockdata.invlockorders.insert(p2);
128 if (lockdata.lockorders.count(p2))
129 potential_deadlock_detected(p1, lockdata.lockorders[p2], lockdata.lockorders[p1]);
133 static void pop_lock()
135 (*lockstack).pop_back();
138 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry)
140 push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry));
148 std::string LocksHeld()
151 for (
const std::pair<void*, CLockLocation> & i : *lockstack)
152 result += i.second.ToString() + std::string(
"\n");
158 for (
const std::pair<void*, CLockLocation> & i : *lockstack)
161 fprintf(stderr,
"Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
167 for (
const std::pair<void*, CLockLocation>& i : *lockstack) {
169 fprintf(stderr,
"Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
177 if (!lockdata.available) {
181 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
182 std::pair<void*, void*> item = std::make_pair(cs,
nullptr);
183 LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
184 while (it != lockdata.lockorders.end() && it->first.first == cs) {
185 std::pair<void*, void*> invitem = std::make_pair(it->first.second, it->first.first);
186 lockdata.invlockorders.erase(invitem);
187 lockdata.lockorders.erase(it++);
189 InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
190 while (invit != lockdata.invlockorders.end() && invit->first == cs) {
191 std::pair<void*, void*> invinvitem = std::make_pair(invit->second, invit->first);
192 lockdata.lockorders.erase(invinvitem);
193 lockdata.invlockorders.erase(invit++);
static void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, void *cs)
static void DeleteLock(void *cs)
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()
std::string itostr(int n)