From 37ab873d21ca1fcd43db8d6a26d5bac4f5285f71 Mon Sep 17 00:00:00 2001 From: Mica White Date: Wed, 25 Dec 2024 17:58:06 -0500 Subject: Move some logic into the Sharable trait --- src/rwlock/read_lock.rs | 29 +++++++++++++++++++++++++++++ src/rwlock/rwlock.rs | 20 ++++++++++---------- src/rwlock/write_lock.rs | 18 +++++++++++++++++- 3 files changed, 56 insertions(+), 11 deletions(-) (limited to 'src/rwlock') diff --git a/src/rwlock/read_lock.rs b/src/rwlock/read_lock.rs index e518cf3..34e3f5e 100644 --- a/src/rwlock/read_lock.rs +++ b/src/rwlock/read_lock.rs @@ -3,9 +3,38 @@ use std::fmt::Debug; use lock_api::RawRwLock; use crate::key::Keyable; +use crate::lockable::{Lockable, RawLock, Sharable}; use super::{ReadLock, RwLock, RwLockReadGuard, RwLockReadRef}; +unsafe impl Lockable for ReadLock<'_, T, R> { + type Guard<'g> + = RwLockReadRef<'g, T, R> + where + Self: 'g; + + fn get_ptrs<'a>(&'a self, ptrs: &mut Vec<&'a dyn RawLock>) { + ptrs.push(self.as_ref()); + } + + unsafe fn guard(&self) -> Self::Guard<'_> { + RwLockReadRef::new(self.as_ref()) + } +} + +// Technically, the exclusive locks can also be shared, but there's currently +// no way to express that. I don't think I want to ever express that. +unsafe impl Sharable for ReadLock<'_, T, R> { + type ReadGuard<'g> + = RwLockReadRef<'g, T, R> + where + Self: 'g; + + unsafe fn read_guard(&self) -> Self::Guard<'_> { + RwLockReadRef::new(self.as_ref()) + } +} + impl Debug for ReadLock<'_, T, R> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // safety: this is just a try lock, and the value is dropped diff --git a/src/rwlock/rwlock.rs b/src/rwlock/rwlock.rs index b5dea75..2c9ba22 100644 --- a/src/rwlock/rwlock.rs +++ b/src/rwlock/rwlock.rs @@ -79,11 +79,6 @@ unsafe impl Lockable for RwLock { where Self: 'g; - type ReadGuard<'g> - = RwLockReadRef<'g, T, R> - where - Self: 'g; - fn get_ptrs<'a>(&'a self, ptrs: &mut Vec<&'a dyn RawLock>) { ptrs.push(self); } @@ -91,10 +86,6 @@ unsafe impl Lockable for RwLock { unsafe fn guard(&self) -> Self::Guard<'_> { RwLockWriteRef::new(self) } - - unsafe fn read_guard(&self) -> Self::ReadGuard<'_> { - RwLockReadRef::new(self) - } } impl LockableIntoInner for RwLock { @@ -116,7 +107,16 @@ impl LockableAsMut for RwLock { } } -unsafe impl Sharable for RwLock {} +unsafe impl Sharable for RwLock { + type ReadGuard<'g> + = RwLockReadRef<'g, T, R> + where + Self: 'g; + + unsafe fn read_guard(&self) -> Self::ReadGuard<'_> { + RwLockReadRef::new(self) + } +} unsafe impl OwnedLockable for RwLock {} diff --git a/src/rwlock/write_lock.rs b/src/rwlock/write_lock.rs index ba05e87..27fa1d2 100644 --- a/src/rwlock/write_lock.rs +++ b/src/rwlock/write_lock.rs @@ -3,8 +3,24 @@ use std::fmt::Debug; use lock_api::RawRwLock; use crate::key::Keyable; +use crate::lockable::{Lockable, RawLock}; -use super::{RwLock, RwLockWriteGuard, WriteLock}; +use super::{RwLock, RwLockWriteGuard, RwLockWriteRef, WriteLock}; + +unsafe impl Lockable for WriteLock<'_, T, R> { + type Guard<'g> + = RwLockWriteRef<'g, T, R> + where + Self: 'g; + + fn get_ptrs<'a>(&'a self, ptrs: &mut Vec<&'a dyn RawLock>) { + ptrs.push(self.as_ref()); + } + + unsafe fn guard(&self) -> Self::Guard<'_> { + RwLockWriteRef::new(self.as_ref()) + } +} impl Debug for WriteLock<'_, T, R> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -- cgit v1.2.3