1 // Copyright (c) 2011-2012 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 #include "utilstrencodings.h"
12 #include <boost/foreach.hpp>
13 #include <boost/thread.hpp>
15 #ifdef DEBUG_LOCKCONTENTION
16 void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
18 LogPrintf("LOCKCONTENTION: %s\n", pszName);
19 LogPrintf("Locker: %s:%d\n", pszFile, nLine);
21 #endif /* DEBUG_LOCKCONTENTION */
23 #ifdef DEBUG_LOCKORDER
25 // Early deadlock detection.
26 // Problem being solved:
27 // Thread 1 locks A, then B, then C
28 // Thread 2 locks D, then C, then A
29 // --> may result in deadlock between the two threads, depending on when they run.
30 // Solution implemented here:
31 // Keep track of pairs of locks: (A before B), (A before C), etc.
32 // Complain if any thread tries to lock in a different order.
35 struct CLockLocation {
36 CLockLocation(const char* pszName, const char* pszFile, int nLine)
43 std::string ToString() const
45 return mutexName + " " + sourceFile + ":" + itostr(sourceLine);
48 std::string MutexName() const { return mutexName; }
51 std::string mutexName;
52 std::string sourceFile;
56 typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
58 static boost::mutex dd_mutex;
59 static std::map<std::pair<void*, void*>, LockStack> lockorders;
60 static boost::thread_specific_ptr<LockStack> lockstack;
63 static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
65 LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
66 LogPrintf("Previous lock order was:\n");
67 BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, s2) {
68 if (i.first == mismatch.first)
70 if (i.first == mismatch.second)
72 LogPrintf(" %s\n", i.second.ToString());
74 LogPrintf("Current lock order is:\n");
75 BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, s1) {
76 if (i.first == mismatch.first)
78 if (i.first == mismatch.second)
80 LogPrintf(" %s\n", i.second.ToString());
84 static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
86 if (lockstack.get() == NULL)
87 lockstack.reset(new LockStack);
89 LogPrint("lock", "Locking: %s\n", locklocation.ToString());
92 (*lockstack).push_back(std::make_pair(c, locklocation));
95 BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, (*lockstack)) {
99 std::pair<void*, void*> p1 = std::make_pair(i.first, c);
100 if (lockorders.count(p1))
102 lockorders[p1] = (*lockstack);
104 std::pair<void*, void*> p2 = std::make_pair(c, i.first);
105 if (lockorders.count(p2)) {
106 potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
114 static void pop_lock()
117 const CLockLocation& locklocation = (*lockstack).rbegin()->second;
118 LogPrint("lock", "Unlocked: %s\n", locklocation.ToString());
121 (*lockstack).pop_back();
125 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
127 push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry);
135 std::string LocksHeld()
138 BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, *lockstack)
139 result += i.second.ToString() + std::string("\n");
143 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
145 BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, *lockstack)
148 fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
152 #endif /* DEBUG_LOCKORDER */