diff options
Diffstat (limited to 'tokio/src/park/thread.rs')
-rw-r--r-- | tokio/src/park/thread.rs | 25 |
1 files changed, 23 insertions, 2 deletions
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 { |