62 lines · cpp
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-exception6//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 21using namespace lldb;22using namespace lldb_private;23 24class SBMutexTest : public testing::Test {25protected: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 33TEST_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 40 ASSERT_TRUE(lock.try_lock());41 lock.unlock();42 43 std::lock_guard<lldb::SBMutex> lock_guard(lock);44 ASSERT_FALSE(locked.exchange(true));45 46 f = std::async(std::launch::async, [&]() {47 ASSERT_TRUE(locked);48 EXPECT_FALSE(lock.try_lock());49 target.BreakpointCreateByName("foo", "bar");50 ASSERT_FALSE(locked);51 });52 ASSERT_TRUE(f.valid());53 54 // Wait 500ms to confirm the thread is blocked.55 auto status = f.wait_for(std::chrono::milliseconds(500));56 ASSERT_EQ(status, std::future_status::timeout);57 58 ASSERT_TRUE(locked.exchange(false));59 }60 f.wait();61}62