diff options
Diffstat (limited to 'tokio/src/park')
-rw-r--r-- | tokio/src/park/either.rs | 2 | ||||
-rw-r--r-- | tokio/src/park/mod.rs | 11 | ||||
-rw-r--r-- | tokio/src/park/thread.rs | 25 |
3 files changed, 29 insertions, 9 deletions
diff --git a/tokio/src/park/either.rs b/tokio/src/park/either.rs index c66d1213..ee02ec15 100644 --- a/tokio/src/park/either.rs +++ b/tokio/src/park/either.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "full"), allow(dead_code))] + use crate::park::{Park, Unpark}; use std::fmt; diff --git a/tokio/src/park/mod.rs b/tokio/src/park/mod.rs index 4085a99a..e4b97512 100644 --- a/tokio/src/park/mod.rs +++ b/tokio/src/park/mod.rs @@ -34,15 +34,12 @@ //! * `park_timeout` does the same as `park` but allows specifying a maximum //! time to block the thread for. -cfg_resource_drivers! { - mod either; - pub(crate) use self::either::Either; +cfg_rt_core! { + pub(crate) mod either; } -mod thread; -pub(crate) use self::thread::ParkThread; - -pub(crate) use self::thread::{CachedParkThread, ParkError}; +#[cfg(any(feature = "rt-core", feature = "rt-util", feature = "sync"))] +pub(crate) mod thread; use std::sync::Arc; use std::time::Duration; diff --git a/tokio/src/park/thread.rs b/tokio/src/park/thread.rs index 494c02b4..2725e456 100644 --- a/tokio/src/park/thread.rs +++ b/tokio/src/park/thread.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "full"), allow(dead_code))] + use crate::loom::sync::atomic::AtomicUsize; use crate::loom::sync::{Arc, Condvar, Mutex}; use crate::park::{Park, Unpark}; @@ -212,10 +214,10 @@ impl Unpark for UnparkThread { } } +use std::future::Future; use std::marker::PhantomData; -use std::rc::Rc; - use std::mem; +use std::rc::Rc; use std::task::{RawWaker, RawWakerVTable, Waker}; /// Blocks the current thread using a condition variable. @@ -246,6 +248,25 @@ impl CachedParkThread { { CURRENT_PARKER.try_with(|inner| f(inner)).map_err(|_| ()) } + + pub(crate) fn block_on<F: Future>(&mut self, f: F) -> Result<F::Output, ParkError> { + use std::task::Context; + use std::task::Poll::Ready; + + // `get_unpark()` should not return a Result + let waker = self.get_unpark()?.into_waker(); + let mut cx = Context::from_waker(&waker); + + pin!(f); + + loop { + if let Ready(v) = crate::coop::budget(|| f.as_mut().poll(&mut cx)) { + return Ok(v); + } + + self.park()?; + } + } } impl Park for CachedParkThread { |