]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/mutex_debug: take const char * to ctor, and require a name
authorSage Weil <sage@redhat.com>
Thu, 20 Sep 2018 17:10:13 +0000 (12:10 -0500)
committerSage Weil <sage@redhat.com>
Fri, 21 Sep 2018 16:52:56 +0000 (11:52 -0500)
Require a name, like Mutex.

Most callers are passing a C string.  This may avoid a std::string
copy?

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

index c9c8a7e6b0c7e4f12799d075829cc770dfe4542a..febc21fa16b883a3a2104094e4db49c59858e7e5 100644 (file)
@@ -25,15 +25,15 @@ enum {
   l_mutex_last
 };
 
-mutex_debugging_base::mutex_debugging_base(const std::string &n, bool bt) :
-  id(-1), backtrace(bt), nlock(0), locked_by(thread::id()) {
-  if (n.empty()) {
-    uuid_d uu;
-    uu.generate_random();
-    name = string("Unnamed-Mutex-") + uu.to_string();
-  } else {
-    name = n;
-  }
+mutex_debugging_base::mutex_debugging_base(const std::string &n, bool bt)
+  : name(n), id(-1), backtrace(bt), nlock(0), locked_by(thread::id())
+{
+  if (g_lockdep)
+    _register();
+}
+mutex_debugging_base::mutex_debugging_base(const char *n, bool bt)
+  : name(n), id(-1), backtrace(bt), nlock(0), locked_by(thread::id())
+{
   if (g_lockdep)
     _register();
 }
index 4c8d6ff74d55917fd9bf6d62782aea6e6fdc2b20..af32357fd226ed3d933b7e6e0b0efb83a2153b29 100644 (file)
@@ -48,7 +48,8 @@ protected:
   void _locked(); // just locked
   void _will_unlock(); // about to unlock
 
-  mutex_debugging_base(const std::string &n = std::string(), bool bt = false);
+  mutex_debugging_base(const std::string &n, bool bt = false);
+  mutex_debugging_base(const char *n, bool bt = false);
   ~mutex_debugging_base();
 
   ceph::mono_time before_lock_blocks();
@@ -74,12 +75,8 @@ 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_base(n, bt) {
+  void _init() {
     pthread_mutexattr_t a;
     pthread_mutexattr_init(&a);
     int r;
@@ -91,6 +88,20 @@ public:
     r = pthread_mutex_init(&m, &a);
     ceph_assert(r == 0);
   }
+
+public:
+  static constexpr bool recursive = Recursive;
+
+  // Mutex concept is DefaultConstructible
+  mutex_debug_impl(const std::string &n, bool bt = false)
+    : mutex_debugging_base(n, bt) {
+    _init();
+  }
+  mutex_debug_impl(const char *n, bool bt = false)
+    : mutex_debugging_base(n, bt) {
+    _init();
+  }
+
   // Mutex is Destructible
   ~mutex_debug_impl() {
     int r = pthread_mutex_destroy(&m);
index 49cd499557f0dd40ac2bd1fde3066bee8d830040..977dfe738a921bc39e47f7db9caa2ff72e0cf8c7 100644 (file)
@@ -31,7 +31,7 @@ static bool test_try_lock(Mutex* m) {
 
 template<typename Mutex>
 static void test_lock() {
-  Mutex m;
+  Mutex m("mutex");
   auto ttl = &test_try_lock<Mutex>;
 
   m.lock();
@@ -58,7 +58,7 @@ TEST(MutexDebug, Lock) {
 }
 
 TEST(MutexDebug, NotRecursive) {
-  ceph::mutex_debug m;
+  ceph::mutex_debug m("foo");
   auto ttl = &test_try_lock<mutex_debug>;
 
   ASSERT_NO_THROW(m.lock());
@@ -80,7 +80,7 @@ TEST(MutexRecursiveDebug, Lock) {
 
 
 TEST(MutexRecursiveDebug, Recursive) {
-  ceph::mutex_recursive_debug m;
+  ceph::mutex_recursive_debug m("m");
   auto ttl = &test_try_lock<mutex_recursive_debug>;
 
   ASSERT_NO_THROW(m.lock());