From 32b89eeba18d2d0a9c3705212768d4ade3f4948d Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 12 Oct 2023 01:35:23 +0200 Subject: [PATCH] net: remove atomic-polyfill. --- cyw43/Cargo.toml | 1 - embassy-net/Cargo.toml | 1 - embassy-net/src/tcp.rs | 15 +++++++-------- examples/stm32h7/src/bin/eth_client.rs | 4 ++-- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/cyw43/Cargo.toml b/cyw43/Cargo.toml index dae7419c..c201405e 100644 --- a/cyw43/Cargo.toml +++ b/cyw43/Cargo.toml @@ -15,7 +15,6 @@ embassy-time = { version = "0.1.3", path = "../embassy-time"} embassy-sync = { version = "0.3.0", path = "../embassy-sync"} embassy-futures = { version = "0.1.0", path = "../embassy-futures"} embassy-net-driver-channel = { version = "0.1.0", path = "../embassy-net-driver-channel"} -atomic-polyfill = "0.1.5" defmt = { version = "0.3", optional = true } log = { version = "0.4.17", optional = true } diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index c2fffba8..a3c18046 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml @@ -64,4 +64,3 @@ stable_deref_trait = { version = "1.2.0", default-features = false } futures = { version = "0.3.17", default-features = false, features = [ "async-await" ] } atomic-pool = "1.0" embedded-nal-async = { version = "0.6.0", optional = true } -atomic-polyfill = { version = "1.0" } diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index a12fd382..b5615cb6 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs @@ -579,11 +579,10 @@ mod embedded_io_impls { /// TCP client compatible with `embedded-nal-async` traits. #[cfg(feature = "nightly")] pub mod client { - use core::cell::UnsafeCell; + use core::cell::{Cell, UnsafeCell}; use core::mem::MaybeUninit; use core::ptr::NonNull; - use atomic_polyfill::{AtomicBool, Ordering}; use embedded_nal_async::IpAddr; use super::*; @@ -702,15 +701,13 @@ pub mod client { } } - unsafe impl Sync for TcpClientState {} - struct Pool { - used: [AtomicBool; N], + used: [Cell; N], data: [UnsafeCell>; N], } impl Pool { - const VALUE: AtomicBool = AtomicBool::new(false); + const VALUE: Cell = Cell::new(false); const UNINIT: UnsafeCell> = UnsafeCell::new(MaybeUninit::uninit()); const fn new() -> Self { @@ -724,7 +721,9 @@ pub mod client { impl Pool { fn alloc(&self) -> Option> { for n in 0..N { - if self.used[n].swap(true, Ordering::SeqCst) == false { + // this can't race because Pool is not Sync. + if !self.used[n].get() { + self.used[n].set(true); let p = self.data[n].get() as *mut T; return Some(unsafe { NonNull::new_unchecked(p) }); } @@ -738,7 +737,7 @@ pub mod client { let n = p.as_ptr().offset_from(origin); assert!(n >= 0); assert!((n as usize) < N); - self.used[n as usize].store(false, Ordering::SeqCst); + self.used[n as usize].set(false); } } } diff --git a/examples/stm32h7/src/bin/eth_client.rs b/examples/stm32h7/src/bin/eth_client.rs index 4db7aa25..09d27cdb 100644 --- a/examples/stm32h7/src/bin/eth_client.rs +++ b/examples/stm32h7/src/bin/eth_client.rs @@ -105,8 +105,8 @@ async fn main(spawner: Spawner) -> ! { info!("Network task initialized"); - static STATE: TcpClientState<1, 1024, 1024> = TcpClientState::new(); - let client = TcpClient::new(&stack, &STATE); + let state: TcpClientState<1, 1024, 1024> = TcpClientState::new(); + let client = TcpClient::new(&stack, &state); loop { let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(10, 42, 0, 1), 8000));