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
//! Interrupt handlers for the epilog layer.

use crate::arch::cpu;

use super::guard::Guarded;

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

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

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

    g.scheduler.resume(true);
}

/// Keyboard epilog
fn keyboard(g: &mut Guarded, key: char) {
    g.keyboard_buf.push_back(key);
    g.keyboard_sema.signal(&mut g.scheduler);
}

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