Avoid using time API when we don't need it
This avoids a syscall to the time API when the result is ignored later anyway. This allows to use the library with default options on wasm32-unknown-unknown, where the unimplemented syscall would panic otherwise.
This commit is contained in:
parent
239ca81db7
commit
48a136b3ba
1 changed files with 17 additions and 11 deletions
28
src/lib.rs
28
src/lib.rs
|
|
@ -784,19 +784,25 @@ fn perform_reductions(
|
|||
try_alpha_reductions(png, &opts.alphas, eval);
|
||||
}
|
||||
|
||||
struct DeadlineImp {
|
||||
start: Instant,
|
||||
timeout: Duration,
|
||||
print_message: AtomicBool,
|
||||
}
|
||||
|
||||
/// Keep track of processing timeout
|
||||
pub(crate) struct Deadline {
|
||||
start: Instant,
|
||||
timeout: Option<Duration>,
|
||||
print_message: AtomicBool,
|
||||
imp: Option<DeadlineImp>,
|
||||
}
|
||||
|
||||
impl Deadline {
|
||||
pub fn new(timeout: Option<Duration>, verbose: bool) -> Self {
|
||||
Self {
|
||||
start: Instant::now(),
|
||||
timeout,
|
||||
print_message: AtomicBool::new(verbose),
|
||||
imp: timeout.map(|timeout| DeadlineImp {
|
||||
start: Instant::now(),
|
||||
timeout,
|
||||
print_message: AtomicBool::new(verbose),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -804,11 +810,11 @@ impl Deadline {
|
|||
///
|
||||
/// If the verbose option is on, it also prints a timeout message once.
|
||||
pub fn passed(&self) -> bool {
|
||||
if let Some(timeout) = self.timeout {
|
||||
let elapsed = self.start.elapsed();
|
||||
if elapsed > timeout {
|
||||
if self.print_message.load(Ordering::Relaxed) {
|
||||
self.print_message.store(false, Ordering::Relaxed);
|
||||
if let Some(imp) = &self.imp {
|
||||
let elapsed = imp.start.elapsed();
|
||||
if elapsed > imp.timeout {
|
||||
if imp.print_message.load(Ordering::Relaxed) {
|
||||
imp.print_message.store(false, Ordering::Relaxed);
|
||||
eprintln!("Timed out after {} second(s)", elapsed.as_secs());
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Reference in a new issue