12 #include <boost/thread.hpp> 14 #ifdef DEBUG_LOCKCONTENTION 15 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine)
17 LogPrintf(
"LOCKCONTENTION: %s\n", pszName);
18 LogPrintf(
"Locker: %s:%d\n", pszFile, nLine);
22 #ifdef DEBUG_LOCKORDER 34 struct CLockLocation {
35 CLockLocation(
const char* pszName,
const char* pszFile,
int nLine,
bool fTryIn)
43 std::string ToString()
const 45 return mutexName +
" " + sourceFile +
":" +
itostr(sourceLine) + (fTry ?
" (TRY)" :
"");
50 std::string mutexName;
51 std::string sourceFile;
55 typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
56 typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
57 typedef std::set<std::pair<void*, void*> > InvLockOrders;
65 LockData() : available(true) {}
66 ~LockData() { available =
false; }
68 LockOrders lockorders;
69 InvLockOrders invlockorders;
70 boost::mutex dd_mutex;
73 boost::thread_specific_ptr<LockStack> lockstack;
75 static void potential_deadlock_detected(
const std::pair<void*, void*>& mismatch,
const LockStack&
s1,
const LockStack&
s2)
77 LogPrintf(
"POTENTIAL DEADLOCK DETECTED\n");
79 for (
const std::pair<void*, CLockLocation> & i : s2) {
80 if (i.first == mismatch.first) {
83 if (i.first == mismatch.second) {
89 for (
const std::pair<void*, CLockLocation> & i : s1) {
90 if (i.first == mismatch.first) {
93 if (i.first == mismatch.second) {
101 static void push_lock(
void*
c,
const CLockLocation& locklocation,
bool fTry)
103 if (lockstack.get() ==
nullptr)
104 lockstack.reset(
new LockStack);
106 boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
108 (*lockstack).push_back(std::make_pair(c, locklocation));
110 for (
const std::pair<void*, CLockLocation> & i : (*lockstack)) {
114 std::pair<void*, void*> p1 = std::make_pair(i.first, c);
115 if (lockdata.lockorders.count(p1))
117 lockdata.lockorders[p1] = (*lockstack);
119 std::pair<void*, void*> p2 = std::make_pair(c, i.first);
120 lockdata.invlockorders.insert(p2);
121 if (lockdata.lockorders.count(p2))
122 potential_deadlock_detected(p1, lockdata.lockorders[p2], lockdata.lockorders[p1]);
126 static void pop_lock()
128 (*lockstack).pop_back();
131 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry)
133 push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry), fTry);
141 std::string LocksHeld()
144 for (
const std::pair<void*, CLockLocation> & i : *lockstack)
145 result += i.second.ToString() + std::string(
"\n");
149 void AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs)
151 for (
const std::pair<void*, CLockLocation> & i : *lockstack)
154 fprintf(stderr,
"Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
158 void DeleteLock(
void* cs)
160 if (!lockdata.available) {
164 boost::unique_lock<boost::mutex> lock(lockdata.dd_mutex);
165 std::pair<void*, void*> item = std::make_pair(cs, (
void*)0);
166 LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
167 while (it != lockdata.lockorders.end() && it->first.first == cs) {
168 std::pair<void*, void*> invitem = std::make_pair(it->first.second, it->first.first);
169 lockdata.invlockorders.erase(invitem);
170 lockdata.lockorders.erase(it++);
172 InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
173 while (invit != lockdata.invlockorders.end() && invit->first == cs) {
174 std::pair<void*, void*> invinvitem = std::make_pair(invit->second, invit->first);
175 lockdata.lockorders.erase(invinvitem);
176 lockdata.invlockorders.erase(invit++);
static const std::string s2("AAD")
assert(len-trim+(2 *lenIndices)<=WIDTH)
std::string itostr(int n)