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.
9 #include <utilstrencodings.h>
13 #ifdef DEBUG_LOCKCONTENTION
14 #if !defined(HAVE_THREAD_LOCAL)
15 static_assert(false, "thread_local is not supported");
17 void PrintLockContention(const char* pszName
, const char* pszFile
, int nLine
)
19 LogPrintf("LOCKCONTENTION: %s\n", pszName
);
20 LogPrintf("Locker: %s:%d\n", pszFile
, nLine
);
22 #endif /* DEBUG_LOCKCONTENTION */
24 #ifdef DEBUG_LOCKORDER
26 // Early deadlock detection.
27 // Problem being solved:
28 // Thread 1 locks A, then B, then C
29 // Thread 2 locks D, then C, then A
30 // --> may result in deadlock between the two threads, depending on when they run.
31 // Solution implemented here:
32 // Keep track of pairs of locks: (A before B), (A before C), etc.
33 // Complain if any thread tries to lock in a different order.
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
;
62 // Very ugly hack: as the global constructs and destructors run single
63 // threaded, we use this boolean to know whether LockData still exists,
64 // as DeleteLock can get called by global CCriticalSection destructors
65 // after LockData disappears.
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 LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
80 LogPrintf("Previous lock order was:\n");
81 for (const std::pair
<void*, CLockLocation
> & i
: s2
) {
82 if (i
.first
== mismatch
.first
) {
85 if (i
.first
== mismatch
.second
) {
88 LogPrintf(" %s\n", i
.second
.ToString());
90 LogPrintf("Current lock order is:\n");
91 for (const std::pair
<void*, CLockLocation
> & i
: s1
) {
92 if (i
.first
== mismatch
.first
) {
95 if (i
.first
== mismatch
.second
) {
98 LogPrintf(" %s\n", i
.second
.ToString());
103 static void push_lock(void* c
, const CLockLocation
& locklocation
)
106 lockstack
.reset(new LockStack
);
108 std::lock_guard
<std::mutex
> lock(lockdata
.dd_mutex
);
110 lockstack
->push_back(std::make_pair(c
, locklocation
));
112 for (const std::pair
<void*, CLockLocation
> & i
: (*lockstack
)) {
116 std::pair
<void*, void*> p1
= std::make_pair(i
.first
, c
);
117 if (lockdata
.lockorders
.count(p1
))
119 lockdata
.lockorders
[p1
] = (*lockstack
);
121 std::pair
<void*, void*> p2
= std::make_pair(c
, i
.first
);
122 lockdata
.invlockorders
.insert(p2
);
123 if (lockdata
.lockorders
.count(p2
))
124 potential_deadlock_detected(p1
, lockdata
.lockorders
[p2
], lockdata
.lockorders
[p1
]);
128 static void pop_lock()
130 (*lockstack
).pop_back();
133 void EnterCritical(const char* pszName
, const char* pszFile
, int nLine
, void* cs
, bool fTry
)
135 push_lock(cs
, CLockLocation(pszName
, pszFile
, nLine
, fTry
));
143 std::string
LocksHeld()
146 for (const std::pair
<void*, CLockLocation
> & i
: *lockstack
)
147 result
+= i
.second
.ToString() + std::string("\n");
151 void AssertLockHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
)
153 for (const std::pair
<void*, CLockLocation
> & i
: *lockstack
)
156 fprintf(stderr
, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName
, pszFile
, nLine
, LocksHeld().c_str());
160 void AssertLockNotHeldInternal(const char* pszName
, const char* pszFile
, int nLine
, void* cs
)
162 for (const std::pair
<void*, CLockLocation
>& i
: *lockstack
) {
164 fprintf(stderr
, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName
, pszFile
, nLine
, LocksHeld().c_str());
170 void DeleteLock(void* cs
)
172 if (!lockdata
.available
) {
173 // We're already shutting down.
176 std::lock_guard
<std::mutex
> lock(lockdata
.dd_mutex
);
177 std::pair
<void*, void*> item
= std::make_pair(cs
, nullptr);
178 LockOrders::iterator it
= lockdata
.lockorders
.lower_bound(item
);
179 while (it
!= lockdata
.lockorders
.end() && it
->first
.first
== cs
) {
180 std::pair
<void*, void*> invitem
= std::make_pair(it
->first
.second
, it
->first
.first
);
181 lockdata
.invlockorders
.erase(invitem
);
182 lockdata
.lockorders
.erase(it
++);
184 InvLockOrders::iterator invit
= lockdata
.invlockorders
.lower_bound(item
);
185 while (invit
!= lockdata
.invlockorders
.end() && invit
->first
== cs
) {
186 std::pair
<void*, void*> invinvitem
= std::make_pair(invit
->second
, invit
->first
);
187 lockdata
.lockorders
.erase(invinvitem
);
188 lockdata
.invlockorders
.erase(invit
++);
192 #endif /* DEBUG_LOCKORDER */