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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::thread::{self, JoinHandle};

#[cfg(target_os = "linux")]
use libc::{
    c_int, cpu_set_t, sched_get_priority_max, sched_get_priority_min,
    sched_param, sched_setaffinity, sched_setscheduler, CPU_SET,
    CPU_ZERO, SCHED_FIFO,
};

#[cfg(target_os = "linux")]
use rand::Rng;

use super::*;

#[cfg(target_os = "linux")]
const POLICY: c_int = SCHED_FIFO;

#[cfg(target_os = "linux")]
pub fn prioritize(prio: c_int) {
    pin_cpu();

    let param = sched_param {
        sched_priority: prio,
    };
    let ret = unsafe {
        sched_setscheduler(0, POLICY, &param as *const sched_param)
    };

    assert_eq!(
        ret,
        0,
        "setscheduler is expected to return zero, was {}: {:?}",
        ret,
        std::io::Error::last_os_error()
    );

    thread::yield_now();
}

#[cfg(target_os = "linux")]
fn pin_cpu() {
    unsafe {
        let mut cpu_set: cpu_set_t = std::mem::zeroed();
        CPU_ZERO(&mut cpu_set);
        CPU_SET(0, &mut cpu_set);
        let ret =
            sched_setaffinity(0, 1, &cpu_set as *const cpu_set_t);
        assert_eq!(
            ret,
            0,
            "sched_setaffinity is expected to return 0, was {}: {:?}",
            ret,
            std::io::Error::last_os_error()
        );
    }
}

/// Spawn a thread with a thread priority determined by a
/// (possibly pre-seeded) `rand::Rng`.
#[cfg(target_os = "linux")]
pub fn spawn_with_random_prio<F, T>(f: F) -> JoinHandle<T>
where
    F: FnOnce() -> T,
    F: Send + 'static,
    T: Send + 'static,
{
    let min = unsafe { sched_get_priority_min(POLICY) };
    let max = unsafe { sched_get_priority_max(POLICY) };

    prioritize(thread_rng().gen_range(min, max));

    let prio = thread_rng().gen_range(min, max);

    let context = context();

    thread::spawn(move || {
        prioritize(prio);

        set_context(context);

        f()
    })
}

/// Spawn a thread with a specific realtime priority.
#[cfg(target_os = "linux")]
pub fn spawn_with_prio<F, T>(prio: c_int, f: F) -> JoinHandle<T>
where
    F: FnOnce() -> T,
    F: Send + 'static,
    T: Send + 'static,
{
    let context = context();

    thread::spawn(move || {
        prioritize(prio);

        set_context(context);

        f()
    })
}

/// Spawn a thread and transfer the deterministic Context.
pub fn spawn<F, T>(f: F) -> JoinHandle<T>
where
    F: FnOnce() -> T,
    F: Send + 'static,
    T: Send + 'static,
{
    let context = context();

    thread::spawn(move || {
        set_context(context);

        f()
    })
}

#[test]
fn ensure_context_is_inherited() {
    use std::ops::Add;
    use std::time::Duration;

    let time = UNIX_EPOCH.add(Duration::from_millis(55));
    context::set_time(time.clone());

    let child_time = spawn(|| context::now())
        .join()
        .expect("child should not crash");

    assert_eq!(time, child_time);
}

#[test]
#[ignore]
#[cfg(target_os = "linux")]
fn gogogo() {
    fn f1() {
        for _ in 0..10 {
            println!("1");
        }
    }
    fn f2() {
        for _ in 0..10 {
            println!("2");
        }
    }
    fn f3() {
        for _ in 0..10 {
            println!("3");
        }
    }

    // it's important to prioritize the spawner!
    prioritize(4);

    let threads = vec![
        spawn_with_prio(3, f1),
        spawn_with_prio(2, f2),
        spawn_with_prio(1, f3),
    ];

    for t in threads.into_iter() {
        t.join().unwrap();
    }
}