vfs: add retain param to poll.wait

This commit is contained in:
Mark Poliakov 2024-08-07 19:20:39 +03:00
parent 4fc322405c
commit 5c090f7a38
12 changed files with 29 additions and 18 deletions

View File

@ -143,7 +143,7 @@ impl Process {
entry: options.entry as _,
argument: options.argument as _,
address_space: space.as_address_with_asid(),
stack_pointer: options.stack_top,
stack_pointer: options.stack_top - 8,
single_step: false,
tls: tls_address,
};

View File

@ -35,15 +35,20 @@ impl FdPoll {
}
/// Polls the channel once, returning either a file descriptor or timeout
pub async fn wait(&self, timeout: Option<Duration>) -> Option<(RawFd, Result<(), Error>)> {
pub async fn wait(
&self,
timeout: Option<Duration>,
retain: bool,
) -> Option<(RawFd, Result<(), Error>)> {
let mut timeout = timeout.map(|t| runtime::sleep(t).boxed());
poll_fn(|cx| self.poll_once(cx, &mut timeout)).await
poll_fn(|cx| self.poll_once(cx, &mut timeout, retain)).await
}
fn poll_once(
&self,
cx: &mut Context<'_>,
timeout: &mut Option<BoxFuture<()>>,
retain: bool,
) -> Poll<Option<(RawFd, Result<(), Error>)>> {
if let Some(timeout) = timeout.as_mut()
&& timeout.as_mut().poll(cx).is_ready()
@ -52,8 +57,12 @@ impl FdPoll {
return Poll::Ready(None);
}
for (&fd, file) in self.fds.lock().unwrap().iter() {
let mut lock = self.fds.lock().unwrap();
for (&fd, file) in lock.iter() {
if let Poll::Ready(result) = file.poll_read(cx) {
if !retain {
lock.remove(&fd);
}
return Poll::Ready(Some((fd, result)));
}
}
@ -65,6 +74,6 @@ impl FdPoll {
impl FileReadiness for FdPoll {
fn poll_read(&self, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
self.poll_once(cx, &mut None).map(|_| Ok(()))
self.poll_once(cx, &mut None, true).map(|_| Ok(()))
}
}

View File

@ -300,6 +300,7 @@ pub(crate) fn create_pipe(ends: &mut [MaybeUninit<RawFd>; 2]) -> Result<(), Erro
pub(crate) fn poll_channel_wait(
poll_fd: RawFd,
timeout: &Option<Duration>,
retain: bool,
output: &mut Option<(RawFd, Result<(), Error>)>,
) -> Result<(), Error> {
let thread = Thread::current();
@ -310,7 +311,7 @@ pub(crate) fn poll_channel_wait(
let poll = poll_file.as_poll_channel()?;
*output = block! {
poll.wait(*timeout).await
poll.wait(*timeout, retain).await
}?;
Ok(())

View File

@ -108,6 +108,7 @@ syscall create_pipe(fds: &mut [MaybeUninit<RawFd>; 2]) -> Result<()>;
syscall poll_channel_wait(
poll_fd: RawFd,
timeout: &Option<Duration>,
retain: bool,
output: &mut Option<(RawFd, Result<()>)>
) -> Result<()>;
syscall poll_channel_control(poll_fd: RawFd, ctl: PollControl, fd: RawFd) -> Result<()>;

View File

@ -534,7 +534,7 @@ impl<'a, 'd> Server<'a, 'd> {
fn run_inner(mut self) -> Result<(), Error> {
loop {
match self.poll.wait(None)? {
match self.poll.wait(None, true)? {
Some((fd, Ok(_))) if fd == self.input.as_poll_fd() => {
let event = self.input.read_event()?;
self.handle_keyboard_event(event)?;

View File

@ -47,7 +47,7 @@ impl Connection {
pub fn receive_file(&mut self) -> Result<OwnedFd, Error> {
loop {
let Some((_, Ok(_))) = self.poll.wait(Some(self.timeout))? else {
let Some((_, Ok(_))) = self.poll.wait(Some(self.timeout), true)? else {
return Err(Error::CommunicationTimeout);
};
@ -71,7 +71,7 @@ impl Connection {
predicate: F,
) -> Result<T, Error> {
loop {
let Some((_, Ok(_))) = self.poll.wait(Some(self.timeout))? else {
let Some((_, Ok(_))) = self.poll.wait(Some(self.timeout), true)? else {
return Err(Error::CommunicationTimeout);
};
@ -98,7 +98,7 @@ impl Connection {
}
loop {
match self.poll.wait(Some(self.timeout))? {
match self.poll.wait(Some(self.timeout), true)? {
Some((_, Ok(_))) => (),
Some((_, Err(e))) => {
todo!("Connection error: {:?}", e)

View File

@ -372,7 +372,7 @@ fn wait_for_dhcp_offer(
timer.start(Duration::from_secs(1))?;
loop {
let (fd, result) = poll.wait(None)?.unwrap();
let (fd, result) = poll.wait(None, true)?.unwrap();
result?;
match fd {
@ -416,7 +416,7 @@ fn wait_for_dhcp_acknowledge(
timer.start(Duration::from_secs(1))?;
loop {
let (fd, result) = poll.wait(None)?.unwrap();
let (fd, result) = poll.wait(None, true)?.unwrap();
result?;
match fd {

View File

@ -161,7 +161,7 @@ fn perform_query(nameservers: &[SocketAddr], name: &str, ty: DnsType) -> Result<
timer.start(Duration::from_millis(500))?;
loop {
let (poll_fd, result) = poll.wait(None)?.unwrap();
let (poll_fd, result) = poll.wait(None, true)?.unwrap();
result?;
match poll_fd {

View File

@ -40,7 +40,7 @@ fn udp_client(address: SocketAddr) -> io::Result<()> {
poll.add(stdin_fd)?;
loop {
let (fd, status) = poll.wait(None)?.unwrap();
let (fd, status) = poll.wait(None, true)?.unwrap();
status?;
match fd {
@ -87,7 +87,7 @@ fn tcp_client(address: SocketAddr) -> io::Result<()> {
poll.add(stdin_fd)?;
loop {
let (fd, status) = poll.wait(None)?.unwrap();
let (fd, status) = poll.wait(None, true)?.unwrap();
status?;
match fd {

View File

@ -250,7 +250,7 @@ fn ping_once(
socket.send(&packet)?;
loop {
let (fd, result) = poll.wait(None)?.unwrap();
let (fd, result) = poll.wait(None, true)?.unwrap();
result?;
match fd {

View File

@ -267,7 +267,7 @@ impl<T: Target> Debugger<T> {
while !self.child_exited {
self.redraw()?;
let (fd, result) = self.poll.wait(None)?.unwrap();
let (fd, result) = self.poll.wait(None, true)?.unwrap();
result?;
match fd {

View File

@ -309,7 +309,7 @@ impl<'a> Terminal<'a> {
fn run_inner(mut self) -> Result<ExitCode, Error> {
while !ABORT.load(Ordering::Acquire) {
match self.poll.wait(None)? {
match self.poll.wait(None, true)? {
Some((fd, Ok(_))) if fd == self.conn_fd => {
self.application.poll_events()?;
}