Skip to content

Commit 3b74e5d

Browse files
JDevlieghereaokblast
authored andcommitted
[lldb] Add try_lock to SBMutex (llvm#164109)
Add `try_lock` to confirm to Lockable, which is necessary to use it with `std::scoped_lock`.
1 parent e12e25b commit 3b74e5d

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

lldb/include/lldb/API/SBMutex.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class LLDB_API SBMutex {
3131
/// Releases ownership of this lock.
3232
void unlock() const;
3333

34+
/// Tries to lock the mutex. Returns immediately. On successful lock
35+
/// acquisition returns true, otherwise returns false.
36+
bool try_lock() const;
37+
3438
private:
3539
// Private constructor used by SBTarget to create the Target API mutex.
3640
// Requires a friend declaration.

lldb/source/API/SBMutex.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ void SBMutex::unlock() const {
5858
if (m_opaque_sp)
5959
m_opaque_sp->unlock();
6060
}
61+
62+
bool SBMutex::try_lock() const {
63+
LLDB_INSTRUMENT_VA(this);
64+
65+
if (m_opaque_sp)
66+
return m_opaque_sp->try_lock();
67+
68+
return false;
69+
}

lldb/unittests/API/SBMutexTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ TEST_F(SBMutexTest, LockTest) {
3636
std::future<void> f;
3737
{
3838
lldb::SBMutex lock = target.GetAPIMutex();
39+
40+
ASSERT_TRUE(lock.try_lock());
41+
lock.unlock();
42+
3943
std::lock_guard<lldb::SBMutex> lock_guard(lock);
4044
ASSERT_FALSE(locked.exchange(true));
4145

4246
f = std::async(std::launch::async, [&]() {
4347
ASSERT_TRUE(locked);
48+
EXPECT_FALSE(lock.try_lock());
4449
target.BreakpointCreateByName("foo", "bar");
4550
ASSERT_FALSE(locked);
4651
});

0 commit comments

Comments
 (0)