summaryrefslogtreecommitdiff
path: root/src/rwlock
diff options
context:
space:
mode:
authorMica White <botahamec@gmail.com>2024-12-25 17:58:06 -0500
committerMica White <botahamec@gmail.com>2024-12-25 17:58:06 -0500
commit37ab873d21ca1fcd43db8d6a26d5bac4f5285f71 (patch)
tree987e0a0604c29ceea8d17e424df65993608c7ea5 /src/rwlock
parentbfdbf20a813bb4b5527a3d6ff4a5c1bac134b466 (diff)
Move some logic into the Sharable trait
Diffstat (limited to 'src/rwlock')
-rw-r--r--src/rwlock/read_lock.rs29
-rw-r--r--src/rwlock/rwlock.rs20
-rw-r--r--src/rwlock/write_lock.rs18
3 files changed, 56 insertions, 11 deletions
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<T: Send, R: RawRwLock + Send + Sync> 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<T: Send, R: RawRwLock + Send + Sync> 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<T: ?Sized + Debug, R: RawRwLock> 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<T: Send, R: RawRwLock + Send + Sync> Lockable for RwLock<T, R> {
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<T: Send, R: RawRwLock + Send + Sync> Lockable for RwLock<T, R> {
unsafe fn guard(&self) -> Self::Guard<'_> {
RwLockWriteRef::new(self)
}
-
- unsafe fn read_guard(&self) -> Self::ReadGuard<'_> {
- RwLockReadRef::new(self)
- }
}
impl<T: Send, R: RawRwLock + Send + Sync> LockableIntoInner for RwLock<T, R> {
@@ -116,7 +107,16 @@ impl<T: Send, R: RawRwLock + Send + Sync> LockableAsMut for RwLock<T, R> {
}
}
-unsafe impl<T: Send, R: RawRwLock + Send + Sync> Sharable for RwLock<T, R> {}
+unsafe impl<T: Send, R: RawRwLock + Send + Sync> Sharable for RwLock<T, R> {
+ type ReadGuard<'g>
+ = RwLockReadRef<'g, T, R>
+ where
+ Self: 'g;
+
+ unsafe fn read_guard(&self) -> Self::ReadGuard<'_> {
+ RwLockReadRef::new(self)
+ }
+}
unsafe impl<T: Send, R: RawRwLock + Send + Sync> OwnedLockable for RwLock<T, R> {}
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<T: Send, R: RawRwLock + Send + Sync> 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<T: ?Sized + Debug, R: RawRwLock> Debug for WriteLock<'_, T, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {