From 48aaedad542b9c6cbdc85d22517cd0d151f38443 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 1 Dec 2024 15:28:44 -0500 Subject: Unit testing --- src/rwlock/rwlock.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/rwlock/rwlock.rs') diff --git a/src/rwlock/rwlock.rs b/src/rwlock/rwlock.rs index 5bff5a3..fddcf5a 100644 --- a/src/rwlock/rwlock.rs +++ b/src/rwlock/rwlock.rs @@ -5,7 +5,7 @@ use lock_api::RawRwLock; use crate::key::Keyable; -use super::{RwLock, RwLockReadGuard, RwLockReadRef, RwLockWriteGuard}; +use super::{RwLock, RwLockReadGuard, RwLockReadRef, RwLockWriteGuard, RwLockWriteRef}; impl RwLock { /// Creates a new instance of an `RwLock` which is unlocked. @@ -194,6 +194,17 @@ impl RwLock { } } + /// Attempts to create an exclusive lock without a key. Locking this + /// without exclusive access to the key is undefined behavior. + pub(crate) unsafe fn try_write_no_key(&self) -> Option> { + if self.raw.try_lock_exclusive() { + // safety: the lock is locked first + Some(RwLockWriteRef(self, PhantomData)) + } else { + None + } + } + /// Locks this `RwLock` with exclusive write access, blocking the current /// until it can be acquired. /// @@ -266,6 +277,11 @@ impl RwLock { } } + /// Returns `true` if the rwlock is currently locked in any way + pub(crate) fn is_locked(&self) -> bool { + self.raw.is_locked() + } + /// Unlocks shared access on the `RwLock`. This is undefined behavior is /// the data is still accessible. pub(super) unsafe fn force_unlock_read(&self) { -- cgit v1.2.3