15#include <system_error>
18#include <unordered_map>
40 std::string&& thread_name)
44 m_thread_name(
std::move(thread_name)),
50 "'%s' in %s:%s%s (in thread '%s')",
51 mutexName, sourceFile, sourceLine, (fTry ?
" (TRY)" :
""), m_thread_name);
54 std::string Name()
const
61 std::string mutexName;
62 std::string sourceFile;
63 const std::string m_thread_name;
67using LockStackItem = std::pair<void*, CLockLocation>;
68using LockStack = std::vector<LockStackItem>;
69using LockStacks = std::unordered_map<std::thread::id, LockStack>;
71using LockPair = std::pair<void*, void*>;
72using LockOrders = std::map<LockPair, LockStack>;
73using InvLockOrders = std::set<LockPair>;
76 LockStacks m_lock_stacks;
77 LockOrders lockorders;
78 InvLockOrders invlockorders;
82LockData& GetLockData() {
87 static LockData& lock_data = *
new LockData();
91static void potential_deadlock_detected(
const LockPair& mismatch,
const LockStack& s1,
const LockStack& s2)
93 LogPrintf(
"POTENTIAL DEADLOCK DETECTED\n");
95 for (
const LockStackItem& i : s1) {
97 if (i.first == mismatch.first) {
100 if (i.first == mismatch.second) {
106 std::string mutex_a, mutex_b;
108 for (
const LockStackItem& i : s2) {
110 if (i.first == mismatch.first) {
112 mutex_a = i.second.Name();
114 if (i.first == mismatch.second) {
116 mutex_b = i.second.Name();
120 if (g_debug_lockorder_abort) {
121 tfm::format(std::cerr,
"Assertion failed: detected inconsistent lock order for %s, details in debug log.\n", s2.back().second.ToString());
124 throw std::logic_error(
strprintf(
"potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
127static void double_lock_detected(
const void* mutex,
const LockStack& lock_stack)
131 for (
const LockStackItem& i : lock_stack) {
133 if (i.first == mutex) {
138 if (g_debug_lockorder_abort) {
140 "Assertion failed: detected double lock for %s, details in debug log.\n",
141 lock_stack.back().second.ToString());
144 throw std::logic_error(
"double lock detected");
147template <
typename MutexType>
148static void push_lock(MutexType* c,
const CLockLocation& locklocation)
150 constexpr bool is_recursive_mutex =
151 std::is_base_of<RecursiveMutex, MutexType>::value ||
152 std::is_base_of<std::recursive_mutex, MutexType>::value;
154 LockData& lockdata = GetLockData();
155 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
157 LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
158 lock_stack.emplace_back(c, locklocation);
159 for (
size_t j = 0; j < lock_stack.size() - 1; ++j) {
160 const LockStackItem& i = lock_stack[j];
162 if (is_recursive_mutex) {
169 auto lock_stack_copy = lock_stack;
170 lock_stack.pop_back();
171 double_lock_detected(c, lock_stack_copy);
175 const LockPair p1 = std::make_pair(i.first, c);
176 if (lockdata.lockorders.count(p1))
179 const LockPair p2 = std::make_pair(c, i.first);
180 if (lockdata.lockorders.count(p2)) {
181 auto lock_stack_copy = lock_stack;
182 lock_stack.pop_back();
183 potential_deadlock_detected(p1, lockdata.lockorders[p2], lock_stack_copy);
187 lockdata.lockorders.emplace(p1, lock_stack);
188 lockdata.invlockorders.insert(p2);
192static void pop_lock()
194 LockData& lockdata = GetLockData();
195 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
197 LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
198 lock_stack.pop_back();
199 if (lock_stack.empty()) {
200 lockdata.m_lock_stacks.erase(std::this_thread::get_id());
204template <
typename MutexType>
205void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine, MutexType*
cs,
bool fTry)
211template void EnterCritical(
const char*,
const char*,
int, std::mutex*,
bool);
212template void EnterCritical(
const char*,
const char*,
int, std::recursive_mutex*,
bool);
214void CheckLastCritical(
void*
cs, std::string& lockname,
const char* guardname,
const char* file,
int line)
216 LockData& lockdata = GetLockData();
217 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
219 const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
220 if (!lock_stack.empty()) {
221 const auto& lastlock = lock_stack.back();
222 if (lastlock.first ==
cs) {
223 lockname = lastlock.second.Name();
228 LogPrintf(
"INCONSISTENT LOCK ORDER DETECTED\n");
229 LogPrintf(
"Current lock order (least recent first) is:\n");
230 for (
const LockStackItem& i : lock_stack) {
233 if (g_debug_lockorder_abort) {
234 tfm::format(std::cerr,
"%s:%s %s was not most recent critical section locked, details in debug log.\n", file, line, guardname);
237 throw std::logic_error(
strprintf(
"%s was not most recent critical section locked", guardname));
245static std::string LocksHeld()
247 LockData& lockdata = GetLockData();
248 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
250 const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
252 for (
const LockStackItem& i : lock_stack)
253 result += i.second.ToString() + std::string(
"\n");
257static bool LockHeld(
void* mutex)
259 LockData& lockdata = GetLockData();
260 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
262 const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
263 for (
const LockStackItem& i : lock_stack) {
264 if (i.first == mutex)
return true;
270template <
typename MutexType>
273 if (LockHeld(
cs))
return;
274 tfm::format(std::cerr,
"Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
280template <
typename MutexType>
283 if (!LockHeld(
cs))
return;
284 tfm::format(std::cerr,
"Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
292 LockData& lockdata = GetLockData();
293 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
294 const LockPair item = std::make_pair(
cs,
nullptr);
295 LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
296 while (it != lockdata.lockorders.end() && it->first.first ==
cs) {
297 const LockPair invitem = std::make_pair(it->first.second, it->first.first);
298 lockdata.invlockorders.erase(invitem);
299 lockdata.lockorders.erase(it++);
301 InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
302 while (invit != lockdata.invlockorders.end() && invit->first ==
cs) {
303 const LockPair invinvitem = std::make_pair(invit->second, invit->first);
304 lockdata.lockorders.erase(invinvitem);
305 lockdata.invlockorders.erase(invit++);
311 LockData& lockdata = GetLockData();
312 std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
313 const auto it = lockdata.m_lock_stacks.find(std::this_thread::get_id());
314 if (it == lockdata.m_lock_stacks.end()) {
317 return it->second.empty();
320bool g_debug_lockorder_abort =
true;
std::string ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
void EnterCritical(const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
void DeleteLock(void *cs)
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) LOCKS_EXCLUDED(cs)