Skip to content

[ORC] Fix synchronization in CoreAPIsTest. #144556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1559,16 +1559,11 @@ TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {
#if LLVM_ENABLE_THREADS

std::mutex WorkThreadsMutex;
std::vector<std::thread> WorkThreads;
SmallVector<std::thread, 0> WorkThreads;
DispatchOverride = [&](std::unique_ptr<Task> T) {
std::promise<void> WaitP;
std::lock_guard<std::mutex> Lock(WorkThreadsMutex);
std::unique_lock Lock(WorkThreadsMutex);
Comment on lines -1565 to +1564
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could remain a lock_guard, since manual locking/unlocking isn't needed here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true, but it just seemed more simple to use the same type everywhere: I don't actually know why std::lock_guard exists when std::unique_lock appears to be simply better than it in every way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, it looks like LLVM now may consider lock_guard deprecated in favor of drive-by replacements of it with unique_lock everywhere? #126434 #146240

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the movement is to scoped_lock over lock_guard, rather than to unique_lock.

I have a slight preference for making these kinds of cleanup (std::vector -> SmallVector, lock_guard -> scoped_lock) in a separate patch, so that the fix for the actual bug is as small as possible. This is a pretty small fix though, so as long as it's noted in the commit message doing the cleanup inline is probably ok.

WorkThreads.push_back(
std::thread([T = std::move(T), WaitF = WaitP.get_future()]() mutable {
WaitF.get();
T->run();
}));
WaitP.set_value();
std::thread([T = std::move(T)]() mutable { T->run(); }));
};

cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
Expand All @@ -1580,8 +1575,15 @@ TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {
EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
<< "lookup returned incorrect flags";

for (auto &WT : WorkThreads)
std::unique_lock Lock(WorkThreadsMutex);
// This works because every child thread that is allowed to use WorkThreads
// must either be in WorkThreads or its parent must be in WorkThreads.
while (!WorkThreads.empty()) {
auto WT = WorkThreads.pop_back_val();
Lock.unlock();
WT.join();
Lock.lock();
}
#endif
}

Expand Down
Loading