2012-05-11 17:00:03 +02:00
|
|
|
// Copyright (c) 2011-2012 The Bitcoin developers
|
|
|
|
// Distributed under the MIT/X11 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
|
|
|
|
|
|
|
#include "sync.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-05-13 17:55:23 +02:00
|
|
|
#include "util.h"
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2012-05-13 17:55:23 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2012-06-05 16:12:32 +02:00
|
|
|
#ifdef DEBUG_LOCKCONTENTION
|
|
|
|
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:
|
|
|
|
// 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
|
|
|
//
|
|
|
|
|
|
|
|
struct CLockLocation
|
|
|
|
{
|
|
|
|
CLockLocation(const char* pszName, const char* pszFile, int nLine)
|
|
|
|
{
|
|
|
|
mutexName = pszName;
|
|
|
|
sourceFile = pszFile;
|
|
|
|
sourceLine = nLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString() const
|
|
|
|
{
|
|
|
|
return mutexName+" "+sourceFile+":"+itostr(sourceLine);
|
|
|
|
}
|
|
|
|
|
2013-11-29 08:25:30 +01:00
|
|
|
std::string MutexName() const { return mutexName; }
|
|
|
|
|
2012-05-11 17:00:03 +02:00
|
|
|
private:
|
|
|
|
std::string mutexName;
|
|
|
|
std::string sourceFile;
|
|
|
|
int sourceLine;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector< std::pair<void*, CLockLocation> > LockStack;
|
|
|
|
|
2012-05-13 17:55:23 +02:00
|
|
|
static boost::mutex dd_mutex;
|
2012-05-11 17:00:03 +02:00
|
|
|
static std::map<std::pair<void*, void*>, LockStack> lockorders;
|
|
|
|
static boost::thread_specific_ptr<LockStack> lockstack;
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
2012-05-11 17:00:03 +02:00
|
|
|
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s2)
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
if (i.first == mismatch.first) LogPrintf(" (1)");
|
|
|
|
if (i.first == mismatch.second) LogPrintf(" (2)");
|
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");
|
2012-05-11 17:00:03 +02:00
|
|
|
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s1)
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
if (i.first == mismatch.first) LogPrintf(" (1)");
|
|
|
|
if (i.first == mismatch.second) LogPrintf(" (2)");
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf(" %s\n", i.second.ToString());
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
|
|
|
|
{
|
|
|
|
if (lockstack.get() == NULL)
|
|
|
|
lockstack.reset(new LockStack);
|
|
|
|
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrint("lock", "Locking: %s\n", locklocation.ToString());
|
2012-05-11 17:00:03 +02:00
|
|
|
dd_mutex.lock();
|
|
|
|
|
|
|
|
(*lockstack).push_back(std::make_pair(c, locklocation));
|
|
|
|
|
2012-05-13 17:55:23 +02:00
|
|
|
if (!fTry) {
|
|
|
|
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, (*lockstack)) {
|
|
|
|
if (i.first == c) break;
|
|
|
|
|
|
|
|
std::pair<void*, void*> p1 = std::make_pair(i.first, c);
|
|
|
|
if (lockorders.count(p1))
|
|
|
|
continue;
|
|
|
|
lockorders[p1] = (*lockstack);
|
|
|
|
|
|
|
|
std::pair<void*, void*> p2 = std::make_pair(c, i.first);
|
|
|
|
if (lockorders.count(p2))
|
|
|
|
{
|
|
|
|
potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dd_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pop_lock()
|
|
|
|
{
|
2012-09-18 21:07:58 +02:00
|
|
|
if (fDebug)
|
2012-05-11 17:00:03 +02:00
|
|
|
{
|
|
|
|
const CLockLocation& locklocation = (*lockstack).rbegin()->second;
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrint("lock", "Unlocked: %s\n", locklocation.ToString());
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
dd_mutex.lock();
|
|
|
|
(*lockstack).pop_back();
|
|
|
|
dd_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
|
|
|
|
{
|
|
|
|
push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LeaveCritical()
|
|
|
|
{
|
|
|
|
pop_lock();
|
|
|
|
}
|
|
|
|
|
2013-11-29 08:25:30 +01:00
|
|
|
std::string LocksHeld()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)&i, *lockstack)
|
|
|
|
result += i.second.ToString() + std::string("\n");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-12-19 09:09:51 +01:00
|
|
|
void AssertLockHeldInternal(const char *pszName, const char* pszFile, int nLine, void *cs)
|
2013-11-29 08:25:30 +01:00
|
|
|
{
|
|
|
|
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)&i, *lockstack)
|
2013-12-19 09:09:51 +01:00
|
|
|
if (i.first == cs) return;
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("Lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
|
2013-11-29 08:25:30 +01:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2012-05-11 17:00:03 +02:00
|
|
|
#endif /* DEBUG_LOCKORDER */
|