Skip to content

Commit

Permalink
Remove UnparkReason to preserve compatibility.
Browse files Browse the repository at this point in the history
`UnparkReason` implementation preserved in Lucretiel/crossbeam/unpark-reason
  • Loading branch information
Lucretiel committed Nov 28, 2020
1 parent 38cd5b3 commit 927c3f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
2 changes: 1 addition & 1 deletion crossbeam-utils/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ mod parker;
mod sharded_lock;
mod wait_group;

pub use self::parker::{Parker, UnparkReason, Unparker};
pub use self::parker::{Parker, Unparker};
pub use self::sharded_lock::{ShardedLock, ShardedLockReadGuard, ShardedLockWriteGuard};
pub use self::wait_group::WaitGroup;
31 changes: 9 additions & 22 deletions crossbeam-utils/src/sync/parker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Parker {
/// // Waits for the token to become available, but will not wait longer than 500 ms.
/// p.park_timeout(Duration::from_millis(500));
/// ```
pub fn park_timeout(&self, timeout: Duration) -> UnparkReason {
pub fn park_timeout(&self, timeout: Duration) {
self.park_deadline(Instant::now() + timeout)
}

Expand All @@ -138,7 +138,7 @@ impl Parker {
/// // Waits for the token to become available, but will not wait longer than 500 ms.
/// p.park_deadline(deadline);
/// ```
pub fn park_deadline(&self, deadline: Instant) -> UnparkReason {
pub fn park_deadline(&self, deadline: Instant) {
self.unparker.inner.park(Some(deadline))
}

Expand Down Expand Up @@ -301,18 +301,6 @@ impl Clone for Unparker {
}
}

/// An enum that reports whether a `Parker::park_timeout` or
/// `Parker::park_deadline` returned because another thread called `unpark` or
/// because of a timeout.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnparkReason {
/// The park method returned due to a call to `unpark`.
Unparked,

/// The park method returned due to a timeout.
Timeout,
}

const EMPTY: usize = 0;
const PARKED: usize = 1;
const NOTIFIED: usize = 2;
Expand All @@ -324,20 +312,20 @@ struct Inner {
}

impl Inner {
fn park(&self, deadline: Option<Instant>) -> UnparkReason {
fn park(&self, deadline: Option<Instant>) {
// If we were previously notified then we consume this notification and return quickly.
if self
.state
.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst)
.is_ok()
{
return UnparkReason::Unparked;
return;
}

// If the timeout is zero, then there is no need to actually block.
if let Some(deadline) = deadline {
if deadline <= Instant::now() {
return UnparkReason::Timeout;
return;
}
}

Expand All @@ -355,7 +343,7 @@ impl Inner {
// do that we must read from the write it made to `state`.
let old = self.state.swap(EMPTY, SeqCst);
assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
return UnparkReason::Unparked;
return;
}
Err(n) => panic!("inconsistent park_timeout state: {}", n),
}
Expand All @@ -373,9 +361,8 @@ impl Inner {
self.cvar.wait_timeout(m, deadline - now).unwrap().0
} else {
// We've timed out; swap out the state back to empty on our way out
return match self.state.swap(EMPTY, SeqCst) {
NOTIFIED => UnparkReason::Unparked, // got a notification
PARKED => UnparkReason::Timeout, // no notification
match self.state.swap(EMPTY, SeqCst) {
NOTIFIED | PARKED => return,
n => panic!("inconsistent park_timeout state: {}", n),
};
}
Expand All @@ -388,7 +375,7 @@ impl Inner {
.is_ok()
{
// got a notification
return UnparkReason::Unparked;
return;
}

// Spurious wakeup, go back to sleep. Alternatively, if we timed out, it will be caught
Expand Down
17 changes: 4 additions & 13 deletions crossbeam-utils/tests/parker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@ use std::thread::sleep;
use std::time::Duration;
use std::u32;

use crossbeam_utils::sync::{Parker, UnparkReason};
use crossbeam_utils::sync::Parker;
use crossbeam_utils::thread;

#[test]
fn park_timeout_unpark_before() {
let p = Parker::new();
for _ in 0..10 {
p.unparker().unpark();
assert_eq!(
p.park_timeout(Duration::from_millis(u32::MAX as u64)),
UnparkReason::Unparked,
);
p.park_timeout(Duration::from_millis(u32::MAX as u64));
}
}

#[test]
fn park_timeout_unpark_not_called() {
let p = Parker::new();
for _ in 0..10 {
assert_eq!(
p.park_timeout(Duration::from_millis(10)),
UnparkReason::Timeout,
);
p.park_timeout(Duration::from_millis(10))
}
}

Expand All @@ -40,10 +34,7 @@ fn park_timeout_unpark_called_other_thread() {
u.unpark();
});

assert_eq!(
p.park_timeout(Duration::from_millis(u32::MAX as u64)),
UnparkReason::Unparked,
);
p.park_timeout(Duration::from_millis(u32::MAX as u64))
})
.unwrap();
}
Expand Down

0 comments on commit 927c3f8

Please sign in to comment.