rustubs/proc/sync/
bellringer.rs

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
//! bellringer puts tasks to sleep and wake them when semptepber ends
//! the bellringer is very much like a SleepSemaphore
use crate::machine::time;
use crate::proc::sync::L2Sync;
use crate::proc::task::TaskId;
use alloc::collections::VecDeque;

pub static BELLRINGER: L2Sync<BellRinger> = L2Sync::new(BellRinger::new());
pub struct BellRinger {
	pub bedroom: VecDeque<Sleeper>,
}

#[derive(Copy, Clone, Debug)]
pub struct Sleeper {
	pub tid: TaskId,
	pub until: u64,
}

impl Sleeper {
	pub fn new(tid: TaskId, ns: u64) -> Self {
		Self { tid, until: time::nsec() + ns }
	}
}

impl BellRinger {
	pub const fn new() -> Self { Self { bedroom: VecDeque::new() } }

	pub fn check_in(s: Sleeper) { BELLRINGER.lock().bedroom.push_back(s); }

	/// check the sleeper queue and wake up if timer is due.
	/// this is only to be called in epilogues
	pub unsafe fn check_all() {
		// there is much room for optimization here: the queue can be sorted and
		// instead of absolute time we can store the differntial. But I'll keep
		// it simple here.
		let now = time::nsec();
		BELLRINGER.get_ref_mut_unguarded().bedroom.retain(|x| {
			if x.until > now {
				true
			} else {
				x.tid.get_task_ref_mut().wakeup();
				false
			}
		})
	}
}