Skip to content

Commit

Permalink
Auto merge of rust-lang#33748 - emilio:mpsc-recv-timeout, r=alexcrichton
Browse files Browse the repository at this point in the history
std: sync: Implement recv_timeout()

This is an attempt to implement rust-lang/rfcs#962.

I'm not sure about if a change like this would require an rfc or something like
that, and this surely needs a lot more testing, but I wanted to take some eyes
on it before following.

cc @metajack @asajeffrey servo/servo#11279 servo/servo#11283

r? @aturon
  • Loading branch information
bors authored Jun 22, 2016
2 parents e41cdab + b94b158 commit 6dcc2c1
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 42 deletions.
14 changes: 13 additions & 1 deletion src/libstd/sync/mpsc/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use sync::Arc;
use marker::{Sync, Send};
use mem;
use clone::Clone;
use time::Instant;

struct Inner {
thread: Thread,
Expand Down Expand Up @@ -74,7 +75,6 @@ impl SignalToken {
pub unsafe fn cast_from_usize(signal_ptr: usize) -> SignalToken {
SignalToken { inner: mem::transmute(signal_ptr) }
}

}

impl WaitToken {
Expand All @@ -83,4 +83,16 @@ impl WaitToken {
thread::park()
}
}

/// Returns true if we wake up normally, false otherwise.
pub fn wait_max_until(self, end: Instant) -> bool {
while !self.inner.woken.load(Ordering::SeqCst) {
let now = Instant::now();
if now >= end {
return false;
}
thread::park_timeout(end - now)
}
true
}
}
Loading

0 comments on commit 6dcc2c1

Please sign in to comment.