Skip to content

Commit

Permalink
switch to async-global-executor
Browse files Browse the repository at this point in the history
Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
  • Loading branch information
Keruspe committed Sep 14, 2020
1 parent 6a6623c commit 03cd32e
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
[features]
default = [
"std",
"async-executor",
"async-global-executor",
"async-io",
"async-task",
"blocking",
Expand Down Expand Up @@ -80,7 +80,7 @@ futures-timer = { version = "3.0.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.0", 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 }
Expand Down
21 changes: 2 additions & 19 deletions src/rt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
//! The runtime.
use std::env;
use std::thread;

use once_cell::sync::Lazy;

use crate::future;

/// Dummy runtime struct.
pub struct Runtime {}

/// The global 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());
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 {}
});
4 changes: 2 additions & 2 deletions src/task/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Builder {
});

let task = wrapped.tag.task().clone();
let handle = task::executor::spawn(wrapped);
let handle = async_global_executor::spawn(wrapped);

Ok(JoinHandle::new(handle, task))
}
Expand All @@ -81,7 +81,7 @@ impl Builder {
});

let task = wrapped.tag.task().clone();
let handle = task::executor::local(wrapped);
let handle = async_global_executor::spawn_local(wrapped);

Ok(JoinHandle::new(handle, task))
}
Expand Down
33 changes: 1 addition & 32 deletions src/task/executor.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/task/join_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down

0 comments on commit 03cd32e

Please sign in to comment.