Refactor error handling in binary_semaphore_t

This commit is contained in:
Mahmoud Al-Qudsi 2024-01-28 12:43:53 -06:00
parent 2ca102193c
commit 45285b3870

View file

@ -206,17 +206,12 @@ impl binary_semaphore_t {
} }
Self::Pipes(pipes) => { Self::Pipes(pipes) => {
// Write exactly one byte. // Write exactly one byte.
let success;
loop { loop {
let ret = unistd::write(pipes.write.fd(), &[0]); match unistd::write(pipes.write.fd(), &[0]) {
if ret.err() == Some(Errno::EINTR) { Err(Errno::EINTR) => continue,
continue; Err(_) => self.die("write"),
Ok(_) => break,
} }
success = ret.is_ok();
break;
}
if !success {
self.die("write");
} }
} }
} }
@ -227,17 +222,13 @@ impl binary_semaphore_t {
pub fn wait(&self) { pub fn wait(&self) {
match self { match self {
Self::Semaphore(sem) => { Self::Semaphore(sem) => {
let mut res;
loop { loop {
res = unsafe { libc::sem_wait(sem.get()) }; match unsafe { libc::sem_wait(sem.get()) } {
if res < 0 && Errno::last() == Errno::EINTR { 0.. => break,
continue; _ if Errno::last() == Errno::EINTR => continue,
// Other errors here are very unexpected.
_ => self.die("sem_wait"),
} }
break;
}
// Other errors here are very unexpected.
if res < 0 {
self.die("sem_wait");
} }
} }
Self::Pipes(pipes) => { Self::Pipes(pipes) => {
@ -252,15 +243,12 @@ impl binary_semaphore_t {
fd_readable_set_t::is_fd_readable(fd, fd_readable_set_t::kNoTimeout); fd_readable_set_t::is_fd_readable(fd, fd_readable_set_t::kNoTimeout);
} }
let mut ignored: u8 = 0; let mut ignored: u8 = 0;
let amt = unistd::read(fd, std::slice::from_mut(&mut ignored)); match unistd::read(fd, std::slice::from_mut(&mut ignored)) {
if amt.ok() == Some(1) { Ok(1) => break,
break; Ok(_) => continue,
} // EAGAIN should only be possible if TSAN workarounds have been applied
// EAGAIN should only be returned in TSan case. Err(Errno::EINTR) | Err(Errno::EAGAIN) => continue,
if amt.is_err() Err(_) => self.die("read"),
&& (amt.err() != Some(Errno::EINTR) && amt.err() != Some(Errno::EAGAIN))
{
self.die("read");
} }
} }
} }