Skip to content

Commit f46c9c4

Browse files
authored
Expose unsafe wrappers for Py_BEGIN_CRITICAL_SECTION_MUTEX API (#5642)
* WIP: attempt to add with_critical_section_mutex * WIP: use an UnsafeCell * finish UnsafeCell rewrite * finish and tweak docs * fix limited API builds * fix deadlocks and abi3 compilation errors * remove unnecessary comparisons with true * add two more tests * add changelog entry * add missing conditional compilation for new tests * add missing conditional compilation for new tests * fix conditional compilation in pyo3::sync * fix Python 3.13 clippy * Update newsfragments/5642.added.md * refactor so to use EnteredCriticalSection struct * tweak docstrings * Add SAFETY comments in tests * fix include guards * remove unnecessary returns * fix whitespace formatting * Move note about minimum Python version * Fix reference leak by adjusting lifetimes * Apply suggestions for test tweaks from code review * refactor into a new module and rewrite docs * adjust imports to satisfy clippy * Fix EnteredCriticalSection conditinal compilation * fix wasm clippy * add changelog for the moved functions * actually deprecate the old export * refer to locking mutexes and acquiring critical sections consistently * remove unnecessary availability note * simplify wasm conditional compilation
1 parent 6c5d391 commit f46c9c4

File tree

11 files changed

+695
-305
lines changed

11 files changed

+695
-305
lines changed

newsfragments/5642.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
added FFI definitions and an unsafe Rust API wrapping `Py_BEGIN_CRITICAL_SECTION_MUTEX` and `Py_BEGIN_CRITICAL_SECTION_MUTEX2`.

newsfragments/5642.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The `with_critical_section` and `with_critical_section2` functions are now available in `pyo3::sync::critical_section`. Aliases that generate deprecation warnings are available in `pyo3::sync`.

pyo3-ffi/src/cpython/critical_section.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(Py_GIL_DISABLED)]
1+
#[cfg(any(Py_3_14, Py_GIL_DISABLED))]
22
use crate::PyMutex;
33
use crate::PyObject;
44

@@ -24,7 +24,15 @@ opaque_struct!(pub PyCriticalSection2);
2424

2525
extern "C" {
2626
pub fn PyCriticalSection_Begin(c: *mut PyCriticalSection, op: *mut PyObject);
27+
#[cfg(Py_3_14)]
28+
pub fn PyCriticalSection_BeginMutex(c: *mut PyCriticalSection, m: *mut PyMutex);
2729
pub fn PyCriticalSection_End(c: *mut PyCriticalSection);
2830
pub fn PyCriticalSection2_Begin(c: *mut PyCriticalSection2, a: *mut PyObject, b: *mut PyObject);
31+
#[cfg(Py_3_14)]
32+
pub fn PyCriticalSection2_BeginMutex(
33+
c: *mut PyCriticalSection2,
34+
m1: *mut PyMutex,
35+
m2: *mut PyMutex,
36+
);
2937
pub fn PyCriticalSection2_End(c: *mut PyCriticalSection2);
3038
}

src/conversions/std/num.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ impl BytesSequenceExtractor<'_, '_> {
342342

343343
match self {
344344
BytesSequenceExtractor::Bytes(b) => copy_slice(b.as_bytes()),
345-
BytesSequenceExtractor::ByteArray(b) => crate::sync::with_critical_section(b, || {
346-
// Safety: b is protected by a critical section
347-
copy_slice(unsafe { b.as_bytes() })
348-
}),
345+
BytesSequenceExtractor::ByteArray(b) => {
346+
crate::sync::critical_section::with_critical_section(b, || {
347+
// Safety: b is protected by a critical section
348+
copy_slice(unsafe { b.as_bytes() })
349+
})
350+
}
349351
}
350352
}
351353
}

0 commit comments

Comments
 (0)