From 2dbd31eb0bdfcc1193e92069b82b9416b9659687 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 26 Jul 2019 00:32:57 +0800 Subject: [PATCH] common/mutex_debug: add lockdep parameter to mutex's ctor so we can disable lockdep when constructing a ceph::mutex. otherwise it's difficult to use the locks from standard library and to use `ceph_assert(ceph_mutex_is_locked(lock))` at the same time. Signed-off-by: Kefu Chai --- src/common/mutex_debug.cc | 13 ++++++------- src/common/mutex_debug.h | 19 +++++++++++-------- src/common/shared_mutex_debug.cc | 25 +++++++++++++------------ src/common/shared_mutex_debug.h | 1 - 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/common/mutex_debug.cc b/src/common/mutex_debug.cc index e6b77c1424d..b832d211e7d 100644 --- a/src/common/mutex_debug.cc +++ b/src/common/mutex_debug.cc @@ -25,20 +25,19 @@ enum { l_mutex_last }; -mutex_debugging_base::mutex_debugging_base(std::string group, bool bt) +mutex_debugging_base::mutex_debugging_base(std::string group, bool ld, bool bt) : group(std::move(group)), - id(-1), - backtrace(bt), - nlock(0), - locked_by(thread::id()) + lockdep(ld), + backtrace(bt) { - if (g_lockdep) + if (_enable_lockdep()) { _register(); + } } mutex_debugging_base::~mutex_debugging_base() { ceph_assert(nlock == 0); - if (g_lockdep) { + if (_enable_lockdep()) { lockdep_unregister(id); } } diff --git a/src/common/mutex_debug.h b/src/common/mutex_debug.h index c68be94a857..551e284c3ac 100644 --- a/src/common/mutex_debug.h +++ b/src/common/mutex_debug.h @@ -36,19 +36,22 @@ class mutex_debugging_base { protected: std::string group; - int id; + int id = -1; + bool lockdep; // track this mutex using lockdep_* bool backtrace; // gather backtrace on lock acquisition - int nlock; - std::thread::id locked_by; - + int nlock = 0; + std::thread::id locked_by = {}; + bool _enable_lockdep() const { + return lockdep && g_lockdep; + } void _register(); void _will_lock(bool recursive=false); // about to lock void _locked(); // just locked void _will_unlock(); // about to unlock - mutex_debugging_base(std::string group, bool bt = false); + mutex_debugging_base(std::string group, bool ld = true, bool bt = false); ~mutex_debugging_base(); public: @@ -90,15 +93,15 @@ private: } else if (no_lockdep) { return false; } else { - return g_lockdep; + return _enable_lockdep(); } } public: static constexpr bool recursive = Recursive; - mutex_debug_impl(std::string group, bool bt = false) - : mutex_debugging_base(group, bt) { + mutex_debug_impl(std::string group, bool ld = true, bool bt = false) + : mutex_debugging_base(group, ld, bt) { _init(); } diff --git a/src/common/shared_mutex_debug.cc b/src/common/shared_mutex_debug.cc index 3dd3814edb6..258cf4039bb 100644 --- a/src/common/shared_mutex_debug.cc +++ b/src/common/shared_mutex_debug.cc @@ -11,9 +11,10 @@ shared_mutex_debug::shared_mutex_debug(std::string group, bool track_lock, bool enable_lock_dep, bool prioritize_write) - : mutex_debugging_base{std::move(group), false /* backtrace */}, - track(track_lock), - lockdep(enable_lock_dep) + : mutex_debugging_base{std::move(group), + enable_lock_dep, + false /* backtrace */}, + track(track_lock) { #ifdef HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP if (prioritize_write) { @@ -40,13 +41,13 @@ shared_mutex_debug::shared_mutex_debug(std::string group, // exclusive void shared_mutex_debug::lock() { - if (g_lockdep && lockdep) { + if (_enable_lockdep()) { _will_lock(); } if (int r = pthread_rwlock_wrlock(&rwlock); r != 0) { throw std::system_error(r, std::generic_category()); } - if (lockdep && g_lockdep) { + if (_enable_lockdep()) { _locked(); } _post_lock(); @@ -57,7 +58,7 @@ bool shared_mutex_debug::try_lock() int r = pthread_rwlock_trywrlock(&rwlock); switch (r) { case 0: - if (lockdep && g_lockdep) { + if (_enable_lockdep()) { _locked(); } _post_lock(); @@ -72,7 +73,7 @@ bool shared_mutex_debug::try_lock() void shared_mutex_debug::unlock() { _pre_unlock(); - if (lockdep && g_lockdep) { + if (_enable_lockdep()) { _will_unlock(); } if (int r = pthread_rwlock_unlock(&rwlock); r != 0) { @@ -83,13 +84,13 @@ void shared_mutex_debug::unlock() // shared locking void shared_mutex_debug::lock_shared() { - if (lockdep && g_lockdep) { + if (_enable_lockdep()) { _will_lock(); } if (int r = pthread_rwlock_rdlock(&rwlock); r != 0) { throw std::system_error(r, std::generic_category()); } - if (lockdep && g_lockdep) { + if (_enable_lockdep()) { _locked(); } _post_lock_shared(); @@ -97,12 +98,12 @@ void shared_mutex_debug::lock_shared() bool shared_mutex_debug::try_lock_shared() { - if (lockdep && g_lockdep) { + if (_enable_lockdep()) { _will_unlock(); } switch (int r = pthread_rwlock_rdlock(&rwlock); r) { case 0: - if (lockdep && g_lockdep) { + if (_enable_lockdep()) { _locked(); } _post_lock_shared(); @@ -117,7 +118,7 @@ bool shared_mutex_debug::try_lock_shared() void shared_mutex_debug::unlock_shared() { _pre_unlock_shared(); - if (lockdep && g_lockdep) { + if (_enable_lockdep()) { _will_unlock(); } if (int r = pthread_rwlock_unlock(&rwlock); r != 0) { diff --git a/src/common/shared_mutex_debug.h b/src/common/shared_mutex_debug.h index 853ab0a271a..0d8d4658725 100644 --- a/src/common/shared_mutex_debug.h +++ b/src/common/shared_mutex_debug.h @@ -15,7 +15,6 @@ class shared_mutex_debug : { pthread_rwlock_t rwlock; const bool track; - const bool lockdep; std::atomic nrlock{0}; public: -- 2.39.5