Upgrade wait_timeout to use Notify
Good news, not a depricated api, bad news I'm not really sure my
adaptation here is correct. It seems to run ok and I think that it's
trivially correct because we're only running one future at a time.
-/// Copied from here https://gist.github.com/alexcrichton/871b5bf058a2ce77ac4fedccf3fda9c9
-/// turns out poll with timeout is quite complex, turned off warnings becuase we've already
-/// scoped these as futures01
+//! Copied from here https://gist.github.com/alexcrichton/871b5bf058a2ce77ac4fedccf3fda9c9
+//! turns out poll with timeout is quite complex. It has also been adapted by myself to use
+//! notify without a really good understanding of the internalls. The park/unpark version
+//! paniced about every 24 hours this version is not correct I don't think for arbitrary futures
+//! but is correct for our trivial case of a single future on a thread. We'll see how it holds up
+use futures01::executor::Notify;
use futures01::executor::{self, Spawn};
-#[allow(deprecated)]
-use futures01::task::Unpark;
use futures01::{Async, Future};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
@@ -19,15 +20,14 @@ pub enum WaitResult<F: Future> {
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()));
+ let thread = Arc::new(ThreadNotify::new(thread::current()));
loop {
let cur = Instant::now();
if cur >= now + dur {
return WaitResult::TimedOut(task);
}
- #[allow(deprecated)]
- match task.poll_future(thread.clone()) {
+ match task.poll_future_notify(&thread, 0) {
Ok(Async::Ready(e)) => return WaitResult::Ok(e),
Ok(Async::NotReady) => {}
Err(e) => return WaitResult::Err(e),
@@ -36,14 +36,14 @@ pub fn wait_timeout<F: Future>(f: F, dur: Duration) -> WaitResult<F> {
thread.park(now + dur - cur);
}
- struct ThreadUnpark {
+ struct ThreadNotify {
thread: thread::Thread,
ready: AtomicBool,
}
- impl ThreadUnpark {
- fn new(thread: thread::Thread) -> ThreadUnpark {
- ThreadUnpark {
+ impl ThreadNotify {
+ fn new(thread: thread::Thread) -> ThreadNotify {
+ ThreadNotify {
thread,
ready: AtomicBool::new(false),
}
@@ -56,9 +56,8 @@ pub fn wait_timeout<F: Future>(f: F, dur: Duration) -> WaitResult<F> {
}
}
- #[allow(deprecated)]
- impl Unpark for ThreadUnpark {
- fn unpark(&self) {
+ impl Notify for ThreadNotify {
+ fn notify(&self, _id: usize) {
self.ready.store(true, Ordering::SeqCst);
self.thread.unpark()
}
