]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/mutex_debug: add lockdep parameter to mutex's ctor
authorKefu Chai <kchai@redhat.com>
Thu, 25 Jul 2019 16:32:57 +0000 (00:32 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 3 Aug 2019 03:27:19 +0000 (11:27 +0800)
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 <kchai@redhat.com>
src/common/mutex_debug.cc
src/common/mutex_debug.h
src/common/shared_mutex_debug.cc
src/common/shared_mutex_debug.h

index e6b77c1424dc29691bb3c247969d5ad865749569..b832d211e7d1f9990eed30b9d3fd016cb26900b2 100644 (file)
@@ -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);
   }
 }
index c68be94a857f0f5baa0c4d598de5f82664c2479d..551e284c3acd2c4357c9365f38fee7143afd01b9 100644 (file)
@@ -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();
   }
 
index 3dd3814edb677c1dfd5cc83d1f8db24368cd9581..258cf4039bbcac3523a584ca8a75ab147bfa289f 100644 (file)
@@ -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) {
index 853ab0a271af69626241bb2ce38b55f01cd2920c..0d8d4658725e2f258d43cf1cea49a982532a0d79 100644 (file)
@@ -15,7 +15,6 @@ class shared_mutex_debug :
 {
   pthread_rwlock_t rwlock;
   const bool track;
-  const bool lockdep;
   std::atomic<unsigned> nrlock{0};
 
 public: