From 51c4be10e03cc5e9aa6bd7e35263a4b7c93caeb2 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sat, 20 Jul 2024 23:17:31 +0200 Subject: [PATCH] Allow waitpid to be interrupted. --- src/sync/wait_map.rs | 9 +++++---- src/syscall/process.rs | 32 ++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/sync/wait_map.rs b/src/sync/wait_map.rs index 9f15d0bbd5..5d0468e741 100644 --- a/src/sync/wait_map.rs +++ b/src/sync/wait_map.rs @@ -26,14 +26,15 @@ where self.inner.lock().remove(key) } - pub fn receive(&self, key: &K, reason: &'static str) -> V { + pub fn receive(&self, key: &K, reason: &'static str) -> Option { loop { let mut inner = self.inner.lock(); if let Some(value) = inner.remove(key) { - return value; + return Some(value); + } + if !self.condition.wait(inner, reason) { + return None; } - //TODO: use false from wait condition to indicate EINTR - let _ = self.condition.wait(inner, reason); } } diff --git a/src/syscall/process.rs b/src/syscall/process.rs index 926daafc74..9790d7c710 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -674,13 +674,15 @@ pub fn waitpid( Some(Ok(ProcessId::from(0))) } } else { - let (w_pid, status) = waitpid.receive( - &WaitpidKey { - pid: None, - pgid: Some(pgid), - }, - "waitpid pgid", - ); + let (w_pid, status) = waitpid + .receive( + &WaitpidKey { + pid: None, + pgid: Some(pgid), + }, + "waitpid pgid", + ) + .ok_or(Error::new(EINTR))?; grim_reaper(w_pid, status) } } else { @@ -716,13 +718,15 @@ pub fn waitpid( Some(Ok(ProcessId::from(0))) } } else { - let (w_pid, status) = waitpid.receive( - &WaitpidKey { - pid: Some(pid), - pgid: None, - }, - "waitpid pid", - ); + let (w_pid, status) = waitpid + .receive( + &WaitpidKey { + pid: Some(pid), + pgid: None, + }, + "waitpid pid", + ) + .ok_or(Error::new(EINTR))?; grim_reaper(w_pid, status) } };