pub struct Mutex<T>(parking_lot::Mutex<T>);
pub struct MutexGuard<'a, T>(parking_lot::MutexGuard<'a, T>);
impl<T> Mutex<T> {
pub fn new(_rank: super::rank::LockRank, value: T) -> Mutex<T> {
Mutex(parking_lot::Mutex::new(value))
}
pub fn lock(&self) -> MutexGuard<T> {
MutexGuard(self.0.lock())
}
}
impl<'a, T> MutexGuard<'a, T> {
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<parking_lot::MappedMutexGuard<'a, U>, ()>
where
F: FnOnce(&mut T) -> Option<&mut U>,
{
parking_lot::MutexGuard::try_map(s.0, f).map_err(|_| ())
}
}
impl<'a, T> std::ops::Deref for MutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl<'a, T> std::ops::DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.deref_mut()
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
pub struct RwLock<T>(parking_lot::RwLock<T>);
pub struct RwLockReadGuard<'a, T>(parking_lot::RwLockReadGuard<'a, T>);
pub struct RwLockWriteGuard<'a, T>(parking_lot::RwLockWriteGuard<'a, T>);
impl<T> RwLock<T> {
pub fn new(_rank: super::rank::LockRank, value: T) -> RwLock<T> {
RwLock(parking_lot::RwLock::new(value))
}
pub fn read(&self) -> RwLockReadGuard<T> {
RwLockReadGuard(self.0.read())
}
pub fn write(&self) -> RwLockWriteGuard<T> {
RwLockWriteGuard(self.0.write())
}
}
impl<'a, T> RwLockWriteGuard<'a, T> {
pub fn downgrade(this: Self) -> RwLockReadGuard<'a, T> {
RwLockReadGuard(parking_lot::RwLockWriteGuard::downgrade(this.0))
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<'a, T> std::ops::Deref for RwLockReadGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl<'a, T> std::ops::Deref for RwLockWriteGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl<'a, T> std::ops::DerefMut for RwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.deref_mut()
}
}