PosixTimer: rearm timer only when calling rearm()
parent
1717aa7845
commit
3573423169
92
src/unix.rs
92
src/unix.rs
|
@ -36,7 +36,7 @@ pub mod timer {
|
|||
//!
|
||||
//! // some time passes, we should receive and handle the SIGALRM
|
||||
//! // The timer remains unarmed since the interval given was zero, until we rearm it explicitly.
|
||||
//! self.timer.rearm();
|
||||
//! timer.rearm();
|
||||
//! ```
|
||||
use libc::clockid_t;
|
||||
use libc::sigevent;
|
||||
|
@ -86,7 +86,7 @@ pub mod timer {
|
|||
|
||||
impl PosixTimer {
|
||||
/// Arm without changing interval and value.
|
||||
pub fn rearm(&mut self) {
|
||||
pub fn rearm(&self) {
|
||||
let spec = itimerspec {
|
||||
it_interval: timespec {
|
||||
tv_sec: self.interval.as_secs().try_into().unwrap_or(0),
|
||||
|
@ -114,63 +114,13 @@ pub mod timer {
|
|||
}
|
||||
}
|
||||
|
||||
/// Sets value and arms timer
|
||||
/// Sets value without arming timer
|
||||
pub fn set_value(&mut self, value: Duration) {
|
||||
let spec = itimerspec {
|
||||
it_interval: timespec {
|
||||
tv_sec: self.interval.as_secs().try_into().unwrap_or(0),
|
||||
tv_nsec: self.interval.subsec_nanos().try_into().unwrap_or(0),
|
||||
},
|
||||
it_value: timespec {
|
||||
tv_sec: value.as_secs().try_into().unwrap_or(0),
|
||||
tv_nsec: value.subsec_nanos().try_into().unwrap_or(0),
|
||||
},
|
||||
};
|
||||
let ret =
|
||||
unsafe { timer_settime(self.timer_id, 0, &spec as *const _, std::ptr::null_mut()) };
|
||||
if ret != 0 {
|
||||
match ret {
|
||||
libc::EFAULT => {
|
||||
panic!(
|
||||
"EFAULT: new_value, old_value, or curr_value is not a valid pointer."
|
||||
);
|
||||
}
|
||||
libc::EINVAL => {
|
||||
panic!("EINVAL: timerid is invalid.");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.value = value;
|
||||
}
|
||||
|
||||
/// Sets interval and arms timer
|
||||
/// Sets interval without arming timer
|
||||
pub fn set_interval(&mut self, interval: Duration) {
|
||||
let spec = itimerspec {
|
||||
it_interval: timespec {
|
||||
tv_sec: interval.as_secs().try_into().unwrap_or(0),
|
||||
tv_nsec: interval.subsec_nanos().try_into().unwrap_or(0),
|
||||
},
|
||||
it_value: timespec {
|
||||
tv_sec: self.value.as_secs().try_into().unwrap_or(0),
|
||||
tv_nsec: self.value.subsec_nanos().try_into().unwrap_or(0),
|
||||
},
|
||||
};
|
||||
let ret =
|
||||
unsafe { timer_settime(self.timer_id, 0, &spec as *const _, std::ptr::null_mut()) };
|
||||
if ret != 0 {
|
||||
match ret {
|
||||
libc::EFAULT => {
|
||||
panic!(
|
||||
"EFAULT: new_value, old_value, or curr_value is not a valid pointer."
|
||||
);
|
||||
}
|
||||
libc::EINVAL => {
|
||||
panic!("EINVAL: timerid is invalid.");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.interval = interval;
|
||||
}
|
||||
|
||||
|
@ -218,39 +168,15 @@ pub mod timer {
|
|||
}
|
||||
}
|
||||
|
||||
let spec = itimerspec {
|
||||
it_interval: timespec {
|
||||
tv_sec: interval.as_secs().try_into().unwrap_or(0),
|
||||
tv_nsec: interval.subsec_nanos().try_into().unwrap_or(0),
|
||||
},
|
||||
it_value: timespec {
|
||||
tv_sec: value.as_secs().try_into().unwrap_or(0),
|
||||
tv_nsec: value.subsec_nanos().try_into().unwrap_or(0),
|
||||
},
|
||||
};
|
||||
|
||||
let ret =
|
||||
unsafe { timer_settime(timer_id, 0, &spec as *const _, std::ptr::null_mut()) };
|
||||
if ret != 0 {
|
||||
match ret {
|
||||
libc::EFAULT => {
|
||||
panic!(
|
||||
"EFAULT: new_value, old_value, or curr_value is not a valid pointer."
|
||||
);
|
||||
}
|
||||
libc::EINVAL => {
|
||||
panic!("EINVAL: timerid is invalid.");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PosixTimer {
|
||||
let ret = PosixTimer {
|
||||
timer_id,
|
||||
interval,
|
||||
value,
|
||||
si_value,
|
||||
})
|
||||
};
|
||||
|
||||
ret.rearm();
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue