Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lldb/include/lldb/API/SBMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class LLDB_API SBMutex {
/// Releases ownership of this lock.
void unlock() const;

/// Tries to lock the mutex. Returns immediately. On successful lock
/// acquisition returns true, otherwise returns false.
bool try_lock() const;

private:
// Private constructor used by SBTarget to create the Target API mutex.
// Requires a friend declaration.
Expand Down
9 changes: 9 additions & 0 deletions lldb/source/API/SBMutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ void SBMutex::unlock() const {
if (m_opaque_sp)
m_opaque_sp->unlock();
}

bool SBMutex::try_lock() const {
LLDB_INSTRUMENT_VA(this);

if (m_opaque_sp)
Copy link
Member

@Michael137 Michael137 Oct 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is what we do in all the other methods too, but under what circumstances can this be nullptr? Isn't its lifetime at least that of the owning SBMutex?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a wrapper around a std::shared_ptr<std::recursive_mutex>. It could be null when you use the constructor that takes an SBTarget that is null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but I don't think the aliasing constructor allows SBTarget to ever be null. After all, we're aliasing one of its members, so we always dereference it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. If that's the case then I don't think the pointer can be null. Doesn't look like we do this in SBTarget either, but we could update the classes where that's the case to have an assert, but that would be a bigger change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea definitely out-of-scope of this PR. Adding an assert and removing the null-checks would be a nice-to-have

return m_opaque_sp->try_lock();

return false;
}
5 changes: 5 additions & 0 deletions lldb/unittests/API/SBMutexTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ TEST_F(SBMutexTest, LockTest) {
std::future<void> f;
{
lldb::SBMutex lock = target.GetAPIMutex();

ASSERT_TRUE(lock.try_lock());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try_lock another time to confirm that it fails?

lock.unlock();

std::lock_guard<lldb::SBMutex> lock_guard(lock);
ASSERT_FALSE(locked.exchange(true));

f = std::async(std::launch::async, [&]() {
ASSERT_TRUE(locked);
EXPECT_FALSE(lock.try_lock());
target.BreakpointCreateByName("foo", "bar");
ASSERT_FALSE(locked);
});
Expand Down
Loading