From cbdce939291800f297acc700e02db8b6798239ce Mon Sep 17 00:00:00 2001 From: Mica White Date: Mon, 23 Dec 2024 12:54:01 -0500 Subject: Utilize mutex death --- src/rwlock/rwlock.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'src/rwlock/rwlock.rs') diff --git a/src/rwlock/rwlock.rs b/src/rwlock/rwlock.rs index 86005e3..94c6062 100644 --- a/src/rwlock/rwlock.rs +++ b/src/rwlock/rwlock.rs @@ -21,6 +21,18 @@ unsafe impl RawLock for RwLock { "The read-write lock has been killed" ); + scopeguard::defer_on_unwind! { + scopeguard::defer_on_unwind! { self.kill() }; + if self.raw_try_lock() { + self.raw_unlock(); + } else { + // We don't know whether this lock is locked by the current + // thread, or another thread. There's not much we can do other + // than kill it. + self.kill(); + } + } + self.raw.lock_exclusive() } @@ -29,10 +41,34 @@ unsafe impl RawLock for RwLock { return false; } + scopeguard::defer_on_unwind! { + scopeguard::defer_on_unwind! { self.kill() }; + if self.raw_try_lock() { + self.raw_unlock(); + } else { + // We don't know whether this lock is locked by the current + // thread, or another thread. There's not much we can do other + // than kill it. + self.kill(); + } + } + self.raw.try_lock_exclusive() } unsafe fn raw_unlock(&self) { + scopeguard::defer_on_unwind! { + scopeguard::defer_on_unwind! { self.kill() }; + if self.raw_try_lock() { + self.raw_unlock(); + } else { + // We don't know whether this lock is locked by the current + // thread, or another thread. There's not much we can do other + // than kill it. + self.kill(); + } + } + self.raw.unlock_exclusive() } @@ -42,6 +78,18 @@ unsafe impl RawLock for RwLock { "The read-write lock has been killed" ); + scopeguard::defer_on_unwind! { + scopeguard::defer_on_unwind! { self.kill() }; + if self.raw_try_read() { + self.raw_unlock_read(); + } else { + // We don't know whether this lock is locked by the current + // thread, or another thread. There's not much we can do other + // than kill it. + self.kill(); + } + } + self.raw.lock_shared() } @@ -50,10 +98,34 @@ unsafe impl RawLock for RwLock { return false; } + scopeguard::defer_on_unwind! { + scopeguard::defer_on_unwind! { self.kill() }; + if self.raw_try_read() { + self.raw_unlock_read(); + } else { + // We don't know whether this lock is locked by the current + // thread, or another thread. There's not much we can do other + // than kill it. + self.kill(); + } + } + self.raw.try_lock_shared() } unsafe fn raw_unlock_read(&self) { + scopeguard::defer_on_unwind! { + scopeguard::defer_on_unwind! { self.kill() }; + if self.raw_try_read() { + self.raw_unlock_read(); + } else { + // We don't know whether this lock is locked by the current + // thread, or another thread. There's not much we can do other + // than kill it. + self.kill(); + } + } + self.raw.unlock_shared() } } -- cgit v1.2.3