libasync: wrap SOCKETS in OnceLock

This commit is contained in:
2025-07-29 13:37:50 +08:00
parent 45f3173b65
commit 8ca9f6f4dd
3 changed files with 6 additions and 6 deletions

1
Cargo.lock generated
View File

@@ -76,6 +76,7 @@ name = "libasync"
version = "0.0.0"
dependencies = [
"embedded-hal",
"libcortex_a9",
"nb 1.0.0",
"smoltcp",
]

View File

@@ -8,6 +8,7 @@ edition = "2018"
[dependencies]
embedded-hal = "0.2"
nb = "1.0"
libcortex_a9 = { path = "../libcortex_a9" }
[dependencies.smoltcp]
version = "0.7"

View File

@@ -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<Sockets> = None;
static SOCKETS: OnceLock<Sockets> = OnceLock::new();
pub struct Sockets {
sockets: RefCell<SocketSet<'static>>,
@@ -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) {