forked from M-Labs/zynq-rs
libasync: wrap SOCKETS in OnceLock
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -76,6 +76,7 @@ name = "libasync"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"embedded-hal",
|
||||
"libcortex_a9",
|
||||
"nb 1.0.0",
|
||||
"smoltcp",
|
||||
]
|
||||
|
||||
@@ -8,6 +8,7 @@ edition = "2018"
|
||||
[dependencies]
|
||||
embedded-hal = "0.2"
|
||||
nb = "1.0"
|
||||
libcortex_a9 = { path = "../libcortex_a9" }
|
||||
|
||||
[dependencies.smoltcp]
|
||||
version = "0.7"
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user