mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-29 16:55:34 +00:00
feat: move executor to async-global-executo
This commit is contained in:
parent
e9cb238f49
commit
352c54bfe6
5 changed files with 8 additions and 56 deletions
|
@ -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 }
|
||||
|
|
|
@ -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<Runtime> = 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());
|
||||
async_global_executor::init_with_config(async_global_executor::GlobalExecutorConfig::default().with_env_var("ASYNC_STD_THREAD_COUNT").with_thread_name(thread_name));
|
||||
|
||||
let thread_name =
|
||||
env::var("ASYNC_STD_THREAD_NAME").unwrap_or_else(|_| "async-std/runtime".to_string());
|
||||
|
||||
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 {}
|
||||
});
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -1,41 +1,10 @@
|
|||
use std::cell::RefCell;
|
||||
use std::future::Future;
|
||||
|
||||
static GLOBAL_EXECUTOR: once_cell::sync::Lazy<async_executor::Executor> = once_cell::sync::Lazy::new(async_executor::Executor::new);
|
||||
|
||||
thread_local! {
|
||||
static EXECUTOR: RefCell<async_executor::LocalExecutor> = RefCell::new(async_executor::LocalExecutor::new());
|
||||
}
|
||||
|
||||
pub(crate) fn spawn<F, T>(future: F) -> async_executor::Task<T>
|
||||
where
|
||||
F: Future<Output = T> + Send + 'static,
|
||||
T: Send + 'static,
|
||||
{
|
||||
GLOBAL_EXECUTOR.spawn(future)
|
||||
}
|
||||
|
||||
#[cfg(feature = "unstable")]
|
||||
pub(crate) fn local<F, T>(future: F) -> async_executor::Task<T>
|
||||
where
|
||||
F: Future<Output = T> + 'static,
|
||||
T: 'static,
|
||||
{
|
||||
EXECUTOR.with(|executor| executor.borrow().spawn(future))
|
||||
}
|
||||
|
||||
pub(crate) fn run<F, T>(future: F) -> T
|
||||
where
|
||||
F: Future<Output = T>,
|
||||
{
|
||||
EXECUTOR.with(|executor| enter(|| async_io::block_on(executor.borrow().run(future))))
|
||||
}
|
||||
|
||||
pub(crate) fn run_global<F, T>(future: F) -> T
|
||||
where
|
||||
F: Future<Output = T>,
|
||||
{
|
||||
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.
|
||||
|
|
|
@ -18,7 +18,7 @@ pub struct JoinHandle<T> {
|
|||
}
|
||||
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
type InnerHandle<T> = async_executor::Task<T>;
|
||||
type InnerHandle<T> = async_global_executor::Task<T>;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
type InnerHandle<T> = futures_channel::oneshot::Receiver<T>;
|
||||
|
||||
|
|
Loading…
Reference in a new issue