Skip to content

Commit 69baa6d

Browse files
committed
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 <[email protected]>
1 parent bb2a220 commit 69baa6d

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/common/mutex_debug.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,16 @@ class mutex_debug_impl : public mutex_debugging_base
169169
}
170170

171171
bool try_lock(bool no_lockdep = false) {
172-
bool locked = try_lock_impl();
173-
if (locked) {
174-
if (enable_lockdep(no_lockdep))
175-
_locked();
176-
_post_lock();
177-
}
178-
return locked;
172+
ceph_assert(recursive || !is_locked_by_me());
173+
return _try_lock(no_lockdep);
179174
}
180175

181176
void lock(bool no_lockdep = false) {
177+
ceph_assert(recursive || !is_locked_by_me());
182178
if (enable_lockdep(no_lockdep))
183179
_will_lock(recursive);
184180

185-
if (try_lock(no_lockdep))
181+
if (_try_lock(no_lockdep))
186182
return;
187183

188184
lock_impl();
@@ -198,6 +194,16 @@ class mutex_debug_impl : public mutex_debugging_base
198194
unlock_impl();
199195
}
200196

197+
private:
198+
bool _try_lock(bool no_lockdep) {
199+
bool locked = try_lock_impl();
200+
if (locked) {
201+
if (enable_lockdep(no_lockdep))
202+
_locked();
203+
_post_lock();
204+
}
205+
return locked;
206+
}
201207
};
202208

203209

0 commit comments

Comments
 (0)