Skip to content

Commit abc2929

Browse files
committed
switch to async-executor
Signed-off-by: Marc-Antoine Perennou <[email protected]>
1 parent 0c51283 commit abc2929

File tree

5 files changed

+18
-37
lines changed

5 files changed

+18
-37
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
2424
[features]
2525
default = [
2626
"std",
27+
"async-executor",
2728
"async-io",
2829
"async-task",
2930
"blocking",
3031
"futures-lite",
3132
"kv-log-macro",
3233
"log",
33-
"multitask",
3434
"num_cpus",
3535
"pin-project-lite",
3636
]
@@ -80,10 +80,10 @@ futures-timer = { version = "3.0.2", optional = true }
8080
surf = { version = "1.0.3", optional = true }
8181

8282
[target.'cfg(not(target_os = "unknown"))'.dependencies]
83+
async-executor = { version = "0.1.1", features = ["async-io"], optional = true }
8384
async-io = { version = "0.1.5", optional = true }
8485
blocking = { version = "0.5.0", optional = true }
8586
futures-lite = { version = "0.1.8", optional = true }
86-
multitask = { version = "0.2.0", optional = true }
8787

8888
[target.'cfg(target_arch = "wasm32")'.dependencies]
8989
futures-timer = { version = "3.0.2", optional = true, features = ["wasm-bindgen"] }

src/rt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
2727
for _ in 0..thread_count {
2828
thread::Builder::new()
2929
.name(thread_name.clone())
30-
.spawn(|| crate::task::block_on(future::pending::<()>()))
30+
.spawn(|| crate::task::executor::run_global(future::pending::<()>()))
3131
.expect("cannot start a runtime thread");
3232
}
3333
Runtime {}

src/task/executor.rs

+13-32
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
use std::cell::RefCell;
22
use std::future::Future;
3-
use std::task::{Context, Poll};
43

5-
static GLOBAL_EXECUTOR: once_cell::sync::Lazy<multitask::Executor> = once_cell::sync::Lazy::new(multitask::Executor::new);
6-
7-
struct Executor {
8-
local_executor: multitask::LocalExecutor,
9-
parker: async_io::parking::Parker,
10-
}
4+
static GLOBAL_EXECUTOR: once_cell::sync::Lazy<async_executor::Executor> = once_cell::sync::Lazy::new(async_executor::Executor::new);
115

126
thread_local! {
13-
static EXECUTOR: RefCell<Executor> = RefCell::new({
14-
let (parker, unparker) = async_io::parking::pair();
15-
let local_executor = multitask::LocalExecutor::new(move || unparker.unpark());
16-
Executor { local_executor, parker }
17-
});
7+
static EXECUTOR: RefCell<async_executor::LocalExecutor> = RefCell::new(async_executor::LocalExecutor::new());
188
}
199

20-
pub(crate) fn spawn<F, T>(future: F) -> multitask::Task<T>
10+
pub(crate) fn spawn<F, T>(future: F) -> async_executor::Task<T>
2111
where
2212
F: Future<Output = T> + Send + 'static,
2313
T: Send + 'static,
@@ -26,35 +16,26 @@ where
2616
}
2717

2818
#[cfg(feature = "unstable")]
29-
pub(crate) fn local<F, T>(future: F) -> multitask::Task<T>
19+
pub(crate) fn local<F, T>(future: F) -> async_executor::Task<T>
3020
where
3121
F: Future<Output = T> + 'static,
3222
T: 'static,
3323
{
34-
EXECUTOR.with(|executor| executor.borrow().local_executor.spawn(future))
24+
EXECUTOR.with(|executor| executor.borrow().spawn(future))
3525
}
3626

3727
pub(crate) fn run<F, T>(future: F) -> T
3828
where
3929
F: Future<Output = T>,
4030
{
41-
enter(|| EXECUTOR.with(|executor| {
42-
let executor = executor.borrow();
43-
let unparker = executor.parker.unparker();
44-
let global_ticker = GLOBAL_EXECUTOR.ticker(move || unparker.unpark());
45-
let unparker = executor.parker.unparker();
46-
let waker = async_task::waker_fn(move || unparker.unpark());
47-
let cx = &mut Context::from_waker(&waker);
48-
pin_utils::pin_mut!(future);
49-
loop {
50-
if let Poll::Ready(res) = future.as_mut().poll(cx) {
51-
return res;
52-
}
53-
if let Ok(false) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| executor.local_executor.tick() || global_ticker.tick())) {
54-
executor.parker.park();
55-
}
56-
}
57-
}))
31+
EXECUTOR.with(|executor| enter(|| GLOBAL_EXECUTOR.enter(|| 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(|| GLOBAL_EXECUTOR.run(future))
5839
}
5940

6041
/// 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> = multitask::Task<T>;
21+
type InnerHandle<T> = async_executor::Task<T>;
2222
#[cfg(target_arch = "wasm32")]
2323
type InnerHandle<T> = futures_channel::oneshot::Receiver<T>;
2424

src/task/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ cfg_default! {
149149
mod builder;
150150
mod current;
151151
#[cfg(not(target_os = "unknown"))]
152-
mod executor;
152+
pub(crate) mod executor;
153153
mod join_handle;
154154
mod sleep;
155155
#[cfg(not(target_os = "unknown"))]

0 commit comments

Comments
 (0)