From 633b2c6336232d0854fd735a6499305b9185c6d1 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Mon, 28 Oct 2024 20:30:27 -0700 Subject: [PATCH] bugfix: Fix missed notification in Mutex example I forgot to run notify() when I rewrote this example without unsafe code. It looks like it prevents deadlocks in this mutex implementation. I believe this fixes #143. Signed-off-by: John Nunley --- examples/mutex.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/mutex.rs b/examples/mutex.rs index 30fbe66..873c8bb 100644 --- a/examples/mutex.rs +++ b/examples/mutex.rs @@ -37,7 +37,10 @@ mod example { /// Attempts to acquire a lock. fn try_lock(&self) -> Option> { - self.data.try_lock().map(MutexGuard) + self.data.try_lock().map(|l| MutexGuard { + lock_ops: &self.lock_ops, + locked: Some(l), + }) } /// Blocks until a lock is acquired. @@ -107,19 +110,29 @@ mod example { } /// A guard holding a lock. - struct MutexGuard<'a, T>(Locked<'a, T>); + struct MutexGuard<'a, T> { + lock_ops: &'a Event, + locked: Option>, + } impl Deref for MutexGuard<'_, T> { type Target = T; fn deref(&self) -> &T { - &self.0 + self.locked.as_deref().unwrap() } } impl DerefMut for MutexGuard<'_, T> { fn deref_mut(&mut self) -> &mut T { - &mut self.0 + self.locked.as_deref_mut().unwrap() + } + } + + impl Drop for MutexGuard<'_, T> { + fn drop(&mut self) { + self.locked = None; + self.lock_ops.notify(1); } }