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..18dc420086d0a 100644 --- a/lldb/unittests/API/SBMutexTest.cpp +++ b/lldb/unittests/API/SBMutexTest.cpp @@ -36,11 +36,16 @@ TEST_F(SBMutexTest, LockTest) { std::future f; { lldb::SBMutex lock = target.GetAPIMutex(); + + ASSERT_TRUE(lock.try_lock()); + lock.unlock(); + std::lock_guard lock_guard(lock); ASSERT_FALSE(locked.exchange(true)); f = std::async(std::launch::async, [&]() { ASSERT_TRUE(locked); + EXPECT_FALSE(lock.try_lock()); target.BreakpointCreateByName("foo", "bar"); ASSERT_FALSE(locked); });