From 71ca5611bb49568e0c0e2efa990d522ce05131a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 21 Feb 2024 14:55:29 +0100 Subject: [PATCH] refactor(scheduler): use `ready!` for extracting `Poll::Ready` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/scheduler/mod.rs | 155 ++++++++++++++++--------------------------- 1 file changed, 57 insertions(+), 98 deletions(-) diff --git a/src/scheduler/mod.rs b/src/scheduler/mod.rs index c9522baf7c..cce4e5768c 100644 --- a/src/scheduler/mod.rs +++ b/src/scheduler/mod.rs @@ -10,7 +10,8 @@ use core::cell::RefCell; use core::future::{self, Future}; use core::ptr; use core::sync::atomic::{AtomicU32, Ordering}; -use core::task::Poll::{Pending, Ready}; +use core::task::ready; +use core::task::Poll::Ready; use ahash::RandomState; use crossbeam_utils::Backoff; @@ -473,20 +474,13 @@ impl PerCoreScheduler { fd: FileDescriptor, ) -> Result, IoError> { future::poll_fn(|cx| { - let x = without_interrupts(|| { + without_interrupts(|| { let borrowed = self.current_task.borrow(); let mut pinned_obj = core::pin::pin!(borrowed.object_map.read()); - let x = if let Ready(guard) = pinned_obj.as_mut().poll(cx) { - Ready(guard.get(&fd).cloned().ok_or(IoError::EINVAL)) - } else { - Pending - }; - - x - }); - - x + let guard = ready!(pinned_obj.as_mut().poll(cx)); + Ready(guard.get(&fd).cloned().ok_or(IoError::EINVAL)) + }) }) .await } @@ -500,27 +494,20 @@ impl PerCoreScheduler { ); future::poll_fn(|cx| { - let x = without_interrupts(|| { + without_interrupts(|| { let borrowed = self.current_task.borrow(); let mut pinned_obj = core::pin::pin!(borrowed.object_map.read()); - let x = if let Ready(guard) = pinned_obj.as_mut().poll(cx) { - // clone standard file descriptors - for i in 0..3 { - if let Some(obj) = guard.get(&i) { - map.insert(i, obj.clone()); - } + let guard = ready!(pinned_obj.as_mut().poll(cx)); + // clone standard file descriptors + for i in 0..3 { + if let Some(obj) = guard.get(&i) { + map.insert(i, obj.clone()); } + } - Ready(Ok(())) - } else { - Pending - }; - - x - }); - - x + Ready(Ok(())) + }) }) .await?; @@ -538,35 +525,28 @@ impl PerCoreScheduler { obj: Arc, ) -> Result { future::poll_fn(|cx| { - let x = without_interrupts(|| { + without_interrupts(|| { let borrowed = self.current_task.borrow(); let mut pinned_obj = core::pin::pin!(borrowed.object_map.write()); - let x = if let Ready(mut guard) = pinned_obj.as_mut().poll(cx) { - let new_fd = || -> Result { - let mut fd: FileDescriptor = 0; - loop { - if !guard.contains_key(&fd) { - break Ok(fd); - } else if fd == FileDescriptor::MAX { - break Err(IoError::EOVERFLOW); - } - - fd = fd.saturating_add(1); + let mut guard = ready!(pinned_obj.as_mut().poll(cx)); + let new_fd = || -> Result { + let mut fd: FileDescriptor = 0; + loop { + if !guard.contains_key(&fd) { + break Ok(fd); + } else if fd == FileDescriptor::MAX { + break Err(IoError::EOVERFLOW); } - }; - let fd = new_fd()?; - let _ = guard.insert(fd, obj.clone()); - Ready(Ok(fd)) - } else { - Pending + fd = fd.saturating_add(1); + } }; - x - }); - - x + let fd = new_fd()?; + let _ = guard.insert(fd, obj.clone()); + Ready(Ok(fd)) + }) }) .await } @@ -579,21 +559,14 @@ impl PerCoreScheduler { obj: Arc, ) -> Result<(), IoError> { future::poll_fn(|cx| { - let x = without_interrupts(|| { + without_interrupts(|| { let borrowed = self.current_task.borrow(); let mut pinned_obj = core::pin::pin!(borrowed.object_map.write()); - let x = if let Ready(mut guard) = pinned_obj.as_mut().poll(cx) { - guard.insert(fd, obj.clone()); - Ready(Ok(())) - } else { - Pending - }; - - x - }); - - x + let mut guard = ready!(pinned_obj.as_mut().poll(cx)); + guard.insert(fd, obj.clone()); + Ready(Ok(())) + }) }) .await } @@ -602,40 +575,33 @@ impl PerCoreScheduler { /// identifier to the new copy pub async fn dup_object(&self, fd: FileDescriptor) -> Result { future::poll_fn(|cx| { - let x = without_interrupts(|| { + without_interrupts(|| { let borrowed = self.current_task.borrow(); let mut pinned_obj = core::pin::pin!(borrowed.object_map.write()); - let x = if let Ready(mut guard) = pinned_obj.as_mut().poll(cx) { - let obj = (*(guard.get(&fd).ok_or(IoError::EINVAL)?)).clone(); - - let new_fd = || -> Result { - let mut fd: FileDescriptor = 0; - loop { - if !guard.contains_key(&fd) { - break Ok(fd); - } else if fd == FileDescriptor::MAX { - break Err(IoError::EOVERFLOW); - } + let mut guard = ready!(pinned_obj.as_mut().poll(cx)); + let obj = (*(guard.get(&fd).ok_or(IoError::EINVAL)?)).clone(); - fd = fd.saturating_add(1); + let new_fd = || -> Result { + let mut fd: FileDescriptor = 0; + loop { + if !guard.contains_key(&fd) { + break Ok(fd); + } else if fd == FileDescriptor::MAX { + break Err(IoError::EOVERFLOW); } - }; - let fd = new_fd()?; - if guard.try_insert(fd, obj).is_err() { - Ready(Err(IoError::EMFILE)) - } else { - Ready(Ok(fd)) + fd = fd.saturating_add(1); } - } else { - Pending }; - x - }); - - x + let fd = new_fd()?; + if guard.try_insert(fd, obj).is_err() { + Ready(Err(IoError::EMFILE)) + } else { + Ready(Ok(fd)) + } + }) }) .await } @@ -646,19 +612,12 @@ impl PerCoreScheduler { fd: FileDescriptor, ) -> Result, IoError> { future::poll_fn(|cx| { - let x = without_interrupts(|| { + without_interrupts(|| { let borrowed = self.current_task.borrow(); let mut pinned_obj = core::pin::pin!(borrowed.object_map.write()); - let x = if let Ready(mut guard) = pinned_obj.as_mut().poll(cx) { - Ready(guard.remove(&fd).ok_or(IoError::EINVAL)) - } else { - Pending - }; - - x - }); - - x + let mut guard = ready!(pinned_obj.as_mut().poll(cx)); + Ready(guard.remove(&fd).ok_or(IoError::EINVAL)) + }) }) .await }