From d9095d8fce59714f75019ecf68911d9931a1af15 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sat, 14 Mar 2026 20:37:26 -0400 Subject: Basic scoped lock and guard implementations --- src/collection/owned.rs | 14 +++++- src/iterator.rs | 18 +++++++ src/iterator/consumed_guard.rs | 58 ++++++++++++++++++++++ src/iterator/guard.rs | 58 ++++++++++++++++++++++ src/iterator/iterator.rs | 109 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 src/iterator.rs create mode 100644 src/iterator/consumed_guard.rs create mode 100644 src/iterator/guard.rs create mode 100644 src/iterator/iterator.rs (limited to 'src') diff --git a/src/collection/owned.rs b/src/collection/owned.rs index 2172b96..516f1ea 100755 --- a/src/collection/owned.rs +++ b/src/collection/owned.rs @@ -647,9 +647,21 @@ impl OwnedLockCollection where for<'a> &'a L: IntoIterator, { - pub fn locking_iter(&self, key: ThreadKey) -> LockingIterator<<&L as IntoIterator>::IntoIter> { + pub fn locking_iter( + &self, + key: ThreadKey, + ) -> LockingIterator<<&L as IntoIterator>::IntoIter, ThreadKey> { LockingIterator::new(key, (&self.child).into_iter()) } + + pub fn scoped_locking_iter( + &self, + key: Key, + f: impl FnOnce(&mut LockingIterator<<&L as IntoIterator>::IntoIter, Key>) -> R, + ) -> R { + let mut iterator = LockingIterator::new(key, (&self.child).into_iter()); + f(&mut iterator) + } } #[cfg(test)] diff --git a/src/iterator.rs b/src/iterator.rs new file mode 100644 index 0000000..30b01e0 --- /dev/null +++ b/src/iterator.rs @@ -0,0 +1,18 @@ +mod consumed_guard; +mod guard; +mod iterator; + +pub struct LockingIterator { + key: Key, + lockable: L, +} + +pub struct IteratorGuard<'a, Guard, Key> { + _key: &'a Key, + guard: Guard, +} + +pub struct ConsumedIteratorGuard { + key: Key, + guard: Guard, +} diff --git a/src/iterator/consumed_guard.rs b/src/iterator/consumed_guard.rs new file mode 100644 index 0000000..5331289 --- /dev/null +++ b/src/iterator/consumed_guard.rs @@ -0,0 +1,58 @@ +use std::fmt::{Debug, Display}; +use std::hash::Hash; +use std::ops::{Deref, DerefMut}; + +use crate::iterator::ConsumedIteratorGuard; + +#[mutants::skip] // hashing involves RNG and is hard to test +#[cfg(not(tarpaulin_include))] +impl Hash for ConsumedIteratorGuard { + fn hash(&self, state: &mut H) { + self.guard.hash(state) + } +} + +// No implementations of Eq, PartialEq, PartialOrd, or Ord +// You can't implement both PartialEq and PartialEq +// It's easier to just implement neither and ask users to dereference +// This is less of a problem when using the scoped lock API + +#[mutants::skip] +#[cfg(not(tarpaulin_include))] +impl Debug for ConsumedIteratorGuard { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Debug::fmt(&**self, f) + } +} + +impl Display for ConsumedIteratorGuard { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Display::fmt(&**self, f) + } +} + +impl Deref for ConsumedIteratorGuard { + type Target = Guard; + + fn deref(&self) -> &Self::Target { + &self.guard + } +} + +impl DerefMut for ConsumedIteratorGuard { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.guard + } +} + +impl AsRef for ConsumedIteratorGuard { + fn as_ref(&self) -> &Guard { + &self.guard + } +} + +impl AsMut for ConsumedIteratorGuard { + fn as_mut(&mut self) -> &mut Guard { + &mut self.guard + } +} diff --git a/src/iterator/guard.rs b/src/iterator/guard.rs new file mode 100644 index 0000000..6393fc2 --- /dev/null +++ b/src/iterator/guard.rs @@ -0,0 +1,58 @@ +use std::fmt::{Debug, Display}; +use std::hash::Hash; +use std::ops::{Deref, DerefMut}; + +use super::{ConsumedIteratorGuard, IteratorGuard}; + +#[mutants::skip] // hashing involves RNG and is hard to test +#[cfg(not(tarpaulin_include))] +impl Hash for IteratorGuard<'_, Guard, Key> { + fn hash(&self, state: &mut H) { + self.guard.hash(state) + } +} + +// No implementations of Eq, PartialEq, PartialOrd, or Ord +// You can't implement both PartialEq and PartialEq +// It's easier to just implement neither and ask users to dereference +// This is less of a problem when using the scoped lock API + +#[mutants::skip] +#[cfg(not(tarpaulin_include))] +impl Debug for IteratorGuard<'_, Guard, Key> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Debug::fmt(&**self, f) + } +} + +impl Display for IteratorGuard<'_, Guard, Key> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Display::fmt(&**self, f) + } +} + +impl Deref for IteratorGuard<'_, Guard, Key> { + type Target = Guard; + + fn deref(&self) -> &Self::Target { + &self.guard + } +} + +impl DerefMut for IteratorGuard<'_, Guard, Key> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.guard + } +} + +impl AsRef for IteratorGuard<'_, Guard, Key> { + fn as_ref(&self) -> &Guard { + &self.guard + } +} + +impl AsMut for IteratorGuard<'_, Guard, Key> { + fn as_mut(&mut self) -> &mut Guard { + &mut self.guard + } +} diff --git a/src/iterator/iterator.rs b/src/iterator/iterator.rs new file mode 100644 index 0000000..1e389a4 --- /dev/null +++ b/src/iterator/iterator.rs @@ -0,0 +1,109 @@ +use std::iter::{Enumerate, Fuse, Skip, Take}; + +use super::{ConsumedIteratorGuard, IteratorGuard, LockingIterator}; + +use crate::{ + lockable::{Lockable, RawLock, Sharable}, + Keyable, +}; + +impl LockingIterator { + pub(crate) const fn new(key: Key, lockable: L) -> Self { + Self { key, lockable } + } + + fn with_iterator(self, f: impl FnOnce(L) -> M) -> LockingIterator { + LockingIterator { + key: self.key, + lockable: f(self.lockable), + } + } +} + +impl<'a, I: Iterator, L: 'a + Lockable + RawLock, Key: Keyable> + LockingIterator +{ + pub fn lock_next(&'a mut self) -> Option::Guard<'a>, Key>> { + if let Some(lock) = self.lockable.next() { + unsafe { + lock.raw_write(); + let guard = lock.guard(); + + Some(IteratorGuard { + _key: &self.key, + guard, + }) + } + } else { + None + } + } + + pub fn lock_last(self) -> Option::Guard<'a>, Key>> { + self.lockable.last().map(|lock| unsafe { + lock.raw_write(); + let guard = lock.guard(); + + ConsumedIteratorGuard { + key: self.key, + guard, + } + }) + } +} + +impl<'a, I: Iterator, L: 'a + Sharable + RawLock, Key: Keyable> + LockingIterator +{ + pub fn read_next( + &'a mut self, + ) -> Option::ReadGuard<'a>, Key>> { + if let Some(lock) = self.lockable.next() { + unsafe { + lock.raw_read(); + let guard = lock.read_guard(); + + Some(IteratorGuard { + _key: &self.key, + guard, + }) + } + } else { + None + } + } + + pub fn read_last(self) -> Option::ReadGuard<'a>, Key>> { + self.lockable.last().map(|lock| unsafe { + lock.raw_read(); + let guard = lock.read_guard(); + + ConsumedIteratorGuard { + key: self.key, + guard, + } + }) + } +} + +impl LockingIterator { + pub fn size_hint(&self) -> (usize, Option) { + self.lockable.size_hint() + } + + pub fn enumerate(self) -> LockingIterator, Key> { + self.with_iterator(Iterator::enumerate) + } + + pub fn skip(self, n: usize) -> LockingIterator, Key> { + self.with_iterator(|i| i.skip(n)) + } + + pub fn take(self, n: usize) -> LockingIterator, Key> { + self.with_iterator(|i| i.take(n)) + } + + pub fn fuse(self) -> LockingIterator, Key> { + self.with_iterator(Iterator::fuse) + } +} -- cgit v1.2.3