Skip to content

Add critical section API wrappers#4587

Merged
davidhewitt merged 8 commits intoPyO3:mainfrom
ngoldbaum:add-critical-section
Oct 5, 2024
Merged

Add critical section API wrappers#4587
davidhewitt merged 8 commits intoPyO3:mainfrom
ngoldbaum:add-critical-section

Conversation

@ngoldbaum
Copy link
Copy Markdown
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
Copy Markdown
Member

Test currently fails on emscripten.

Comment thread 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
Copy Markdown
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
Copy Markdown
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?

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
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!

Comment thread src/sync.rs Outdated
Comment thread pyo3-ffi/src/cpython/critical_section.rs Outdated
@davidhewitt davidhewitt enabled auto-merge October 4, 2024 15:12
@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
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