INNER CODE UNIT · Rust
with_write_gil_aware
RyanCodrai/turbovec · turbovec-python/src/lib.rs:352
fn with_write_gil_aware<T: Send + Sync, R: Send>(
py: Python<'_>,
lock: &std::sync::RwLock<T>,
f: impl FnOnce(&mut T) -> R + Send,
) -> R {
match lock.try_write() {
Ok(mut guard) => f(&mut guard),
Err(std::sync::TryLockError::Poisoned(p)) => f(&mut p.into_inner()),
// Contended: one blocking acquire, detached, with the work done
// inside it. No retry loop — the acquire is queued and cannot be
// jumped by a reader that arrives after it.
Err(std::sync::TryLockError::WouldBlock) => py.detach(|| f(&mut lock_write(lock))),
}
}
// Locking discipline (both pyclasses): the pyclass is `frozen`, so pyo3
// performs no runtime borrow-checking of its own and every method takes
// `&self`; the inner index sits behind an `RwLock` instead. Concurrent