Skip to content

Conversation

@JDevlieghere
Copy link
Member

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

@JDevlieghere JDevlieghere requested a review from da-viper October 18, 2025 18:10
@llvmbot llvmbot added the lldb label Oct 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

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 --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));
 

Add try_lock to confirm to Lockable, which is necessary to use it with
std::scoped_lock.
{
lldb::SBMutex lock = target.GetAPIMutex();

ASSERT_TRUE(lock.try_lock());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try_lock another time to confirm that it fails?

bool SBMutex::try_lock() const {
LLDB_INSTRUMENT_VA(this);

if (m_opaque_sp)
Copy link
Member

@Michael137 Michael137 Oct 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is what we do in all the other methods too, but under what circumstances can this be nullptr? Isn't its lifetime at least that of the owning SBMutex?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a wrapper around a std::shared_ptr<std::recursive_mutex>. It could be null when you use the constructor that takes an SBTarget that is null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but I don't think the aliasing constructor allows SBTarget to ever be null. After all, we're aliasing one of its members, so we always dereference it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. If that's the case then I don't think the pointer can be null. Doesn't look like we do this in SBTarget either, but we could update the classes where that's the case to have an assert, but that would be a bigger change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea definitely out-of-scope of this PR. Adding an assert and removing the null-checks would be a nice-to-have

@da-viper
Copy link
Contributor

Add to Release note new SBAPI Function

@JDevlieghere
Copy link
Member Author

Add to Release note new SBAPI Function

I don't think individual SB APIs are wort release noting, but @DavidSpickett has been championing the release notes so let's see what he thinks.

The other alternative is to write something like "SBMutex can now be used with a scoped lock", but this class was added primarily to support locking in lldb-dap, so I don't expect this to be widely adopted.

@DavidSpickett
Copy link
Collaborator

I don't think individual SB APIs are wort release noting, but @DavidSpickett has been championing the release notes so let's see what he thinks.

If we were already doing a better job about tracking SBAPI changes then yes they should be release noted but we aren't. A list of SBIAPI changes per release would be cool but is definitely in the "nice to have" bucket right now.

I would add a note if it was:

  • A removal ("your build will break")
  • A deprecation ("use this other thing")
  • A high profile request or had some other reason to make publicity out of it (support for multiple address spaces for example, if that were to happen)

@da-viper
Copy link
Contributor

The last force push undid the EXPECT_FALSE(lock.try_lock()); commit

@JDevlieghere
Copy link
Member Author

The last force push undid the EXPECT_FALSE(lock.try_lock()); commit

I moved it because it's a recursive mutex, so try_lock will continue succeeding if you do it on the same thread. We now do it from the async call.

@JDevlieghere JDevlieghere merged commit c01a223 into llvm:main Oct 20, 2025
9 of 10 checks passed
@JDevlieghere JDevlieghere deleted the try_lock branch October 20, 2025 18:12
@llvm-ci
Copy link
Collaborator

llvm-ci commented Oct 20, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building lldb at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/33585

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: functionalities/tail_call_frames/disambiguate_paths_to_common_sink/TestDisambiguatePathsToCommonSink.py (279 of 3252)
PASS: lldb-api :: functionalities/thread/jump/TestThreadJump.py (280 of 3252)
PASS: lldb-api :: functionalities/tail_call_frames/disambiguate_tail_call_seq/TestDisambiguateTailCallSeq.py (281 of 3252)
PASS: lldb-api :: tools/lldb-server/signal-filtering/TestGdbRemote_QPassSignals.py (282 of 3252)
PASS: lldb-api :: lang/c/bitfields/TestBitfields.py (283 of 3252)
PASS: lldb-api :: functionalities/inferior-changed/TestInferiorChanged.py (284 of 3252)
UNSUPPORTED: lldb-api :: functionalities/data-formatter/data-formatter-stl/generic/u8string/TestDataFormatterStdU8String.py (285 of 3252)
PASS: lldb-api :: tools/lldb-dap/stop-hooks/TestDAP_stop_hooks.py (286 of 3252)
XFAIL: lldb-api :: functionalities/longjmp/TestLongjmp.py (287 of 3252)
PASS: lldb-api :: tools/lldb-dap/exception/TestDAP_exception.py (288 of 3252)
FAIL: lldb-api :: tools/lldb-dap/output/TestDAP_output.py (289 of 3252)
******************** TEST 'lldb-api :: tools/lldb-dap/output/TestDAP_output.py' FAILED ********************
Script:
--
/usr/bin/python3 /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./lib --env LLVM_INCLUDE_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/include --env LLVM_TOOLS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./bin --arch x86_64 --build-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex --lldb-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/lldb --compiler /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/clang --dsymutil /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./bin --lldb-obj-root /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb --lldb-libs-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./lib --cmake-build-type Release -t /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/output -p TestDAP_output.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 22.0.0git (https://github.com/llvm/llvm-project.git revision c01a22363034b98a21affde44b1b45ba5d022c71)
  clang revision c01a22363034b98a21affde44b1b45ba5d022c71
  llvm revision c01a22363034b98a21affde44b1b45ba5d022c71
Skipping the following test categories: ['libc++', 'msvcstl', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
Change dir to: /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/output
runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 


Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
Add `try_lock` to confirm to Lockable, which is necessary to use it with
`std::scoped_lock`.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
Add `try_lock` to confirm to Lockable, which is necessary to use it with
`std::scoped_lock`.
da-viper pushed a commit to da-viper/llvm-project that referenced this pull request Nov 20, 2025
Add `try_lock` to confirm to Lockable, which is necessary to use it with
`std::scoped_lock`.

(cherry picked from commit c01a223)
adrian-prantl added a commit to swiftlang/llvm-project that referenced this pull request Dec 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants