1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#![allow(unused)]

use arraydeque::ArrayDeque;

use super::scheduler::APPS;
use super::Scheduler;

/// Manages and activates time-triggered activities.
///
/// The Bellringer is regularly activated and checks whether any of the bells should ring.
/// The bells are stored in a queue that is managed by the Bellringer.
/// A clever implementation avoids iterating through the whole list for every iteration by
/// keeping the bells sorted and storing delta times. This approach leads to a complexity
/// of O(1) for the method called by the timer interrupt in case no bells need to be rung.
pub struct BellRinger {
    bells: ArrayDeque<Bell, APPS>,
}

#[derive(Debug, Clone)]
struct Bell {
    thread: usize,
    offset: usize,
}
impl Bell {
    const fn new(thread: usize, offset: usize) -> Self {
        Self { thread, offset }
    }
}

impl BellRinger {
    pub const fn new() -> Self {
        Self {
            bells: ArrayDeque::new(),
        }
    }

    /// Blocks the current thread for the number of milliseconds
    ///
    /// Tip regarding the borrow checker:
    /// ```ignore
    /// let g = &mut *GUARD.enter(); // borrow here, to get mutable references to different fields
    /// g.bell_ringer.sleep(&mut g.scheduler, *dur as _);
    /// ```
    pub fn sleep(&mut self, scheduler: &mut Scheduler, ms: usize) {
        todo!("BSB A6");
    }

    /// Wakes up waiting threads
    pub fn check(&mut self, scheduler: &mut Scheduler) {
        todo!("BSB A6");
    }

    pub fn is_empty(&self) -> bool {
        self.bells.is_empty()
    }
}