Skip to content

Commit

Permalink
Revert "chore: fix version detection for addr_of! (tokio-rs#4863)"
Browse files Browse the repository at this point in the history
This reverts commit ffff9e6.
  • Loading branch information
manaswini05 committed Jul 28, 2022
1 parent b2ada60 commit 01b819b
Show file tree
Hide file tree
Showing 92 changed files with 205 additions and 276 deletions.
8 changes: 4 additions & 4 deletions tokio/Cargo.toml
Expand Up @@ -117,7 +117,7 @@ mio = { version = "0.8.4", optional = true }
num_cpus = { version = "1.8.0", optional = true }
parking_lot = { version = "0.12.0", optional = true }

[target.'cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))'.dependencies]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
socket2 = { version = "0.4.4", features = [ "all" ] }

# Currently unstable. The API exposed by these features may be broken at any time.
Expand Down Expand Up @@ -150,14 +150,14 @@ mockall = "0.11.1"
tempfile = "3.1.0"
async-stream = "0.3"

[target.'cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))'.dev-dependencies]
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
proptest = "1"
socket2 = "0.4"

[target.'cfg(not(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown")))'.dev-dependencies]
[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dev-dependencies]
rand = "0.8.0"

[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), not(target_os = "wasi")))'.dev-dependencies]
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies]
wasm-bindgen-test = "0.3.0"

[target.'cfg(target_os = "freebsd")'.dev-dependencies]
Expand Down
44 changes: 2 additions & 42 deletions tokio/build.rs
Expand Up @@ -10,16 +10,8 @@ const CONST_THREAD_LOCAL_PROBE: &str = r#"
}
"#;

const ADDR_OF_PROBE: &str = r#"
{
let my_var = 10;
::std::ptr::addr_of!(my_var)
}
"#;

