Skip to content

Commit f7124b8

Browse files
committed
Address Pavel's feedback
1 parent acb6a6e commit f7124b8

File tree

6 files changed

+34
-48
lines changed

6 files changed

+34
-48
lines changed

lldb/include/lldb/API/LLDB.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "lldb/API/SBLaunchInfo.h"
4747
#include "lldb/API/SBLineEntry.h"
4848
#include "lldb/API/SBListener.h"
49+
#include "lldb/API/SBLock.h"
4950
#include "lldb/API/SBMemoryRegionInfo.h"
5051
#include "lldb/API/SBMemoryRegionInfoList.h"
5152
#include "lldb/API/SBModule.h"

lldb/include/lldb/API/SBLock.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "lldb/API/SBDefines.h"
1313
#include "lldb/lldb-forward.h"
14-
1514
#include <mutex>
1615

1716
namespace lldb_private {
@@ -32,7 +31,7 @@ class LLDB_API SBLock {
3231

3332
// Private constructor used by SBTarget to create the Target API lock.
3433
// Requires a friend declaration.
35-
SBLock(std::recursive_mutex &mutex, lldb::TargetSP target_sp);
34+
SBLock(lldb::TargetSP target_sp);
3635
friend class SBTarget;
3736

3837
SBLock(const SBLock &rhs) = delete;

lldb/include/lldb/Target/Target.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,8 +1702,8 @@ struct APILock {
17021702
/// The private implementation used by SBLock to hand out the target API mutex.
17031703
/// It has a TargetSP to ensure the lock cannot outlive the target.
17041704
struct TargetAPILock : public APILock {
1705-
TargetAPILock(std::recursive_mutex &mutex, lldb::TargetSP target_sp)
1706-
: APILock(mutex), target_sp(target_sp) {}
1705+
TargetAPILock(lldb::TargetSP target_sp)
1706+
: APILock(target_sp->GetAPIMutex()), target_sp(target_sp) {}
17071707
~TargetAPILock() override = default;
17081708
lldb::TargetSP target_sp;
17091709
};

lldb/source/API/SBLock.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@
1010
#include "lldb/Target/Target.h"
1111
#include "lldb/Utility/Instrumentation.h"
1212
#include "lldb/lldb-forward.h"
13-
1413
#include <memory>
1514
#include <mutex>
1615

1716
using namespace lldb;
1817
using namespace lldb_private;
1918

20-
#ifndef SWIG
21-
22-
SBLock::SBLock(std::recursive_mutex &mutex, lldb::TargetSP target_sp)
23-
: m_opaque_up(std::make_unique<TargetAPILock>(mutex, target_sp)) {
19+
SBLock::SBLock(lldb::TargetSP target_sp)
20+
: m_opaque_up(std::make_unique<TargetAPILock>(target_sp)) {
2421
LLDB_INSTRUMENT_VA(this);
2522
}
2623

@@ -31,5 +28,3 @@ bool SBLock::IsValid() const {
3128

3229
return static_cast<bool>(m_opaque_up);
3330
}
34-
35-
#endif

lldb/source/API/SBTarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ lldb::SBLock SBTarget::AcquireAPILock() const {
24392439
LLDB_INSTRUMENT_VA(this);
24402440

24412441
if (TargetSP target_sp = GetSP())
2442-
return lldb::SBLock(target_sp->GetAPIMutex(), target_sp);
2442+
return lldb::SBLock(target_sp);
24432443
return lldb::SBLock();
24442444
}
24452445
#endif

lldb/unittests/API/SBLockTest.cpp

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,50 @@
66
//
77
//===----------------------------------------------------------------------===/
88

9-
#include "lldb/API/SBLock.h"
9+
// Use the umbrella header for -Wdocumentation.
10+
#include "lldb/API/LLDB.h"
11+
12+
#include "TestingSupport/SubsystemRAII.h"
1013
#include "lldb/API/SBDebugger.h"
1114
#include "lldb/API/SBTarget.h"
1215
#include "gtest/gtest.h"
1316
#include <atomic>
1417
#include <chrono>
15-
#include <thread>
16-
17-
TEST(SBLockTest, LockTest) {
18-
lldb::SBDebugger debugger = lldb::SBDebugger::Create();
19-
lldb::SBTarget target = debugger.GetDummyTarget();
18+
#include <future>
2019

21-
std::mutex m;
22-
std::condition_variable cv;
23-
bool wakeup = false;
24-
std::atomic<bool> locked = false;
20+
using namespace lldb;
21+
using namespace lldb_private;
2522

26-
std::thread test_thread([&]() {
27-
{
28-
{
29-
std::unique_lock lk(m);
30-
cv.wait(lk, [&] { return wakeup; });
31-
}
23+
class SBLockTest : public testing::Test {
24+
SubsystemRAII<lldb::SBDebugger> subsystems;
25+
};
3226

33-
ASSERT_TRUE(locked);
34-
target.BreakpointCreateByName("foo", "bar");
35-
ASSERT_FALSE(locked);
36-
37-
cv.notify_one();
38-
}
39-
});
27+
TEST_F(SBLockTest, LockTest) {
28+
lldb::SBDebugger debugger = lldb::SBDebugger::Create();
29+
lldb::SBTarget target = debugger.GetDummyTarget();
4030

41-
// Take the API lock.
31+
std::future<void> f;
4232
{
33+
std::atomic<bool> locked = false;
4334
lldb::SBLock lock = target.AcquireAPILock();
4435
ASSERT_FALSE(locked.exchange(true));
4536

46-
// Wake up the test thread.
47-
{
48-
std::lock_guard lk(m);
49-
wakeup = true;
50-
}
51-
cv.notify_one();
37+
f = std::async(std::launch::async, [&]() {
38+
{
39+
ASSERT_TRUE(locked);
40+
target.BreakpointCreateByName("foo", "bar");
41+
ASSERT_FALSE(locked);
42+
}
43+
});
44+
ASSERT_TRUE(f.valid());
5245

5346
// Wait 500ms to confirm the thread is blocked.
54-
{
55-
std::unique_lock<std::mutex> lk(m);
56-
auto result = cv.wait_for(lk, std::chrono::milliseconds(500));
57-
ASSERT_EQ(result, std::cv_status::timeout);
58-
}
47+
auto status = f.wait_for(std::chrono::milliseconds(500));
48+
ASSERT_EQ(status, std::future_status::timeout);
5949

6050
ASSERT_TRUE(locked.exchange(false));
6151
}
52+
f.wait();
6253

63-
test_thread.join();
54+
lldb::SBDebugger::Destroy(debugger);
6455
}

0 commit comments

Comments
 (0)