2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2011-2012 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
|
|
|
|
|
|
|
#include "sync.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-05-13 17:55:23 +02:00
|
|
|
#include "util.h"
|
2014-08-28 01:23:13 +02:00
|
|
|
#include "utilstrencodings.h"
|
2012-05-11 17:00:03 +02:00
|
|
|
|
2014-09-14 12:43:56 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-05-13 17:55:23 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2014-08-28 01:23:13 +02:00
|
|
|
#include <boost/thread.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
|
|
|
//
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-29 08:25:30 +01:00
|
|
|
std::string MutexName() const { return mutexName; }
|
|
|
|
|
2015-08-07 22:18:16 +02:00
|
|
|
bool fTry;
|
2012-05-11 17:00:03 +02:00
|
|
|
private:
|
|
|
|
std::string mutexName;
|
|
|
|
std::string sourceFile;
|
|
|
|
int sourceLine;
|
|
|
|
};
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
|
2012-05-11 17:00:03 +02:00
|
|
|
|
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)
|
|
|
|
{
|
2015-08-07 22:18:16 +02:00
|
|
|
// We attempt to not assert on probably-not deadlocks by assuming that
|
|
|
|
// a try lock will immediately have otherwise bailed if it had
|
|
|
|
// failed to get the lock
|
|
|
|
// We do this by, for the locks which triggered the potential deadlock,
|
|
|
|
// in either lockorder, checking that the second of the two which is locked
|
|
|
|
// is only a TRY_LOCK, ignoring locks if they are reentrant.
|
|
|
|
bool firstLocked = false;
|
|
|
|
bool secondLocked = false;
|
|
|
|
bool onlyMaybeDeadlock = false;
|
|
|
|
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
|
|
|
|
LogPrintf("Previous lock order was:\n");
|
2014-09-19 19:21:46 +02:00
|
|
|
BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, s2) {
|
2015-08-07 22:18:16 +02:00
|
|
|
if (i.first == mismatch.first) {
|
2014-09-19 19:21:46 +02:00
|
|
|
LogPrintf(" (1)");
|
2015-08-07 22:18:16 +02:00
|
|
|
if (!firstLocked && secondLocked && i.second.fTry)
|
|
|
|
onlyMaybeDeadlock = true;
|
|
|
|
firstLocked = true;
|
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2014-09-19 19:21:46 +02:00
|
|
|
LogPrintf(" (2)");
|
2015-08-07 22:18:16 +02:00
|
|
|
if (!secondLocked && firstLocked && i.second.fTry)
|
|
|
|
onlyMaybeDeadlock = true;
|
|
|
|
secondLocked = true;
|
|
|
|
}
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf(" %s\n", i.second.ToString());
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
2015-08-07 22:18:16 +02:00
|
|
|
firstLocked = false;
|
|
|
|
secondLocked = false;
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("Current lock order is:\n");
|
2014-09-19 19:21:46 +02:00
|
|
|
BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, s1) {
|
2015-08-07 22:18:16 +02:00
|
|
|
if (i.first == mismatch.first) {
|
2014-09-19 19:21:46 +02:00
|
|
|
LogPrintf(" (1)");
|
2015-08-07 22:18:16 +02:00
|
|
|
if (!firstLocked && secondLocked && i.second.fTry)
|
|
|
|
onlyMaybeDeadlock = true;
|
|
|
|
firstLocked = true;
|
|
|
|
}
|
|
|
|
if (i.first == mismatch.second) {
|
2014-09-19 19:21:46 +02:00
|
|
|
LogPrintf(" (2)");
|
2015-08-07 22:18:16 +02:00
|
|
|
if (!secondLocked && firstLocked && i.second.fTry)
|
|
|
|
onlyMaybeDeadlock = true;
|
|
|
|
secondLocked = true;
|
|
|
|
}
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf(" %s\n", i.second.ToString());
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
2015-08-07 22:18:16 +02:00
|
|
|
assert(onlyMaybeDeadlock);
|
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);
|
|
|
|
|
|
|
|
dd_mutex.lock();
|
|
|
|
|
|
|
|
(*lockstack).push_back(std::make_pair(c, locklocation));
|
|
|
|
|
2012-05-13 17:55:23 +02:00
|
|
|
if (!fTry) {
|
2014-09-19 19:21:46 +02:00
|
|
|
BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, (*lockstack)) {
|
|
|
|
if (i.first == c)
|
|
|
|
break;
|
2012-05-13 17:55:23 +02:00
|
|
|
|
|
|
|
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);
|
2015-08-07 22:18:16 +02:00
|
|
|
if (lockorders.count(p2))
|
2012-05-13 17:55:23 +02:00
|
|
|
potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
|
2012-05-11 17:00:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dd_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pop_lock()
|
|
|
|
{
|
|
|
|
dd_mutex.lock();
|
|
|
|
(*lockstack).pop_back();
|
|
|
|
dd_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
|
|
|
|
{
|
2015-08-07 22:18:16 +02:00
|
|
|
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry), 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;
|
2014-09-19 19:21:46 +02:00
|
|
|
BOOST_FOREACH (const PAIRTYPE(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
|
|
|
{
|
2014-09-19 19:21:46 +02:00
|
|
|
BOOST_FOREACH (const PAIRTYPE(void*, CLockLocation) & i, *lockstack)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-05-11 17:00:03 +02:00
|
|
|
#endif /* DEBUG_LOCKORDER */
|