Skip to content
Open
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)
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());
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