use crate::free_slots::FreeSlots;
use core::cell::UnsafeCell;
use critical_section::Mutex;
use heapless::Vec;
pub struct CriticalSlots<const N: usize> {
inner: Mutex<UnsafeCell<Vec<usize, N>>>,
}
impl<const N: usize> CriticalSlots<N> {
pub const fn new() -> Self {
Self {
inner: Mutex::new(UnsafeCell::new(Vec::new())),
}
}
}
impl<const N: usize> FreeSlots<N> for CriticalSlots<N> {
fn new() -> Self {
Self::new()
}
fn fill(&self, count: usize) {
critical_section::with(|cs| {
let vec = unsafe { &mut *self.inner.borrow(cs).get() };
vec.clear();
for i in 0..count {
let _ = vec.push(i);
}
});
}
fn take(&self) -> Option<usize> {
critical_section::with(|cs| {
let vec = unsafe { &mut *self.inner.borrow(cs).get() };
vec.pop()
})
}
fn put(&self, idx: usize) {
critical_section::with(|cs| {
let vec = unsafe { &mut *self.inner.borrow(cs).get() };
let _ = vec.push(idx);
})
}
}
unsafe impl<const N: usize> Sync for CriticalSlots<N> {}