Skip to content

Commit

Permalink
Merge pull request ceph#38765 from tchaikov/wip-mutex-debug-atomic
Browse files Browse the repository at this point in the history
common/mutex_debug: add memory barrier before load/store nlock

Reviewed-by: Josh Durgin <[email protected]>
  • Loading branch information
tchaikov authored Jan 13, 2021
2 parents 6c3cc15 + 966c78e commit 058cbfb
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/common/mutex_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef CEPH_COMMON_MUTEX_DEBUG_H
#define CEPH_COMMON_MUTEX_DEBUG_H

#include <atomic>
#include <system_error>
#include <thread>

Expand All @@ -38,7 +39,7 @@ class mutex_debugging_base
bool lockdep; // track this mutex using lockdep_*
bool backtrace; // gather backtrace on lock acquisition

int nlock = 0;
std::atomic<int> nlock = 0;
std::thread::id locked_by = {};

bool _enable_lockdep() const {
Expand All @@ -57,10 +58,10 @@ class mutex_debugging_base
return (nlock > 0);
}
bool is_locked_by_me() const {
return nlock > 0 && locked_by == std::this_thread::get_id();
return nlock.load(std::memory_order_acquire) > 0 && locked_by == std::this_thread::get_id();
}
operator bool() const {
return nlock > 0 && locked_by == std::this_thread::get_id();
return is_locked_by_me();
}
};

Expand Down Expand Up @@ -152,17 +153,19 @@ class mutex_debug_impl : public mutex_debugging_base
if (!recursive)
ceph_assert(nlock == 0);
locked_by = std::this_thread::get_id();
nlock++;
nlock.fetch_add(1, std::memory_order_release);
}

void _pre_unlock() {
ceph_assert(nlock > 0);
--nlock;
if (recursive) {
ceph_assert(nlock > 0);
} else {
ceph_assert(nlock == 1);
}
ceph_assert(locked_by == std::this_thread::get_id());
if (!recursive)
ceph_assert(nlock == 0);
if (nlock == 0)
if (nlock == 1)
locked_by = std::thread::id();
nlock.fetch_sub(1, std::memory_order_release);
}

bool try_lock(bool no_lockdep = false) {
Expand Down

0 comments on commit 058cbfb

Please sign in to comment.