aboutsummaryrefslogtreecommitdiff
path: root/src/sync.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sync.cpp')
-rw-r--r--src/sync.cpp40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/sync.cpp b/src/sync.cpp
index 7de8439d6f..d020b4e334 100644
--- a/src/sync.cpp
+++ b/src/sync.cpp
@@ -20,6 +20,7 @@
#include <set>
#include <system_error>
#include <thread>
+#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
@@ -138,17 +139,50 @@ static void potential_deadlock_detected(const LockPair& mismatch, const LockStac
throw std::logic_error(strprintf("potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
}
+static void double_lock_detected(const void* mutex, LockStack& lock_stack)
+{
+ LogPrintf("DOUBLE LOCK DETECTED\n");
+ LogPrintf("Lock order:\n");
+ for (const LockStackItem& i : lock_stack) {
+ if (i.first == mutex) {
+ LogPrintf(" (*)"); /* Continued */
+ }
+ LogPrintf(" %s\n", i.second.ToString());
+ }
+ if (g_debug_lockorder_abort) {
+ tfm::format(std::cerr, "Assertion failed: detected double lock at %s:%i, details in debug log.\n", __FILE__, __LINE__);
+ abort();
+ }
+ throw std::logic_error("double lock detected");
+}
+
template <typename MutexType>
static void push_lock(MutexType* c, const CLockLocation& locklocation)
{
+ constexpr bool is_recursive_mutex =
+ std::is_base_of<RecursiveMutex, MutexType>::value ||
+ std::is_base_of<std::recursive_mutex, MutexType>::value;
+
LockData& lockdata = GetLockData();
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
lock_stack.emplace_back(c, locklocation);
- for (const LockStackItem& i : lock_stack) {
- if (i.first == c)
- break;
+ for (size_t j = 0; j < lock_stack.size() - 1; ++j) {
+ const LockStackItem& i = lock_stack[j];
+ if (i.first == c) {
+ if (is_recursive_mutex) {
+ break;
+ }
+ // It is not a recursive mutex and it appears in the stack two times:
+ // at position `j` and at the end (which we added just before this loop).
+ // Can't allow locking the same (non-recursive) mutex two times from the
+ // same thread as that results in an undefined behavior.
+ auto lock_stack_copy = lock_stack;
+ lock_stack.pop_back();
+ double_lock_detected(c, lock_stack_copy);
+ // double_lock_detected() does not return.
+ }
const LockPair p1 = std::make_pair(i.first, c);
if (lockdata.lockorders.count(p1))