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); } }