Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missed notification in Mutex example #145

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions examples/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ mod example {

/// Attempts to acquire a lock.
fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
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.
Expand Down Expand Up @@ -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<Locked<'a, T>>,
}

impl<T> Deref for MutexGuard<'_, T> {
type Target = T;

fn deref(&self) -> &T {
&self.0
self.locked.as_deref().unwrap()
}
}

impl<T> DerefMut for MutexGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.0
self.locked.as_deref_mut().unwrap()
}
}

impl<T> Drop for MutexGuard<'_, T> {
fn drop(&mut self) {
self.locked = None;
self.lock_ops.notify(1);
}
}

Expand Down