use crate::{lockable::Lock, Keyable, Lockable, OwnedLockable, Sharable}; use std::collections::HashSet; use std::marker::PhantomData; use super::{LockGuard, RetryingLockCollection}; fn contains_duplicates(data: L) -> bool { let mut locks = Vec::new(); data.get_ptrs(&mut locks); let locks = locks.into_iter().map(|l| l as *const dyn Lock); let mut locks_set = HashSet::new(); for lock in locks { if !locks_set.insert(lock) { return true; } } false } unsafe impl Lockable for RetryingLockCollection { type Guard<'g> = L::Guard<'g> where Self: 'g; type ReadGuard<'g> = L::ReadGuard<'g> where Self: 'g; fn get_ptrs<'a>(&'a self, ptrs: &mut Vec<&'a dyn Lock>) { self.data.get_ptrs(ptrs) } unsafe fn guard(&self) -> Self::Guard<'_> { self.data.guard() } unsafe fn read_guard(&self) -> Self::ReadGuard<'_> { self.data.read_guard() } } unsafe impl Sharable for RetryingLockCollection {} unsafe impl OwnedLockable for RetryingLockCollection {} impl IntoIterator for RetryingLockCollection where L: IntoIterator, { type Item = ::Item; type IntoIter = ::IntoIter; fn into_iter(self) -> Self::IntoIter { self.data.into_iter() } } impl<'a, L> IntoIterator for &'a RetryingLockCollection where &'a L: IntoIterator, { type Item = <&'a L as IntoIterator>::Item; type IntoIter = <&'a L as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { self.data.into_iter() } } impl<'a, L> IntoIterator for &'a mut RetryingLockCollection where &'a mut L: IntoIterator, { type Item = <&'a mut L as IntoIterator>::Item; type IntoIter = <&'a mut L as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { self.data.into_iter() } } impl + OwnedLockable> FromIterator for RetryingLockCollection { fn from_iter>(iter: T) -> Self { let iter: I = iter.into_iter().collect(); Self::new(iter) } } impl, L: OwnedLockable> Extend for RetryingLockCollection { fn extend>(&mut self, iter: T) { self.data.extend(iter) } } impl AsRef for RetryingLockCollection { fn as_ref(&self) -> &L { &self.data } } impl AsMut for RetryingLockCollection { fn as_mut(&mut self) -> &mut L { &mut self.data } } impl Default for RetryingLockCollection { fn default() -> Self { Self::new(L::default()) } } impl From for RetryingLockCollection { fn from(value: L) -> Self { Self::new(value) } } impl RetryingLockCollection { #[must_use] pub const fn new(data: L) -> Self { Self { data } } } impl<'a, L: OwnedLockable> RetryingLockCollection<&'a L> { #[must_use] pub const fn new_ref(data: &'a L) -> Self { Self { data } } } impl RetryingLockCollection { #[must_use] pub const unsafe fn new_unchecked(data: L) -> Self { Self { data } } pub fn try_new(data: L) -> Option { contains_duplicates(&data).then_some(Self { data }) } pub fn into_inner(self) -> L { self.data } pub fn lock<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, ) -> LockGuard<'key, L::Guard<'g>, Key> { let mut first_index = 0; let mut locks = Vec::new(); self.data.get_ptrs(&mut locks); if locks.is_empty() { return LockGuard { // safety: there's no data being returned guard: unsafe { self.data.guard() }, key, _phantom: PhantomData, }; } let guard = unsafe { 'outer: loop { // safety: we have the thread key locks[first_index].lock(); for (i, lock) in locks.iter().enumerate() { if i == first_index { continue; } // safety: we have the thread key if !lock.try_lock() { for lock in locks.iter().take(i) { // safety: we already locked all of these lock.unlock(); } if first_index >= i { // safety: this is already locked and can't be unlocked // by the previous loop locks[first_index].unlock(); } first_index = i; continue 'outer; } } // safety: we locked all the data break self.data.guard(); } }; LockGuard { guard, key, _phantom: PhantomData, } } pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, ) -> Option, Key>> { let mut locks = Vec::new(); self.data.get_ptrs(&mut locks); if locks.is_empty() { return Some(LockGuard { // safety: there's no data being returned guard: unsafe { self.data.guard() }, key, _phantom: PhantomData, }); } let guard = unsafe { for (i, lock) in locks.iter().enumerate() { // safety: we have the thread key if !lock.try_lock() { for lock in locks.iter().take(i) { // safety: we already locked all of these lock.unlock(); } return None; } } // safety: we locked all the data self.data.guard() }; Some(LockGuard { guard, key, _phantom: PhantomData, }) } pub fn unlock<'key, Key: Keyable + 'key>(guard: LockGuard<'key, L::Guard<'_>, Key>) -> Key { drop(guard.guard); guard.key } } impl RetryingLockCollection { pub fn read<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, ) -> LockGuard<'key, L::ReadGuard<'g>, Key> { let mut first_index = 0; let mut locks = Vec::new(); self.data.get_ptrs(&mut locks); if locks.is_empty() { return LockGuard { // safety: there's no data being returned guard: unsafe { self.data.read_guard() }, key, _phantom: PhantomData, }; } let guard = unsafe { 'outer: loop { // safety: we have the thread key locks[first_index].read(); for (i, lock) in locks.iter().enumerate() { if i == first_index { continue; } // safety: we have the thread key if !lock.try_read() { for lock in locks.iter().take(i) { // safety: we already locked all of these lock.unlock_read(); } if first_index >= i { // safety: this is already locked and can't be unlocked // by the previous loop locks[first_index].unlock_read(); } first_index = i; continue 'outer; } } // safety: we locked all the data break self.data.read_guard(); } }; LockGuard { guard, key, _phantom: PhantomData, } } pub fn try_read<'g, 'key: 'g, Key: Keyable + 'key>( &'g self, key: Key, ) -> Option, Key>> { let mut locks = Vec::new(); self.data.get_ptrs(&mut locks); if locks.is_empty() { return Some(LockGuard { // safety: there's no data being returned guard: unsafe { self.data.read_guard() }, key, _phantom: PhantomData, }); } let guard = unsafe { for (i, lock) in locks.iter().enumerate() { // safety: we have the thread key if !lock.try_read() { for lock in locks.iter().take(i) { // safety: we already locked all of these lock.unlock_read(); } return None; } } // safety: we locked all the data self.data.read_guard() }; Some(LockGuard { guard, key, _phantom: PhantomData, }) } pub fn unlock_read<'key, Key: Keyable + 'key>( guard: LockGuard<'key, L::ReadGuard<'_>, Key>, ) -> Key { drop(guard.guard); guard.key } } impl<'a, L: 'a> RetryingLockCollection where &'a L: IntoIterator, { /// Returns an iterator over references to each value in the collection. #[must_use] pub fn iter(&'a self) -> <&'a L as IntoIterator>::IntoIter { self.into_iter() } } impl<'a, L: 'a> RetryingLockCollection where &'a mut L: IntoIterator, { /// Returns an iterator over mutable references to each value in the /// collection. #[must_use] pub fn iter_mut(&'a mut self) -> <&'a mut L as IntoIterator>::IntoIter { self.into_iter() } }