use std::cell::Cell;
use std::collections::HashSet;
use crate::collection::utils;
use crate::handle_unwind::handle_unwind;
use crate::lockable::{
Lockable, LockableGetMut, LockableIntoInner, OwnedLockable, RawLock, Sharable,
};
use crate::{Keyable, ThreadKey};
use super::utils::{
attempt_to_recover_reads_from_panic, attempt_to_recover_writes_from_panic, get_locks_unsorted,
scoped_read, scoped_try_read, scoped_try_write, scoped_write,
};
use super::{LockGuard, RetryingLockCollection};
fn contains_duplicates<L: Lockable>(data: L) -> bool {
let mut locks = Vec::new();
data.get_ptrs(&mut locks);
let locks = locks.into_iter().map(|l| (&raw const *l).cast::<()>());
let mut locks_set = HashSet::with_capacity(locks.len());
for lock in locks {
if !locks_set.insert(lock) {
return true;
}
}
false
}
unsafe impl<L: Lockable> RawLock for RetryingLockCollection<L> {
#[mutants::skip] #[cfg(not(tarpaulin_include))]
fn poison(&self) {
let locks = get_locks_unsorted(&self.child);
for lock in locks {
lock.poison();
}
}
unsafe fn raw_write(&self) {
let locks = get_locks_unsorted(&self.child);
if locks.is_empty() {
return;
}
let first_index = Cell::new(0);
let locked = Cell::new(0);
handle_unwind(
|| unsafe {
'outer: loop {
locks[first_index.get()].raw_write();
for (i, lock) in locks.iter().enumerate() {
if i == first_index.get() {
continue;
}
if lock.raw_try_write() {
locked.set(locked.get() + 1);
} else {
attempt_to_recover_writes_from_panic(&locks[0..i]);
if first_index.get() >= i {
locks[first_index.get()].raw_unlock_write();
}
locked.set(0);
first_index.set(i);
continue 'outer;
}
}
break;
}
},
|| {
utils::attempt_to_recover_writes_from_panic(&locks[0..locked.get()]);
if first_index.get() >= locked.get() {
locks[first_index.get()].raw_unlock_write();
}
},
)
}
unsafe fn raw_try_write(&self) -> bool {
let locks = get_locks_unsorted(&self.child);
if locks.is_empty() {
return true;
}
let locked = Cell::new(0);
handle_unwind(
|| unsafe {
for (i, lock) in locks.iter().enumerate() {
if lock.raw_try_write() {
locked.set(locked.get() + 1);
} else {
attempt_to_recover_writes_from_panic(&locks[0..i]);
return false;
}
}
true
},
|| utils::attempt_to_recover_writes_from_panic(&locks[0..locked.get()]),
)
}
unsafe fn raw_unlock_write(&self) {
let locks = get_locks_unsorted(&self.child);
for lock in locks {
lock.raw_unlock_write();
}
}
unsafe fn raw_read(&self) {
let locks = get_locks_unsorted(&self.child);
if locks.is_empty() {
return;
}
let locked = Cell::new(0);
let first_index = Cell::new(0);
handle_unwind(
|| 'outer: loop {
locks[first_index.get()].raw_read();
for (i, lock) in locks.iter().enumerate() {
if i == first_index.get() {
continue;
}
if lock.raw_try_read() {
locked.set(locked.get() + 1);
} else {
attempt_to_recover_reads_from_panic(&locks[0..i]);
if first_index.get() >= i {
locks[first_index.get()].raw_unlock_read();
}
locked.set(0);
first_index.set(i);
continue 'outer;
}
}
break;
},
|| {
utils::attempt_to_recover_reads_from_panic(&locks[0..locked.get()]);
if first_index.get() >= locked.get() {
locks[first_index.get()].raw_unlock_read();
}
},
)
}
unsafe fn raw_try_read(&self) -> bool {
let locks = get_locks_unsorted(&self.child);
if locks.is_empty() {
return true;
}
let locked = Cell::new(0);
handle_unwind(
|| unsafe {
for (i, lock) in locks.iter().enumerate() {
if lock.raw_try_read() {
locked.set(locked.get() + 1);
} else {
attempt_to_recover_reads_from_panic(&locks[0..i]);
return false;
}
}
true
},
|| utils::attempt_to_recover_reads_from_panic(&locks[0..locked.get()]),
)
}
unsafe fn raw_unlock_read(&self) {
let locks = get_locks_unsorted(&self.child);
for lock in locks {
lock.raw_unlock_read();
}
}
}
unsafe impl<L: Lockable> Lockable for RetryingLockCollection<L> {
type Guard<'g>
= L::Guard<'g>
where
Self: 'g;
type DataMut<'a>
= L::DataMut<'a>
where
Self: 'a;
fn get_ptrs<'a>(&'a self, ptrs: &mut Vec<&'a dyn RawLock>) {
self.child.get_ptrs(ptrs)
}
unsafe fn guard(&self) -> Self::Guard<'_> {
self.child.guard()
}
unsafe fn data_mut(&self) -> Self::DataMut<'_> {
self.child.data_mut()
}
}
unsafe impl<L: Sharable> Sharable for RetryingLockCollection<L> {
type ReadGuard<'g>
= L::ReadGuard<'g>
where
Self: 'g;
type DataRef<'a>
= L::DataRef<'a>
where
Self: 'a;
unsafe fn read_guard(&self) -> Self::ReadGuard<'_> {
self.child.read_guard()
}
unsafe fn data_ref(&self) -> Self::DataRef<'_> {
self.child.data_ref()
}
}
unsafe impl<L: OwnedLockable> OwnedLockable for RetryingLockCollection<L> {}
impl<L: LockableGetMut> LockableGetMut for RetryingLockCollection<L> {
type Inner<'a>
= L::Inner<'a>
where
Self: 'a;
fn get_mut(&mut self) -> Self::Inner<'_> {
self.child.get_mut()
}
}
impl<L: LockableIntoInner> LockableIntoInner for RetryingLockCollection<L> {
type Inner = L::Inner;
fn into_inner(self) -> Self::Inner {
self.child.into_inner()
}
}
impl<L> IntoIterator for RetryingLockCollection<L>
where
L: IntoIterator,
{
type Item = <L as IntoIterator>::Item;
type IntoIter = <L as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.child.into_iter()
}
}
impl<'a, L> IntoIterator for &'a RetryingLockCollection<L>
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.child.into_iter()
}
}
impl<'a, L> IntoIterator for &'a mut RetryingLockCollection<L>
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.child.into_iter()
}
}
impl<L: OwnedLockable, I: FromIterator<L> + OwnedLockable> FromIterator<L>
for RetryingLockCollection<I>
{
fn from_iter<T: IntoIterator<Item = L>>(iter: T) -> Self {
let iter: I = iter.into_iter().collect();
Self::new(iter)
}
}
impl<E: OwnedLockable + Extend<L>, L: OwnedLockable> Extend<L> for RetryingLockCollection<E> {
fn extend<T: IntoIterator<Item = L>>(&mut self, iter: T) {
self.child.extend(iter)
}
}
impl<T: ?Sized, L: AsRef<T>> AsRef<T> for RetryingLockCollection<L> {
fn as_ref(&self) -> &T {
self.child.as_ref()
}
}
impl<T: ?Sized, L: AsMut<T>> AsMut<T> for RetryingLockCollection<L> {
fn as_mut(&mut self) -> &mut T {
self.child.as_mut()
}
}
impl<L: OwnedLockable + Default> Default for RetryingLockCollection<L> {
fn default() -> Self {
Self::new(L::default())
}
}
impl<L: OwnedLockable> From<L> for RetryingLockCollection<L> {
fn from(value: L) -> Self {
Self::new(value)
}
}
impl<L: OwnedLockable> RetryingLockCollection<L> {
#[must_use]
pub const fn new(data: L) -> Self {
unsafe { Self::new_unchecked(data) }
}
}
impl<'a, L: OwnedLockable> RetryingLockCollection<&'a L> {
#[must_use]
pub const fn new_ref(data: &'a L) -> Self {
unsafe { Self::new_unchecked(data) }
}
}
impl<L> RetryingLockCollection<L> {
#[must_use]
pub const unsafe fn new_unchecked(data: L) -> Self {
Self { child: data }
}
#[must_use]
pub const fn child(&self) -> &L {
&self.child
}
#[must_use]
pub fn child_mut(&mut self) -> &mut L {
&mut self.child
}
#[must_use]
pub fn into_child(self) -> L {
self.child
}
}
impl<L: Lockable> RetryingLockCollection<L> {
#[must_use]
pub fn try_new(data: L) -> Option<Self> {
(!contains_duplicates(&data)).then_some(unsafe { Self::new_unchecked(data) })
}
pub fn scoped_lock<'a, R>(
&'a self,
key: impl Keyable,
f: impl FnOnce(L::DataMut<'a>) -> R,
) -> R {
scoped_write(self, key, f)
}
pub fn scoped_try_lock<'a, Key: Keyable, R>(
&'a self,
key: Key,
f: impl FnOnce(L::DataMut<'a>) -> R,
) -> Result<R, Key> {
scoped_try_write(self, key, f)
}
pub fn lock(&self, key: ThreadKey) -> LockGuard<L::Guard<'_>> {
unsafe {
self.raw_write();
LockGuard {
guard: self.guard(),
key,
}
}
}
pub fn try_lock(&self, key: ThreadKey) -> Result<LockGuard<L::Guard<'_>>, ThreadKey> {
unsafe {
if self.raw_try_write() {
Ok(LockGuard {
guard: self.guard(),
key,
})
} else {
Err(key)
}
}
}
pub fn unlock(guard: LockGuard<L::Guard<'_>>) -> ThreadKey {
drop(guard.guard);
guard.key
}
}
impl<L: Sharable> RetryingLockCollection<L> {
pub fn scoped_read<'a, R>(
&'a self,
key: impl Keyable,
f: impl FnOnce(L::DataRef<'a>) -> R,
) -> R {
scoped_read(self, key, f)
}
pub fn scoped_try_read<'a, Key: Keyable, R>(
&'a self,
key: Key,
f: impl FnOnce(L::DataRef<'a>) -> R,
) -> Result<R, Key> {
scoped_try_read(self, key, f)
}
pub fn read(&self, key: ThreadKey) -> LockGuard<L::ReadGuard<'_>> {
unsafe {
self.raw_read();
LockGuard {
guard: self.read_guard(),
key,
}
}
}
pub fn try_read(&self, key: ThreadKey) -> Result<LockGuard<L::ReadGuard<'_>>, ThreadKey> {
unsafe {
if !self.raw_try_read() {
return Err(key);
}
Ok(LockGuard {
guard: self.read_guard(),
key,
})
}
}
pub fn unlock_read(guard: LockGuard<L::ReadGuard<'_>>) -> ThreadKey {
drop(guard.guard);
guard.key
}
}
impl<L: LockableGetMut> RetryingLockCollection<L> {
pub fn get_mut(&mut self) -> L::Inner<'_> {
LockableGetMut::get_mut(self)
}
}
impl<L: LockableIntoInner> RetryingLockCollection<L> {
pub fn into_inner(self) -> L::Inner {
LockableIntoInner::into_inner(self)
}
}
impl<'a, L: 'a> RetryingLockCollection<L>
where
&'a L: IntoIterator,
{
#[must_use]
pub fn iter(&'a self) -> <&'a L as IntoIterator>::IntoIter {
self.into_iter()
}
}
impl<'a, L: 'a> RetryingLockCollection<L>
where
&'a mut L: IntoIterator,
{
#[must_use]
pub fn iter_mut(&'a mut self) -> <&'a mut L as IntoIterator>::IntoIter {
self.into_iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::collection::BoxedLockCollection;
use crate::{Mutex, RwLock, ThreadKey};
#[test]
fn nonduplicate_lock_references_are_allowed() {
let mutex1 = Mutex::new(0);
let mutex2 = Mutex::new(0);
assert!(RetryingLockCollection::try_new([&mutex1, &mutex2]).is_some());
}
#[test]
fn duplicate_lock_references_are_disallowed() {
let mutex = Mutex::new(0);
assert!(RetryingLockCollection::try_new([&mutex, &mutex]).is_none());
}
#[test]
#[allow(clippy::float_cmp)]
fn uses_correct_default() {
let collection =
RetryingLockCollection::<(RwLock<f64>, Mutex<Option<i32>>, Mutex<usize>)>::default();
let tuple = collection.into_inner();
assert_eq!(tuple.0, 0.0);
assert!(tuple.1.is_none());
assert_eq!(tuple.2, 0)
}
#[test]
fn from() {
let key = ThreadKey::get().unwrap();
let collection =
RetryingLockCollection::from([Mutex::new("foo"), Mutex::new("bar"), Mutex::new("baz")]);
let guard = collection.lock(key);
assert_eq!(*guard[0], "foo");
assert_eq!(*guard[1], "bar");
assert_eq!(*guard[2], "baz");
}
#[test]
fn new_ref_works() {
let key = ThreadKey::get().unwrap();
let mutexes = [Mutex::new(0), Mutex::new(1)];
let collection = RetryingLockCollection::new_ref(&mutexes);
collection.scoped_lock(key, |guard| {
assert_eq!(*guard[0], 0);
assert_eq!(*guard[1], 1);
})
}
#[test]
fn scoped_read_sees_changes() {
let mut key = ThreadKey::get().unwrap();
let mutexes = [RwLock::new(24), RwLock::new(42)];
let collection = RetryingLockCollection::new(mutexes);
collection.scoped_lock(&mut key, |guard| *guard[0] = 128);
let sum = collection.scoped_read(&mut key, |guard| {
assert_eq!(*guard[0], 128);
assert_eq!(*guard[1], 42);
*guard[0] + *guard[1]
});
assert_eq!(sum, 128 + 42);
}
#[test]
fn get_mut_affects_scoped_read() {
let mut key = ThreadKey::get().unwrap();
let mutexes = [RwLock::new(24), RwLock::new(42)];
let mut collection = RetryingLockCollection::new(mutexes);
let guard = collection.get_mut();
*guard[0] = 128;
let sum = collection.scoped_read(&mut key, |guard| {
assert_eq!(*guard[0], 128);
assert_eq!(*guard[1], 42);
*guard[0] + *guard[1]
});
assert_eq!(sum, 128 + 42);
}
#[test]
fn scoped_try_lock_can_fail() {
let key = ThreadKey::get().unwrap();
let collection = RetryingLockCollection::new([Mutex::new(1), Mutex::new(2)]);
let guard = collection.lock(key);
std::thread::scope(|s| {
s.spawn(|| {
let key = ThreadKey::get().unwrap();
let r = collection.scoped_try_lock(key, |_| {});
assert!(r.is_err());
});
});
drop(guard);
}
#[test]
fn scoped_try_read_can_fail() {
let key = ThreadKey::get().unwrap();
let collection = RetryingLockCollection::new([RwLock::new(1), RwLock::new(2)]);
let guard = collection.lock(key);
std::thread::scope(|s| {
s.spawn(|| {
let key = ThreadKey::get().unwrap();
let r = collection.scoped_try_read(key, |_| {});
assert!(r.is_err());
});
});
drop(guard);
}
#[test]
fn try_lock_works() {
let key = ThreadKey::get().unwrap();
let collection = RetryingLockCollection::new([Mutex::new(1), Mutex::new(2)]);
let guard = collection.try_lock(key);
std::thread::scope(|s| {
s.spawn(|| {
let key = ThreadKey::get().unwrap();
let guard = collection.try_lock(key);
assert!(guard.is_err());
});
});
assert!(guard.is_ok());
}
#[test]
fn try_read_works() {
let key = ThreadKey::get().unwrap();
let collection = RetryingLockCollection::new([RwLock::new(1), RwLock::new(2)]);
let guard = collection.try_read(key);
std::thread::scope(|s| {
s.spawn(|| {
let key = ThreadKey::get().unwrap();
let guard = collection.try_read(key);
assert!(guard.is_ok());
});
});
assert!(guard.is_ok());
}
#[test]
fn try_read_fails_for_locked_collection() {
let key = ThreadKey::get().unwrap();
let mutexes = [RwLock::new(24), RwLock::new(42)];
let collection = RetryingLockCollection::new_ref(&mutexes);
std::thread::scope(|s| {
s.spawn(|| {
let key = ThreadKey::get().unwrap();
let guard = mutexes[1].write(key);
assert_eq!(*guard, 42);
std::mem::forget(guard);
});
});
let guard = collection.try_read(key);
assert!(guard.is_err());
}
#[test]
fn locks_all_inner_mutexes() {
let key = ThreadKey::get().unwrap();
let mutex1 = Mutex::new(0);
let mutex2 = Mutex::new(0);
let collection = RetryingLockCollection::try_new([&mutex1, &mutex2]).unwrap();
let guard = collection.lock(key);
assert!(mutex1.is_locked());
assert!(mutex2.is_locked());
drop(guard);
}
#[test]
fn locks_all_inner_rwlocks() {
let key = ThreadKey::get().unwrap();
let rwlock1 = RwLock::new(0);
let rwlock2 = RwLock::new(0);
let collection = RetryingLockCollection::try_new([&rwlock1, &rwlock2]).unwrap();
let guard = collection.read(key);
assert!(rwlock1.is_locked());
assert!(rwlock2.is_locked());
drop(guard);
}
#[test]
fn works_with_other_collections() {
let key = ThreadKey::get().unwrap();
let mutex1 = Mutex::new(0);
let mutex2 = Mutex::new(0);
let collection = BoxedLockCollection::try_new(
RetryingLockCollection::try_new([&mutex1, &mutex2]).unwrap(),
)
.unwrap();
let guard = collection.lock(key);
assert!(mutex1.is_locked());
assert!(mutex2.is_locked());
drop(guard);
}
#[test]
fn from_iterator() {
let key = ThreadKey::get().unwrap();
let collection: RetryingLockCollection<Vec<Mutex<&str>>> =
[Mutex::new("foo"), Mutex::new("bar"), Mutex::new("baz")]
.into_iter()
.collect();
let guard = collection.lock(key);
assert_eq!(*guard[0], "foo");
assert_eq!(*guard[1], "bar");
assert_eq!(*guard[2], "baz");
}
#[test]
fn into_owned_iterator() {
let collection = RetryingLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]);
for (i, mutex) in collection.into_iter().enumerate() {
assert_eq!(mutex.into_inner(), i);
}
}
#[test]
fn into_ref_iterator() {
let mut key = ThreadKey::get().unwrap();
let collection = RetryingLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]);
for (i, mutex) in (&collection).into_iter().enumerate() {
mutex.scoped_lock(&mut key, |val| assert_eq!(*val, i))
}
}
#[test]
fn ref_iterator() {
let mut key = ThreadKey::get().unwrap();
let collection = RetryingLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]);
for (i, mutex) in collection.iter().enumerate() {
mutex.scoped_lock(&mut key, |val| assert_eq!(*val, i))
}
}
#[test]
fn mut_iterator() {
let mut key = ThreadKey::get().unwrap();
let mut collection =
RetryingLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]);
for (i, mutex) in collection.iter_mut().enumerate() {
mutex.scoped_lock(&mut key, |val| assert_eq!(*val, i))
}
}
#[test]
fn extend_collection() {
let mutex1 = Mutex::new(0);
let mutex2 = Mutex::new(0);
let mut collection = RetryingLockCollection::new(vec![mutex1]);
collection.extend([mutex2]);
assert_eq!(collection.into_inner().len(), 2);
}
#[test]
fn lock_empty_lock_collection() {
let key = ThreadKey::get().unwrap();
let collection: RetryingLockCollection<[RwLock<i32>; 0]> = RetryingLockCollection::new([]);
let guard = collection.lock(key);
assert!(guard.is_empty());
let key = RetryingLockCollection::<[RwLock<_>; 0]>::unlock(guard);
let guard = collection.read(key);
assert!(guard.is_empty());
}
#[test]
fn read_empty_lock_collection() {
let key = ThreadKey::get().unwrap();
let collection: RetryingLockCollection<[RwLock<i32>; 0]> = RetryingLockCollection::new([]);
let guard = collection.read(key);
assert!(guard.is_empty());
let key = RetryingLockCollection::<[RwLock<_>; 0]>::unlock_read(guard);
let guard = collection.lock(key);
assert!(guard.is_empty());
}
#[test]
fn as_ref_works() {
let mutexes = [Mutex::new(0), Mutex::new(1)];
let collection = RetryingLockCollection::new_ref(&mutexes);
assert!(std::ptr::addr_eq(&raw const mutexes, collection.as_ref()))
}
#[test]
fn as_mut_works() {
let mut mutexes = [Mutex::new(0), Mutex::new(1)];
let mut collection = RetryingLockCollection::new(&mut mutexes);
collection.as_mut()[0] = Mutex::new(42);
assert_eq!(*collection.as_mut()[0].get_mut(), 42);
}
#[test]
fn child() {
let mutexes = [Mutex::new(0), Mutex::new(1)];
let collection = RetryingLockCollection::new_ref(&mutexes);
assert!(std::ptr::addr_eq(&raw const mutexes, *collection.child()))
}
#[test]
fn child_mut_works() {
let mut mutexes = [Mutex::new(0), Mutex::new(1)];
let mut collection = RetryingLockCollection::new(&mut mutexes);
collection.child_mut()[0] = Mutex::new(42);
assert_eq!(*collection.child_mut()[0].get_mut(), 42);
}
#[test]
fn into_child_works() {
let mutexes = [Mutex::new(0), Mutex::new(1)];
let mut collection = RetryingLockCollection::new(mutexes);
collection.child_mut()[0] = Mutex::new(42);
assert_eq!(
*collection
.into_child()
.as_mut()
.get_mut(0)
.unwrap()
.get_mut(),
42
);
}
}