diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-11-22 18:46:57 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-11-22 12:46:57 -0500 |
commit | c6bb3d5a10ba8acceadcaa66050abcaefb7bc0bb (patch) | |
tree | e60f007215fc0f146db4457db85eda8f4716d314 /cli/tokio_util.rs | |
parent | 363b968bfcef26c30f84e485beec6194e5b1dd98 (diff) |
remove tokio_util::block_on (#3388)
This PR removes tokio_util::block_on - refactored compiler and file
fetcher slightly so that we can safely block there - that's because
only blocking path consist of only synchronous operations.
Additionally I removed excessive use of tokio_util::panic_on_error
and tokio_util::run_in_task and moved both functions to cli/worker.rs,
to tests module.
Closes #2960
Diffstat (limited to 'cli/tokio_util.rs')
-rw-r--r-- | cli/tokio_util.rs | 64 |
1 files changed, 0 insertions, 64 deletions
diff --git a/cli/tokio_util.rs b/cli/tokio_util.rs index 050080b70..fea47792e 100644 --- a/cli/tokio_util.rs +++ b/cli/tokio_util.rs @@ -1,5 +1,4 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -use deno::ErrBox; use futures; use futures::future::FutureExt; use futures::future::TryFutureExt; @@ -29,66 +28,3 @@ where { tokio::runtime::current_thread::run(future.boxed().compat()); } - -/// THIS IS A HACK AND SHOULD BE AVOIDED. -/// -/// This spawns a new thread and creates a single-threaded tokio runtime on that thread, -/// to execute the given future. -/// -/// This is useful when we want to block the main runtime to -/// resolve a future without worrying that we'll use up all the threads in the -/// main runtime. -pub fn block_on<F, R>(future: F) -> Result<R, ErrBox> -where - F: Send + 'static + Future<Output = Result<R, ErrBox>> + Unpin, - R: Send + 'static, -{ - use std::sync::mpsc::channel; - use std::thread; - let (sender, receiver) = channel(); - // Create a new runtime to evaluate the future asynchronously. - thread::spawn(move || { - let r = tokio::runtime::current_thread::block_on_all(future.compat()); - sender - .send(r) - .expect("Unable to send blocking future result") - }); - receiver - .recv() - .expect("Unable to receive blocking future result") -} - -// Set the default executor so we can use tokio::spawn(). It's difficult to -// pass around mut references to the runtime, so using with_default is -// preferable. Ideally Tokio would provide this function. -#[cfg(test)] -pub fn init<F>(f: F) -where - F: FnOnce(), -{ - let rt = create_threadpool_runtime().expect("Unable to create Tokio runtime"); - let mut executor = rt.executor(); - let mut enter = tokio_executor::enter().expect("Multiple executors at once"); - tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f()); -} - -pub fn panic_on_error<I, E, F>(f: F) -> impl Future<Output = Result<I, ()>> -where - F: Future<Output = Result<I, E>>, - E: std::fmt::Debug, -{ - f.map_err(|err| panic!("Future got unexpected error: {:?}", err)) -} - -#[cfg(test)] -pub fn run_in_task<F>(f: F) -where - F: FnOnce() + Send + 'static, -{ - let fut = futures::future::lazy(move |_cx| { - f(); - Ok(()) - }); - - run(fut) -} |