-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Make System::Layer::mHandleSelectThread atomic. #7828
Merged
woody-apple
merged 1 commit into
project-chip:master
from
bzbarsky-apple:fix-race-on-thread-id
Jun 23, 2021
Merged
Make System::Layer::mHandleSelectThread atomic. #7828
woody-apple
merged 1 commit into
project-chip:master
from
bzbarsky-apple:fix-race-on-thread-id
Jun 23, 2021
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mHandleSelectThread is used for an optimization: when it's set to the current thread id, WakeIOThread knows that: 1) We're in the middle of running Layer::HandleTimeout 2) We're doing that on the same thread where WakeIOThread was called and hence WakeIOThread doesn't need to do anything, because we're already on the IO thread and it's already awake. The read of this member in WakeIOThread is not synchronized in any way, but as long as we guarantee that it correctly reads a value that was actually assigned to the member (which std::atomic does), things will work correctly. Either the value we read will be equal to the current thread id, in which case we know we're on the one thread that called Layer::HandleTimeout and are inside that function, or it will not be equal to our thread id and then we need to do the actual wakeup work, whatever that value is (including if it's null). Fixes project-chip#7818
pullapprove
bot
requested review from
andy31415,
chrisdecenzo,
Damian-Nordic,
hawk248,
jepenven-silabs,
msandstedt and
woody-apple
June 22, 2021 23:38
msandstedt
approved these changes
Jun 23, 2021
saurabhst
approved these changes
Jun 23, 2021
Size increase report for "nrfconnect-example-build" from 90592a7
Full report output
|
andy31415
approved these changes
Jun 23, 2021
woody-apple
approved these changes
Jun 23, 2021
nikita-s-wrk
pushed a commit
to nikita-s-wrk/connectedhomeip
that referenced
this pull request
Sep 23, 2021
mHandleSelectThread is used for an optimization: when it's set to the current thread id, WakeIOThread knows that: 1) We're in the middle of running Layer::HandleTimeout 2) We're doing that on the same thread where WakeIOThread was called and hence WakeIOThread doesn't need to do anything, because we're already on the IO thread and it's already awake. The read of this member in WakeIOThread is not synchronized in any way, but as long as we guarantee that it correctly reads a value that was actually assigned to the member (which std::atomic does), things will work correctly. Either the value we read will be equal to the current thread id, in which case we know we're on the one thread that called Layer::HandleTimeout and are inside that function, or it will not be equal to our thread id and then we need to do the actual wakeup work, whatever that value is (including if it's null). Fixes project-chip#7818
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
mHandleSelectThread is used for an optimization: when it's set to the
current thread id, WakeIOThread knows that:
and hence WakeIOThread doesn't need to do anything, because we're
already on the IO thread and it's already awake.
The read of this member in WakeIOThread is not synchronized in any
way, but as long as we guarantee that it correctly reads a value that
was actually assigned to the member (which std::atomic does), things
will work correctly. Either the value we read will be equal to the
current thread id, in which case we know we're on the one thread that
called Layer::HandleTimeout and are inside that function, or it will
not be equal to our thread id and then we need to do the actual wakeup
work, whatever that value is (including if it's null).
Fixes #7818
Problem
TSan shows that we have a data race on Linux if
PlatformManager::PostEvent
is called whileLayer::HandleTimeout
is running on another thread.Change overview
Use
std::atomic
to guarantee that we don't have a data race. With that fixed, the overall logic does not seem to have races that lead to incorrect behavior.Testing
Manually tested this with #7797 and changes to make CHIP-tool use
PostEvent
which failed CI without this change and pass with it.