From 77acda721910964faa52175afc9d32a5fec8e415 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 20 Sep 2018 12:10:13 -0500 Subject: [PATCH] common/mutex_debug: take const char * to ctor, and require a name Require a name, like Mutex. Most callers are passing a C string. This may avoid a std::string copy? Signed-off-by: Sage Weil --- src/common/mutex_debug.cc | 18 +++++++++--------- src/common/mutex_debug.h | 23 +++++++++++++++++------ src/test/common/test_mutex_debug.cc | 6 +++--- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/common/mutex_debug.cc b/src/common/mutex_debug.cc index c9c8a7e6b0c7..febc21fa16b8 100644 --- a/src/common/mutex_debug.cc +++ b/src/common/mutex_debug.cc @@ -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(); } diff --git a/src/common/mutex_debug.h b/src/common/mutex_debug.h index 4c8d6ff74d55..af32357fd226 100644 --- a/src/common/mutex_debug.h +++ b/src/common/mutex_debug.h @@ -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); diff --git a/src/test/common/test_mutex_debug.cc b/src/test/common/test_mutex_debug.cc index 49cd499557f0..977dfe738a92 100644 --- a/src/test/common/test_mutex_debug.cc +++ b/src/test/common/test_mutex_debug.cc @@ -31,7 +31,7 @@ static bool test_try_lock(Mutex* m) { template static void test_lock() { - Mutex m; + Mutex m("mutex"); auto ttl = &test_try_lock; 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; 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; ASSERT_NO_THROW(m.lock()); -- 2.47.3