From 02a902068f9df39c3fedb2097e11569a69600ca2 Mon Sep 17 00:00:00 2001 From: Juan Aguilar Date: Sun, 13 Dec 2020 20:26:57 +0100 Subject: [PATCH] Refactor LocalWaker (#224) --- actix-rt/src/arbiter.rs | 2 +- actix-server/src/builder.rs | 2 +- actix-server/src/signals.rs | 7 ++----- actix-utils/src/dispatcher.rs | 6 ++---- actix-utils/src/inflight.rs | 2 +- actix-utils/src/order.rs | 7 ++++++- actix-utils/src/task.rs | 11 ++++++++--- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/actix-rt/src/arbiter.rs b/actix-rt/src/arbiter.rs index 9a5f1f93..295d2624 100644 --- a/actix-rt/src/arbiter.rs +++ b/actix-rt/src/arbiter.rs @@ -333,7 +333,7 @@ impl Future for CleanupPending { let mut pending = cell.borrow_mut(); let mut i = 0; while i != pending.len() { - if let Poll::Ready(_) = Pin::new(&mut pending[i]).poll(cx) { + if Pin::new(&mut pending[i]).poll(cx).is_ready() { pending.remove(i); } else { i += 1; diff --git a/actix-server/src/builder.rs b/actix-server/src/builder.rs index 8a90d598..64a45df9 100644 --- a/actix-server/src/builder.rs +++ b/actix-server/src/builder.rs @@ -286,7 +286,7 @@ impl ServerBuilder { // handle signals if !self.no_signals { - Signals::start(self.server.clone()).unwrap(); + Signals::start(self.server.clone()); } // start http server actor diff --git a/actix-server/src/signals.rs b/actix-server/src/signals.rs index b6339621..4fc51fc1 100644 --- a/actix-server/src/signals.rs +++ b/actix-server/src/signals.rs @@ -1,5 +1,4 @@ use std::future::Future; -use std::io; use std::pin::Pin; use std::task::{Context, Poll}; @@ -24,13 +23,13 @@ pub(crate) enum Signal { pub(crate) struct Signals { srv: Server, #[cfg(not(unix))] - stream: Pin>>>, + stream: Pin>>>, #[cfg(unix)] streams: Vec<(Signal, actix_rt::signal::unix::Signal)>, } impl Signals { - pub(crate) fn start(srv: Server) -> io::Result<()> { + pub(crate) fn start(srv: Server) { actix_rt::spawn(lazy(|_| { #[cfg(not(unix))] { @@ -66,8 +65,6 @@ impl Signals { actix_rt::spawn(Signals { srv, streams }) } })); - - Ok(()) } } diff --git a/actix-utils/src/dispatcher.rs b/actix-utils/src/dispatcher.rs index 15d3ccf7..1ee72564 100644 --- a/actix-utils/src/dispatcher.rs +++ b/actix-utils/src/dispatcher.rs @@ -290,10 +290,8 @@ where } State::Error(_) => { // flush write buffer - if !this.framed.is_write_buf_empty() { - if let Poll::Pending = this.framed.flush(cx) { - return Poll::Pending; - } + if !this.framed.is_write_buf_empty() && this.framed.flush(cx).is_pending() { + return Poll::Pending; } Poll::Ready(Err(this.state.take_error())) } diff --git a/actix-utils/src/inflight.rs b/actix-utils/src/inflight.rs index 5ed987c7..8975a2d2 100644 --- a/actix-utils/src/inflight.rs +++ b/actix-utils/src/inflight.rs @@ -74,7 +74,7 @@ where type Future = InFlightServiceResponse; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - if let Poll::Pending = self.service.poll_ready(cx)? { + if self.service.poll_ready(cx)?.is_pending() { Poll::Pending } else if !self.count.available(cx) { log::trace!("InFlight limit exceeded"); diff --git a/actix-utils/src/order.rs b/actix-utils/src/order.rs index c418f1d3..2d11b491 100644 --- a/actix-utils/src/order.rs +++ b/actix-utils/src/order.rs @@ -160,7 +160,12 @@ where } // check nested service - if let Poll::Pending = self.service.poll_ready(cx).map_err(InOrderError::Service)? { + if self + .service + .poll_ready(cx) + .map_err(InOrderError::Service)? + .is_pending() + { Poll::Pending } else { Poll::Ready(Ok(())) diff --git a/actix-utils/src/task.rs b/actix-utils/src/task.rs index dca386b8..cb32eb8d 100644 --- a/actix-utils/src/task.rs +++ b/actix-utils/src/task.rs @@ -19,6 +19,7 @@ use std::{fmt, rc}; /// /// A single `AtomicWaker` may be reused for any number of calls to `register` or /// `wake`. +// TODO: Refactor to Cell when remove deprecated methods (@botika) #[derive(Default)] pub struct LocalWaker { pub(crate) waker: UnsafeCell>, @@ -34,6 +35,10 @@ impl LocalWaker { } } + #[deprecated( + since = "2.1.0", + note = "In favor of `wake`. State of the register doesn't matter at `wake` up" + )] #[inline] /// Check if waker has been registered. pub fn is_registered(&self) -> bool { @@ -47,9 +52,8 @@ impl LocalWaker { pub fn register(&self, waker: &Waker) -> bool { unsafe { let w = self.waker.get(); - let is_registered = (*w).is_some(); - *w = Some(waker.clone()); - is_registered + let last_waker = w.replace(Some(waker.clone())); + last_waker.is_some() } } @@ -63,6 +67,7 @@ impl LocalWaker { } } + #[inline] /// Returns the last `Waker` passed to `register`, so that the user can wake it. /// /// If a waker has not been registered, this returns `None`.