diff --git a/base/src/sys/unix/timerfd.rs b/base/src/sys/unix/timerfd.rs index b6cb935411..c7e84e5eb4 100644 --- a/base/src/sys/unix/timerfd.rs +++ b/base/src/sys/unix/timerfd.rs @@ -209,6 +209,10 @@ impl FakeTimerFd { pub fn resolution() -> Result { Ok(Duration::from_nanos(1)) } + + pub fn try_clone(&self) -> std::result::Result { + unimplemented!() + } } impl AsRawFd for FakeTimerFd { diff --git a/base/src/timer.rs b/base/src/timer.rs index 0912120f7a..af5370f2f5 100644 --- a/base/src/timer.rs +++ b/base/src/timer.rs @@ -6,13 +6,12 @@ use crate::descriptor::{AsRawDescriptor, FromRawDescriptor, IntoRawDescriptor}; use crate::{FakeClock, RawDescriptor, Result}; use crate::platform::{FakeTimerFd, TimerFd}; -use std::{ - os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}, - sync::Arc, - time::Duration, -}; +use std::{sync::Arc, time::Duration}; use sync::Mutex; +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; + /// See [TimerFd](crate::platform::TimerFd) for struct- and method-level /// documentation. pub struct Timer(pub TimerFd); @@ -22,6 +21,13 @@ impl Timer { } } +#[cfg(unix)] +impl AsRawFd for Timer { + fn as_raw_fd(&self) -> RawFd { + self.0.as_raw_fd() + } +} + /// See [FakeTimerFd](crate::platform::FakeTimerFd) for struct- and method-level /// documentation. pub struct FakeTimer(FakeTimerFd); @@ -49,6 +55,10 @@ macro_rules! build_timer { pub fn resolution() -> Result { $inner::resolution() } + + pub fn try_clone(&self) -> std::result::Result { + Ok(Timer(self.0.try_clone()?)) + } } impl AsRawDescriptor for $timer { diff --git a/cros_async/src/timer.rs b/cros_async/src/timer.rs index 6ce3ff756d..e281d0ab0f 100644 --- a/cros_async/src/timer.rs +++ b/cros_async/src/timer.rs @@ -2,10 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +use base::{Result as SysResult, Timer as TimerFd}; use std::time::Duration; -use base::{Result as SysResult, TimerFd}; - use super::{AsyncResult, Error, Executor, IntoAsync, IoSourceExt}; #[cfg(test)] @@ -39,7 +38,7 @@ impl TimerAsync { /// Async sleep for the given duration pub async fn sleep(ex: &Executor, dur: Duration) -> std::result::Result<(), Error> { - let tfd = TimerFd::new().map_err(Error::TimerFd)?; + let mut tfd = TimerFd::new().map_err(Error::TimerFd)?; tfd.reset(dur, None).map_err(Error::TimerFd)?; let t = TimerAsync::new(tfd, ex).map_err(Error::TimerAsync)?; t.next_val().await.map_err(Error::TimerAsync)?; @@ -68,7 +67,7 @@ mod tests { } async fn this_test(ex: &URingExecutor) { - let tfd = TimerFd::new().expect("failed to create timerfd"); + let mut tfd = TimerFd::new().expect("failed to create timerfd"); let dur = Duration::from_millis(200); let now = Instant::now(); @@ -88,7 +87,7 @@ mod tests { #[test] fn one_shot_fd() { async fn this_test(ex: &FdExecutor) { - let tfd = TimerFd::new().expect("failed to create timerfd"); + let mut tfd = TimerFd::new().expect("failed to create timerfd"); let dur = Duration::from_millis(200); let now = Instant::now(); diff --git a/devices/src/virtio/block/asynchronous.rs b/devices/src/virtio/block/asynchronous.rs index e0002cf161..55d2100626 100644 --- a/devices/src/virtio/block/asynchronous.rs +++ b/devices/src/virtio/block/asynchronous.rs @@ -398,7 +398,7 @@ fn run_worker( let flush_timer = Rc::new(RefCell::new( TimerAsync::new( // Call try_clone() to share the same underlying FD with the `flush_disk` task. - timer.0.try_clone().expect("Failed to clone flush_timer"), + timer.try_clone().expect("Failed to clone flush_timer"), &ex, ) .expect("Failed to create an async timer"), @@ -428,7 +428,7 @@ fn run_worker( .into_future(); // Flushes the disk periodically. - let flush_timer = TimerAsync::new(timer.0, &ex).expect("Failed to create an async timer"); + let flush_timer = TimerAsync::new(timer, &ex).expect("Failed to create an async timer"); let disk_flush = flush_disk(disk_state.clone(), flush_timer, flush_timer_armed); pin_mut!(disk_flush); @@ -972,7 +972,7 @@ mod tests { let timer = Timer::new().expect("Failed to create a timer"); let flush_timer = Rc::new(RefCell::new( - TimerAsync::new(timer.0, &ex).expect("Failed to create an async timer"), + TimerAsync::new(timer, &ex).expect("Failed to create an async timer"), )); let flush_timer_armed = Rc::new(RefCell::new(false)); @@ -1042,7 +1042,7 @@ mod tests { let af = SingleFileDisk::new(f, &ex).expect("Failed to create SFD"); let timer = Timer::new().expect("Failed to create a timer"); let flush_timer = Rc::new(RefCell::new( - TimerAsync::new(timer.0, &ex).expect("Failed to create an async timer"), + TimerAsync::new(timer, &ex).expect("Failed to create an async timer"), )); let flush_timer_armed = Rc::new(RefCell::new(false)); let disk_state = Rc::new(AsyncMutex::new(DiskState { @@ -1110,7 +1110,7 @@ mod tests { let af = SingleFileDisk::new(f, &ex).expect("Failed to create SFD"); let timer = Timer::new().expect("Failed to create a timer"); let flush_timer = Rc::new(RefCell::new( - TimerAsync::new(timer.0, &ex).expect("Failed to create an async timer"), + TimerAsync::new(timer, &ex).expect("Failed to create an async timer"), )); let flush_timer_armed = Rc::new(RefCell::new(false)); diff --git a/devices/src/virtio/vhost/user/device/block.rs b/devices/src/virtio/vhost/user/device/block.rs index d7eb038f05..90fd9cd2d6 100644 --- a/devices/src/virtio/vhost/user/device/block.rs +++ b/devices/src/virtio/vhost/user/device/block.rs @@ -103,7 +103,7 @@ impl BlockBackend { let flush_timer_write = Rc::new(RefCell::new( TimerAsync::new( // Call try_clone() to share the same underlying FD with the `flush_disk` task. - timer.0.try_clone().context("Failed to clone flush_timer")?, + timer.try_clone().context("Failed to clone flush_timer")?, ex, ) .context("Failed to create an async timer")?, @@ -113,7 +113,6 @@ impl BlockBackend { // still borrow their copy momentarily to set timeouts. // Call try_clone() to share the same underlying FD with the `flush_disk` task. let flush_timer_read = timer - .0 .try_clone() .context("Failed to clone flush_timer") .and_then(|t| TimerAsync::new(t, ex).context("Failed to create an async timer"))?; diff --git a/devices/src/virtio/vhost/user/device/gpu.rs b/devices/src/virtio/vhost/user/device/gpu.rs index c7b6c2fdb1..b09e4b6298 100644 --- a/devices/src/virtio/vhost/user/device/gpu.rs +++ b/devices/src/virtio/vhost/user/device/gpu.rs @@ -9,7 +9,7 @@ use argh::FromArgs; use async_task::Task; use base::{ clone_descriptor, error, warn, Event, FromRawDescriptor, IntoRawDescriptor, SafeDescriptor, - TimerFd, Tube, UnixSeqpacketListener, UnlinkUnixSeqpacketListener, + Timer, Tube, UnixSeqpacketListener, UnlinkUnixSeqpacketListener, }; use cros_async::{AsyncTube, AsyncWrapper, EventAsync, Executor, IoSourceExt, TimerAsync}; use futures::{ @@ -305,8 +305,8 @@ impl VhostUserBackend for GpuBackend { self.display_worker = Some(task); } - let timer = TimerFd::new() - .context("failed to create TimerFd") + let timer = Timer::new() + .context("failed to create Timer") .and_then(|t| TimerAsync::new(t, &self.ex).context("failed to create TimerAsync"))?; let task = self .ex