From: Patrick Donnelly Date: Sat, 28 Sep 2024 01:21:34 +0000 (-0400) Subject: common: assert debug mutex lock is not held if !recursive X-Git-Tag: v20.0.0~885^2~4 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=69baa6de2579f0e3ce8298e14b970b8c68deae9c;p=ceph.git common: assert debug mutex lock is not held if !recursive There's appropriate checks for unlock and post-lock but nothing to stop the undefined behavior of a double-lock on a non-recursive mutex. Signed-off-by: Patrick Donnelly --- diff --git a/src/common/mutex_debug.h b/src/common/mutex_debug.h index c1a4ff2a43501..d56d0ebee9987 100644 --- a/src/common/mutex_debug.h +++ b/src/common/mutex_debug.h @@ -169,20 +169,16 @@ public: } bool try_lock(bool no_lockdep = false) { - bool locked = try_lock_impl(); - if (locked) { - if (enable_lockdep(no_lockdep)) - _locked(); - _post_lock(); - } - return locked; + ceph_assert(recursive || !is_locked_by_me()); + return _try_lock(no_lockdep); } void lock(bool no_lockdep = false) { + ceph_assert(recursive || !is_locked_by_me()); if (enable_lockdep(no_lockdep)) _will_lock(recursive); - if (try_lock(no_lockdep)) + if (_try_lock(no_lockdep)) return; lock_impl(); @@ -198,6 +194,16 @@ public: unlock_impl(); } +private: + bool _try_lock(bool no_lockdep) { + bool locked = try_lock_impl(); + if (locked) { + if (enable_lockdep(no_lockdep)) + _locked(); + _post_lock(); + } + return locked; + } };