libasync/smoltcp/mod: prevent duplicated wakers

This commit is contained in:
pca006132 2020-08-24 15:25:03 +08:00
parent c13ca614ef
commit 39f672dde8

View File

@ -30,7 +30,7 @@ impl Sockets {
let sockets = RefCell::new(SocketSet::new(sockets_storage)); let sockets = RefCell::new(SocketSet::new(sockets_storage));
let wakers = RefCell::new(Vec::new()); let wakers = RefCell::new(Vec::new());
let instance = Sockets { let instance = Sockets {
sockets, sockets,
wakers, wakers,
@ -57,7 +57,7 @@ impl Sockets {
pub(crate) fn instance() -> &'static Self { pub(crate) fn instance() -> &'static Self {
unsafe { SOCKETS.as_ref().expect("Sockets") } unsafe { SOCKETS.as_ref().expect("Sockets") }
} }
fn poll<'b, 'c, 'e, D: for<'d> Device<'d>>( fn poll<'b, 'c, 'e, D: for<'d> Device<'d>>(
&self, &self,
iface: &mut EthernetInterface<'b, 'c, 'e, D>, iface: &mut EthernetInterface<'b, 'c, 'e, D>,
@ -81,7 +81,14 @@ impl Sockets {
/// TODO: this was called through eg. TcpStream, another poll() /// TODO: this was called through eg. TcpStream, another poll()
/// might want to send packets before sleeping for an interrupt. /// might want to send packets before sleeping for an interrupt.
pub(crate) fn register_waker(waker: Waker) { pub(crate) fn register_waker(waker: Waker) {
Self::instance().wakers.borrow_mut() let mut wakers = Self::instance().wakers.borrow_mut();
.push(waker); for (i, w) in wakers.iter().enumerate() {
if w.will_wake(&waker) {
let last = wakers.len() - 1;
wakers.swap(i, last);
return;
}
}
wakers.push(waker);
} }
} }