use std::marker::PhantomData;
use crate::lockable::{
Lockable, LockableGetMut, LockableIntoInner, OwnedLockable, RawLock, Sharable,
};
use crate::Keyable;
use super::{utils, LockGuard, OwnedLockCollection};
#[mutants::skip] // it's hard to test individual locks in an OwnedLockCollection
#[cfg(not(tarpaulin_include))]
fn get_locks<L: Lockable>(data: &L) -> Vec<&dyn RawLock> {
let mut locks = Vec::new();
data.get_ptrs(&mut locks);
locks
}
unsafe impl<L: Lockable> RawLock for OwnedLockCollection<L> {
#[mutants::skip] // this should never run
#[cfg(not(tarpaulin_include))]
fn poison(&self) {
let locks = get_locks(&self.data);
for lock in locks {
lock.poison();
}
}
unsafe fn raw_lock(&self) {
utils::ordered_lock(&get_locks(&self.data))
}
unsafe fn raw_try_lock(&self) -> bool {
let locks = get_locks(&self.data);
utils::ordered_try_lock(&locks)
}
unsafe fn raw_unlock(&self) {
let locks = get_locks(&self.data);
for lock in locks {
lock.raw_unlock();
}
}
unsafe fn raw_read(&self) {
utils::ordered_read(&get_locks(&self.data))
}
unsafe fn raw_try_read(&self) -> bool {
let locks = get_locks(&self.data);
utils::ordered_try_read(&locks)
}
unsafe fn raw_unlock_read(&self) {
let locks = get_locks(&self.data);
for lock in locks {
lock.raw_unlock_read();
}
}
}
unsafe impl<L: Lockable> Lockable for OwnedLockCollection<L> {
type Guard<'g>
= L::Guard<'g>
where
Self: 'g;
#[mutants::skip] // It's hard to test lkocks in an OwnedLockCollection, because they're owned
#[cfg(not(tarpaulin_include))]
fn get_ptrs<'a>(&'a self, ptrs: &mut Vec<&'a dyn RawLock>) {
self.data.get_ptrs(ptrs)
}
unsafe fn guard(&self) -> Self::Guard<'_> {
self.data.guard()
}
}
impl<L: LockableGetMut> LockableGetMut for OwnedLockCollection<L> {
type Inner<'a>
= L::Inner<'a>
where
Self: 'a;
fn get_mut(&mut self) -> Self::Inner<'_> {
self.data.get_mut()
}
}
impl<L: LockableIntoInner> LockableIntoInner for OwnedLockCollection<L> {
type Inner = L::Inner;
fn into_inner(self) -> Self::Inner {
self.data.into_inner()
}
}
unsafe impl<L: Sharable> Sharable for OwnedLockCollection<L> {
type ReadGuard<'g>
= L::ReadGuard<'g>
where
Self: 'g;
unsafe fn read_guard(&self) -> Self::ReadGuard<'_> {
self.data.read_guard()
}
}
unsafe impl<L: OwnedLockable> OwnedLockable for OwnedLockCollection<L> {}
impl<L> IntoIterator for OwnedLockCollection<L>
where
L: IntoIterator,
{
type Item = <L as IntoIterator>::Item;
type IntoIter = <L as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}
impl<L: OwnedLockable, I: FromIterator<L> + OwnedLockable> FromIterator<L>
for OwnedLockCollection<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 OwnedLockCollection<E> {
fn extend<T: IntoIterator<Item = L>>(&mut self, iter: T) {
self.data.extend(iter)
}
}
// AsRef can't be implemented because an impl of AsRef<L> for L could break the
// invariant that there is only one way to lock the collection. AsMut is fine,
// because the collection can't be locked as long as the reference is valid.
impl<T, L: AsMut<T>> AsMut<T> for OwnedLockCollection<L> {
fn as_mut(&mut self) -> &mut T {
self.data.as_mut()
}
}
impl<L: OwnedLockable + Default> Default for OwnedLockCollection<L> {
fn default() -> Self {
Self::new(L::default())
}
}
impl<L: OwnedLockable> From<L> for OwnedLockCollection<L> {
fn from(value: L) -> Self {
Self::new(value)
}
}
impl<L: OwnedLockable> OwnedLockCollection<L> {
/// Creates a new collection of owned locks.
///
/// Because the locks are owned, there's no need to do any checks for
/// duplicate values. The locks also don't need to be sorted by memory
/// address because they aren't used anywhere else.
///
/// # Examples
///
/// ```
/// use happylock::Mutex;
/// use happylock::collection::OwnedLockCollection;
///
/// let data = (Mutex::new(0), Mutex::new(""));
/// let lock = OwnedLockCollection::new(data);
/// ```
#[must_use]
pub const fn new(data: L) -> Self {
Self { data }
}
/// Locks the collection
///
/// This function returns a guard that can be used to access the underlying
/// data. When the guard is dropped, the locks in the collection are also
/// dropped.
///
/// # Examples
///
/// ```
/// use happylock::{Mutex, ThreadKey};
/// use happylock::collection::OwnedLockCollection;
///
/// let key = ThreadKey::get().unwrap();
/// let data = (Mutex::new(0), Mutex::new(""));
/// let lock = OwnedLockCollection::new(data);
///
/// let mut guard = lock.lock(key);
/// *guard.0 += 1;
/// *guard.1 = "1";
/// ```
pub fn lock<'g, 'key: 'g, Key: Keyable + 'key>(
&'g self,
key: Key,
) -> LockGuard<'key, L::Guard<'g>, Key> {
let guard = unsafe {
// safety: we have the thread key, and these locks happen in a
// predetermined order
self.raw_lock();
// safety: we've locked all of this already
self.data.guard()
};
LockGuard {
guard,
key,
_phantom: PhantomData,
}
}
/// Attempts to lock the without blocking.
///
/// If the access could not be granted at this time, then `Err` is
/// returned. Otherwise, an RAII guard is returned which will release the
/// locks when it is dropped.
///
/// # Errors
///
/// If any of the locks in this collection are already locked, this returns
/// an error containing the given key.
///
/// # Examples
///
/// ```
/// use happylock::{Mutex, ThreadKey};
/// use happylock::collection::OwnedLockCollection;
///
/// let key = ThreadKey::get().unwrap();
/// let data = (Mutex::new(0), Mutex::new(""));
/// let lock = OwnedLockCollection::new(data);
///
/// match lock.try_lock(key) {
/// Ok(mut guard) => {
/// *guard.0 += 1;
/// *guard.1 = "1";
/// },
/// Err(_) => unreachable!(),
/// };
///
/// ```
pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>(
&'g self,
key: Key,
) -> Result<LockGuard<'key, L::Guard<'g>, Key>, Key> {
let guard = unsafe {
if !self.raw_try_lock() {
return Err(key);
}
// safety: we've acquired the locks
self.data.guard()
};
Ok(LockGuard {
guard,
key,
_phantom: PhantomData,
})
}
/// Unlocks the underlying lockable data type, returning the key that's
/// associated with it.
///
/// # Examples
///
/// ```
/// use happylock::{Mutex, ThreadKey};
/// use happylock::collection::OwnedLockCollection;
///
/// let key = ThreadKey::get().unwrap();
/// let data = (Mutex::new(0), Mutex::new(""));
/// let lock = OwnedLockCollection::new(data);
///
/// let mut guard = lock.lock(key);
/// *guard.0 += 1;
/// *guard.1 = "1";
/// let key = OwnedLockCollection::<(Mutex<i32>, Mutex<&str>)>::unlock(guard);
/// ```
#[allow(clippy::missing_const_for_fn)]
pub fn unlock<'g, 'key: 'g, Key: Keyable + 'key>(
guard: LockGuard<'key, L::Guard<'g>, Key>,
) -> Key {
drop(guard.guard);
guard.key
}
}
impl<L: Sharable> OwnedLockCollection<L> {
/// Locks the collection, so that other threads can still read from it
///
/// This function returns a guard that can be used to access the underlying
/// data immutably. When the guard is dropped, the locks in the collection
/// are also dropped.
///
/// # Examples
///
/// ```
/// use happylock::{RwLock, ThreadKey};
/// use happylock::collection::OwnedLockCollection;
///
/// let key = ThreadKey::get().unwrap();
/// let data = (RwLock::new(0), RwLock::new(""));
/// let lock = OwnedLockCollection::new(data);
///
/// let mut guard = lock.read(key);
/// assert_eq!(*guard.0, 0);
/// assert_eq!(*guard.1, "");
/// ```
pub fn read<'g, 'key: 'g, Key: Keyable + 'key>(
&'g self,
key: Key,
) -> LockGuard<'key, L::ReadGuard<'g>, Key> {
unsafe {
// safety: we have the thread key
self.raw_read();
LockGuard {
// safety: we've already acquired the lock
guard: self.data.read_guard(),
key,
_phantom: PhantomData,
}
}
}
/// Attempts to lock the without blocking, in such a way that other threads
/// can still read from the collection.
///
/// If the access could not be granted at this time, then `Err` is
/// returned. Otherwise, an RAII guard is returned which will release the
/// shared access when it is dropped.
///
/// # Errors
///
/// If any of the locks in this collection can't be acquired, then an error
/// is returned containing the given key.
///
/// # Examples
///
/// ```
/// use happylock::{RwLock, ThreadKey};
/// use happylock::collection::OwnedLockCollection;
///
/// let key = ThreadKey::get().unwrap();
/// let data = (RwLock::new(5), RwLock::new("6"));
/// let lock = OwnedLockCollection::new(data);
///
/// match lock.try_read(key) {
/// Some(mut guard) => {
/// assert_eq!(*guard.0, 5);
/// assert_eq!(*guard.1, "6");
/// },
/// None => unreachable!(),
/// };
///
/// ```
pub fn try_read<'g, 'key: 'g, Key: Keyable + 'key>(
&'g self,
key: Key,
) -> Option<LockGuard<'key, L::ReadGuard<'g>, Key>> {
let guard = unsafe {
// safety: we have the thread key
if !self.raw_try_read() {
return None;
}
// safety: we've acquired the locks
self.data.read_guard()
};
Some(LockGuard {
guard,
key,
_phantom: PhantomData,
})
}
/// Unlocks the underlying lockable data type, returning the key that's
/// associated with it.
///
/// # Examples
///
/// ```
/// use happylock::{RwLock, ThreadKey};
/// use happylock::collection::OwnedLockCollection;
///
/// let key = ThreadKey::get().unwrap();
/// let data = (RwLock::new(0), RwLock::new(""));
/// let lock = OwnedLockCollection::new(data);
///
/// let mut guard = lock.read(key);
/// let key = OwnedLockCollection::<(RwLock<i32>, RwLock<&str>)>::unlock_read(guard);
/// ```
#[allow(clippy::missing_const_for_fn)]
pub fn unlock_read<'g, 'key: 'g, Key: Keyable + 'key>(
guard: LockGuard<'key, L::ReadGuard<'g>, Key>,
) -> Key {
drop(guard.guard);
guard.key
}
}
impl<L> OwnedLockCollection<L> {
/// Gets the underlying collection, consuming this collection.
///
/// # Examples
///
/// ```
/// use happylock::{Mutex, ThreadKey};
/// use happylock::collection::OwnedLockCollection;
///
/// let data = (Mutex::new(42), Mutex::new(""));
/// let lock = OwnedLockCollection::new(data);
///
/// let key = ThreadKey::get().unwrap();
/// let inner = lock.into_child();
/// let guard = inner.0.lock(key);
/// assert_eq!(*guard, 42);
/// ```
#[must_use]
pub fn into_child(self) -> L {
self.data
}
/// Gets a mutable reference to the underlying collection.
///
/// # Examples
///
/// ```
/// use happylock::{Mutex, ThreadKey};
/// use happylock::collection::OwnedLockCollection;
///
/// let data = (Mutex::new(42), Mutex::new(""));
/// let mut lock = OwnedLockCollection::new(data);
///
/// let key = ThreadKey::get().unwrap();
/// let mut inner = lock.child_mut();
/// let guard = inner.0.get_mut();
/// assert_eq!(*guard, 42);
/// ```
#[must_use]
pub fn child_mut(&mut self) -> &mut L {
&mut self.data
}
}
impl<L: LockableGetMut> OwnedLockCollection<L> {
/// Gets a mutable reference to the data behind this `OwnedLockCollection`.
///
/// Since this call borrows the `OwnedLockCollection` mutably, no actual
/// locking needs to take place - the mutable borrow statically guarantees
/// no locks exist.
///
/// # Examples
///
/// ```
/// use happylock::{Mutex, LockCollection};
/// use happylock::collection::OwnedLockCollection;
///
/// let mut mutex = OwnedLockCollection::new([Mutex::new(0), Mutex::new(0)]);
/// assert_eq!(mutex.get_mut(), [&mut 0, &mut 0]);
/// ```
pub fn get_mut(&mut self) -> L::Inner<'_> {
LockableGetMut::get_mut(self)
}
}
impl<L: LockableIntoInner> OwnedLockCollection<L> {
/// Consumes this `OwnedLockCollection`, returning the underlying data.
///
/// # Examples
///
/// ```
/// use happylock::{Mutex, LockCollection};
/// use happylock::collection::OwnedLockCollection;
///
/// let mutex = OwnedLockCollection::new([Mutex::new(0), Mutex::new(0)]);
/// assert_eq!(mutex.into_inner(), [0, 0]);
/// ```
#[must_use]
pub fn into_inner(self) -> L::Inner {
LockableIntoInner::into_inner(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Mutex;
#[test]
fn can_be_extended() {
let mutex1 = Mutex::new(0);
let mutex2 = Mutex::new(1);
let mut collection = OwnedLockCollection::new(vec![mutex1, mutex2]);
collection.extend([Mutex::new(2)]);
assert_eq!(collection.data.len(), 3);
}
}
|