16 #ifdef DEBUG_LOCKCONTENTION 17 #if !defined(HAVE_THREAD_LOCAL) 18 static_assert(
false,
"thread_local is not supported");
20 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine)
22 LogPrintf(
"LOCKCONTENTION: %s\n", pszName);
23 LogPrintf(
"Locker: %s:%d\n", pszFile, nLine);
27 #ifdef DEBUG_LOCKORDER 39 struct CLockLocation {
40 CLockLocation(
const char* pszName,
const char* pszFile,
int nLine,
bool fTryIn)
48 std::string ToString()
const 50 return mutexName +
" " + sourceFile +
":" +
itostr(sourceLine) + (fTry ?
" (TRY)" :
"");
55 std::string mutexName;
56 std::string sourceFile;
60 typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
61 typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
62 typedef std::set<std::pair<void*, void*> > InvLockOrders;
70 LockData() : available(true) {}
71 ~LockData() { available =
false; }
73 LockOrders lockorders;
74 InvLockOrders invlockorders;
78 static thread_local LockStack g_lockstack;
80 static void potential_deadlock_detected(
const std::pair<void*, void*>& mismatch,
const LockStack& s1,
const LockStack& s2)
82 LogPrintf(
"POTENTIAL DEADLOCK DETECTED\n");
83 LogPrintf(
"Previous lock order was:\n");
84 for (
const std::pair<void*, CLockLocation> & i : s2) {
85 if (i.first == mismatch.first) {
88 if (i.first == mismatch.second) {
91 LogPrintf(
" %s\n", i.second.ToString());
93 LogPrintf(
"Current lock order is:\n");
94 for (
const std::pair<void*, CLockLocation> & i : s1) {
95 if (i.first == mismatch.first) {
98 if (i.first == mismatch.second) {
101 LogPrintf(
" %s\n", i.second.ToString());
103 if (g_debug_lockorder_abort) {
104 fprintf(stderr,
"Assertion failed: detected inconsistent lock order at %s:%i, details in debug log.\n", __FILE__, __LINE__);
107 throw std::logic_error(
"potential deadlock detected");
110 static void push_lock(
void* c,
const CLockLocation& locklocation)
112 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
114 g_lockstack.push_back(std::make_pair(c, locklocation));
116 for (
const std::pair<void*, CLockLocation>& i : g_lockstack) {
120 std::pair<void*, void*> p1 = std::make_pair(i.first, c);
121 if (lockdata.lockorders.count(p1))
123 lockdata.lockorders[p1] = g_lockstack;
125 std::pair<void*, void*> p2 = std::make_pair(c, i.first);
126 lockdata.invlockorders.insert(p2);
127 if (lockdata.lockorders.count(p2))
128 potential_deadlock_detected(p1, lockdata.lockorders[p2], lockdata.lockorders[p1]);
132 static void pop_lock()
134 g_lockstack.pop_back();
137 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry)
139 push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry));
147 std::string LocksHeld()
150 for (
const std::pair<void*, CLockLocation>& i : g_lockstack)
151 result += i.second.ToString() + std::string(
"\n");
155 void AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs)
157 for (
const std::pair<void*, CLockLocation>& i : g_lockstack)
160 fprintf(stderr,
"Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
164 void AssertLockNotHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs)
166 for (
const std::pair<void*, CLockLocation>& i : g_lockstack) {
168 fprintf(stderr,
"Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
174 void DeleteLock(
void* cs)
176 if (!lockdata.available) {
180 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
181 std::pair<void*, void*> item = std::make_pair(cs,
nullptr);
182 LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
183 while (it != lockdata.lockorders.end() && it->first.first == cs) {
184 std::pair<void*, void*> invitem = std::make_pair(it->first.second, it->first.first);
185 lockdata.invlockorders.erase(invitem);
186 lockdata.lockorders.erase(it++);
188 InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
189 while (invit != lockdata.invlockorders.end() && invit->first == cs) {
190 std::pair<void*, void*> invinvitem = std::make_pair(invit->second, invit->first);
191 lockdata.lockorders.erase(invinvitem);
192 lockdata.invlockorders.erase(invit++);
196 bool g_debug_lockorder_abort =
true;
std::string itostr(int n)