From 8ca9f6f4dd3efa5e1e06bf67e9d24880159c9cd7 Mon Sep 17 00:00:00 2001 From: Simon Renblad Date: Tue, 29 Jul 2025 13:37:50 +0800 Subject: [PATCH] libasync: wrap SOCKETS in OnceLock --- Cargo.lock | 1 + libasync/Cargo.toml | 1 + libasync/src/smoltcp/mod.rs | 10 ++++------ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc46c80..dab31fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,7 @@ name = "libasync" version = "0.0.0" dependencies = [ "embedded-hal", + "libcortex_a9", "nb 1.0.0", "smoltcp", ] diff --git a/libasync/Cargo.toml b/libasync/Cargo.toml index 90eeb74..0da632f 100644 --- a/libasync/Cargo.toml +++ b/libasync/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] embedded-hal = "0.2" nb = "1.0" +libcortex_a9 = { path = "../libcortex_a9" } [dependencies.smoltcp] version = "0.7" diff --git a/libasync/src/smoltcp/mod.rs b/libasync/src/smoltcp/mod.rs index b650183..7f786e2 100644 --- a/libasync/src/smoltcp/mod.rs +++ b/libasync/src/smoltcp/mod.rs @@ -1,12 +1,13 @@ use alloc::vec::Vec; use core::{cell::RefCell, task::Waker}; +use libcortex_a9::once_lock::OnceLock; use smoltcp::{iface::EthernetInterface, phy::Device, socket::SocketSet, time::Instant}; mod tcp_stream; pub use tcp_stream::TcpStream; -static mut SOCKETS: Option = None; +static SOCKETS: OnceLock = OnceLock::new(); pub struct Sockets { sockets: RefCell>, @@ -24,14 +25,11 @@ impl Sockets { let wakers = RefCell::new(Vec::new()); let instance = Sockets { sockets, wakers }; - unsafe { - SOCKETS = Some(instance); - } + SOCKETS.set(instance).expect("SOCKETS can only be initialized once"); } - #[allow(static_mut_refs)] pub fn instance() -> &'static Self { - unsafe { SOCKETS.as_ref().expect("Sockets") } + SOCKETS.get().expect("cannot get instance before it is initialized") } pub fn poll<'b, D: for<'d> Device<'d>>(&self, iface: &mut EthernetInterface<'b, D>, instant: Instant) {