rstubs/interrupts/
epilogue.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
47
48
49
50
51
52
53
54
//! Interrupt handlers for the epilogue layer.

use super::guard::Guarded;
use crate::arch::cpu;

/// The different types of epilogues that can be deferred to the epilogue layer.
///
/// They are created in the interrupt service routines and enqueued for
/// execution by the `Guard`.
/// The so called "prologs" of the prologue/epilogue model are directly executed
/// In the ISR and are not part of this object.
#[derive(Debug, Clone)]
pub enum Epilogue {
    /// LAPIC Timer interrupt
    Timer,
    /// PS2 Keyboard interrupt
    Keyboard { key: char },
    /// Assassin IPI
    Assassin,
}

impl Epilogue {
    /// Possibly delayed, sequentially executed interrupt handling routine.
    pub fn run(&mut self, g: &mut Guarded) {
        match self {
            Self::Timer => timer(g),
            Self::Keyboard { key } => keyboard(g, *key),
            Self::Assassin => assassin(g),
        }
    }
}

/// Timer epilogue
fn timer(g: &mut Guarded) {
    if cpu::id() == 0 {
        g.bell_ringer.check(&mut g.scheduler);
    }

    g.scheduler.resume(true);
}

/// Keyboard epilogue
fn keyboard(g: &mut Guarded, key: char) {
    g.keyboard.push(&mut g.scheduler, key);
}

/// Assassin epilogue
fn assassin(g: &mut Guarded) {
    if let Some(active) = g.scheduler.active().and_then(|t| g.scheduler.thread(t))
        && active.exited
    {
        g.scheduler.resume(false);
    }
}