Changeset View
Changeset View
Standalone View
Standalone View
src/sync.cpp
// Copyright (c) 2011-2016 The Bitcoin Core developers | // Copyright (c) 2011-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <logging.h> | #include <logging.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/threadnames.h> | #include <util/threadnames.h> | ||||
#include <tinyformat.h> | #include <tinyformat.h> | ||||
#include <cstdio> | #include <cstdio> | ||||
#include <map> | #include <map> | ||||
#include <set> | #include <set> | ||||
#include <system_error> | |||||
#ifdef DEBUG_LOCKCONTENTION | #ifdef DEBUG_LOCKCONTENTION | ||||
void PrintLockContention(const char *pszName, const char *pszFile, int nLine) { | void PrintLockContention(const char *pszName, const char *pszFile, int nLine) { | ||||
LogPrintf("LOCKCONTENTION: %s\n", pszName); | LogPrintf("LOCKCONTENTION: %s\n", pszName); | ||||
LogPrintf("Locker: %s:%d\n", pszFile, nLine); | LogPrintf("Locker: %s:%d\n", pszFile, nLine); | ||||
} | } | ||||
#endif /* DEBUG_LOCKCONTENTION */ | #endif /* DEBUG_LOCKCONTENTION */ | ||||
Show All 17 Lines | CLockLocation(const char *pszName, const char *pszFile, int nLine, | ||||
m_thread_name(thread_name), sourceLine(nLine) {} | m_thread_name(thread_name), sourceLine(nLine) {} | ||||
std::string ToString() const { | std::string ToString() const { | ||||
return strprintf("%s %s:%s%s (in thread %s)", mutexName, sourceFile, | return strprintf("%s %s:%s%s (in thread %s)", mutexName, sourceFile, | ||||
itostr(sourceLine), (fTry ? " (TRY)" : ""), | itostr(sourceLine), (fTry ? " (TRY)" : ""), | ||||
m_thread_name); | m_thread_name); | ||||
} | } | ||||
std::string Name() const { return mutexName; } | |||||
private: | private: | ||||
bool fTry; | bool fTry; | ||||
std::string mutexName; | std::string mutexName; | ||||
std::string sourceFile; | std::string sourceFile; | ||||
const std::string &m_thread_name; | const std::string &m_thread_name; | ||||
int sourceLine; | int sourceLine; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
void EnterCritical(const char *pszName, const char *pszFile, int nLine, | void EnterCritical(const char *pszName, const char *pszFile, int nLine, | ||||
void *cs, bool fTry) { | void *cs, bool fTry) { | ||||
push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, | push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, | ||||
util::ThreadGetInternalName())); | util::ThreadGetInternalName())); | ||||
} | } | ||||
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)); | |||||
} | |||||
void LeaveCritical() { | void LeaveCritical() { | ||||
pop_lock(); | pop_lock(); | ||||
} | } | ||||
std::string LocksHeld() { | std::string LocksHeld() { | ||||
std::string result; | std::string result; | ||||
for (const std::pair<void *, CLockLocation> &i : g_lockstack) { | for (const std::pair<void *, CLockLocation> &i : g_lockstack) { | ||||
result += i.second.ToString() + std::string("\n"); | result += i.second.ToString() + std::string("\n"); | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |