Skip to content

Commit 352c54b

Browse files
authored
feat: move executor to async-global-executo
1 parent e9cb238 commit 352c54b

File tree

5 files changed

+8
-56
lines changed

5 files changed

+8
-56
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
2424
[features]
2525
default = [
2626
"std",
27-
"async-executor",
27+
"async-global-executor",
2828
"async-io",
2929
"async-task",
3030
"blocking",
@@ -80,7 +80,7 @@ slab = { version = "0.4.2", optional = true }
8080
surf = { version = "1.0.3", optional = true }
8181

8282
[target.'cfg(not(target_os = "unknown"))'.dependencies]
83-
async-executor = { version = "1.0.0", optional = true }
83+
async-global-executor = { version = "1.0.1", optional = true, features = ["async-io"] }
8484
async-io = { version = "1.0.1", optional = true }
8585
blocking = { version = "1.0.0", optional = true }
8686
futures-lite = { version = "1.0.0", optional = true }

src/rt/mod.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
11
//! The runtime.
22
33
use std::env;
4-
use std::thread;
54

65
use once_cell::sync::Lazy;
76

8-
use crate::future;
9-
107
/// Dummy runtime struct.
118
pub struct Runtime {}
129

1310
/// The global runtime.
1411
pub static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
1512
// Create an executor thread pool.
1613

17-
let thread_count = env::var("ASYNC_STD_THREAD_COUNT")
18-
.map(|env| {
19-
env.parse()
20-
.expect("ASYNC_STD_THREAD_COUNT must be a number")
21-
})
22-
.unwrap_or_else(|_| num_cpus::get())
23-
.max(1);
24-
25-
let thread_name =
26-
env::var("ASYNC_STD_THREAD_NAME").unwrap_or_else(|_| "async-std/runtime".to_string());
14+
let thread_name = env::var("ASYNC_STD_THREAD_NAME").unwrap_or_else(|_| "async-std/runtime".to_string());
15+
async_global_executor::init_with_config(async_global_executor::GlobalExecutorConfig::default().with_env_var("ASYNC_STD_THREAD_COUNT").with_thread_name(thread_name));
2716

28-
for _ in 0..thread_count {
29-
thread::Builder::new()
30-
.name(thread_name.clone())
31-
.spawn(|| crate::task::executor::run_global(future::pending::<()>()))
32-
.expect("cannot start a runtime thread");
33-
}
3417
Runtime {}
3518
});

src/task/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Builder {
6060
});
6161

6262
let task = wrapped.tag.task().clone();
63-
let handle = crate::task::executor::spawn(wrapped);
63+
let handle = async_global_executor::spawn(wrapped);
6464

6565
Ok(JoinHandle::new(handle, task))
6666
}
@@ -80,7 +80,7 @@ impl Builder {
8080
});
8181

8282
let task = wrapped.tag.task().clone();
83-
let handle = crate::task::executor::local(wrapped);
83+
let handle = async_global_executor::spawn_local(wrapped);
8484

8585
Ok(JoinHandle::new(handle, task))
8686
}

src/task/executor.rs

+1-32
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
1-
use std::cell::RefCell;
21
use std::future::Future;
32

4-
static GLOBAL_EXECUTOR: once_cell::sync::Lazy<async_executor::Executor> = once_cell::sync::Lazy::new(async_executor::Executor::new);
5-
6-
thread_local! {
7-
static EXECUTOR: RefCell<async_executor::LocalExecutor> = RefCell::new(async_executor::LocalExecutor::new());
8-
}
9-
10-
pub(crate) fn spawn<F, T>(future: F) -> async_executor::Task<T>
11-
where
12-
F: Future<Output = T> + Send + 'static,
13-
T: Send + 'static,
14-
{
15-
GLOBAL_EXECUTOR.spawn(future)
16-
}
17-
18-
#[cfg(feature = "unstable")]
19-
pub(crate) fn local<F, T>(future: F) -> async_executor::Task<T>
20-
where
21-
F: Future<Output = T> + 'static,
22-
T: 'static,
23-
{
24-
EXECUTOR.with(|executor| executor.borrow().spawn(future))
25-
}
26-
273
pub(crate) fn run<F, T>(future: F) -> T
284
where
295
F: Future<Output = T>,
306
{
31-
EXECUTOR.with(|executor| enter(|| async_io::block_on(executor.borrow().run(future))))
32-
}
33-
34-
pub(crate) fn run_global<F, T>(future: F) -> T
35-
where
36-
F: Future<Output = T>,
37-
{
38-
enter(|| async_io::block_on(GLOBAL_EXECUTOR.run(future)))
7+
enter(|| async_global_executor::block_on(future))
398
}
409

4110
/// Enters the tokio context if the `tokio` feature is enabled.

src/task/join_handle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct JoinHandle<T> {
1818
}
1919

2020
#[cfg(not(target_os = "unknown"))]
21-
type InnerHandle<T> = async_executor::Task<T>;
21+
type InnerHandle<T> = async_global_executor::Task<T>;
2222
#[cfg(target_arch = "wasm32")]
2323
type InnerHandle<T> = futures_channel::oneshot::Receiver<T>;
2424

0 commit comments

Comments
 (0)