Skip to content

Commit

Permalink
remove clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Feb 17, 2024
1 parent 05379ad commit 65b65a7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ pub fn readdir(name: &str) -> Result<Vec<DirectoryEntry>, IoError> {
/// Open a directory to read the directory entries
pub(crate) fn opendir(name: &str) -> Result<FileDescriptor, IoError> {
let obj = FILESYSTEM.get().unwrap().opendir(name)?;
Ok(insert_object(obj)?)
insert_object(obj)
}

use crate::fd::{self, FileDescriptor};
Expand Down
164 changes: 101 additions & 63 deletions src/scheduler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#![allow(clippy::type_complexity)]

use alloc::boxed::Box;
use alloc::collections::{BTreeMap, VecDeque};
use alloc::rc::Rc;
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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -467,15 +472,19 @@ impl PerCoreScheduler {
) -> Result<Arc<dyn ObjectInterface>, 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]
Expand All @@ -485,26 +494,34 @@ impl PerCoreScheduler {
) -> Result<FileDescriptor, IoError> {
// 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<i32, IoError> {
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<i32, IoError> {
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]
Expand All @@ -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<FileDescriptor, IoError> {
// 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<i32, IoError> {
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<i32, IoError> {
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(
Expand All @@ -559,13 +592,18 @@ impl PerCoreScheduler {
) -> Result<Arc<dyn ObjectInterface>, 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]
Expand Down
2 changes: 2 additions & 0 deletions src/scheduler/task.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::type_complexity)]

#[cfg(not(feature = "common-os"))]
use alloc::boxed::Box;
use alloc::collections::{LinkedList, VecDeque};
Expand Down

0 comments on commit 65b65a7

Please sign in to comment.