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

use parking_lot #2

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ keywords = ["park", "notify", "thread", "wake", "condition"]
categories = ["concurrency"]
readme = "README.md"

[dependencies]
parking_lot = "0.11.0"

[dev-dependencies]
easy-parallel = "3.0.0"
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ use std::fmt;
use std::marker::PhantomData;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::{Arc, Condvar, Mutex};
use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::{Condvar, Mutex};

/// Creates a parker and an associated unparker.
///
/// # Examples
Expand Down Expand Up @@ -280,7 +282,7 @@ impl Inner {
}

// Otherwise we need to coordinate going to sleep.
let mut m = self.lock.lock().unwrap();
let mut m = self.lock.lock();

match self.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
Ok(_) => {}
Expand All @@ -302,9 +304,13 @@ impl Inner {
None => {
loop {
// Block the current thread on the conditional variable.
m = self.cvar.wait(m).unwrap();
self.cvar.wait(&mut m);

if self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
if self
.state
.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst)
.is_ok()
{
// got a notification
return true;
}
Expand All @@ -314,7 +320,7 @@ impl Inner {
// Wait with a timeout, and if we spuriously wake up or otherwise wake up from a
// notification we just want to unconditionally set `state` back to `EMPTY`, either
// consuming a notification or un-flagging ourselves as parked.
let (_m, _result) = self.cvar.wait_timeout(m, timeout).unwrap();
let _result = self.cvar.wait_for(&mut m, timeout);

match self.state.swap(EMPTY, SeqCst) {
NOTIFIED => true, // got a notification
Expand Down Expand Up @@ -345,7 +351,7 @@ impl Inner {
//
// Releasing `lock` before the call to `notify_one` means that when the parked thread wakes
// it doesn't get woken only to have to wait for us to release `lock`.
drop(self.lock.lock().unwrap());
drop(self.lock.lock());
self.cvar.notify_one();
true
}
Expand Down