cros_async: make AsyncTimer use base::Timer

TimerFd is the sys_util era Unix-specific timer, which doesn't support Windows.
This CL moves us to base::Timer, which is the cross platform equivalent.

Thanks to acourbot@ for suggesting the splits in this series.

BUG=b:213147081
TEST=see final CL in series.

Change-Id: I41c8ad88da77c48397ed466ff11aecd703c470a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3583612
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
This commit is contained in:
Noah Gold 2022-04-12 21:36:24 -07:00 committed by Chromeos LUCI
parent 084aa95af1
commit 78ef113617
6 changed files with 32 additions and 20 deletions

View file

@ -209,6 +209,10 @@ impl FakeTimerFd {
pub fn resolution() -> Result<Duration> {
Ok(Duration::from_nanos(1))
}
pub fn try_clone(&self) -> std::result::Result<TimerFd, std::io::Error> {
unimplemented!()
}
}
impl AsRawFd for FakeTimerFd {

View file

@ -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<Duration> {
$inner::resolution()
}
pub fn try_clone(&self) -> std::result::Result<Timer, std::io::Error> {
Ok(Timer(self.0.try_clone()?))
}
}
impl AsRawDescriptor for $timer {

View file

@ -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();

View file

@ -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));

View file

@ -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"))?;

View file

@ -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