2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2011-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <sync.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2018-04-02 20:31:40 +02:00
|
|
|
#include <memory>
|
2017-11-28 08:37:50 +01:00
|
|
|
#include <set>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <util.h>
|
|
|
|
#include <utilstrencodings.h>
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2014-09-14 12:43:56 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-06-05 16:12:32 +02:00
|
|
|
#ifdef DEBUG_LOCKCONTENTION
|
2017-11-28 08:37:50 +01:00
|
|
|
#if !defined(HAVE_THREAD_LOCAL)
|
|
|
|
static_assert(false, "thread_local is not supported");
|
|
|
|
#endif
|
2012-06-05 16:12:32 +02:00
|
|
|
void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
|
|
|
|
{
|
2016-03-25 00:15:03 +01:00
|
|
|
LogPrintf("LOCKCONTENTION: %s Locker: %s:%d\n", pszName, pszFile, nLine);
|
2012-06-05 16:12:32 +02:00
|
|
|
}
|
|
|
|
#endif /* DEBUG_LOCKCONTENTION */
|
|
|
|
|
2012-05-11 17:00:03 +02:00
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
//
|
|
|
|
// Early deadlock detection.
|
|
|
|
// Problem being solved:
|
|
|
|
// Thread 1 locks A, then B, then C
|
|
|
|
// Thread 2 locks D, then C, then A
|
|
|
|
// --> may result in deadlock between the two threads, depending on when they run.
|
|
|
|
// Solution implemented here:
|
|
|
|
// Keep track of pairs of locks: (A before B), (A before C), etc.
|
2012-06-29 11:26:45 +02:00
|
|
|
// Complain if any thread tries to lock in a different order.
|
2012-05-11 17:00:03 +02:00
|
|
|
//
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
struct CLockLocation {
|
2015-08-07 22:18:16 +02:00
|
|
|
CLockLocation(const char* pszName, const char* pszFile, int nLine, bool fTryIn)
|
2012-05-11 17:00:03 +02:00
|
|
|
{
|
|
|
|
mutexName = pszName;
|
|
|
|
sourceFile = pszFile;
|
|
|
|
sourceLine = nLine;
|
2015-08-07 22:18:16 +02:00
|
|
|
fTry = fTryIn;
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString() const
|
|
|
|
{
|
2015-08-07 22:18:16 +02:00
|
|
|
return mutexName + " " + sourceFile + ":" + itostr(sourceLine) + (fTry ? " (TRY)" : "");
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-11-28 08:37:50 +01:00
|
|
|
bool fTry;
|
2012-05-11 17:00:03 +02:00
|
|
|
std::string mutexName;
|
|
|
|
std::string sourceFile;
|
|
|
|
int sourceLine;
|
|
|
|
};
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
|
2016-04-14 14:57:13 +02:00
|
|
|
typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
|
|
|
|
typedef std::set<std::pair<void*, void*> > InvLockOrders;
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2016-04-14 14:57:13 +02:00
|
|
|
struct LockData {
|
|
|
|
// Very ugly hack: as the global constructs and destructors run single
|
|
|
|
// threaded, we use this boolean to know whether LockData still exists,
|
|
|
|
// as DeleteLock can get called by global CCriticalSection destructors
|
|
|
|
// after LockData disappears.
|
|
|
|
bool available;
|
|
|
|
LockData() : available(true) {}
|
|
|
|
~LockData() { available = false; }
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2016-04-14 14:57:13 +02:00
|
|
|
LockOrders lockorders;
|
|
|
|
InvLockOrders invlockorders;
|
2017-11-28 08:37:50 +01:00
|
|
|
std::mutex dd_mutex;
|
2016-04-14 14:57:13 +02:00
|
|
|
} static lockdata;
|
|
|
|
|
2017-11-28 08:37:50 +01:00
|
|
|
static thread_local std::unique_ptr<LockStack> lockstack;
|
2012-05-11 17:00:03 +02:00
|
|
|
|
|
|
|
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
|
|
|
|
{
|
2017-01-11 00:00:34 +01:00
|
|
|
std::string strOutput = "";
|
|
|
|
strOutput += "POTENTIAL DEADLOCK DETECTED\n";
|
|
|
|
strOutput += "Previous lock order was:\n";
|
2019-07-05 09:08:44 +02:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : s2) {
|
2015-08-07 22:18:16 +02:00
|
|
|
if (i.first == mismatch.first) {
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += " (1)";
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += " (2)";
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += strprintf(" %s\n", i.second.ToString().c_str());
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += "Current lock order is:\n";
|
2019-07-05 09:08:44 +02:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : s1) {
|
2015-08-07 22:18:16 +02:00
|
|
|
if (i.first == mismatch.first) {
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += " (1)";
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += " (2)";
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += strprintf(" %s\n", i.second.ToString().c_str());
|
|
|
|
}
|
2018-01-23 12:27:28 +01:00
|
|
|
|
|
|
|
printf("%s\n", strOutput.c_str());
|
|
|
|
LogPrintf("%s\n", strOutput.c_str());
|
|
|
|
|
2017-02-08 14:46:35 +01:00
|
|
|
assert(false);
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 14:54:42 +02:00
|
|
|
static void push_lock(void* c, const CLockLocation& locklocation)
|
2012-05-11 17:00:03 +02:00
|
|
|
{
|
2017-11-28 08:37:50 +01:00
|
|
|
if (!lockstack)
|
2012-05-11 17:00:03 +02:00
|
|
|
lockstack.reset(new LockStack);
|
|
|
|
|
2017-11-28 08:37:50 +01:00
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2017-11-28 08:37:50 +01:00
|
|
|
lockstack->push_back(std::make_pair(c, locklocation));
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2019-07-05 09:08:44 +02:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : (*lockstack)) {
|
2017-02-08 14:46:35 +01:00
|
|
|
if (i.first == c)
|
|
|
|
break;
|
2012-05-13 17:55:23 +02:00
|
|
|
|
2017-02-08 14:46:35 +01:00
|
|
|
std::pair<void*, void*> p1 = std::make_pair(i.first, c);
|
|
|
|
if (lockdata.lockorders.count(p1))
|
|
|
|
continue;
|
|
|
|
lockdata.lockorders[p1] = (*lockstack);
|
2012-05-13 17:55:23 +02:00
|
|
|
|
2017-02-08 14:46:35 +01:00
|
|
|
std::pair<void*, void*> p2 = std::make_pair(c, i.first);
|
|
|
|
lockdata.invlockorders.insert(p2);
|
|
|
|
if (lockdata.lockorders.count(p2))
|
|
|
|
potential_deadlock_detected(p1, lockdata.lockorders[p2], lockdata.lockorders[p1]);
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pop_lock()
|
|
|
|
{
|
|
|
|
(*lockstack).pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
|
|
|
|
{
|
2017-10-02 14:54:42 +02:00
|
|
|
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry));
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeaveCritical()
|
|
|
|
{
|
|
|
|
pop_lock();
|
|
|
|
}
|
|
|
|
|
2013-11-29 08:25:30 +01:00
|
|
|
std::string LocksHeld()
|
|
|
|
{
|
|
|
|
std::string result;
|
2019-07-05 09:08:44 +02:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : *lockstack)
|
2013-11-29 08:25:30 +01:00
|
|
|
result += i.second.ToString() + std::string("\n");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
|
2013-11-29 08:25:30 +01:00
|
|
|
{
|
2019-07-05 09:08:44 +02:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : *lockstack)
|
2014-09-19 19:21:46 +02:00
|
|
|
if (i.first == cs)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
|
2014-02-18 18:11:46 +01:00
|
|
|
abort();
|
2013-11-29 08:25:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 23:42:46 +01:00
|
|
|
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
|
|
|
|
{
|
|
|
|
for (const std::pair<void*, CLockLocation>& i : *lockstack) {
|
|
|
|
if (i.first == cs) {
|
|
|
|
fprintf(stderr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 14:57:13 +02:00
|
|
|
void DeleteLock(void* cs)
|
|
|
|
{
|
|
|
|
if (!lockdata.available) {
|
|
|
|
// We're already shutting down.
|
|
|
|
return;
|
|
|
|
}
|
2017-11-28 08:37:50 +01:00
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
2017-08-16 15:54:51 +02:00
|
|
|
std::pair<void*, void*> item = std::make_pair(cs, nullptr);
|
2016-04-14 14:57:13 +02:00
|
|
|
LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
|
|
|
|
while (it != lockdata.lockorders.end() && it->first.first == cs) {
|
|
|
|
std::pair<void*, void*> invitem = std::make_pair(it->first.second, it->first.first);
|
|
|
|
lockdata.invlockorders.erase(invitem);
|
|
|
|
lockdata.lockorders.erase(it++);
|
|
|
|
}
|
|
|
|
InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
|
|
|
|
while (invit != lockdata.invlockorders.end() && invit->first == cs) {
|
|
|
|
std::pair<void*, void*> invinvitem = std::make_pair(invit->second, invit->first);
|
|
|
|
lockdata.lockorders.erase(invinvitem);
|
|
|
|
lockdata.invlockorders.erase(invit++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-11 17:00:03 +02:00
|
|
|
#endif /* DEBUG_LOCKORDER */
|