-
Notifications
You must be signed in to change notification settings - Fork 6
/
sched.rs
53 lines (44 loc) · 1.18 KB
/
sched.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
use libc;
use errno;
/// Priority to use in realtime mode.
const RT_PRIO: libc::c_int = 50;
fn set_scheduler(policy: libc::c_int, prio: libc::c_int) {
let param = libc::sched_param {
sched_priority: prio,
};
unsafe {
assert_eq!(0, libc::sched_setscheduler(0, policy, ¶m));
}
}
/// RAII helper for entering/exiting realtime scheduler priority.
pub struct Realtime {
orig_policy: libc::c_int,
orig_prio: libc::c_int,
}
impl Realtime {
/// Enter a realtime scheduler context.
///
/// Returns to the original scheduler on drop.
pub fn enter() -> Realtime {
let policy = unsafe {
libc::sched_getscheduler(0)
};
assert!(policy >= 0);
// stupid errno dance
errno::set_errno(errno::Errno(0));
let prio = unsafe {
libc::getpriority(libc::PRIO_PROCESS as u32, 0)
};
assert_eq!(0, errno::errno().0);
set_scheduler(libc::SCHED_FIFO, RT_PRIO);
Realtime {
orig_policy: policy,
orig_prio: prio,
}
}
}
impl Drop for Realtime {
fn drop(&mut self) {
set_scheduler(self.orig_policy, self.orig_prio);
}
}