From 65b65a746a2e62fb321f0742573994bd5855e633 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sat, 17 Feb 2024 16:20:13 +0100 Subject: [PATCH] remove clippy warnings --- src/fs/mod.rs | 2 +- src/scheduler/mod.rs | 164 ++++++++++++++++++++++++++---------------- src/scheduler/task.rs | 2 + 3 files changed, 104 insertions(+), 64 deletions(-) diff --git a/src/fs/mod.rs b/src/fs/mod.rs index de86fa3a0d..16b03ecf23 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -381,7 +381,7 @@ pub fn readdir(name: &str) -> Result, IoError> { /// Open a directory to read the directory entries pub(crate) fn opendir(name: &str) -> Result { let obj = FILESYSTEM.get().unwrap().opendir(name)?; - Ok(insert_object(obj)?) + insert_object(obj) } use crate::fd::{self, FileDescriptor}; diff --git a/src/scheduler/mod.rs b/src/scheduler/mod.rs index c8484bf002..2d5d15d1cd 100644 --- a/src/scheduler/mod.rs +++ b/src/scheduler/mod.rs @@ -1,3 +1,5 @@ +#![allow(clippy::type_complexity)] + use alloc::boxed::Box; use alloc::collections::{BTreeMap, VecDeque}; use alloc::rc::Rc; @@ -5,8 +7,10 @@ use alloc::sync::Arc; #[cfg(feature = "smp")] use alloc::vec::Vec; 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 ahash::RandomState; use crossbeam_utils::Backoff; @@ -323,6 +327,7 @@ impl PerCoreScheduler { prio: current_task_borrowed.prio, core_id, stacks: TaskStacks::new(current_task_borrowed.stacks.get_user_stack_size()), + object_map: current_task_borrowed.object_map.clone(), }; // Add it to the task lists. @@ -467,15 +472,19 @@ impl PerCoreScheduler { ) -> Result, IoError> { // executor disables the interrupts during the polling of the futures // => Borrowing of the current_task is not interrupted by the scheduler - Ok((*(self - .current_task - .borrow() - .object_map - .read() - .await - .get(&fd) - .ok_or(IoError::EINVAL)?)) - .clone()) + future::poll_fn(|cx| { + let borrowed = self.current_task.borrow(); + let mut pinned_obj = core::pin::pin!(borrowed.object_map.write()); + + let x = if let Ready(guard) = pinned_obj.as_mut().poll(cx) { + Ready(guard.get(&fd).cloned().ok_or(IoError::EINVAL)) + } else { + Pending + }; + + x + }) + .await } #[inline] @@ -485,26 +494,34 @@ impl PerCoreScheduler { ) -> Result { // executor disables the interrupts during the polling of the futures // => Borrowing of the current_task is not interrupted by the scheduler - let borrowed = self.current_task.borrow(); - let mut guard = borrowed.object_map.write().await; - - 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); - } - }; + future::poll_fn(|cx| { + 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 fd = new_fd()?; - let _ = guard.insert(fd, obj.clone()); + let fd = new_fd()?; + let _ = guard.insert(fd, obj.clone()); + Ready(Ok(fd)) + } else { + Pending + }; - Ok(fd) + x + }) + .await } #[inline] @@ -515,42 +532,58 @@ impl PerCoreScheduler { ) -> Result<(), IoError> { // executor disables the interrupts during the polling of the futures // => Borrowing of the current_task is not interrupted by the scheduler - let _ = self - .current_task - .borrow() - .object_map - .write() - .await - .insert(fd, obj); - Ok(()) + future::poll_fn(|cx| { + 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 + }) + .await } pub async fn dup_object(&self, fd: FileDescriptor) -> Result { // executor disables the interrupts during the polling of the futures // => Borrowing of the current_task is not interrupted by the scheduler - let borrowed = self.current_task.borrow(); - let mut guard = borrowed.object_map.write().await; - 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); - } + future::poll_fn(|cx| { + 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); + } + + fd = fd.saturating_add(1); + } + }; - fd = fd.saturating_add(1); - } - }; + let fd = new_fd()?; + if guard.try_insert(fd, obj).is_err() { + Ready(Err(IoError::EMFILE)) + } else { + Ready(Ok(fd as FileDescriptor)) + } + } else { + Pending + }; - let fd = new_fd()?; - if guard.try_insert(fd, obj).is_err() { - Err(IoError::EMFILE) - } else { - Ok(fd as FileDescriptor) - } + x + }) + .await } pub async fn remove_object( @@ -559,13 +592,18 @@ impl PerCoreScheduler { ) -> Result, IoError> { // executor disables the interrupts during the polling of the futures // => Borrowing of the current_task is not interrupted by the scheduler - self.current_task - .borrow() - .object_map - .write() - .await - .remove(&fd) - .ok_or(IoError::EINVAL) + future::poll_fn(|cx| { + 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 + }) + .await } #[inline] diff --git a/src/scheduler/task.rs b/src/scheduler/task.rs index af56accfda..a777dba692 100644 --- a/src/scheduler/task.rs +++ b/src/scheduler/task.rs @@ -1,3 +1,5 @@ +#![allow(clippy::type_complexity)] + #[cfg(not(feature = "common-os"))] use alloc::boxed::Box; use alloc::collections::{LinkedList, VecDeque};