2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2011-2018 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
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <sync.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2018-05-15 11:27:14 +02:00
|
|
|
#include <logging.h>
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <utilstrencodings.h>
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2014-09-14 12:43:56 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-05-15 11:27:14 +02:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
|
2012-06-05 16:12:32 +02:00
|
|
|
#ifdef DEBUG_LOCKCONTENTION
|
2017-11-27 23:08:27 +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)
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("LOCKCONTENTION: %s\n", pszName);
|
|
|
|
LogPrintf("Locker: %s:%d\n", 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-01-28 13:14:54 +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 {
|
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-18 20:35:14 +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-08 22:14:19 +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-08 22:14:19 +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-08 22:14:19 +02:00
|
|
|
LockOrders lockorders;
|
|
|
|
InvLockOrders invlockorders;
|
2017-11-18 20:35:14 +01:00
|
|
|
std::mutex dd_mutex;
|
2016-04-08 22:14:19 +02:00
|
|
|
} static lockdata;
|
|
|
|
|
2018-05-22 03:35:47 +02:00
|
|
|
static thread_local LockStack g_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)
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
|
|
|
|
LogPrintf("Previous lock order was:\n");
|
2017-06-02 03:28:42 +02:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : s2) {
|
2015-08-07 22:18:16 +02:00
|
|
|
if (i.first == mismatch.first) {
|
2018-04-05 16:17:32 +02:00
|
|
|
LogPrintf(" (1)"); /* Continued */
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2018-04-05 16:17:32 +02:00
|
|
|
LogPrintf(" (2)"); /* Continued */
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf(" %s\n", i.second.ToString());
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("Current lock order is:\n");
|
2017-06-02 03:28:42 +02:00
|
|
|
for (const std::pair<void*, CLockLocation> & i : s1) {
|
2015-08-07 22:18:16 +02:00
|
|
|
if (i.first == mismatch.first) {
|
2018-04-05 16:17:32 +02:00
|
|
|
LogPrintf(" (1)"); /* Continued */
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2018-04-05 16:17:32 +02:00
|
|
|
LogPrintf(" (2)"); /* Continued */
|
2015-08-07 22:18:16 +02:00
|
|
|
}
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf(" %s\n", i.second.ToString());
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
2017-02-03 04:22:01 +01:00
|
|
|
assert(false);
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 23:43:55 +02:00
|
|
|
static void push_lock(void* c, const CLockLocation& locklocation)
|
2012-05-11 17:00:03 +02:00
|
|
|
{
|
2017-11-18 20:35:14 +01:00
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2018-05-22 03:35:47 +02:00
|
|
|
g_lockstack.push_back(std::make_pair(c, locklocation));
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2018-05-22 03:35:47 +02:00
|
|
|
for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
|
2017-02-07 20:15:28 +01: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-22 03:35:47 +02:00
|
|
|
lockdata.lockorders[p1] = g_lockstack;
|
2017-02-07 20:15:28 +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()
|
|
|
|
{
|
2018-05-22 03:35:47 +02:00
|
|
|
g_lockstack.pop_back();
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
|
|
|
|
{
|
2017-09-30 23:43:55 +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;
|
2018-05-22 03:35:47 +02:00
|
|
|
for (const std::pair<void*, CLockLocation>& i : g_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
|
|
|
{
|
2018-05-22 03:35:47 +02:00
|
|
|
for (const std::pair<void*, CLockLocation>& i : g_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)
|
|
|
|
{
|
2018-05-22 03:35:47 +02:00
|
|
|
for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
|
2017-01-17 23:42:46 +01:00
|
|
|
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-08 22:14:19 +02:00
|
|
|
void DeleteLock(void* cs)
|
|
|
|
{
|
|
|
|
if (!lockdata.available) {
|
|
|
|
// We're already shutting down.
|
|
|
|
return;
|
|
|
|
}
|
2017-11-18 20:35:14 +01:00
|
|
|
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
|
2017-06-21 21:10:00 +02:00
|
|
|
std::pair<void*, void*> item = std::make_pair(cs, nullptr);
|
2016-04-08 22:14:19 +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 */
|