[Lldb-commits] [lldb] [lldb] Add try_lock to SBMutex (PR #164109)
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Mon Oct 20 07:45:31 PDT 2025
https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/164109
>From 32cbb920cf8ec42f25f05f40951ebfdd92a956b8 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Sat, 18 Oct 2025 11:07:46 -0700
Subject: [PATCH 1/2] [lldb] Add try_lock to SBMutex
Add try_lock to confirm to Lockable, which is necessary to use it with
std::scoped_lock.
---
lldb/include/lldb/API/SBMutex.h | 4 ++++
lldb/source/API/SBMutex.cpp | 9 +++++++++
lldb/unittests/API/SBMutexTest.cpp | 4 ++++
3 files changed, 17 insertions(+)
diff --git a/lldb/include/lldb/API/SBMutex.h b/lldb/include/lldb/API/SBMutex.h
index 717d5f86cbc1c..826ad077f159f 100644
--- a/lldb/include/lldb/API/SBMutex.h
+++ b/lldb/include/lldb/API/SBMutex.h
@@ -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.
diff --git a/lldb/source/API/SBMutex.cpp b/lldb/source/API/SBMutex.cpp
index 445076b5a9174..c7844dec658cc 100644
--- a/lldb/source/API/SBMutex.cpp
+++ b/lldb/source/API/SBMutex.cpp
@@ -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;
+}
diff --git a/lldb/unittests/API/SBMutexTest.cpp b/lldb/unittests/API/SBMutexTest.cpp
index aafad59d58c17..dd3258405544a 100644
--- a/lldb/unittests/API/SBMutexTest.cpp
+++ b/lldb/unittests/API/SBMutexTest.cpp
@@ -36,6 +36,10 @@ 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));
>From a48b0e9715943c6f5211cc79893f2afb251b6a8b Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Mon, 20 Oct 2025 07:30:25 -0700
Subject: [PATCH 2/2] Try calling try_lock when the mutex is locked
---
lldb/unittests/API/SBMutexTest.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/lldb/unittests/API/SBMutexTest.cpp b/lldb/unittests/API/SBMutexTest.cpp
index dd3258405544a..18dc420086d0a 100644
--- a/lldb/unittests/API/SBMutexTest.cpp
+++ b/lldb/unittests/API/SBMutexTest.cpp
@@ -45,6 +45,7 @@ TEST_F(SBMutexTest, LockTest) {
f = std::async(std::launch::async, [&]() {
ASSERT_TRUE(locked);
+ EXPECT_FALSE(lock.try_lock());
target.BreakpointCreateByName("foo", "bar");
ASSERT_FALSE(locked);
});
More information about the lldb-commits
mailing list