pub fn with_critical_section_mutex2<F, R, T1, T2>(
py: Python<'_>,
m1: &PyMutex<T1>,
m2: &PyMutex<T2>,
f: F,
) -> Rwhere
F: for<'s> FnOnce(EnteredCriticalSection<'s, T1>, Option<EnteredCriticalSection<'s, T2>>) -> R,Py_3_14 and non-Py_LIMITED_API only.Expand description
Executes a closure with a Python critical section held on two PyMutex instances.
Simultaneously locks the mutexes m1 and m2 and holds them until the closure f is
finished. The mutexes may be temporarily unlock and re-acquired if the closure calls back into
the interpreter. See the notes in the
pyo3::sync::critical_section module documentation for more
details.
Rather than receiving the wrapped data directly, access is gated via the
pyo3::sync::critical_section::EnteredCriticalSection
struct. Note that f receives an EnteredCriticalSection<'s, T1> for the
data protected by m1 but an Option<EnteredCriticalSection<'s, T2>> for
the data protected by m2. If m1 and m2 are the same object, then the
Option will contain None, otherwise it contains a wrapper for the data
protected by m2.
This is structurally equivalent to the use of the paired Py_BEGIN_CRITICAL_SECTION2_MUTEX and Py_END_CRITICAL_SECTION2 C-API macros.
A no-op on GIL-enabled builds, where the critical section API is exposed as a no-op by the Python C API.
§Safety
The caller must ensure the closure cannot implicitly release the critical section. See the
safety notes in the documentation for
pyo3::sync::critical_section::EnteredCriticalSection
for more details.