From 2d01c1df92238d19a61360a0de8c6f328afe692e Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Mon, 4 Apr 2022 14:12:42 -0700 Subject: [PATCH] base, sys_util: remove Timer::is_armed function This is currently only used in tests, and removing it makes the cross-platform Timer API simpler to implement. The Windows version of Timer did not implement this API, so it was already unusable in portable code. BUG=b:215618361 TEST=tools/presubmit TEST=cargo test -p base timer Change-Id: I57ab15e8b652d0df3664d95bc7759b9c84fe5e10 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3570178 Reviewed-by: Noah Gold Reviewed-by: Alexandre Courbot Tested-by: kokoro Commit-Queue: Daniel Verkamp --- base/src/timer.rs | 4 ---- base/src/unix/timerfd.rs | 29 +---------------------------- base/src/windows/timer.rs | 9 --------- common/cros_async/src/timer.rs | 6 ------ common/sys_util/src/timerfd.rs | 29 +---------------------------- cros_async/src/timer.rs | 6 ------ 6 files changed, 2 insertions(+), 81 deletions(-) diff --git a/base/src/timer.rs b/base/src/timer.rs index 29f78e55bc..0912120f7a 100644 --- a/base/src/timer.rs +++ b/base/src/timer.rs @@ -42,10 +42,6 @@ macro_rules! build_timer { self.0.wait().map(|_| ()) } - pub fn is_armed(&self) -> Result { - self.0.is_armed() - } - pub fn clear(&mut self) -> Result<()> { self.0.clear() } diff --git a/base/src/unix/timerfd.rs b/base/src/unix/timerfd.rs index a09e80a7ab..b6cb935411 100644 --- a/base/src/unix/timerfd.rs +++ b/base/src/unix/timerfd.rs @@ -12,10 +12,7 @@ use std::{ }; use sync::Mutex; -use libc::{ - clock_getres, timerfd_create, timerfd_gettime, timerfd_settime, CLOCK_MONOTONIC, TFD_CLOEXEC, - {self}, -}; +use libc::{self, clock_getres, timerfd_create, timerfd_settime, CLOCK_MONOTONIC, TFD_CLOEXEC}; use super::{errno_result, EventFd, FakeClock, Result}; @@ -92,20 +89,6 @@ impl TimerFd { Ok(count) } - /// Returns `true` if the timer is currently armed. - pub fn is_armed(&self) -> Result { - // Safe because we are zero-initializing a struct with only primitive member fields. - let mut spec: libc::itimerspec = unsafe { mem::zeroed() }; - - // Safe because timerfd_gettime is trusted to only modify `spec`. - let ret = unsafe { timerfd_gettime(self.as_raw_fd(), &mut spec) }; - if ret < 0 { - return errno_result(); - } - - Ok(spec.it_value.tv_sec != 0 || spec.it_value.tv_nsec != 0) - } - /// Disarms the timer. pub fn clear(&self) -> Result<()> { // Safe because we are zero-initializing a struct with only primitive member fields. @@ -215,11 +198,6 @@ impl FakeTimerFd { } } - /// Returns `true` if the timer is currently armed. - pub fn is_armed(&self) -> Result { - Ok(self.deadline_ns.is_some()) - } - /// Disarms the timer. pub fn clear(&mut self) -> Result<()> { self.deadline_ns = None; @@ -256,14 +234,11 @@ mod tests { #[test] fn one_shot() { let tfd = TimerFd::new().expect("failed to create timerfd"); - assert!(!tfd.is_armed().unwrap()); let dur = Duration::from_millis(200); let now = Instant::now(); tfd.reset(dur, None).expect("failed to arm timer"); - assert!(tfd.is_armed().unwrap()); - let count = tfd.wait().expect("unable to wait for timer"); assert_eq!(count, 1); @@ -288,12 +263,10 @@ mod tests { fn fake_one_shot() { let clock = Arc::new(Mutex::new(FakeClock::new())); let mut tfd = FakeTimerFd::new(clock.clone()); - assert!(!tfd.is_armed().unwrap()); let dur = Duration::from_nanos(200); tfd.reset(dur, None).expect("failed to arm timer"); - assert!(tfd.is_armed().unwrap()); clock.lock().add_ns(200); let count = tfd.wait().expect("unable to wait for timer"); diff --git a/base/src/windows/timer.rs b/base/src/windows/timer.rs index ff05201edd..a20c931b4e 100644 --- a/base/src/windows/timer.rs +++ b/base/src/windows/timer.rs @@ -151,11 +151,6 @@ impl FakeTimer { } } - /// Returns `true` if the timer is currently armed. - pub fn is_armed(&self) -> Result { - Ok(self.deadline_ns.is_some()) - } - /// Disarms the timer. pub fn clear(&mut self) -> Result<()> { self.deadline_ns = None; @@ -276,12 +271,10 @@ mod tests { fn fake_one_shot() { let clock = Arc::new(Mutex::new(FakeClock::new())); let mut tfd = FakeTimer::new(clock.clone()); - assert_eq!(tfd.is_armed().unwrap(), false); let dur = Duration::from_nanos(200); tfd.reset(dur, None).expect("failed to arm timer"); - assert_eq!(tfd.is_armed().unwrap(), true); clock.lock().add_ns(200); let result = tfd.wait(None).expect("unable to wait for timer"); @@ -293,11 +286,9 @@ mod tests { fn fake_one_shot_timeout() { let clock = Arc::new(Mutex::new(FakeClock::new())); let mut tfd = FakeTimer::new(clock.clone()); - assert_eq!(tfd.is_armed().unwrap(), false); let dur = Duration::from_nanos(200); tfd.reset(dur, None).expect("failed to arm timer"); - assert_eq!(tfd.is_armed().unwrap(), true); clock.lock().add_ns(100); let result = tfd diff --git a/common/cros_async/src/timer.rs b/common/cros_async/src/timer.rs index f9624e0e9c..2bc3ebdb0a 100644 --- a/common/cros_async/src/timer.rs +++ b/common/cros_async/src/timer.rs @@ -69,14 +69,11 @@ mod tests { async fn this_test(ex: &URingExecutor) { let tfd = TimerFd::new().expect("failed to create timerfd"); - assert_eq!(tfd.is_armed().unwrap(), false); let dur = Duration::from_millis(200); let now = Instant::now(); tfd.reset(dur, None).expect("failed to arm timer"); - assert_eq!(tfd.is_armed().unwrap(), true); - let t = TimerAsync::new_uring(tfd, ex).unwrap(); let count = t.next_val().await.expect("unable to wait for timer"); @@ -92,14 +89,11 @@ mod tests { fn one_shot_fd() { async fn this_test(ex: &FdExecutor) { let tfd = TimerFd::new().expect("failed to create timerfd"); - assert_eq!(tfd.is_armed().unwrap(), false); let dur = Duration::from_millis(200); let now = Instant::now(); tfd.reset(dur, None).expect("failed to arm timer"); - assert_eq!(tfd.is_armed().unwrap(), true); - let t = TimerAsync::new_poll(tfd, ex).unwrap(); let count = t.next_val().await.expect("unable to wait for timer"); diff --git a/common/sys_util/src/timerfd.rs b/common/sys_util/src/timerfd.rs index a09e80a7ab..b6cb935411 100644 --- a/common/sys_util/src/timerfd.rs +++ b/common/sys_util/src/timerfd.rs @@ -12,10 +12,7 @@ use std::{ }; use sync::Mutex; -use libc::{ - clock_getres, timerfd_create, timerfd_gettime, timerfd_settime, CLOCK_MONOTONIC, TFD_CLOEXEC, - {self}, -}; +use libc::{self, clock_getres, timerfd_create, timerfd_settime, CLOCK_MONOTONIC, TFD_CLOEXEC}; use super::{errno_result, EventFd, FakeClock, Result}; @@ -92,20 +89,6 @@ impl TimerFd { Ok(count) } - /// Returns `true` if the timer is currently armed. - pub fn is_armed(&self) -> Result { - // Safe because we are zero-initializing a struct with only primitive member fields. - let mut spec: libc::itimerspec = unsafe { mem::zeroed() }; - - // Safe because timerfd_gettime is trusted to only modify `spec`. - let ret = unsafe { timerfd_gettime(self.as_raw_fd(), &mut spec) }; - if ret < 0 { - return errno_result(); - } - - Ok(spec.it_value.tv_sec != 0 || spec.it_value.tv_nsec != 0) - } - /// Disarms the timer. pub fn clear(&self) -> Result<()> { // Safe because we are zero-initializing a struct with only primitive member fields. @@ -215,11 +198,6 @@ impl FakeTimerFd { } } - /// Returns `true` if the timer is currently armed. - pub fn is_armed(&self) -> Result { - Ok(self.deadline_ns.is_some()) - } - /// Disarms the timer. pub fn clear(&mut self) -> Result<()> { self.deadline_ns = None; @@ -256,14 +234,11 @@ mod tests { #[test] fn one_shot() { let tfd = TimerFd::new().expect("failed to create timerfd"); - assert!(!tfd.is_armed().unwrap()); let dur = Duration::from_millis(200); let now = Instant::now(); tfd.reset(dur, None).expect("failed to arm timer"); - assert!(tfd.is_armed().unwrap()); - let count = tfd.wait().expect("unable to wait for timer"); assert_eq!(count, 1); @@ -288,12 +263,10 @@ mod tests { fn fake_one_shot() { let clock = Arc::new(Mutex::new(FakeClock::new())); let mut tfd = FakeTimerFd::new(clock.clone()); - assert!(!tfd.is_armed().unwrap()); let dur = Duration::from_nanos(200); tfd.reset(dur, None).expect("failed to arm timer"); - assert!(tfd.is_armed().unwrap()); clock.lock().add_ns(200); let count = tfd.wait().expect("unable to wait for timer"); diff --git a/cros_async/src/timer.rs b/cros_async/src/timer.rs index 52df7053ce..6ce3ff756d 100644 --- a/cros_async/src/timer.rs +++ b/cros_async/src/timer.rs @@ -69,14 +69,11 @@ mod tests { async fn this_test(ex: &URingExecutor) { let tfd = TimerFd::new().expect("failed to create timerfd"); - assert_eq!(tfd.is_armed().unwrap(), false); let dur = Duration::from_millis(200); let now = Instant::now(); tfd.reset(dur, None).expect("failed to arm timer"); - assert_eq!(tfd.is_armed().unwrap(), true); - let t = TimerAsync::new_uring(tfd, ex).unwrap(); let count = t.next_val().await.expect("unable to wait for timer"); @@ -92,14 +89,11 @@ mod tests { fn one_shot_fd() { async fn this_test(ex: &FdExecutor) { let tfd = TimerFd::new().expect("failed to create timerfd"); - assert_eq!(tfd.is_armed().unwrap(), false); let dur = Duration::from_millis(200); let now = Instant::now(); tfd.reset(dur, None).expect("failed to arm timer"); - assert_eq!(tfd.is_armed().unwrap(), true); - let t = TimerAsync::new_poll(tfd, ex).unwrap(); let count = t.next_val().await.expect("unable to wait for timer");