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

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Jun 17, 2025

The code previously appeared to have a (benign?) race condition on WorkThreads.size, since it was being accessed outside of the mutex lock that protected it on the threads. This is usually okay since 1a1d6e6, but doesn't seem reliable in general, so fix this code to express the intent more accurately. This instead relies on the same general principles as ref-counting, where each existing reference (thread) can add new references (threads) because they already have a reference themselves (until joined).

The code previously appeared to have a (benign?) race condition on
`WorkThreads.size`, since it was being accessed outside of the mutex
lock that protected it on the threads. This is usually okay since
1a1d6e6, but doesn't seem reliable in
general, so fix this code to express the intent more accurately. This
relies on the same general principles as ref-counting, where each
existing reference (thread) can add new references (threads) because
they already have a reference themselves (until joined).
@vtjnash vtjnash requested a review from lhames June 17, 2025 16:08
Copy link

github-actions bot commented Jun 17, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@lhames lhames left a comment

Choose a reason for hiding this comment

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

Otherwise LGTM. Thanks very much for spotting this bug!

std::vector<std::thread> WorkThreads;
SmallVector<std::thread,0> WorkThreads;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change in vector type necessary? If not it'd be better to leave out.

Copy link
Member Author

Choose a reason for hiding this comment

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

pop_back_val is only in SmallVector, so it made the code simpler. As far as I know, they are essentially the same type, except the LLVM one has a better API on top, plus it has bounds checking on [] which the std::vector doesn't have for more safety.

Comment on lines -1565 to +1564
std::lock_guard<std::mutex> Lock(WorkThreadsMutex);
std::unique_lock Lock(WorkThreadsMutex);
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

@vtjnash
Copy link
Member Author

vtjnash commented Jul 15, 2025

bump?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants