Fix race condition caused by init_table failure #11
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.
I agree with your previous conclusion that there are no race conditions during probing. However this patch is regarding the function probe_table and a possible init failure. Note that this race condition will never occur if all init's are successful (as was probably the case in your previous testing).
Consider the scenario where the same position is probed from two separate threads and init has not yet been done for the corresponding tb. If init_table fails it sets tbHash[hashIdx].ptr to NULL but the second thread checks the variable tbHash[hashIdx].ptr and then uses it if it is not NULL. This is a race condition.
We could either lock each time when checking the tbHash[hashIdx].ptr variable, but this is unnecessarily slow. My patch introduces a variable called error which handles this scenario.