Created
January 26, 2017 00:06
-
-
Save alexcrichton/871b5bf058a2ce77ac4fedccf3fda9c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate futures; | |
use std::sync::atomic::{AtomicBool, Ordering}; | |
use std::thread; | |
use std::sync::Arc; | |
use std::time::{Instant, Duration}; | |
use futures::{Async, Future}; | |
use futures::executor::{self, Spawn}; | |
use futures::task::Unpark; | |
enum WaitResult<F: Future> { | |
Ok(F::Item), | |
Err(F::Error), | |
TimedOut(Spawn<F>), | |
} | |
pub fn wait_timeout<F: Future>(f: F, dur: Duration) -> WaitResult<F> { | |
let now = Instant::now(); | |
let mut task = executor::spawn(f); | |
let thread = Arc::new(ThreadUnpark::new(thread::current())); | |
loop { | |
let cur = Instant::now(); | |
if cur >= now + dur { | |
return WaitResult::TimedOut(task) | |
} | |
match task.poll_future(thread.clone()) { | |
Ok(Async::Ready(e)) => return WaitResult::Ok(e), | |
Ok(Async::NotReady) => {} | |
Err(e) => return WaitResult::Err(e), | |
} | |
thread.park(now + dur - cur); | |
} | |
struct ThreadUnpark { | |
thread: thread::Thread, | |
ready: AtomicBool, | |
} | |
impl ThreadUnpark { | |
fn new(thread: thread::Thread) -> ThreadUnpark { | |
ThreadUnpark { | |
thread: thread, | |
ready: AtomicBool::new(false), | |
} | |
} | |
fn park(&self, dur: Duration) { | |
if !self.ready.swap(false, Ordering::SeqCst) { | |
thread::park_timeout(dur); | |
} | |
} | |
} | |
impl Unpark for ThreadUnpark { | |
fn unpark(&self) { | |
self.ready.store(true, Ordering::SeqCst); | |
self.thread.unpark() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was really helpful and thanks for posting it. One patch needed -
WaitResult
needspub
to be exported bywait_timeout
.