Merge pull request #882 from Keruspe/tokio-fix

fix tokio compatibility
pull/884/head
Yoshua Wuyts 4 years ago committed by GitHub
commit e8126633a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -57,7 +57,7 @@ alloc = [
"futures-core/alloc",
"pin-project-lite",
]
tokio02 = ["tokio"]
tokio02 = ["async-global-executor/tokio02"]
[dependencies]
async-attributes = { version = "1.1.1", optional = true }
@ -78,7 +78,7 @@ slab = { version = "0.4.2", optional = true }
surf = { version = "1.0.3", optional = true }
[target.'cfg(not(target_os = "unknown"))'.dependencies]
async-global-executor = { version = "1.0.2", optional = true, features = ["async-io"] }
async-global-executor = { version = "1.2.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 }

@ -168,7 +168,7 @@ impl Builder {
TaskLocalsWrapper::set_current(&wrapped.tag, || {
let res = if should_run {
// The first call should run the executor
crate::task::executor::run(wrapped)
async_global_executor::block_on(wrapped)
} else {
futures_lite::future::block_on(wrapped)
};

@ -1,41 +0,0 @@
use std::future::Future;
pub(crate) fn run<F, T>(future: F) -> T
where
F: Future<Output = T>,
{
enter(|| async_global_executor::block_on(future))
}
/// Enters the tokio context if the `tokio` feature is enabled.
fn enter<T>(f: impl FnOnce() -> T) -> T {
#[cfg(not(feature = "tokio02"))]
return f();
#[cfg(feature = "tokio02")]
{
use std::cell::Cell;
use tokio::runtime::Runtime;
thread_local! {
/// The level of nested `enter` calls we are in, to ensure that the outermost always
/// has a runtime spawned.
static NESTING: Cell<usize> = Cell::new(0);
}
/// The global tokio runtime.
static RT: once_cell::sync::Lazy<Runtime> = once_cell::sync::Lazy::new(|| Runtime::new().expect("cannot initialize tokio"));
NESTING.with(|nesting| {
let res = if nesting.get() == 0 {
nesting.replace(1);
RT.enter(f)
} else {
nesting.replace(nesting.get() + 1);
f()
};
nesting.replace(nesting.get() - 1);
res
})
}
}

@ -148,8 +148,6 @@ cfg_default! {
mod block_on;
mod builder;
mod current;
#[cfg(not(target_os = "unknown"))]
pub(crate) mod executor;
mod join_handle;
mod sleep;
#[cfg(not(target_os = "unknown"))]

Loading…
Cancel
Save