Skip to content

Commit

Permalink
use select! instead of recv_deadline
Browse files Browse the repository at this point in the history
this commit aim to achieve the same behaviour as before but using the
select! construct instead of recv_timeout because in the following
commits we want to add a case to the select!
  • Loading branch information
RCasatta committed Oct 22, 2024
1 parent 7e7a786 commit 1e37f20
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/signal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crossbeam_channel as channel;
use crossbeam_channel::RecvTimeoutError;
use crossbeam_channel::{self as channel, after, select};
use std::thread;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -36,24 +35,24 @@ impl Waiter {
}

pub fn wait(&self, duration: Duration, accept_sigusr: bool) -> Result<()> {
// Determine the deadline time based on the duration, so that it doesn't
// get pushed back when wait_deadline() recurses
self.wait_deadline(Instant::now() + duration, accept_sigusr)
}

fn wait_deadline(&self, deadline: Instant, accept_sigusr: bool) -> Result<()> {
match self.receiver.recv_deadline(deadline) {
Ok(sig) if sig == SIGUSR1 => {
trace!("notified via SIGUSR1");
if accept_sigusr {
Ok(())
} else {
self.wait_deadline(deadline, accept_sigusr)
let start = Instant::now();
select! {
recv(self.receiver) -> msg => {
match msg {
Ok(sig) if sig == SIGUSR1 => {
trace!("notified via SIGUSR1");
if accept_sigusr {
Ok(())
} else {
let wait_more = duration.saturating_sub(start.elapsed());
self.wait(wait_more, accept_sigusr)
}
}
Ok(sig) => bail!(ErrorKind::Interrupt(sig)),
Err(_) => bail!("signal hook channel disconnected"),
}
}
Ok(sig) => bail!(ErrorKind::Interrupt(sig)),
Err(RecvTimeoutError::Timeout) => Ok(()),
Err(RecvTimeoutError::Disconnected) => bail!("signal hook channel disconnected"),
},
recv(after(duration)) -> _ => Ok(()),
}
}
}

0 comments on commit 1e37f20

Please sign in to comment.