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

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

>From 7a06676904b5636582561dbf26266b6ec7372d91 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <[email protected]>
Date: Sat, 18 Oct 2025 11:07:46 -0700
Subject: [PATCH] [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..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));
 

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to