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
|
|
|
|
2019-05-16 20:17:53 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2021-09-11 06:14:58 +02:00
|
|
|
#include <config/dash-config.h>
|
2019-05-16 20:17:53 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <sync.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2018-05-17 18:25:09 +02:00
|
|
|
#include <logging.h>
|
2020-05-05 07:07:47 +02:00
|
|
|
#include <tinyformat.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/strencodings.h>
|
|
|
|
#include <util/threadnames.h>
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2014-09-14 12:43:56 +02:00
|
|
|
|
2018-05-17 18:25:09 +02:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2020-05-05 07:07:47 +02:00
|
|
|
#include <system_error>
|
|
|
|
#include <thread>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2018-05-17 18:25:09 +02:00
|
|
|
|
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:
|
2018-02-02 11:35:42 +01:00
|
|
|
// Thread 1 locks A, then B, then C
|
|
|
|
// Thread 2 locks D, then C, then A
|
2012-05-11 17:00:03 +02: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 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 {
|
2021-06-24 19:54:20 +02: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 17:00:03 +02:00
|
|
|
|
|
|
|
std::string ToString() const
|
|
|
|
{
|
2021-10-24 12:51:47 +02:00
|
|
|
return strprintf(
|
2020-06-22 17:12:26 +02:00
|
|
|
"'%s' in %s:%s%s (in thread '%s')",
|
2021-06-24 19:54:20 +02:00
|
|
|
mutexName, sourceFile, itostr(sourceLine), (fTry ? " (TRY)" : ""), m_thread_name);
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 20:47:49 +01:00
|
|
|
std::string Name() const
|
|
|
|
{
|
|
|
|
return mutexName;
|
|
|
|
}
|
|
|
|
|
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;
|
2021-06-24 19:54:20 +02:00
|
|
|
const std::string& m_thread_name;
|
2012-05-11 17:00:03 +02:00
|
|
|
int sourceLine;
|
|
|
|
};
|
|
|
|
|
2020-05-05 07:07:47 +02:00
|
|
|
using LockStackItem = std::pair<void*, CLockLocation>;
|
|
|
|
using LockStack = std::vector<LockStackItem>;
|
|
|
|
using LockStacks = std::unordered_map<std::thread::id, LockStack>;
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2020-05-05 07:07:47 +02:00
|
|
|
using LockPair = std::pair<void*, void*>;
|
|
|
|
using LockOrders = std::map<LockPair, LockStack>;
|
|
|
|
using InvLockOrders = std::set<LockPair>;
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2020-05-05 07:07:47 +02:00
|
|
|
struct LockData {
|
|
|
|
LockStacks m_lock_stacks;
|
2016-04-14 14:57:13 +02:00
|
|
|
LockOrders lockorders;
|
|
|
|
InvLockOrders invlockorders;
|
2017-11-28 08:37:50 +01:00
|
|
|
std::mutex dd_mutex;
|
2019-01-25 07:11:17 +01:00
|
|
|
};
|
2020-05-05 07:07:47 +02:00
|
|
|
|
2019-01-25 07:11:17 +01:00
|
|
|
LockData& GetLockData() {
|
2020-05-05 07:07:47 +02: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.
|
|
|
|
// Since the ~LockData() destructor is never called, the LockData class and all
|
|
|
|
// its subclasses must have implicitly-defined destructors.
|
|
|
|
static LockData& lock_data = *new LockData();
|
|
|
|
return lock_data;
|
2019-01-25 07:11:17 +01:00
|
|
|
}
|
2016-04-14 14:57:13 +02:00
|
|
|
|
2020-05-05 07:07:47 +02:00
|
|
|
static void potential_deadlock_detected(const LockPair& mismatch, const LockStack& s1, const LockStack& s2)
|
2012-05-11 17:00:03 +02:00
|
|
|
{
|
2017-01-11 00:00:34 +01:00
|
|
|
std::string strOutput = "";
|
|
|
|
strOutput += "POTENTIAL DEADLOCK DETECTED\n";
|
|
|
|
strOutput += "Previous lock order was:\n";
|
2020-06-22 17:12:26 +02:00
|
|
|
for (const LockStackItem& 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
|
|
|
}
|
2022-03-21 18:28:10 +01:00
|
|
|
strOutput += strprintf(" %s\n", i.second.ToString());
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
2020-06-22 17:12:26 +02:00
|
|
|
|
|
|
|
std::string mutex_a, mutex_b;
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += "Current lock order is:\n";
|
2020-06-22 17:12:26 +02:00
|
|
|
for (const LockStackItem& 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)";
|
2020-06-22 17:12:26 +02:00
|
|
|
mutex_a = i.second.Name();
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2017-01-11 00:00:34 +01:00
|
|
|
strOutput += " (2)";
|
2020-06-22 17:12:26 +02:00
|
|
|
mutex_b = i.second.Name();
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
2022-03-21 18:28:10 +01:00
|
|
|
strOutput += strprintf(" %s\n", i.second.ToString());
|
2017-01-11 00:00:34 +01:00
|
|
|
}
|
2018-01-23 12:27:28 +01:00
|
|
|
|
2022-03-21 18:28:10 +01:00
|
|
|
LogPrintf("%s\n", strOutput);
|
2018-01-23 12:27:28 +01:00
|
|
|
|
2017-11-08 21:28:35 +01:00
|
|
|
if (g_debug_lockorder_abort) {
|
2022-03-21 18:28:10 +01:00
|
|
|
tfm::format(std::cerr, "Assertion failed: detected inconsistent lock order for %s, details in debug log.\n", s2.back().second.ToString());
|
2017-11-08 21:28:35 +01:00
|
|
|
abort();
|
|
|
|
}
|
2020-06-22 17:12:26 +02:00
|
|
|
throw std::logic_error(strprintf("potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
|
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
|
|
|
{
|
2019-01-25 07:11:17 +01:00
|
|
|
LockData& lockdata = GetLockData();
|
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
|
|
|
|
2020-05-05 07:07:47 +02:00
|
|
|
LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
|
|
|
|
lock_stack.emplace_back(c, locklocation);
|
|
|
|
for (const LockStackItem& i : lock_stack) {
|
2017-02-08 14:46:35 +01:00
|
|
|
if (i.first == c)
|
|
|
|
break;
|
2012-05-13 17:55:23 +02:00
|
|
|
|
2020-05-05 07:07:47 +02:00
|
|
|
const LockPair p1 = std::make_pair(i.first, c);
|
2017-02-08 14:46:35 +01:00
|
|
|
if (lockdata.lockorders.count(p1))
|
|
|
|
continue;
|
2012-05-13 17:55:23 +02:00
|
|
|
|
2020-05-05 07:07:47 +02:00
|
|
|
const LockPair p2 = std::make_pair(c, i.first);
|
2020-06-22 20:12:14 +02:00
|
|
|
if (lockdata.lockorders.count(p2)) {
|
|
|
|
auto lock_stack_copy = lock_stack;
|
|
|
|
lock_stack.pop_back();
|
|
|
|
potential_deadlock_detected(p1, lockdata.lockorders[p2], lock_stack_copy);
|
|
|
|
// potential_deadlock_detected() does not return.
|
|
|
|
}
|
|
|
|
|
|
|
|
lockdata.lockorders.emplace(p1, lock_stack);
|
2017-02-08 14:46:35 +01:00
|
|
|
lockdata.invlockorders.insert(p2);
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pop_lock()
|
|
|
|
{
|
2020-05-05 07:07:47 +02:00
|
|
|
LockData& lockdata = GetLockData();
|
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
|
|
|
|
|
|
|
LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
|
|
|
|
lock_stack.pop_back();
|
|
|
|
if (lock_stack.empty()) {
|
|
|
|
lockdata.m_lock_stacks.erase(std::this_thread::get_id());
|
|
|
|
}
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
|
|
|
|
{
|
2021-06-24 19:54:20 +02:00
|
|
|
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, util::ThreadGetInternalName()));
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 20:47:49 +01:00
|
|
|
void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line)
|
|
|
|
{
|
2020-05-05 07:07:47 +02:00
|
|
|
{
|
|
|
|
LockData& lockdata = GetLockData();
|
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
|
|
|
|
|
|
|
const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
|
|
|
|
if (!lock_stack.empty()) {
|
|
|
|
const auto& lastlock = lock_stack.back();
|
|
|
|
if (lastlock.first == cs) {
|
|
|
|
lockname = lastlock.second.Name();
|
|
|
|
return;
|
|
|
|
}
|
2020-03-06 20:47:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 17:00:03 +02:00
|
|
|
void LeaveCritical()
|
|
|
|
{
|
|
|
|
pop_lock();
|
|
|
|
}
|
|
|
|
|
2013-11-29 08:25:30 +01:00
|
|
|
std::string LocksHeld()
|
|
|
|
{
|
2020-05-05 07:07:47 +02:00
|
|
|
LockData& lockdata = GetLockData();
|
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
|
|
|
|
|
|
|
const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
|
2013-11-29 08:25:30 +01:00
|
|
|
std::string result;
|
2020-05-05 07:07:47 +02:00
|
|
|
for (const LockStackItem& i : lock_stack)
|
2013-11-29 08:25:30 +01:00
|
|
|
result += i.second.ToString() + std::string("\n");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-05 07:07:47 +02:00
|
|
|
static bool LockHeld(void* mutex)
|
|
|
|
{
|
|
|
|
LockData& lockdata = GetLockData();
|
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
|
|
|
|
|
|
|
const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
|
|
|
|
for (const LockStackItem& i : lock_stack) {
|
|
|
|
if (i.first == mutex) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:02:42 +02:00
|
|
|
template <typename MutexType>
|
|
|
|
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs)
|
2013-11-29 08:25:30 +01:00
|
|
|
{
|
2020-05-05 07:07:47 +02:00
|
|
|
if (LockHeld(cs)) return;
|
2022-03-21 18:28:10 +01: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 18:11:46 +01:00
|
|
|
abort();
|
2013-11-29 08:25:30 +01:00
|
|
|
}
|
2021-06-09 14:02:42 +02:00
|
|
|
template void AssertLockHeldInternal(const char*, const char*, int, Mutex*);
|
|
|
|
template void AssertLockHeldInternal(const char*, const char*, int, CCriticalSection*);
|
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)
|
|
|
|
{
|
2020-05-05 07:07:47 +02:00
|
|
|
if (!LockHeld(cs)) return;
|
2022-03-21 18:28:10 +01:00
|
|
|
tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
|
2020-05-05 07:07:47 +02:00
|
|
|
abort();
|
2017-01-17 23:42:46 +01:00
|
|
|
}
|
|
|
|
|
2016-04-14 14:57:13 +02:00
|
|
|
void DeleteLock(void* cs)
|
|
|
|
{
|
2019-01-25 07:11:17 +01:00
|
|
|
LockData& lockdata = GetLockData();
|
2017-11-28 08:37:50 +01:00
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
2020-05-05 07:07:47 +02:00
|
|
|
const LockPair 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) {
|
2020-05-05 07:07:47 +02:00
|
|
|
const LockPair invitem = std::make_pair(it->first.second, it->first.first);
|
2016-04-14 14:57:13 +02:00
|
|
|
lockdata.invlockorders.erase(invitem);
|
|
|
|
lockdata.lockorders.erase(it++);
|
|
|
|
}
|
|
|
|
InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
|
|
|
|
while (invit != lockdata.invlockorders.end() && invit->first == cs) {
|
2020-05-05 07:07:47 +02:00
|
|
|
const LockPair invinvitem = std::make_pair(invit->second, invit->first);
|
2016-04-14 14:57:13 +02:00
|
|
|
lockdata.lockorders.erase(invinvitem);
|
|
|
|
lockdata.invlockorders.erase(invit++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 20:12:14 +02:00
|
|
|
bool LockStackEmpty()
|
|
|
|
{
|
|
|
|
LockData& lockdata = GetLockData();
|
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
|
|
|
const auto it = lockdata.m_lock_stacks.find(std::this_thread::get_id());
|
|
|
|
if (it == lockdata.m_lock_stacks.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return it->second.empty();
|
|
|
|
}
|
|
|
|
|
2017-11-08 21:28:35 +01:00
|
|
|
bool g_debug_lockorder_abort = true;
|
|
|
|
|
2012-05-11 17:00:03 +02:00
|
|
|
#endif /* DEBUG_LOCKORDER */
|