Fix a race in thread wakeup #459
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When unparking a worker thread, we do the following steps:
The problem occurs if the thread transitions from SLEEP to IDLE between steps 1 and 2. In that case, step 3 will fail to transition and simply return from
unpark.This PR changes the step 3 so that we always transition to NOTIFY, no matter what the previous state was.
The park/unpark mechanism in
std::threadworks somewhat similarly. In step 3, it attempts to CAS from SLEEP to NOTIFY, but jumps back to step 1 if the previous state was IDLE. See here. I've decided to unconditionally move to state NOTIFY instead - the end result should be the same.In addition to that, I did a bunch of random improvements to the documentation.