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

Adding wait_while to the loom Condvar. #167

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions src/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ impl Condvar {
Ok(guard)
}

/// Blocks the current thread until this condition variable receives a
/// notification and the given condition returns false.
Comment on lines +41 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, take it or leave it: we could just copy and paste the entire doc comment from std::sync::Condvar here, since the API is identical. But, it doesn't really matter (and it looks like other methods on our mock Condvar don't reproduce the entire std comment...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but yeah. It didn't seem like that would fit in with the existing comments that were there.

pub fn wait_while<'a, T, F>(
&self,
mut guard: MutexGuard<'a, T>,
mut condition: F,
) -> LockResult<MutexGuard<'a, T>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could elide these?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I tried. Eliding the lifetime completely gives a warning because it’s part of the MutexGuard struct. Trying to make it anonymous gives a complete error because it tries to use the same lifetime as &self.

Also messing with signature seems like a bad idea since we’re trying match what’s in std.

where
F: FnMut(&mut T) -> bool,
{
while condition(&mut *guard) {
guard = self.wait(guard)?;
}
Ok(guard)
}

/// Waits on this condition variable for a notification, timing out after a
/// specified duration.
pub fn wait_timeout<'a, T>(
Expand Down
26 changes: 26 additions & 0 deletions tests/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ fn notify_all() {
});
}

#[test]
fn wait_while() {
loom::model(|| {
let inc = Arc::new(Inc::new());

let j = {
let inc = inc.clone();
thread::spawn(move || inc.wait_for_1())
};

thread::spawn(move || inc.inc()).join().expect("inc");

j.join().expect("waiter")
});
}

struct Inc {
num: AtomicUsize,
mutex: Mutex<()>,
Expand Down Expand Up @@ -70,6 +86,16 @@ impl Inc {
}
}

fn wait_for_1(&self) {
let guard = self.mutex.lock().unwrap();

drop(
self.condvar
.wait_while(guard, |_| self.num.load(SeqCst) < 1)
.unwrap(),
);
}

fn inc(&self) {
self.num.store(1, SeqCst);
drop(self.mutex.lock().unwrap());
Expand Down