use core::cell::Cell;
use core::slice::Iter;
use core::sync::atomic::Ordering::*;
use super::Debt;
const DEBT_SLOT_CNT: usize = 8;
#[derive(Default)]
pub(super) struct Local {
offset: Cell<usize>,
}
#[derive(Default)]
pub(super) struct Slots([Debt; DEBT_SLOT_CNT]);
impl Slots {
#[inline]
pub(super) fn get_debt(&self, ptr: usize, local: &Local) -> Option<&Debt> {
let offset = local.offset.get();
let len = self.0.len();
for i in 0..len {
let i = (i + offset) % len;
let slot = &self.0[i];
if slot.0.load(Relaxed) == Debt::NONE {
let old = slot.0.swap(ptr, SeqCst);
debug_assert_eq!(Debt::NONE, old);
local.offset.set(i + 1);
return Some(&self.0[i]);
}
}
None
}
}
impl<'a> IntoIterator for &'a Slots {
type Item = &'a Debt;
type IntoIter = Iter<'a, Debt>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}