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 pyo3::sync::with_critical_section_ptr #4922

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4922.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Added an unsafe wrapper for the critical section API that accepts an FFI PyObject pointer.
16 changes: 15 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,20 @@ impl Interned {
/// type is managed.
#[cfg_attr(not(Py_GIL_DISABLED), allow(unused_variables))]
pub fn with_critical_section<F, R>(object: &Bound<'_, PyAny>, f: F) -> R
where
F: FnOnce() -> R,
{
unsafe { with_critical_section_ptr(object.as_ptr(), f) }
}

/// like with_critical_section, but accepts a raw FFI pointer.
///
/// See the docs for with_critical_section for more details
///
/// # Safety
/// object must be a valid non-null pointer to an object
#[cfg_attr(not(Py_GIL_DISABLED), allow(unused_variables))]
pub unsafe fn with_critical_section_ptr<F, R>(object: *mut crate::ffi::PyObject, f: F) -> R
where
F: FnOnce() -> R,
{
Expand All @@ -471,7 +485,7 @@ where
}

let mut guard = Guard(unsafe { std::mem::zeroed() });
unsafe { crate::ffi::PyCriticalSection_Begin(&mut guard.0, object.as_ptr()) };
unsafe { crate::ffi::PyCriticalSection_Begin(&mut guard.0, object) };
f()
}
#[cfg(not(Py_GIL_DISABLED))]
Expand Down
Loading