]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/mutex_debug: refactor to remove intermediate class
authorSage Weil <sage@redhat.com>
Mon, 17 Sep 2018 16:54:40 +0000 (11:54 -0500)
committerSage Weil <sage@redhat.com>
Fri, 21 Sep 2018 16:51:09 +0000 (11:51 -0500)
I don't see any purpose for this, and it prevents us from knowing whether
the mutex is recursive when _will_lock() is called.

Signed-off-by: Sage Weil <sage@redhat.com>
src/common/mutex_debug.h

index 9ec6aae2c2ba824bc26cdbaab903fd812a81ed79..5823909b6df23083455f56c3b7677e71a7e462b4 100644 (file)
@@ -31,7 +31,9 @@ class PerfCounters;
 
 namespace ceph {
 namespace mutex_debug_detail {
-class mutex_debugging_base {
+
+class mutex_debugging_base
+{
 protected:
   std::string name;
   int id;
@@ -65,76 +67,19 @@ public:
   }
 };
 
-template<typename Mutex>
-class mutex_debugging : public mutex_debugging_base {
-  Mutex* impl;
-
-public:
-  mutex_debugging(const std::string &n = std::string(), bool bt = false) :
-    mutex_debugging_base(n, bt), impl(static_cast<Mutex*>(this)) {}
-
-  ~mutex_debugging() = default;
-
-  void _post_lock() {
-    if (!impl->recursive)
-      ceph_assert(nlock == 0);
-    locked_by = std::this_thread::get_id();
-    nlock++;
-  }
-
-  void _pre_unlock() {
-    ceph_assert(nlock > 0);
-    --nlock;
-    ceph_assert(locked_by == std::this_thread::get_id());
-    if (!impl->recursive)
-      ceph_assert(nlock == 0);
-    if (nlock == 0)
-      locked_by = std::thread::id();
-  }
-
-  bool try_lock(bool no_lockdep = false) {
-    bool locked = impl->try_lock_impl();
-    if (locked) {
-      if (g_lockdep && !no_lockdep)
-       _locked();
-      _post_lock();
-    }
-    return locked;
-  }
-
-  void lock(bool no_lockdep = false) {
-    if (g_lockdep && !no_lockdep)
-      _will_lock();
-
-    if (try_lock())
-      return;
-
-    auto t = before_lock_blocks();
-    impl->lock_impl();
-    after_lock_blocks(t, no_lockdep);
-    _post_lock();
-  }
-
-  void unlock(bool no_lockdep = false) {
-    _pre_unlock();
-    if (!no_lockdep && g_lockdep)
-      _will_unlock();
-    impl->unlock_impl();
-  }
-};
-
 // Since this is a /debugging/ mutex just define it in terms of the
 // pthread error check mutex.
 template<bool Recursive>
-class mutex_debug_impl : public mutex_debugging<mutex_debug_impl<Recursive> > {
+class mutex_debug_impl : public mutex_debugging_base
+{
 private:
   pthread_mutex_t m;
 public:
   static constexpr bool recursive = Recursive;
 
   // Mutex concept is DefaultConstructible
-  mutex_debug_impl(const std::string &n = std::string(), bool bt = false) :
-    mutex_debugging<mutex_debug_impl<Recursive> >(n, bt) {
+  mutex_debug_impl(const std::string &n = std::string(), bool bt = false)
+    : mutex_debugging_base(n, bt) {
     pthread_mutexattr_t a;
     pthread_mutexattr_init(&a);
     int r;
@@ -190,7 +135,57 @@ public:
   pthread_mutex_t* native_handle() {
     return &m;
   }
+
+  void _post_lock() {
+    if (!recursive)
+      ceph_assert(nlock == 0);
+    locked_by = std::this_thread::get_id();
+    nlock++;
+  }
+
+  void _pre_unlock() {
+    ceph_assert(nlock > 0);
+    --nlock;
+    ceph_assert(locked_by == std::this_thread::get_id());
+    if (!recursive)
+      ceph_assert(nlock == 0);
+    if (nlock == 0)
+      locked_by = std::thread::id();
+  }
+
+  bool try_lock(bool no_lockdep = false) {
+    bool locked = try_lock_impl();
+    if (locked) {
+      if (g_lockdep && !no_lockdep)
+       _locked();
+      _post_lock();
+    }
+    return locked;
+  }
+
+  void lock(bool no_lockdep = false) {
+    if (g_lockdep && !no_lockdep)
+      _will_lock();
+
+    if (try_lock())
+      return;
+
+    auto t = before_lock_blocks();
+    lock_impl();
+    after_lock_blocks(t, no_lockdep);
+    _post_lock();
+  }
+
+  void unlock(bool no_lockdep = false) {
+    _pre_unlock();
+    if (!no_lockdep && g_lockdep)
+      _will_unlock();
+    unlock_impl();
+  }
+
 };
+
+
 } // namespace mutex_debug_detail
 typedef mutex_debug_detail::mutex_debug_impl<false> mutex_debug;
 typedef mutex_debug_detail::mutex_debug_impl<true> mutex_recursive_debug;