use super::*;
const MAX_YIELD: u8 = 16;
#[derive(Debug, Default)]
pub struct LinearBackoff {
count: AtomicCell<u8>,
disturbed: AtomicCell<usize>,
completed: AtomicCell<usize>,
}
impl LinearBackoff {
pub fn new() -> Self { Self::default() }
pub fn snooze(&self) {
if !self.is_completed() {
self.count.fetch_add(1);
}
let count = self.count.load();
for _ in 0 .. count {
thread::yield_now();
}
}
pub fn reset(&self) -> bool {
let napped = self.did_sleep() && !self.is_completed();
if napped {
self.disturbed.fetch_add(1);
}
self.count.store(0);
napped
}
pub fn is_completed(&self) -> bool { self.count.load() == MAX_YIELD }
pub fn did_sleep(&self) -> bool { self.count.load() > 0 }
}