Skip to content

Commit

Permalink
add bindings for critical section API (#4477)
Browse files Browse the repository at this point in the history
* add bindings for critical section API

* fix build on python < 3.13

* add release note

* fix clippy on gil-enabled 3.13 build
  • Loading branch information
ngoldbaum committed Aug 24, 2024
1 parent 18bca6e commit 03e85c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions newsfragments/4477.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Added bindings for the Python critical section API available on Python 3.13 and newer.
30 changes: 30 additions & 0 deletions pyo3-ffi/src/cpython/critical_section.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[cfg(Py_GIL_DISABLED)]
use crate::PyMutex;
use crate::PyObject;

#[repr(C)]
#[cfg(Py_GIL_DISABLED)]
pub struct PyCriticalSection {
_cs_prev: usize,
_cs_mutex: *mut PyMutex,
}

#[repr(C)]
#[cfg(Py_GIL_DISABLED)]
pub struct PyCriticalSection2 {
_cs_base: PyCriticalSection,
_cs_mutex2: *mut PyMutex,
}

#[cfg(not(Py_GIL_DISABLED))]
opaque_struct!(PyCriticalSection);

#[cfg(not(Py_GIL_DISABLED))]
opaque_struct!(PyCriticalSection2);

extern "C" {
pub fn PyCriticalSection_Begin(c: *mut PyCriticalSection, op: *mut PyObject);
pub fn PyCriticalSection_End(c: *mut PyCriticalSection);
pub fn PyCriticalSection2_Begin(c: *mut PyCriticalSection2, a: *mut PyObject, b: *mut PyObject);
pub fn PyCriticalSection2_End(c: *mut PyCriticalSection2);
}
4 changes: 4 additions & 0 deletions pyo3-ffi/src/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub(crate) mod bytesobject;
pub(crate) mod ceval;
pub(crate) mod code;
pub(crate) mod compile;
#[cfg(Py_3_13)]
pub(crate) mod critical_section;
pub(crate) mod descrobject;
#[cfg(not(PyPy))]
pub(crate) mod dictobject;
Expand Down Expand Up @@ -45,6 +47,8 @@ pub use self::bytesobject::*;
pub use self::ceval::*;
pub use self::code::*;
pub use self::compile::*;
#[cfg(Py_3_13)]
pub use self::critical_section::*;
pub use self::descrobject::*;
#[cfg(not(PyPy))]
pub use self::dictobject::*;
Expand Down

0 comments on commit 03e85c2

Please sign in to comment.