mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-05 18:20:34 +00:00
base: unix: timer: use duration_to_timespec()
Replace the duplicated Duration to timespec conversion code with calls to the existing helper function. BUG=None TEST=cargo test -p base timer -- --include-ignored Change-Id: I99d9e77c2056c760c37fd014ca99363276a473d1 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3652892 Reviewed-by: Anton Romanov <romanton@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
parent
f3be99cd03
commit
515694f155
1 changed files with 6 additions and 21 deletions
|
@ -15,6 +15,7 @@ use libc::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::super::{errno_result, Error, Result};
|
use super::super::{errno_result, Error, Result};
|
||||||
|
use super::duration_to_timespec;
|
||||||
use crate::descriptor::{AsRawDescriptor, FromRawDescriptor, SafeDescriptor};
|
use crate::descriptor::{AsRawDescriptor, FromRawDescriptor, SafeDescriptor};
|
||||||
|
|
||||||
use crate::timer::{Timer, WaitResult};
|
use crate::timer::{Timer, WaitResult};
|
||||||
|
@ -46,23 +47,14 @@ impl Timer {
|
||||||
/// the period for repeated expirations after the initial expiration. Otherwise
|
/// the period for repeated expirations after the initial expiration. Otherwise
|
||||||
/// the timer will expire just once. Cancels any existing duration and repeating interval.
|
/// the timer will expire just once. Cancels any existing duration and repeating interval.
|
||||||
pub fn reset(&mut self, dur: Duration, interval: Option<Duration>) -> Result<()> {
|
pub fn reset(&mut self, dur: Duration, interval: Option<Duration>) -> Result<()> {
|
||||||
// Safe because we are zero-initializing a struct with only primitive member fields.
|
|
||||||
let mut spec: libc::itimerspec = unsafe { mem::zeroed() };
|
|
||||||
spec.it_value.tv_sec = dur.as_secs() as libc::time_t;
|
|
||||||
// nsec always fits in i32 because subsec_nanos is defined to be less than one billion.
|
|
||||||
let nsec = dur.subsec_nanos() as i32;
|
|
||||||
spec.it_value.tv_nsec = libc::c_long::from(nsec);
|
|
||||||
|
|
||||||
// The posix implementation of timer does not need self.interval, but we
|
// The posix implementation of timer does not need self.interval, but we
|
||||||
// save it anyways to keep a consistent interface.
|
// save it anyways to keep a consistent interface.
|
||||||
self.interval = interval;
|
self.interval = interval;
|
||||||
|
|
||||||
if let Some(int) = interval {
|
let spec = libc::itimerspec {
|
||||||
spec.it_interval.tv_sec = int.as_secs() as libc::time_t;
|
it_interval: duration_to_timespec(interval.unwrap_or_default()),
|
||||||
// nsec always fits in i32 because subsec_nanos is defined to be less than one billion.
|
it_value: duration_to_timespec(dur),
|
||||||
let nsec = int.subsec_nanos() as i32;
|
};
|
||||||
spec.it_interval.tv_nsec = libc::c_long::from(nsec);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Safe because this doesn't modify any memory and we check the return value.
|
// Safe because this doesn't modify any memory and we check the return value.
|
||||||
let ret = unsafe { timerfd_settime(self.as_raw_descriptor(), 0, &spec, ptr::null_mut()) };
|
let ret = unsafe { timerfd_settime(self.as_raw_descriptor(), 0, &spec, ptr::null_mut()) };
|
||||||
|
@ -87,15 +79,8 @@ impl Timer {
|
||||||
revents: 0,
|
revents: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Safe because we are zero-initializing a struct with only primitive member fields.
|
|
||||||
|
|
||||||
let ret = if let Some(timeout_inner) = timeout {
|
let ret = if let Some(timeout_inner) = timeout {
|
||||||
let mut timeoutspec: libc::timespec = unsafe { mem::zeroed() };
|
let timeoutspec = duration_to_timespec(timeout_inner);
|
||||||
|
|
||||||
timeoutspec.tv_sec = timeout_inner.as_secs() as libc::time_t;
|
|
||||||
// nsec always fits in i32 because subsec_nanos is defined to be less than one billion.
|
|
||||||
let nsec = timeout_inner.subsec_nanos() as i32;
|
|
||||||
timeoutspec.tv_nsec = libc::c_long::from(nsec);
|
|
||||||
// Safe because this only modifies |pfd| and we check the return value
|
// Safe because this only modifies |pfd| and we check the return value
|
||||||
unsafe {
|
unsafe {
|
||||||
libc::ppoll(
|
libc::ppoll(
|
||||||
|
|
Loading…
Reference in a new issue