| 1 | //===-- SBMutexTest.cpp ---------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // Use the umbrella header for -Wdocumentation. |
| 10 | #include "lldb/API/LLDB.h" |
| 11 | |
| 12 | #include "TestingSupport/SubsystemRAII.h" |
| 13 | #include "lldb/API/SBDebugger.h" |
| 14 | #include "lldb/API/SBTarget.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | #include <atomic> |
| 17 | #include <chrono> |
| 18 | #include <future> |
| 19 | #include <mutex> |
| 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | class SBMutexTest : public testing::Test { |
| 25 | protected: |
| 26 | void SetUp() override { debugger = SBDebugger::Create(); } |
| 27 | void TearDown() override { SBDebugger::Destroy(debugger); } |
| 28 | |
| 29 | SubsystemRAII<lldb::SBDebugger> subsystems; |
| 30 | SBDebugger debugger; |
| 31 | }; |
| 32 | |
| 33 | TEST_F(SBMutexTest, LockTest) { |
| 34 | lldb::SBTarget target = debugger.GetDummyTarget(); |
| 35 | std::atomic<bool> locked = false; |
| 36 | std::future<void> f; |
| 37 | { |
| 38 | lldb::SBMutex lock = target.GetAPIMutex(); |
| 39 | std::lock_guard<lldb::SBMutex> lock_guard(lock); |
| 40 | ASSERT_FALSE(locked.exchange(true)); |
| 41 | |
| 42 | f = std::async(policy: std::launch::async, fn: [&]() { |
| 43 | ASSERT_TRUE(locked); |
| 44 | target.BreakpointCreateByName(symbol_name: "foo", module_name: "bar"); |
| 45 | ASSERT_FALSE(locked); |
| 46 | }); |
| 47 | ASSERT_TRUE(f.valid()); |
| 48 | |
| 49 | // Wait 500ms to confirm the thread is blocked. |
| 50 | auto status = f.wait_for(rel: std::chrono::milliseconds(500)); |
| 51 | ASSERT_EQ(status, std::future_status::timeout); |
| 52 | |
| 53 | ASSERT_TRUE(locked.exchange(false)); |
| 54 | } |
| 55 | f.wait(); |
| 56 | } |
| 57 |
