Skip to content
Merged
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: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ explicit_iter_loop = "warn"
filter_map_next = "warn"
flat_map_option = "warn"
format_collect = "warn"
from_iter_instead_of_collect = "warn"
inconsistent_struct_constructor = "warn"
index_refutable_slice = "warn"
inefficient_to_string = "warn"
Expand Down
16 changes: 15 additions & 1 deletion crates/common/src/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::lock::{
MapImmutable, PyImmutableMappedMutexGuard, PyMappedMutexGuard, PyMappedRwLockReadGuard,
MapImmutable, PyImmutableMappedMutexGuard, PyMappedDetachingRwLockReadGuard,
PyMappedDetachingRwLockWriteGuard, PyMappedMutexGuard, PyMappedRwLockReadGuard,
PyMappedRwLockWriteGuard, PyMutexGuard, PyRwLockReadGuard, PyRwLockWriteGuard,
};
use alloc::fmt;
Expand All @@ -24,13 +25,15 @@ pub enum BorrowedValue<'a, T: ?Sized> {
MappedMuLock(PyImmutableMappedMutexGuard<'a, T>),
ReadLock(PyRwLockReadGuard<'a, T>),
MappedReadLock(PyMappedRwLockReadGuard<'a, T>),
MappedDetachingReadLock(PyMappedDetachingRwLockReadGuard<'a, T>),
}
impl_from!('a, T, BorrowedValue<'a, T>,
Ref(&'a T),
MuLock(PyMutexGuard<'a, T>),
MappedMuLock(PyImmutableMappedMutexGuard<'a, T>),
ReadLock(PyRwLockReadGuard<'a, T>),
MappedReadLock(PyMappedRwLockReadGuard<'a, T>),
MappedDetachingReadLock(PyMappedDetachingRwLockReadGuard<'a, T>),
);

impl<'a, T: ?Sized> BorrowedValue<'a, T> {
Expand Down Expand Up @@ -59,6 +62,9 @@ impl<'a, T: ?Sized> BorrowedValue<'a, T> {
Self::MappedReadLock(m) => {
BorrowedValue::MappedReadLock(PyMappedRwLockReadGuard::map(m, f))
}
Self::MappedDetachingReadLock(m) => {
BorrowedValue::MappedDetachingReadLock(PyMappedDetachingRwLockReadGuard::map(m, f))
}
}
}
}
Expand All @@ -73,6 +79,7 @@ impl<T: ?Sized> Deref for BorrowedValue<'_, T> {
Self::MappedMuLock(m) => m,
Self::ReadLock(r) => r,
Self::MappedReadLock(m) => m,
Self::MappedDetachingReadLock(m) => m,
}
}
}
Expand All @@ -90,6 +97,7 @@ pub enum BorrowedValueMut<'a, T: ?Sized> {
MappedMuLock(PyMappedMutexGuard<'a, T>),
WriteLock(PyRwLockWriteGuard<'a, T>),
MappedWriteLock(PyMappedRwLockWriteGuard<'a, T>),
MappedDetachingWriteLock(PyMappedDetachingRwLockWriteGuard<'a, T>),
}

impl_from!('a, T, BorrowedValueMut<'a, T>,
Expand All @@ -98,6 +106,7 @@ impl_from!('a, T, BorrowedValueMut<'a, T>,
MappedMuLock(PyMappedMutexGuard<'a, T>),
WriteLock(PyRwLockWriteGuard<'a, T>),
MappedWriteLock(PyMappedRwLockWriteGuard<'a, T>),
MappedDetachingWriteLock(PyMappedDetachingRwLockWriteGuard<'a, T>),
);

impl<'a, T: ?Sized> BorrowedValueMut<'a, T> {
Expand All @@ -113,6 +122,9 @@ impl<'a, T: ?Sized> BorrowedValueMut<'a, T> {
Self::MappedWriteLock(m) => {
BorrowedValueMut::MappedWriteLock(PyMappedRwLockWriteGuard::map(m, f))
}
Self::MappedDetachingWriteLock(m) => BorrowedValueMut::MappedDetachingWriteLock(
PyMappedDetachingRwLockWriteGuard::map(m, f),
),
}
}
}
Expand All @@ -127,6 +139,7 @@ impl<T: ?Sized> Deref for BorrowedValueMut<'_, T> {
Self::MappedMuLock(m) => m,
Self::WriteLock(w) => w,
Self::MappedWriteLock(w) => w,
Self::MappedDetachingWriteLock(w) => w,
}
}
}
Expand All @@ -139,6 +152,7 @@ impl<T: ?Sized> DerefMut for BorrowedValueMut<'_, T> {
Self::MappedMuLock(m) => &mut *m,
Self::WriteLock(w) => &mut *w,
Self::MappedWriteLock(w) => &mut *w,
Self::MappedDetachingWriteLock(w) => &mut *w,
}
}
}
16 changes: 16 additions & 0 deletions crates/common/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use lock_api::{

cfg_select! {
feature = "threading" => {
pub use detaching::{BlockingWaitHook, set_blocking_wait_hook, set_world_stopped};
pub use parking_lot::{RawMutex, RawRwLock, RawThreadId};
pub use std::sync::OnceLock as OnceCell;
pub use core::cell::LazyCell;
Expand Down Expand Up @@ -47,6 +48,8 @@ cfg_select! {
}
}

mod detaching;
pub use detaching::RawDetachingRwLock;
mod immutable_mutex;
pub use immutable_mutex::*;
mod thread_mutex;
Expand All @@ -60,6 +63,19 @@ pub type PyThreadMutex<T> = ThreadMutex<RawMutex, RawThreadId, T>;
pub type PyThreadMutexGuard<'a, T> = ThreadMutexGuard<'a, RawMutex, RawThreadId, T>;
pub type PyMappedThreadMutexGuard<'a, T> = MappedThreadMutexGuard<'a, RawMutex, RawThreadId, T>;

/// A `PyRwLock` for data a thread may hold locked across a blocking call.
///
/// Waiting for one of these leaves the interpreter first, so a thread blocked
/// on it is a thread stop-the-world can park. That is only safe where a
/// collection never takes the same lock — see [`RawDetachingRwLock`] — so this
/// is opt-in per lock rather than what every `PyRwLock` does.
pub type PyDetachingRwLock<T> = RwLock<RawDetachingRwLock, T>;
pub type PyDetachingRwLockReadGuard<'a, T> = RwLockReadGuard<'a, RawDetachingRwLock, T>;
pub type PyDetachingRwLockWriteGuard<'a, T> = RwLockWriteGuard<'a, RawDetachingRwLock, T>;
pub type PyMappedDetachingRwLockReadGuard<'a, T> = MappedRwLockReadGuard<'a, RawDetachingRwLock, T>;
pub type PyMappedDetachingRwLockWriteGuard<'a, T> =
MappedRwLockWriteGuard<'a, RawDetachingRwLock, T>;

pub type PyRwLock<T> = RwLock<RawRwLock, T>;
pub type PyRwLockUpgradableReadGuard<'a, T> = RwLockUpgradableReadGuard<'a, RawRwLock, T>;
pub type PyRwLockReadGuard<'a, T> = RwLockReadGuard<'a, RawRwLock, T>;
Expand Down
Loading
Loading