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
use std::thread;
use vm::{VMEvent, VM};

#[derive(Default)]
pub struct Scheduler {
    next_pid: u32,
    max_pid: u32,
}

impl Scheduler {
    pub fn new() -> Scheduler {
        Scheduler {
            next_pid: 0,
            max_pid: 50000,
        }
    }

    /// Takes a VM and runs it in a background thread
    pub fn get_thread(&mut self, mut vm: VM) -> thread::JoinHandle<Vec<VMEvent>> {
        thread::spawn(move || {
            let events = vm.run();
            println!("VM Events");
            println!("--------------------------");
            for event in &events {
                println!("{:#?}", event);
            }
            events
        })
    }

    pub fn get_next_pid(&self) -> u32 {
        self.next_pid
    }

    pub fn get_max_pid(&self) -> u32 {
        self.max_pid
    }

    fn _next_pid(&mut self) -> u32 {
        let result = self.next_pid;
        self.next_pid += 1;
        result
    }
}

mod tests {
    #[allow(unused_imports)]
    use scheduler::Scheduler;

    #[test]
    fn test_make_scheduler() {
        let s = Scheduler::new();
        assert_eq!(s.next_pid, 0);
    }
}