Add critical section API wrappers#4587
Merged
davidhewitt merged 8 commits intoPyO3:mainfrom Oct 5, 2024
Merged
Conversation
Member
|
Test currently fails on emscripten. |
mejrs
reviewed
Oct 1, 2024
Comment on lines
+370
to
+374
| let _guard = unsafe { | ||
| let mut section = std::mem::zeroed(); | ||
| crate::ffi::PyCriticalSection_Begin(&mut section, object.as_ptr()); | ||
| Guard(section) | ||
| }; |
Member
There was a problem hiding this comment.
You should not be moving this
Suggested change
| let _guard = unsafe { | |
| let mut section = std::mem::zeroed(); | |
| crate::ffi::PyCriticalSection_Begin(&mut section, object.as_ptr()); | |
| Guard(section) | |
| }; | |
| let mut guard = Guard(unsafe {std::mem::zeroed()}); | |
| unsafe { crate::ffi::PyCriticalSection_Begin(&mut guard.section, object.as_ptr()) }; |
Member
There was a problem hiding this comment.
Should we add PhantomPinned to PyCriticalSection?
Member
There was a problem hiding this comment.
Should we add
PhantomPinnedtoPyCriticalSection?
It doesn't really matter in this case.
Implementing a type that "needs pinning" requires unsafe code, and writing code that needs pinned types also requires unsafe code. Pin is just a way to let safe code carry that promise between two pieces of unsafe code. The latter doesn't happen here.
Contributor
Author
|
Added the ci-build-full label to see if there are any other corner cases in the full tests. |
davidhewitt
approved these changes
Oct 1, 2024
Member
davidhewitt
left a comment
There was a problem hiding this comment.
Overall looks great, thanks!
This file contains hidden or 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
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.
ping @bschoenmaeckers for your other PR.
Adds
Defaultimpls forffi::PyCriticalSectionandffi::PyCriticalSection2. These are needed to actually use these structs, since the fields are private.I'm not sure how to write a test to check that critical sections are closed when a panic happens. I think I'd need to instrument the RAII guard for that to work, and that seems wrong to me.
I'd also appreciate suggestions for an example to go in the docstring.