rstubs/
user.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! The application code.

use core::fmt::Write;

use crate::device::cga::{Attribute, Rect, Window};
use crate::interrupts::guard::GUARD;
use crate::threading::APPS;
use crate::MAX_CPUS;

/// An app that is started at the beginning and terminates instantly.
pub extern "C" fn init_action() -> ! {
    {
        let _g = GUARD.enter();
        println!("Hello from the Init App!");
    }

    {
        let g = GUARD.enter();
        let active = g.scheduler.active().unwrap();
        let t = g.scheduler.thread(active).unwrap();
        println!("self: {t:?}");
    }

    {
        let mut g = GUARD.enter();
        g.scheduler.exit();
    }
}

pub extern "C" fn app_action() -> ! {
    let tid = {
        let g = GUARD.enter();
        g.scheduler.active().unwrap()
    };

    let row = (tid - MAX_CPUS) as u8;

    let style = Attribute::with(((row + 3) & 0b1111).into(), (row & 0b111).into());
    let mut screen = Window::new(Rect::new(0, row, 40, 1)).with_style(style);

    {
        let _g = GUARD.enter();
        screen.clear();
    }

    let mut i = 0_u16;
    loop {
        {
            let _g = GUARD.enter();
            screen.set_cursor((0, row));
            write!(screen, "i: {i}").unwrap();
        }
        i = i.wrapping_add(1);

        if row >= 1 {
            let g = &mut *GUARD.enter();
            g.bell_ringer.sleep(&mut g.scheduler, 50 << row);
        }

        let border = APPS.div_ceil(2);
        if i == 100 && row as usize >= border {
            let mut g = GUARD.enter();
            g.scheduler.kill(MAX_CPUS + row as usize - border);
        }
    }
}

pub extern "C" fn keyboard_action() -> ! {
    loop {
        {
            let g = &mut *GUARD.enter();
            print!("{}", g.keyboard.pop(&mut g.scheduler));
        }
    }
}