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
use std::os::raw::c_int;
use std::ptr::null_mut;
#[repr(C)]
#[derive(Clone)]
struct Timeval {
pub tv_sec: i64,
pub tv_usec: i64,
}
#[repr(C)]
#[derive(Clone)]
struct Itimerval {
pub it_interval: Timeval,
pub it_value: Timeval,
}
extern "C" {
fn setitimer(which: c_int, new_value: *mut Itimerval, old_value: *mut Itimerval) -> c_int;
}
const ITIMER_PROF: c_int = 2;
pub struct Timer {
_frequency: c_int,
}
impl Timer {
pub fn new(frequency: c_int) -> Timer {
let interval = 1e6 as i64 / i64::from(frequency);
let it_interval = Timeval {
tv_sec: interval / 1e6 as i64,
tv_usec: interval % 1e6 as i64,
};
let it_value = it_interval.clone();
unsafe {
setitimer(
ITIMER_PROF,
&mut Itimerval {
it_interval,
it_value,
},
null_mut(),
)
};
Timer {
_frequency: frequency,
}
}
}
impl Drop for Timer {
fn drop(&mut self) {
let it_interval = Timeval {
tv_sec: 0,
tv_usec: 0,
};
let it_value = it_interval.clone();
unsafe {
setitimer(
ITIMER_PROF,
&mut Itimerval {
it_interval,
it_value,
},
null_mut(),
)
};
}
}