Skip to content

Commit 1e37f20

Browse files
committed
use select! instead of recv_deadline
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!
1 parent 7e7a786 commit 1e37f20

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/signal.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crossbeam_channel as channel;
2-
use crossbeam_channel::RecvTimeoutError;
1+
use crossbeam_channel::{self as channel, after, select};
32
use std::thread;
43
use std::time::{Duration, Instant};
54

@@ -36,24 +35,24 @@ impl Waiter {
3635
}
3736

3837
pub fn wait(&self, duration: Duration, accept_sigusr: bool) -> Result<()> {
39-
// Determine the deadline time based on the duration, so that it doesn't
40-
// get pushed back when wait_deadline() recurses
41-
self.wait_deadline(Instant::now() + duration, accept_sigusr)
42-
}
43-
44-
fn wait_deadline(&self, deadline: Instant, accept_sigusr: bool) -> Result<()> {
45-
match self.receiver.recv_deadline(deadline) {
46-
Ok(sig) if sig == SIGUSR1 => {
47-
trace!("notified via SIGUSR1");
48-
if accept_sigusr {
49-
Ok(())
50-
} else {
51-
self.wait_deadline(deadline, accept_sigusr)
38+
let start = Instant::now();
39+
select! {
40+
recv(self.receiver) -> msg => {
41+
match msg {
42+
Ok(sig) if sig == SIGUSR1 => {
43+
trace!("notified via SIGUSR1");
44+
if accept_sigusr {
45+
Ok(())
46+
} else {
47+
let wait_more = duration.saturating_sub(start.elapsed());
48+
self.wait(wait_more, accept_sigusr)
49+
}
50+
}
51+
Ok(sig) => bail!(ErrorKind::Interrupt(sig)),
52+
Err(_) => bail!("signal hook channel disconnected"),
5253
}
53-
}
54-
Ok(sig) => bail!(ErrorKind::Interrupt(sig)),
55-
Err(RecvTimeoutError::Timeout) => Ok(()),
56-
Err(RecvTimeoutError::Disconnected) => bail!("signal hook channel disconnected"),
54+
},
55+
recv(after(duration)) -> _ => Ok(()),
5756
}
5857
}
5958
}

0 commit comments

Comments
 (0)