Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 8 additions & 27 deletions src/ticker/signal/timer.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
use std::os::raw::c_int;
use std::ptr::null_mut;
use std::time::{Duration, Instant};

#[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;
use libc::{itimerval, setitimer, timeval, ITIMER_PROF};

pub struct Timer {
pub start_instant: Instant,
Expand All @@ -30,16 +11,16 @@ impl Timer {
pub fn new(frequency: u32) -> Timer {
let time = 1f64 / f64::from(frequency);
let duration = Duration::from_secs_f64(time);
let it_interval = Timeval {
let it_interval = timeval {
tv_sec: duration.as_secs().try_into().unwrap(),
tv_usec: duration.subsec_micros().into(),
tv_usec: duration.subsec_micros() as _,
};
let it_value = it_interval.clone();
let it_value = it_interval;

unsafe {
setitimer(
ITIMER_PROF,
&mut Itimerval {
&itimerval {
it_interval,
it_value,
},
Expand All @@ -59,15 +40,15 @@ impl Timer {

impl Drop for Timer {
fn drop(&mut self) {
let it_interval = Timeval {
let it_interval = timeval {
tv_sec: 0,
tv_usec: 0,
};
let it_value = it_interval.clone();
let it_value = it_interval;
unsafe {
setitimer(
ITIMER_PROF,
&mut Itimerval {
&itimerval {
it_interval,
it_value,
},
Expand Down
Loading