From 39f672dde86bed989b29d083fd7409b927d6b18f Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 24 Aug 2020 15:25:03 +0800 Subject: [PATCH] libasync/smoltcp/mod: prevent duplicated wakers --- libasync/src/smoltcp/mod.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libasync/src/smoltcp/mod.rs b/libasync/src/smoltcp/mod.rs index fd5191e..6390cd8 100644 --- a/libasync/src/smoltcp/mod.rs +++ b/libasync/src/smoltcp/mod.rs @@ -30,7 +30,7 @@ impl Sockets { let sockets = RefCell::new(SocketSet::new(sockets_storage)); let wakers = RefCell::new(Vec::new()); - + let instance = Sockets { sockets, wakers, @@ -57,7 +57,7 @@ impl Sockets { pub(crate) fn instance() -> &'static Self { unsafe { SOCKETS.as_ref().expect("Sockets") } } - + fn poll<'b, 'c, 'e, D: for<'d> Device<'d>>( &self, iface: &mut EthernetInterface<'b, 'c, 'e, D>, @@ -81,7 +81,14 @@ impl Sockets { /// TODO: this was called through eg. TcpStream, another poll() /// might want to send packets before sleeping for an interrupt. pub(crate) fn register_waker(waker: Waker) { - Self::instance().wakers.borrow_mut() - .push(waker); + let mut wakers = Self::instance().wakers.borrow_mut(); + 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); } }