aboutsummaryrefslogtreecommitdiff
path: root/lldb/unittests/API/SBMutexTest.cpp
blob: aafad59d58c1768cf0914bbfcf69d5999c2ebe4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//===-- SBMutexTest.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// Use the umbrella header for -Wdocumentation.
#include "lldb/API/LLDB.h"

#include "TestingSupport/SubsystemRAII.h"
#include "lldb/API/SBDebugger.h"
#include "lldb/API/SBTarget.h"
#include "gtest/gtest.h"
#include <atomic>
#include <chrono>
#include <future>
#include <mutex>

using namespace lldb;
using namespace lldb_private;

class SBMutexTest : public testing::Test {
protected:
  void SetUp() override { debugger = SBDebugger::Create(); }
  void TearDown() override { SBDebugger::Destroy(debugger); }

  SubsystemRAII<lldb::SBDebugger> subsystems;
  SBDebugger debugger;
};

TEST_F(SBMutexTest, LockTest) {
  lldb::SBTarget target = debugger.GetDummyTarget();
  std::atomic<bool> locked = false;
  std::future<void> f;
  {
    lldb::SBMutex lock = target.GetAPIMutex();
    std::lock_guard<lldb::SBMutex> lock_guard(lock);
    ASSERT_FALSE(locked.exchange(true));

    f = std::async(std::launch::async, [&]() {
      ASSERT_TRUE(locked);
      target.BreakpointCreateByName("foo", "bar");
      ASSERT_FALSE(locked);
    });
    ASSERT_TRUE(f.valid());

    // Wait 500ms to confirm the thread is blocked.
    auto status = f.wait_for(std::chrono::milliseconds(500));
    ASSERT_EQ(status, std::future_status::timeout);

    ASSERT_TRUE(locked.exchange(false));
  }
  f.wait();
}