add with_timeout constructor for Observer

This commit is contained in:
toka 2021-03-15 10:20:13 +09:00
parent ae9486814e
commit ff759e2ca7
2 changed files with 15 additions and 11 deletions

View File

@ -71,11 +71,7 @@ where
#[inline]
fn run_target(&mut self, input: &I) -> Result<ExitKind, Error> {
let bytes = input.target_bytes();
let ret = (self.harness_fn)(self, bytes.as_slice());
#[cfg(unix)]
unsafe {
write_volatile(

View File

@ -128,6 +128,15 @@ impl TimeObserver {
}
}
pub fn with_timeout(name: &'static str, tmout: u64) -> Self {
Self {
name: name.to_string(),
start_time: Duration::from_secs(0),
last_runtime: None,
exec_tmout: Some(Duration::from_secs(tmout)),
}
}
pub fn last_runtime(&self) -> &Option<Duration> {
&self.last_runtime
}
@ -135,22 +144,21 @@ impl TimeObserver {
impl Observer for TimeObserver {
fn pre_exec(&mut self) -> Result<(), Error> {
#[cfg(unix)]
match self.exec_tmout {
Some(exec_tmout) => unsafe {
let milli_sec = exec_tmout.as_millis() as i64;
let it_value = Timeval{
let it_value = Timeval {
tv_sec: milli_sec / 1000,
tv_usec: milli_sec % 1000,
};
let it_interval = Timeval{
let it_interval = Timeval {
tv_sec: 0,
tv_usec: 0,
};
setitimer(
ITIMER_REAL,
&mut Itimerval{
&mut Itimerval {
it_interval,
it_value,
},
@ -171,17 +179,17 @@ impl Observer for TimeObserver {
#[cfg(unix)]
match self.exec_tmout {
Some(_) => unsafe {
let it_value = Timeval{
let it_value = Timeval {
tv_sec: 0,
tv_usec: 0,
};
let it_interval = Timeval{
let it_interval = Timeval {
tv_sec: 0,
tv_usec: 0,
};
setitimer(
ITIMER_REAL,
&mut Itimerval{
&mut Itimerval {
it_interval,
it_value,
},