From 58abf5872023aca7ee6459fa3b2e067d57923ba5 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 9 Mar 2025 20:49:56 -0400 Subject: Finish testing and fixing --- src/rwlock/rwlock.rs | 115 +++++++++++++-------------------------------------- 1 file changed, 28 insertions(+), 87 deletions(-) (limited to 'src/rwlock/rwlock.rs') diff --git a/src/rwlock/rwlock.rs b/src/rwlock/rwlock.rs index 905ecf8..5f407d1 100644 --- a/src/rwlock/rwlock.rs +++ b/src/rwlock/rwlock.rs @@ -5,6 +5,7 @@ use std::panic::AssertUnwindSafe; use lock_api::RawRwLock; +use crate::collection::utils; use crate::handle_unwind::handle_unwind; use crate::lockable::{ Lockable, LockableGetMut, LockableIntoInner, OwnedLockable, RawLock, Sharable, @@ -18,7 +19,7 @@ unsafe impl RawLock for RwLock { self.poison.poison(); } - unsafe fn raw_lock(&self) { + unsafe fn raw_write(&self) { assert!( !self.poison.is_poisoned(), "The read-write lock has been killed" @@ -29,7 +30,7 @@ unsafe impl RawLock for RwLock { handle_unwind(|| this.raw.lock_exclusive(), || self.poison()) } - unsafe fn raw_try_lock(&self) -> bool { + unsafe fn raw_try_write(&self) -> bool { if self.poison.is_poisoned() { return false; } @@ -39,7 +40,7 @@ unsafe impl RawLock for RwLock { handle_unwind(|| this.raw.try_lock_exclusive(), || self.poison()) } - unsafe fn raw_unlock(&self) { + unsafe fn raw_unlock_write(&self) { // if the closure unwraps, then the mutex will be killed let this = AssertUnwindSafe(self); handle_unwind(|| this.raw.unlock_exclusive(), || self.poison()) @@ -73,7 +74,7 @@ unsafe impl RawLock for RwLock { } } -unsafe impl Lockable for RwLock { +unsafe impl Lockable for RwLock { type Guard<'g> = RwLockWriteRef<'g, T, R> where @@ -97,7 +98,7 @@ unsafe impl Lockable for RwLock { } } -unsafe impl Sharable for RwLock { +unsafe impl Sharable for RwLock { type ReadGuard<'g> = RwLockReadRef<'g, T, R> where @@ -117,9 +118,9 @@ unsafe impl Sharable for RwLock { } } -unsafe impl OwnedLockable for RwLock {} +unsafe impl OwnedLockable for RwLock {} -impl LockableIntoInner for RwLock { +impl LockableIntoInner for RwLock { type Inner = T; fn into_inner(self) -> Self::Inner { @@ -127,7 +128,7 @@ impl LockableIntoInner for RwLock { } } -impl LockableGetMut for RwLock { +impl LockableGetMut for RwLock { type Inner<'a> = &'a mut T where @@ -160,7 +161,7 @@ impl RwLock { #[mutants::skip] #[cfg(not(tarpaulin_include))] -impl Debug for RwLock { +impl Debug for RwLock { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // safety: this is just a try lock, and the value is dropped // immediately after, so there's no risk of blocking ourselves @@ -247,85 +248,29 @@ impl RwLock { } } -impl RwLock { - pub fn scoped_read(&self, key: impl Keyable, f: impl Fn(&T) -> Ret) -> Ret { - unsafe { - // safety: we have the thread key - self.raw_read(); - - // safety: the rwlock was just locked - let r = f(self.data.get().as_ref().unwrap_unchecked()); - - // safety: the rwlock is already locked - self.raw_unlock_read(); - - drop(key); // ensure the key stays valid for long enough - - r - } +impl RwLock { + pub fn scoped_read<'a, Ret>(&'a self, key: impl Keyable, f: impl Fn(&'a T) -> Ret) -> Ret { + utils::scoped_read(self, key, f) } - pub fn scoped_try_read( - &self, + pub fn scoped_try_read<'a, Key: Keyable, Ret>( + &'a self, key: Key, - f: impl Fn(&T) -> Ret, + f: impl Fn(&'a T) -> Ret, ) -> Result { - unsafe { - // safety: we have the thread key - if !self.raw_try_read() { - return Err(key); - } - - // safety: the rwlock was just locked - let r = f(self.data.get().as_ref().unwrap_unchecked()); - - // safety: the rwlock is already locked - self.raw_unlock_read(); - - drop(key); // ensure the key stays valid for long enough - - Ok(r) - } + utils::scoped_try_read(self, key, f) } - pub fn scoped_write(&self, key: impl Keyable, f: impl Fn(&mut T) -> Ret) -> Ret { - unsafe { - // safety: we have the thread key - self.raw_lock(); - - // safety: we just locked the rwlock - let r = f(self.data.get().as_mut().unwrap_unchecked()); - - // safety: the rwlock is already locked - self.raw_unlock(); - - drop(key); // ensure the key stays valid for long enough - - r - } + pub fn scoped_write<'a, Ret>(&'a self, key: impl Keyable, f: impl Fn(&'a mut T) -> Ret) -> Ret { + utils::scoped_write(self, key, f) } - pub fn scoped_try_write( - &self, + pub fn scoped_try_write<'a, Key: Keyable, Ret>( + &'a self, key: Key, - f: impl Fn(&mut T) -> Ret, + f: impl Fn(&'a mut T) -> Ret, ) -> Result { - unsafe { - // safety: we have the thread key - if !self.raw_try_lock() { - return Err(key); - } - - // safety: the rwlock was just locked - let r = f(self.data.get().as_mut().unwrap_unchecked()); - - // safety: the rwlock is already locked - self.raw_unlock(); - - drop(key); // ensure the key stays valid for long enough - - Ok(r) - } + utils::scoped_try_write(self, key, f) } /// Locks this `RwLock` with shared read access, blocking the current @@ -426,7 +371,7 @@ impl RwLock { /// without exclusive access to the key is undefined behavior. #[cfg(test)] pub(crate) unsafe fn try_write_no_key(&self) -> Option> { - if self.raw_try_lock() { + if self.raw_try_write() { // safety: the lock is locked first Some(RwLockWriteRef(self, PhantomData)) } else { @@ -463,7 +408,7 @@ impl RwLock { /// [`ThreadKey`]: `crate::ThreadKey` pub fn write(&self, key: ThreadKey) -> RwLockWriteGuard<'_, T, R> { unsafe { - self.raw_lock(); + self.raw_write(); // safety: the lock is locked first RwLockWriteGuard::new(self, key) @@ -498,7 +443,7 @@ impl RwLock { /// ``` pub fn try_write(&self, key: ThreadKey) -> Result, ThreadKey> { unsafe { - if self.raw_try_lock() { + if self.raw_try_write() { // safety: the lock is locked first Ok(RwLockWriteGuard::new(self, key)) } else { @@ -533,9 +478,7 @@ impl RwLock { /// ``` #[must_use] pub fn unlock_read(guard: RwLockReadGuard<'_, T, R>) -> ThreadKey { - unsafe { - guard.rwlock.0.raw_unlock_read(); - } + drop(guard.rwlock); guard.thread_key } @@ -560,9 +503,7 @@ impl RwLock { /// ``` #[must_use] pub fn unlock_write(guard: RwLockWriteGuard<'_, T, R>) -> ThreadKey { - unsafe { - guard.rwlock.0.raw_unlock(); - } + drop(guard.rwlock); guard.thread_key } } -- cgit v1.2.3