rustubs/proc/sync/
bellringer.rs

1//! bellringer puts tasks to sleep and wake them when semptepber ends
2//! the bellringer is very much like a SleepSemaphore
3use 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	/// check the sleeper queue and wake up if timer is due.
31	/// this is only to be called in epilogues
32	pub unsafe fn check_all() {
33		// there is much room for optimization here: the queue can be sorted and
34		// instead of absolute time we can store the differntial. But I'll keep
35		// it simple here.
36		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}