rustubs/proc/sync/
bellringer.rs1use crate::machine::time;
4use crate::proc::sync::L2Sync;
5use crate::proc::task::TaskId;
6use alloc::collections::VecDeque;
7
8pub static BELLRINGER: L2Sync<BellRinger> = L2Sync::new(BellRinger::new());
9pub struct BellRinger {
10 pub bedroom: VecDeque<Sleeper>,
11}
12
13#[derive(Copy, Clone, Debug)]
14pub struct Sleeper {
15 pub tid: TaskId,
16 pub until: u64,
17}
18
19impl Sleeper {
20 pub fn new(tid: TaskId, ns: u64) -> Self {
21 Self { tid, until: time::nsec() + ns }
22 }
23}
24
25impl BellRinger {
26 pub const fn new() -> Self { Self { bedroom: VecDeque::new() } }
27
28 pub fn check_in(s: Sleeper) { BELLRINGER.lock().bedroom.push_back(s); }
29
30 pub unsafe fn check_all() {
33 let now = time::nsec();
37 BELLRINGER.get_ref_mut_unguarded().bedroom.retain(|x| {
38 if x.until > now {
39 true
40 } else {
41 x.tid.get_task_ref_mut().wakeup();
42 false
43 }
44 })
45 }
46}