[Lldb-commits] [lldb] [lldb] Add try_lock to SBMutex (PR #164109)

via lldb-commits lldb-commits at lists.llvm.org
Sat Oct 18 11:11:09 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

<details>
<summary>Changes</summary>

Add try_lock to confirm to Lockable, which is necessary to use it with std::scoped_lock.

---
Full diff: https://github.com/llvm/llvm-project/pull/164109.diff


3 Files Affected:

- (modified) lldb/include/lldb/API/SBMutex.h (+4) 
- (modified) lldb/source/API/SBMutex.cpp (+9) 
- (modified) lldb/unittests/API/SBMutexTest.cpp (+4) 


``````````diff
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..ded8038b92760 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)
+    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));
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/164109


More information about the lldb-commits mailing list