2020-01-14 16:17:38 -03:00
|
|
|
// Copyright (c) 2011-2020 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 10:02:28 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2012-05-11 11:00:03 -04:00
|
|
|
|
2019-05-07 02:11:35 -04:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config/bitcoin-config.h>
|
|
|
|
#endif
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <sync.h>
|
2018-05-05 15:15:30 -03:00
|
|
|
#include <tinyformat.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2018-05-15 05:27:14 -04:00
|
|
|
#include <logging.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/strencodings.h>
|
2018-05-05 15:15:30 -03:00
|
|
|
#include <util/threadnames.h>
|
2012-05-11 11:00:03 -04:00
|
|
|
|
2018-05-15 05:27:14 -04:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2020-03-27 10:05:31 -03:00
|
|
|
#include <system_error>
|
2018-05-15 05:27:14 -04:00
|
|
|
|
2012-06-05 10:12:32 -04:00
|
|
|
#ifdef DEBUG_LOCKCONTENTION
|
2017-11-27 19:08:27 -03:00
|
|
|
#if !defined(HAVE_THREAD_LOCAL)
|
|
|
|
static_assert(false, "thread_local is not supported");
|
|
|
|
#endif
|
2012-06-05 10:12:32 -04:00
|
|
|
void PrintLockContention(const char* pszName, const char* pszFile, int nLine)
|
|
|
|
{
|
2013-09-18 07:38:08 -03:00
|
|
|
LogPrintf("LOCKCONTENTION: %s\n", pszName);
|
|
|
|
LogPrintf("Locker: %s:%d\n", pszFile, nLine);
|
2012-06-05 10:12:32 -04:00
|
|
|
}
|
|
|
|
#endif /* DEBUG_LOCKCONTENTION */
|
|
|
|
|
2012-05-11 11:00:03 -04:00
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
//
|
|
|
|
// Early deadlock detection.
|
|
|
|
// Problem being solved:
|
2018-01-28 09:14:54 -03:00
|
|
|
// Thread 1 locks A, then B, then C
|
|
|
|
// Thread 2 locks D, then C, then A
|
2012-05-11 11:00:03 -04:00
|
|
|
// --> 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 05:26:45 -04:00
|
|
|
// Complain if any thread tries to lock in a different order.
|
2012-05-11 11:00:03 -04:00
|
|
|
//
|
|
|
|
|
2014-09-19 14:21:46 -03:00
|
|
|
struct CLockLocation {
|
2018-05-05 15:15:30 -03:00
|
|
|
CLockLocation(
|
|
|
|
const char* pszName,
|
|
|
|
const char* pszFile,
|
|
|
|
int nLine,
|
|
|
|
bool fTryIn,
|
|
|
|
const std::string& thread_name)
|
|
|
|
: fTry(fTryIn),
|
|
|
|
mutexName(pszName),
|
|
|
|
sourceFile(pszFile),
|
|
|
|
m_thread_name(thread_name),
|
|
|
|
sourceLine(nLine) {}
|
2012-05-11 11:00:03 -04:00
|
|
|
|
|
|
|
std::string ToString() const
|
|
|
|
{
|
2019-06-13 09:43:24 -04:00
|
|
|
return strprintf(
|
2018-05-05 15:15:30 -03:00
|
|
|
"%s %s:%s%s (in thread %s)",
|
2020-03-27 10:05:31 -03:00
|
|
|
mutexName, sourceFile, sourceLine, (fTry ? " (TRY)" : ""), m_thread_name);
|
2012-05-11 11:00:03 -04:00
|
|
|
}
|
|
|
|
|
2020-03-01 01:22:37 -03:00
|
|
|
std::string Name() const
|
|
|
|
{
|
|
|
|
return mutexName;
|
|
|
|
}
|
|
|
|
|
2012-05-11 11:00:03 -04:00
|
|
|
private:
|
2017-11-18 16:35:14 -03:00
|
|
|
bool fTry;
|
2012-05-11 11:00:03 -04:00
|
|
|
std::string mutexName;
|
|
|
|
std::string sourceFile;
|
2018-05-05 15:15:30 -03:00
|
|
|
const std::string& m_thread_name;
|
2012-05-11 11:00:03 -04:00
|
|
|
int sourceLine;
|
|
|
|
};
|
|
|
|
|
2014-09-19 14:21:46 -03:00
|
|
|
typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
|
2016-04-08 17:14:19 -03:00
|
|
|
typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
|
|
|
|
typedef std::set<std::pair<void*, void*> > InvLockOrders;
|
2012-05-11 11:00:03 -04:00
|
|
|
|
2016-04-08 17:14:19 -03:00
|
|
|
struct LockData {
|
|
|
|
LockOrders lockorders;
|
|
|
|
InvLockOrders invlockorders;
|
2017-11-18 16:35:14 -03:00
|
|
|
std::mutex dd_mutex;
|
2019-01-22 22:57:16 -03:00
|
|
|
};
|
2020-05-05 01:07:47 -04:00
|
|
|
|
2019-01-22 22:57:16 -03:00
|
|
|
LockData& GetLockData() {
|
2020-05-05 01:07:47 -04:00
|
|
|
// This approach guarantees that the object is not destroyed until after its last use.
|
|
|
|
// The operating system automatically reclaims all the memory in a program's heap when that program exits.
|
|
|
|
static LockData& lock_data = *new LockData();
|
|
|
|
return lock_data;
|
2019-01-22 22:57:16 -03:00
|
|
|
}
|
2016-04-08 17:14:19 -03:00
|
|
|
|
2018-05-21 21:35:47 -04:00
|
|
|
static thread_local LockStack g_lockstack;
|
2012-05-11 11:00:03 -04:00
|
|
|
|
|
|
|
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
|
|
|
|
{
|
2013-09-18 07:38:08 -03:00
|
|
|
LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
|
|
|
|
LogPrintf("Previous lock order was:\n");
|
2017-06-01 21:28:42 -04:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : s2) {
|
2015-08-07 17:18:16 -03:00
|
|
|
if (i.first == mismatch.first) {
|
2018-04-05 11:17:32 -03:00
|
|
|
LogPrintf(" (1)"); /* Continued */
|
2015-08-07 17:18:16 -03:00
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2018-04-05 11:17:32 -03:00
|
|
|
LogPrintf(" (2)"); /* Continued */
|
2015-08-07 17:18:16 -03:00
|
|
|
}
|
2014-01-16 12:15:27 -03:00
|
|
|
LogPrintf(" %s\n", i.second.ToString());
|
2012-05-11 11:00:03 -04:00
|
|
|
}
|
2013-09-18 07:38:08 -03:00
|
|
|
LogPrintf("Current lock order is:\n");
|
2017-06-01 21:28:42 -04:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : s1) {
|
2015-08-07 17:18:16 -03:00
|
|
|
if (i.first == mismatch.first) {
|
2018-04-05 11:17:32 -03:00
|
|
|
LogPrintf(" (1)"); /* Continued */
|
2015-08-07 17:18:16 -03:00
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2018-04-05 11:17:32 -03:00
|
|
|
LogPrintf(" (2)"); /* Continued */
|
2015-08-07 17:18:16 -03:00
|
|
|
}
|
2014-01-16 12:15:27 -03:00
|
|
|
LogPrintf(" %s\n", i.second.ToString());
|
2012-05-11 11:00:03 -04:00
|
|
|
}
|
2017-11-08 17:28:35 -03:00
|
|
|
if (g_debug_lockorder_abort) {
|
2019-06-13 09:16:10 -04:00
|
|
|
tfm::format(std::cerr, "Assertion failed: detected inconsistent lock order at %s:%i, details in debug log.\n", __FILE__, __LINE__);
|
2017-11-08 17:28:35 -03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
throw std::logic_error("potential deadlock detected");
|
2012-05-11 11:00:03 -04:00
|
|
|
}
|
|
|
|
|
2017-09-30 18:43:55 -03:00
|
|
|
static void push_lock(void* c, const CLockLocation& locklocation)
|
2012-05-11 11:00:03 -04:00
|
|
|
{
|
2019-01-22 22:57:16 -03:00
|
|
|
LockData& lockdata = GetLockData();
|
2017-11-18 16:35:14 -03:00
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
2012-05-11 11:00:03 -04:00
|
|
|
|
2018-05-21 21:35:47 -04:00
|
|
|
g_lockstack.push_back(std::make_pair(c, locklocation));
|
2012-05-11 11:00:03 -04:00
|
|
|
|
2018-05-21 21:35:47 -04:00
|
|
|
for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
|
2017-02-07 16:15:28 -03:00
|
|
|
if (i.first == c)
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::pair<void*, void*> p1 = std::make_pair(i.first, c);
|
|
|
|
if (lockdata.lockorders.count(p1))
|
|
|
|
continue;
|
2018-05-05 15:15:30 -03:00
|
|
|
lockdata.lockorders.emplace(p1, g_lockstack);
|
2017-02-07 16:15:28 -03: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 11:00:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pop_lock()
|
|
|
|
{
|
2018-05-21 21:35:47 -04:00
|
|
|
g_lockstack.pop_back();
|
2012-05-11 11:00:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
|
|
|
|
{
|
2018-05-05 15:15:30 -03:00
|
|
|
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, util::ThreadGetInternalName()));
|
2012-05-11 11:00:03 -04:00
|
|
|
}
|
|
|
|
|
2020-03-01 01:22:37 -03:00
|
|
|
void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line)
|
|
|
|
{
|
|
|
|
if (!g_lockstack.empty()) {
|
|
|
|
const auto& lastlock = g_lockstack.back();
|
|
|
|
if (lastlock.first == cs) {
|
|
|
|
lockname = lastlock.second.Name();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw std::system_error(EPERM, std::generic_category(), strprintf("%s:%s %s was not most recent critical section locked", file, line, guardname));
|
|
|
|
}
|
|
|
|
|
2012-05-11 11:00:03 -04:00
|
|
|
void LeaveCritical()
|
|
|
|
{
|
|
|
|
pop_lock();
|
|
|
|
}
|
|
|
|
|
2013-11-29 04:25:30 -03:00
|
|
|
std::string LocksHeld()
|
|
|
|
{
|
|
|
|
std::string result;
|
2018-05-21 21:35:47 -04:00
|
|
|
for (const std::pair<void*, CLockLocation>& i : g_lockstack)
|
2013-11-29 04:25:30 -03:00
|
|
|
result += i.second.ToString() + std::string("\n");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-19 14:21:46 -03:00
|
|
|
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
|
2013-11-29 04:25:30 -03:00
|
|
|
{
|
2018-05-21 21:35:47 -04:00
|
|
|
for (const std::pair<void*, CLockLocation>& i : g_lockstack)
|
2014-09-19 14:21:46 -03:00
|
|
|
if (i.first == cs)
|
|
|
|
return;
|
2019-10-28 09:30:20 -03:00
|
|
|
tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
|
2014-02-18 14:11:46 -03:00
|
|
|
abort();
|
2013-11-29 04:25:30 -03:00
|
|
|
}
|
|
|
|
|
2017-01-17 19:42:46 -03:00
|
|
|
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
|
|
|
|
{
|
2018-05-21 21:35:47 -04:00
|
|
|
for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
|
2017-01-17 19:42:46 -03:00
|
|
|
if (i.first == cs) {
|
2019-10-28 09:30:20 -03:00
|
|
|
tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
|
2017-01-17 19:42:46 -03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 17:14:19 -03:00
|
|
|
void DeleteLock(void* cs)
|
|
|
|
{
|
2019-01-22 22:57:16 -03:00
|
|
|
LockData& lockdata = GetLockData();
|
2017-11-18 16:35:14 -03:00
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
2017-06-21 15:10:00 -04:00
|
|
|
std::pair<void*, void*> item = std::make_pair(cs, nullptr);
|
2016-04-08 17:14:19 -03: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++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 17:28:35 -03:00
|
|
|
bool g_debug_lockorder_abort = true;
|
|
|
|
|
2012-05-11 11:00:03 -04:00
|
|
|
#endif /* DEBUG_LOCKORDER */
|