-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecutor.rs
More file actions
141 lines (126 loc) · 3.92 KB
/
Copy pathexecutor.rs
File metadata and controls
141 lines (126 loc) · 3.92 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use alloc::boxed::Box;
use alloc::collections::VecDeque;
use alloc::sync::Arc;
use alloc::task::Wake;
use alloc::vec::Vec;
use core::future::{poll_fn, Future};
use core::pin::Pin;
use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use core::task::{Context, Poll, Waker};
use spin::Mutex;
use uefi::boot::{EventType, TimerTrigger, Tpl};
use crate::os::timer::Timer;
struct BoxFuture(Pin<Box<dyn Future<Output = ()> + 'static>>);
// SAFETY: there are no threads.
unsafe impl Send for BoxFuture {}
struct Task {
name: &'static str,
in_queue: AtomicBool,
future: Mutex<BoxFuture>,
micros: AtomicU64,
}
impl Task {
pub(super) fn new<Fut>(name: &'static str, future: Fut) -> Arc<Task>
where
Fut: Future<Output = ()> + 'static,
{
Arc::new(Task {
name,
future: Mutex::new(BoxFuture(Box::pin(future))),
micros: AtomicU64::new(0),
in_queue: AtomicBool::new(false),
})
}
}
impl Wake for Task {
fn wake(self: Arc<Self>) {
if !self.in_queue.swap(true, Ordering::Relaxed) {
EXECUTOR.lock().ready_tasks.push_back(self);
}
}
}
static EXECUTOR: Mutex<Executor> = Mutex::new(Executor {
ready_tasks: VecDeque::new(),
tasks: vec![],
});
pub struct Executor {
// TODO(veluca): scheduling.
ready_tasks: VecDeque<Arc<Task>>,
tasks: Vec<Arc<Task>>,
}
impl Executor {
pub fn run() -> ! {
loop {
let task = EXECUTOR
.lock()
.ready_tasks
.pop_front()
.expect("Executor should never run out of ready tasks");
task.in_queue.store(false, Ordering::Relaxed);
let waker = Waker::from(task.clone());
let mut context = Context::from_waker(&waker);
let mut fut = task.future.try_lock().unwrap();
let begin = Timer::micros();
let _ = fut.0.as_mut().poll(&mut context);
let end = Timer::micros();
task.micros
.fetch_add((end - begin) as u64, Ordering::Relaxed);
}
}
/// Interrupt task execution.
/// This is useful to yield the CPU to other tasks.
pub fn sched_yield() -> impl Future<Output = ()> {
let mut ready = false;
poll_fn(move |cx| {
if ready {
Poll::Ready(())
} else {
ready = true;
cx.waker().wake_by_ref();
Poll::Pending
}
})
}
pub fn sleep_us(us: u64) -> impl Future<Output = ()> {
let tgt = Timer::micros() as u64 + us;
poll_fn(move |cx| {
let now = Timer::micros() as u64;
if now >= tgt {
Poll::Ready(())
} else {
// TODO(veluca): actually suspend the task.
cx.waker().wake_by_ref();
Poll::Pending
}
})
}
/// **WARNING**: this function halts all tasks
pub fn deep_sleep_us(us: u64) {
// SAFETY: we are not using a callback
let e =
unsafe { uefi::boot::create_event(EventType::TIMER, Tpl::NOTIFY, None, None).unwrap() };
uefi::boot::set_timer(&e, TimerTrigger::Relative(10 * us)).unwrap();
uefi::boot::wait_for_event(&mut [e]).unwrap();
}
/// Spawn a new task.
pub fn spawn<Fut>(name: &'static str, f: Fut)
where
Fut: Future<Output = ()> + 'static,
{
let task = Task::new(name, f);
let mut executor = EXECUTOR.lock();
executor.tasks.push(task.clone());
executor.ready_tasks.push_back(task);
}
pub fn top_tasks(n: usize) -> Vec<(u64, &'static str)> {
let mut tasks: Vec<_> = EXECUTOR
.lock()
.tasks
.iter()
.map(|x| (x.micros.load(Ordering::Relaxed), x.name))
.collect();
tasks.sort_by_key(|t| -(t.0 as i64));
tasks.truncate(n);
tasks
}
}