use arraydeque::ArrayDeque;
use super::{scheduler::APPS, Scheduler};
#[derive(Debug)]
pub struct Semaphore {
counter: usize,
waiting: ArrayDeque<usize, APPS>,
}
impl Semaphore {
pub const fn new(counter: usize) -> Self {
Self {
counter,
waiting: ArrayDeque::new(),
}
}
pub fn count(&self) -> usize {
self.counter
}
pub fn wait(&mut self, scheduler: &mut Scheduler) {
if let Some(c) = self.counter.checked_sub(1) {
self.counter = c;
} else {
let tid = scheduler.active().expect("No running threads");
self.waiting.push_back(tid).unwrap();
scheduler.resume(false);
}
}
pub fn signal(&mut self, scheduler: &mut Scheduler) {
if let Some(thread) = self.waiting.pop_front() {
scheduler.ready(thread);
} else {
self.counter += 1;
}
}
}