1 // Copyright (c) 2011-2016 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/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
);
20 #endif /* DEBUG_LOCKCONTENTION */
22 #ifdef DEBUG_LOCKORDER
24 // Early deadlock detection.
25 // Problem being solved:
26 // Thread 1 locks A, then B, then C
27 // Thread 2 locks D, then C, then A
28 // --> may result in deadlock between the two threads, depending on when they run.
29 // Solution implemented here:
30 // Keep track of pairs of locks: (A before B), (A before C), etc.
31 // Complain if any thread tries to lock in a different order.
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
;
60 // Very ugly hack: as the global constructs and destructors run single
61 // threaded, we use this boolean to know whether LockData still exists,
62 // as DeleteLock can get called by global CCriticalSection destructors
63 // after LockData disappears.
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");
78 LogPrintf("Previous lock order was:\n");
79 for (const std::pair
<void*, CLockLocation
> & i
: s2
) {
80 if (i
.first
== mismatch
.first
) {
83 if (i
.first
== mismatch
.second
) {
86 LogPrintf(" %s\n", i
.second
.ToString());
88 LogPrintf("Current lock order is:\n");
89 for (const std::pair
<void*, CLockLocation
> & i
: s1
) {
90 if (i
.first
== mismatch
.first
) {
93 if (i
.first
== mismatch
.second
) {
96 LogPrintf(" %s\n", i
.second
.ToString());
101 static void push_lock(void* c
, const CLockLocation
& locklocation
)
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
));
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
) {
161 // We're already shutting down.
164 boost::unique_lock
<boost::mutex
> lock(lockdata
.dd_mutex
);
165 std::pair
<void*, void*> item
= std::make_pair(cs
, nullptr);
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
++);
180 #endif /* DEBUG_LOCKORDER */