#![allow(clippy::mutex_atomic)]
use std::sync::{Condvar, Mutex};
#[derive(Debug)]
pub struct ThreadInterrupt(Mutex<usize>, Condvar);
impl crate::Interrupt for ThreadInterrupt {
fn new() -> Self {
ThreadInterrupt(Mutex::new(0), Condvar::new())
}
fn interrupt(&self) {
let mut num = self.0.lock().unwrap();
*num += 1;
self.1.notify_one();
}
fn wait_for(&self) {
let mut guard = self.0.lock().unwrap();
if *guard != 0 {
*guard -= 1;
if *guard != 0 {
return;
}
}
let _guard = self.1.wait(guard).unwrap();
}
}