Skip to content
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

Add critical section API wrappers #4587

Merged
merged 8 commits into from
Oct 5, 2024

Conversation

ngoldbaum
Copy link
Contributor

@ngoldbaum ngoldbaum commented Sep 30, 2024

ping @bschoenmaeckers for your other PR.

Adds Default impls for ffi::PyCriticalSection and ffi::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.

@bschoenmaeckers
Copy link
Contributor

Test currently fails on emscripten.

src/sync.rs Outdated
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)
};
Copy link
Member

Choose a reason for hiding this comment

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

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()) };

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add PhantomPinned to PyCriticalSection?

Copy link
Member

Choose a reason for hiding this comment

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

Should we add PhantomPinned to PyCriticalSection?

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.

@ngoldbaum
Copy link
Contributor Author

Added the ci-build-full label to see if there are any other corner cases in the full tests.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Overall looks great, thanks!

src/sync.rs Outdated Show resolved Hide resolved
pyo3-ffi/src/cpython/critical_section.rs Outdated Show resolved Hide resolved
@davidhewitt davidhewitt added this pull request to the merge queue Oct 4, 2024
Merged via the queue into PyO3:main with commit 822b523 Oct 5, 2024
79 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants