Skip to content

Commit

Permalink
refactor(scheduler): use ready! for extracting Poll::Ready
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Feb 21, 2024
1 parent 568de48 commit 71ca561
Showing 1 changed file with 57 additions and 98 deletions.
155 changes: 57 additions & 98 deletions src/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -473,20 +474,13 @@ impl PerCoreScheduler {
fd: FileDescriptor,
) -> Result<Arc<dyn ObjectInterface>, 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
}
Expand All @@ -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?;

Expand All @@ -538,35 +525,28 @@ impl PerCoreScheduler {
obj: Arc<dyn ObjectInterface>,
) -> Result<FileDescriptor, 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) {
let new_fd = || -> Result<FileDescriptor, 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 mut guard = ready!(pinned_obj.as_mut().poll(cx));
let new_fd = || -> Result<FileDescriptor, IoError> {
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
}
Expand All @@ -579,21 +559,14 @@ impl PerCoreScheduler {
obj: Arc<dyn ObjectInterface>,
) -> 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
}
Expand All @@ -602,40 +575,33 @@ impl PerCoreScheduler {
/// identifier to the new copy
pub async fn dup_object(&self, fd: FileDescriptor) -> Result<FileDescriptor, 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) {
let obj = (*(guard.get(&fd).ok_or(IoError::EINVAL)?)).clone();

let new_fd = || -> Result<FileDescriptor, IoError> {
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<FileDescriptor, IoError> {
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
}
Expand All @@ -646,19 +612,12 @@ impl PerCoreScheduler {
fd: FileDescriptor,
) -> Result<Arc<dyn ObjectInterface>, 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
}
Expand Down

0 comments on commit 71ca561

Please sign in to comment.