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
use crate::task::{Context, Task};

pub struct Scheduler {
    tasks: [Task; 2],
    current_index: usize,
}

impl Scheduler {
    pub fn new() -> Self {
        extern "C" {
            static TASK1_INITIAL_SP: ();
            static TASK1_KERNEL_INITIAL_SP: ();
            static TASK2_INITIAL_SP: ();
            static TASK2_KERNEL_INITIAL_SP: ();
        }

        let task_context =
            Context::new(task1 as *const _, unsafe { &TASK1_INITIAL_SP } as *const _);
        let task1 = Task::new(unsafe { &TASK1_KERNEL_INITIAL_SP }, task_context);
        let task_context =
            Context::new(task2 as *const _, unsafe { &TASK2_INITIAL_SP } as *const _);
        let task2 = Task::new(unsafe { &TASK2_KERNEL_INITIAL_SP }, task_context);

        Self {
            tasks: [task1, task2],
            current_index: 0,
        }
    }

    pub fn schedule(&mut self) -> &Task {
        self.current_index += 1;
        self.current_index %= 4;

        &self.tasks[self.current_index >> 1]
    }

    pub fn start(&mut self) -> ! {
        self.tasks[self.current_index >> 1].start();
    }
}

fn task1() {
    log::trace!("task1 start");

    loop {
        log::trace!("task1");
        for _ in 0..500000 {}
    }
}

fn task2() {
    log::trace!("task2 start");

    let mut x = 0;
    loop {
        log::trace!("task2");
        for _ in 0..1000000 {}
        x += 1;
        if x > 10 {
            unsafe { core::arch::asm!("svc #0") }
        }
    }
}