fn main() {
let mut enable_const_thread_local = false;
let mut enable_addr_of = false;

match AutoCfg::new() {
Ok(ac) => {
Expand All @@ -43,19 +35,8 @@ fn main() {
}
}

// The `addr_of` and `addr_of_mut` macros were stabilized in 1.51.
if ac.probe_rustc_version(1, 52) {
enable_addr_of = true;
} else if ac.probe_rustc_version(1, 51) {
// This compiler claims to be 1.51, but there are some nightly
// compilers that claim to be 1.51 without supporting the
// feature. Explicitly probe to check if code using them
// compiles.
//
// The oldest nightly that supports the feature is 2021-01-31.
if ac.probe_expression(ADDR_OF_PROBE) {
enable_addr_of = true;
}
if !ac.probe_rustc_version(1, 51) {
autocfg::emit("tokio_no_addr_of")
}
}

Expand All @@ -77,25 +58,4 @@ fn main() {
// RUSTFLAGS="--cfg tokio_no_const_thread_local"
autocfg::emit("tokio_no_const_thread_local")
}

if !enable_addr_of {
// To disable this feature on compilers that support it, you can
// explicitly pass this flag with the following environment variable:
//
// RUSTFLAGS="--cfg tokio_no_addr_of"
autocfg::emit("tokio_no_addr_of")
}

let target = ::std::env::var("TARGET").unwrap_or_default();

// We emit cfgs instead of using `target_family = "wasm"` that requires Rust 1.54.
// Note that these cfgs are unavailable in `Cargo.toml`.
if target.starts_with("wasm") {
autocfg::emit("tokio_wasm");
if target.contains("wasi") {
autocfg::emit("tokio_wasi");
} else {
autocfg::emit("tokio_wasm_not_wasi");
}
}
}
2 changes: 1 addition & 1 deletion tokio/src/coop.rs
Expand Up @@ -207,7 +207,7 @@ cfg_coop! {
mod test {
use super::*;

#[cfg(tokio_wasm_not_wasi)]
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
use wasm_bindgen_test::wasm_bindgen_test as test;

fn get() -> Budget {
Expand Down
10 changes: 5 additions & 5 deletions tokio/src/io/driver/mod.rs
Expand Up @@ -73,7 +73,7 @@ pub(super) struct Inner {

/// Used to wake up the reactor from a call to `turn`.
/// Not supported on Wasi due to lack of threading support.
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
waker: mio::Waker,

metrics: IoDriverMetrics,
Expand Down Expand Up @@ -117,7 +117,7 @@ impl Driver {
/// creation.
pub(crate) fn new() -> io::Result<Driver> {
let poll = mio::Poll::new()?;
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
let waker = mio::Waker::new(poll.registry(), TOKEN_WAKEUP)?;
let registry = poll.registry().try_clone()?;

Expand All @@ -132,7 +132,7 @@ impl Driver {
inner: Arc::new(Inner {
registry,
io_dispatch: RwLock::new(IoDispatcher::new(allocator)),
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
waker,
metrics: IoDriverMetrics::default(),
}),
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Driver {
match self.poll.poll(&mut events, max_wait) {
Ok(_) => {}
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
#[cfg(tokio_wasi)]
#[cfg(target_os = "wasi")]
Err(e) if e.kind() == io::ErrorKind::InvalidInput => {
// In case of wasm32_wasi this error happens, when trying to poll without subscriptions
// just return from the park, as there would be nothing, which wakes us up.
Expand Down Expand Up @@ -310,7 +310,7 @@ impl Handle {
/// blocked in `turn`, then the next call to `turn` will not block and
/// return immediately.
fn wakeup(&self) {
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
self.inner.waker.wake().expect("failed to wake I/O driver");
}
}
Expand Down
1 change: 0 additions & 1 deletion tokio/src/io/driver/registration.rs
Expand Up @@ -115,7 +115,6 @@ impl Registration {

// Uses the poll path, requiring the caller to ensure mutual exclusion for
// correctness. Only the last task to call this function is notified.
#[cfg(not(tokio_wasi))]
pub(crate) fn poll_read_io<R>(
&self,
cx: &mut Context<'_>,
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/io/mod.rs
Expand Up @@ -211,11 +211,11 @@ cfg_io_driver_impl! {
pub use driver::{Interest, Ready};
}

#[cfg_attr(tokio_wasi, allow(unused_imports))]
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
mod poll_evented;

#[cfg(not(loom))]
#[cfg_attr(tokio_wasi, allow(unused_imports))]
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
pub(crate) use poll_evented::PollEvented;
}

Expand Down
34 changes: 9 additions & 25 deletions tokio/src/io/poll_evented.rs
Expand Up @@ -153,32 +153,16 @@ feature! {
{
use std::io::Read;

loop {
let evt = ready!(self.registration.poll_read_ready(cx))?;

let n = ready!(self.registration.poll_read_io(cx, || {
let b = &mut *(buf.unfilled_mut() as *mut [std::mem::MaybeUninit<u8>] as *mut [u8]);
let len = b.len();

match self.io.as_ref().unwrap().read(b) {
Ok(n) => {
// if we read a partially full buffer, this is sufficient on unix to show
// that the socket buffer has been drained
if n > 0 && (!cfg!(windows) && n < len) {
self.registration.clear_readiness(evt);
}

// Safety: We trust `TcpStream::read` to have filled up `n` bytes in the
// buffer.
buf.assume_init(n);
buf.advance(n);
return Poll::Ready(Ok(()));
},
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
self.registration.clear_readiness(evt);
}
Err(e) => return Poll::Ready(Err(e)),
}
}
self.io.as_ref().unwrap().read(b)
}))?;

// Safety: We trust `TcpStream::read` to have filled up `n` bytes in the
// buffer.
buf.assume_init(n);
buf.advance(n);
Poll::Ready(Ok(()))
}

pub(crate) fn poll_write<'a>(&'a self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>>
Expand Down
19 changes: 1 addition & 18 deletions tokio/src/lib.rs
Expand Up @@ -393,26 +393,9 @@ compile_error! {
"Tokio requires the platform pointer width to be 32, 64, or 128 bits"
}

// Ensure that our build script has correctly set cfg flags for wasm.
//
// Each condition is written all(a, not(b)). This should be read as
// "if a, then we must also have b".
#[cfg(any(
all(target_arch = "wasm32", not(tokio_wasm)),
all(target_arch = "wasm64", not(tokio_wasm)),
all(target_family = "wasm", not(tokio_wasm)),
all(target_os = "wasi", not(tokio_wasm)),
all(target_os = "wasi", not(tokio_wasi)),
all(target_os = "wasi", tokio_wasm_not_wasi),
all(tokio_wasm, not(any(target_arch = "wasm32", target_arch = "wasm64"))),
all(tokio_wasm_not_wasi, not(tokio_wasm)),
all(tokio_wasi, not(tokio_wasm))
))]
compile_error!("Tokio's build script has incorrectly detected wasm.");

#[cfg(all(
not(tokio_unstable),
tokio_wasm,
target_arch = "wasm32",
any(
feature = "fs",
feature = "io-std",
Expand Down
16 changes: 8 additions & 8 deletions tokio/src/macros/cfg.rs
Expand Up @@ -61,7 +61,7 @@ macro_rules! cfg_fs {
($($item:item)*) => {
$(
#[cfg(feature = "fs")]
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
$item
)*
Expand Down Expand Up @@ -252,7 +252,7 @@ macro_rules! cfg_process {
#[cfg(feature = "process")]
#[cfg_attr(docsrs, doc(cfg(feature = "process")))]
#[cfg(not(loom))]
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
$item
)*
}
Expand Down Expand Up @@ -281,7 +281,7 @@ macro_rules! cfg_signal {
#[cfg(feature = "signal")]
#[cfg_attr(docsrs, doc(cfg(feature = "signal")))]
#[cfg(not(loom))]
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
$item
)*
}
Expand Down Expand Up @@ -341,7 +341,7 @@ macro_rules! cfg_not_rt {
macro_rules! cfg_rt_multi_thread {
($($item:item)*) => {
$(
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
#[cfg_attr(docsrs, doc(cfg(feature = "rt-multi-thread")))]
$item
)*
Expand Down Expand Up @@ -459,7 +459,7 @@ macro_rules! cfg_has_atomic_u64 {
target_arch = "mips",
target_arch = "powerpc",
target_arch = "riscv32",
tokio_wasm
target_arch = "wasm32"
)))]
$item
)*
Expand All @@ -474,7 +474,7 @@ macro_rules! cfg_not_has_atomic_u64 {
target_arch = "mips",
target_arch = "powerpc",
target_arch = "riscv32",
tokio_wasm
target_arch = "wasm32"
))]
$item
)*
Expand All @@ -484,7 +484,7 @@ macro_rules! cfg_not_has_atomic_u64 {
macro_rules! cfg_not_wasi {
($($item:item)*) => {
$(
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
$item
)*
}
Expand All @@ -493,7 +493,7 @@ macro_rules! cfg_not_wasi {
macro_rules! cfg_is_wasm_not_wasi {
($($item:item)*) => {
$(
#[cfg(tokio_wasm_not_wasi)]
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
$item
)*
}
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/net/tcp/listener.rs
Expand Up @@ -275,7 +275,7 @@ impl TcpListener {
.map(|raw_socket| unsafe { std::net::TcpListener::from_raw_socket(raw_socket) })
}

#[cfg(tokio_wasi)]
#[cfg(target_os = "wasi")]
{
use std::os::wasi::io::{FromRawFd, IntoRawFd};
self.io
Expand Down Expand Up @@ -403,7 +403,7 @@ mod sys {
}

cfg_unstable! {
#[cfg(tokio_wasi)]
#[cfg(target_os = "wasi")]
mod sys {
use super::TcpListener;
use std::os::wasi::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/net/tcp/stream.rs
Expand Up @@ -252,7 +252,7 @@ impl TcpStream {
.map(|raw_socket| unsafe { std::net::TcpStream::from_raw_socket(raw_socket) })
}

#[cfg(tokio_wasi)]
#[cfg(target_os = "wasi")]
{
use std::os::wasi::io::{FromRawFd, IntoRawFd};
self.io
Expand Down Expand Up @@ -1334,7 +1334,7 @@ mod sys {
}
}

#[cfg(all(tokio_unstable, tokio_wasi))]
#[cfg(all(tokio_unstable, target_os = "wasi"))]
mod sys {
use super::TcpStream;
use std::os::wasi::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/park/thread.rs
Expand Up @@ -65,9 +65,9 @@ impl Park for ParkThread {

fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> {
// Wasi doesn't have threads, so just sleep.
#[cfg(not(tokio_wasi))]
#[cfg(not(target_os = "wasi"))]
self.inner.park_timeout(duration);
#[cfg(tokio_wasi)]
#[cfg(target_os = "wasi")]
std::thread::sleep(duration);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/blocking/pool.rs
Expand Up @@ -125,7 +125,7 @@ const KEEP_ALIVE: Duration = Duration::from_secs(10);
/// Tasks will be scheduled as non-mandatory, meaning they may not get executed
/// in case of runtime shutdown.
#[track_caller]
#[cfg_attr(tokio_wasi, allow(dead_code))]
#[cfg_attr(target_os = "wasi", allow(dead_code))]
pub(crate) fn spawn_blocking<F, R>(func: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
Expand Down
6 changes: 3 additions & 3 deletions tokio/src/runtime/builder.rs
Expand Up @@ -174,7 +174,7 @@ pub(crate) type ThreadNameFn = std::sync::Arc<dyn Fn() -> String + Send + Sync +

pub(crate) enum Kind {
CurrentThread,
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
MultiThread,
}

Expand Down Expand Up @@ -619,7 +619,7 @@ impl Builder {
pub fn build(&mut self) -> io::Result<Runtime> {
match &self.kind {
Kind::CurrentThread => self.build_basic_runtime(),
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
Kind::MultiThread => self.build_threaded_runtime(),
}
}
Expand All @@ -628,7 +628,7 @@ impl Builder {
driver::Cfg {
enable_pause_time: match self.kind {
Kind::CurrentThread => true,
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
Kind::MultiThread => false,
},
enable_io: self.enable_io,
Expand Down

0 comments on commit 01b819b

Please sign in to comment.