From 352c54bfe6701bbe782f2b80100056e497fa3e30 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Perennou Date: Tue, 15 Sep 2020 19:04:12 +0200 Subject: [PATCH] feat: move executor to async-global-executo --- Cargo.toml | 4 ++-- src/rt/mod.rs | 21 ++------------------- src/task/builder.rs | 4 ++-- src/task/executor.rs | 33 +-------------------------------- src/task/join_handle.rs | 2 +- 5 files changed, 8 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6f6cfe4b..00a23845 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ rustdoc-args = ["--cfg", "feature=\"docs\""] [features] default = [ "std", - "async-executor", + "async-global-executor", "async-io", "async-task", "blocking", @@ -80,7 +80,7 @@ slab = { version = "0.4.2", optional = true } surf = { version = "1.0.3", optional = true } [target.'cfg(not(target_os = "unknown"))'.dependencies] -async-executor = { version = "1.0.0", optional = true } +async-global-executor = { version = "1.0.1", optional = true, features = ["async-io"] } async-io = { version = "1.0.1", optional = true } blocking = { version = "1.0.0", optional = true } futures-lite = { version = "1.0.0", optional = true } diff --git a/src/rt/mod.rs b/src/rt/mod.rs index 917db198..da78d9f8 100644 --- a/src/rt/mod.rs +++ b/src/rt/mod.rs @@ -1,12 +1,9 @@ //! The runtime. use std::env; -use std::thread; use once_cell::sync::Lazy; -use crate::future; - /// Dummy runtime struct. pub struct Runtime {} @@ -14,22 +11,8 @@ pub struct Runtime {} pub static RUNTIME: Lazy = Lazy::new(|| { // Create an executor thread pool. - let thread_count = env::var("ASYNC_STD_THREAD_COUNT") - .map(|env| { - env.parse() - .expect("ASYNC_STD_THREAD_COUNT must be a number") - }) - .unwrap_or_else(|_| num_cpus::get()) - .max(1); - - let thread_name = - env::var("ASYNC_STD_THREAD_NAME").unwrap_or_else(|_| "async-std/runtime".to_string()); + let thread_name = env::var("ASYNC_STD_THREAD_NAME").unwrap_or_else(|_| "async-std/runtime".to_string()); + async_global_executor::init_with_config(async_global_executor::GlobalExecutorConfig::default().with_env_var("ASYNC_STD_THREAD_COUNT").with_thread_name(thread_name)); - for _ in 0..thread_count { - thread::Builder::new() - .name(thread_name.clone()) - .spawn(|| crate::task::executor::run_global(future::pending::<()>())) - .expect("cannot start a runtime thread"); - } Runtime {} }); diff --git a/src/task/builder.rs b/src/task/builder.rs index 391201d8..8e69a10c 100644 --- a/src/task/builder.rs +++ b/src/task/builder.rs @@ -60,7 +60,7 @@ impl Builder { }); let task = wrapped.tag.task().clone(); - let handle = crate::task::executor::spawn(wrapped); + let handle = async_global_executor::spawn(wrapped); Ok(JoinHandle::new(handle, task)) } @@ -80,7 +80,7 @@ impl Builder { }); let task = wrapped.tag.task().clone(); - let handle = crate::task::executor::local(wrapped); + let handle = async_global_executor::spawn_local(wrapped); Ok(JoinHandle::new(handle, task)) } diff --git a/src/task/executor.rs b/src/task/executor.rs index 0f511d0f..0cd05032 100644 --- a/src/task/executor.rs +++ b/src/task/executor.rs @@ -1,41 +1,10 @@ -use std::cell::RefCell; use std::future::Future; -static GLOBAL_EXECUTOR: once_cell::sync::Lazy = once_cell::sync::Lazy::new(async_executor::Executor::new); - -thread_local! { - static EXECUTOR: RefCell = RefCell::new(async_executor::LocalExecutor::new()); -} - -pub(crate) fn spawn(future: F) -> async_executor::Task -where - F: Future + Send + 'static, - T: Send + 'static, -{ - GLOBAL_EXECUTOR.spawn(future) -} - -#[cfg(feature = "unstable")] -pub(crate) fn local(future: F) -> async_executor::Task -where - F: Future + 'static, - T: 'static, -{ - EXECUTOR.with(|executor| executor.borrow().spawn(future)) -} - pub(crate) fn run(future: F) -> T where F: Future, { - EXECUTOR.with(|executor| enter(|| async_io::block_on(executor.borrow().run(future)))) -} - -pub(crate) fn run_global(future: F) -> T -where - F: Future, -{ - enter(|| async_io::block_on(GLOBAL_EXECUTOR.run(future))) + enter(|| async_global_executor::block_on(future)) } /// Enters the tokio context if the `tokio` feature is enabled. diff --git a/src/task/join_handle.rs b/src/task/join_handle.rs index 25ca79da..9fbab44c 100644 --- a/src/task/join_handle.rs +++ b/src/task/join_handle.rs @@ -18,7 +18,7 @@ pub struct JoinHandle { } #[cfg(not(target_os = "unknown"))] -type InnerHandle = async_executor::Task; +type InnerHandle = async_global_executor::Task; #[cfg(target_arch = "wasm32")] type InnerHandle = futures_channel::oneshot::Receiver;