From cdb9e125ae00c3bb6149ae6a723fc541517e5eae Mon Sep 17 00:00:00 2001 From: Vikram Auradkar Date: Tue, 3 May 2022 18:20:18 +0000 Subject: [PATCH] base: rename EventFd to Event Note that - the file/symbols within `src/sys/unix/eventfd.rs` have not changed yet. - base still exports EventFd to keep common/cros_asyncv2 happy BUG=b:213153157 TEST=presubmit Change-Id: Ie0a4308e8501d2e91364b049e6575d656af866cd Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3624568 Reviewed-by: Noah Gold Commit-Queue: Vikram Auradkar Tested-by: kokoro Reviewed-by: Daniel Verkamp --- base/src/event.rs | 11 ++++--- base/src/sys/unix/file_flags.rs | 6 ++-- base/src/sys/unix/mod.rs | 5 ++- base/src/sys/unix/poll.rs | 14 ++++----- base/src/sys/unix/sock_ctrl_msg.rs | 10 +++--- cros_async/src/sys/unix/event.rs | 16 +++++----- cros_async/src/sys/unix/fd_executor.rs | 41 ++++++++++++------------- cros_async/src/sys/unix/uring_source.rs | 10 +++--- devices/src/bat.rs | 2 +- 9 files changed, 58 insertions(+), 57 deletions(-) diff --git a/base/src/event.rs b/base/src/event.rs index 7593628c82..60d88a7992 100644 --- a/base/src/event.rs +++ b/base/src/event.rs @@ -8,16 +8,17 @@ use serde::{Deserialize, Serialize}; use crate::descriptor::{AsRawDescriptor, FromRawDescriptor, IntoRawDescriptor}; pub use crate::platform::EventReadResult; -use crate::{generate_scoped_event, platform::EventFd, RawDescriptor, Result}; +use crate::{generate_scoped_event, platform::Event as PlatformEvent, RawDescriptor, Result}; -/// See [EventFd](crate::platform::EventFd) for struct- and method-level +/// See [PlatformEvent](crate::platform::PlatformEvent) for struct- and method-level /// documentation. +// TODO(b:231344063) Move/update documentation. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(transparent)] -pub struct Event(pub EventFd); +pub struct Event(pub PlatformEvent); impl Event { pub fn new() -> Result { - EventFd::new().map(Event) + PlatformEvent::new().map(Event) } pub fn write(&self, v: u64) -> Result<()> { @@ -45,7 +46,7 @@ impl AsRawDescriptor for Event { impl FromRawDescriptor for Event { unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self { - Event(EventFd::from_raw_descriptor(descriptor)) + Event(PlatformEvent::from_raw_descriptor(descriptor)) } } diff --git a/base/src/sys/unix/file_flags.rs b/base/src/sys/unix/file_flags.rs index 210f891df6..47a03c8f7e 100644 --- a/base/src/sys/unix/file_flags.rs +++ b/base/src/sys/unix/file_flags.rs @@ -36,7 +36,7 @@ impl FileFlags { #[cfg(test)] mod tests { use super::{ - super::{pipe, EventFd}, + super::{pipe, Event}, *, }; @@ -48,8 +48,8 @@ mod tests { } #[test] - fn eventfd() { - let evt = EventFd::new().unwrap(); + fn event() { + let evt = Event::new().unwrap(); assert_eq!(FileFlags::from_file(&evt).unwrap(), FileFlags::ReadWrite); } } diff --git a/base/src/sys/unix/mod.rs b/base/src/sys/unix/mod.rs index 9e55249a09..9377858877 100644 --- a/base/src/sys/unix/mod.rs +++ b/base/src/sys/unix/mod.rs @@ -56,7 +56,10 @@ pub use acpi_event::*; pub use base_poll_token_derive::*; pub use capabilities::drop_capabilities; pub use descriptor::*; -pub use eventfd::*; +// EventFd is deprecated. Use Event instead. EventFd will be removed as soon as rest of the current +// users migrate. +// TODO(b:231344063): Remove EventFd. +pub use eventfd::{EventFd as Event, EventFd, EventReadResult}; pub use file_flags::*; pub use get_filesystem_type::*; pub use ioctl::*; diff --git a/base/src/sys/unix/poll.rs b/base/src/sys/unix/poll.rs index bb6dc0a196..2143a6d0a9 100644 --- a/base/src/sys/unix/poll.rs +++ b/base/src/sys/unix/poll.rs @@ -500,10 +500,10 @@ impl IntoRawDescriptor for EpollContext { /// # Example /// /// ``` -/// # use base::platform::{Result, EventFd, PollContext, PollEvents}; +/// # use base::platform::{Result, Event, PollContext, PollEvents}; /// # fn test() -> Result<()> { -/// let evt1 = EventFd::new()?; -/// let evt2 = EventFd::new()?; +/// let evt1 = Event::new()?; +/// let evt2 = Event::new()?; /// evt2.write(1)?; /// /// let ctx: PollContext = PollContext::new()?; @@ -692,14 +692,14 @@ impl IntoRawDescriptor for PollContext { #[cfg(test)] mod tests { - use super::{super::EventFd, *}; + use super::{super::Event, *}; use base_poll_token_derive::PollToken; use std::{os::unix::net::UnixStream, time::Instant}; #[test] fn poll_context() { - let evt1 = EventFd::new().unwrap(); - let evt2 = EventFd::new().unwrap(); + let evt1 = Event::new().unwrap(); + let evt2 = Event::new().unwrap(); evt1.write(1).unwrap(); evt2.write(1).unwrap(); let ctx: PollContext = PollContext::build_with(&[(&evt1, 1), (&evt2, 2)]).unwrap(); @@ -730,7 +730,7 @@ mod tests { let ctx: PollContext = PollContext::new().unwrap(); let mut evts = Vec::with_capacity(EVT_COUNT); for i in 0..EVT_COUNT { - let evt = EventFd::new().unwrap(); + let evt = Event::new().unwrap(); evt.write(1).unwrap(); ctx.add(&evt, i).unwrap(); evts.push(evt); diff --git a/base/src/sys/unix/sock_ctrl_msg.rs b/base/src/sys/unix/sock_ctrl_msg.rs index 5bc0784e2b..f654302036 100644 --- a/base/src/sys/unix/sock_ctrl_msg.rs +++ b/base/src/sys/unix/sock_ctrl_msg.rs @@ -421,7 +421,7 @@ mod tests { use libc::cmsghdr; - use super::super::EventFd; + use super::super::Event; // Doing this as a macro makes it easier to see the line if it fails macro_rules! CMSG_SPACE_TEST { @@ -483,7 +483,7 @@ mod tests { fn send_recv_only_fd() { let (s1, s2) = UnixDatagram::pair().expect("failed to create socket pair"); - let evt = EventFd::new().expect("failed to create eventfd"); + let evt = Event::new().expect("failed to create event"); let ioslice = IoSlice::new([].as_ref()); let write_count = s1 .send_with_fd(&[ioslice], evt.as_raw_fd()) @@ -507,14 +507,14 @@ mod tests { file.write_all(unsafe { from_raw_parts(&1203u64 as *const u64 as *const u8, 8) }) .expect("failed to write to sent fd"); - assert_eq!(evt.read().expect("failed to read from eventfd"), 1203); + assert_eq!(evt.read().expect("failed to read from event"), 1203); } #[test] fn send_recv_with_fd() { let (s1, s2) = UnixDatagram::pair().expect("failed to create socket pair"); - let evt = EventFd::new().expect("failed to create eventfd"); + let evt = Event::new().expect("failed to create event"); let ioslice = IoSlice::new([237].as_ref()); let write_count = s1 .send_with_fds(&[ioslice], &[evt.as_raw_fd()]) @@ -541,6 +541,6 @@ mod tests { file.write_all(unsafe { from_raw_parts(&1203u64 as *const u64 as *const u8, 8) }) .expect("failed to write to sent fd"); - assert_eq!(evt.read().expect("failed to read from eventfd"), 1203); + assert_eq!(evt.read().expect("failed to read from event"), 1203); } } diff --git a/cros_async/src/sys/unix/event.rs b/cros_async/src/sys/unix/event.rs index 30d4aca78b..9a95dbe16b 100644 --- a/cros_async/src/sys/unix/event.rs +++ b/cros_async/src/sys/unix/event.rs @@ -5,10 +5,10 @@ #[cfg(test)] use super::{FdExecutor, URingExecutor}; use crate::{AsyncResult, EventAsync, Executor}; -use base::Event as EventFd; +use base::Event; impl EventAsync { - pub fn new(event: EventFd, ex: &Executor) -> AsyncResult { + pub fn new(event: Event, ex: &Executor) -> AsyncResult { ex.async_from(event) .map(|io_source| EventAsync { io_source }) } @@ -19,12 +19,12 @@ impl EventAsync { } #[cfg(test)] - pub(crate) fn new_poll(event: EventFd, ex: &FdExecutor) -> AsyncResult { + pub(crate) fn new_poll(event: Event, ex: &FdExecutor) -> AsyncResult { super::executor::async_poll_from(event, ex).map(|io_source| EventAsync { io_source }) } #[cfg(test)] - pub(crate) fn new_uring(event: EventFd, ex: &URingExecutor) -> AsyncResult { + pub(crate) fn new_uring(event: Event, ex: &URingExecutor) -> AsyncResult { super::executor::async_uring_from(event, ex).map(|io_source| EventAsync { io_source }) } } @@ -37,12 +37,12 @@ mod tests { #[test] fn next_val_reads_value() { - async fn go(event: EventFd, ex: &Executor) -> u64 { + async fn go(event: Event, ex: &Executor) -> u64 { let event_async = EventAsync::new(event, ex).unwrap(); event_async.next_val().await.unwrap() } - let eventfd = EventFd::new().unwrap(); + let eventfd = Event::new().unwrap(); eventfd.write(0xaa).unwrap(); let ex = Executor::new().unwrap(); let val = ex.run_until(go(eventfd, &ex)).unwrap(); @@ -59,7 +59,7 @@ mod tests { event_async.next_val().await.unwrap() } - let eventfd = EventFd::new().unwrap(); + let eventfd = Event::new().unwrap(); eventfd.write(0xaa).unwrap(); let uring_ex = URingExecutor::new().unwrap(); let val = uring_ex @@ -67,7 +67,7 @@ mod tests { .unwrap(); assert_eq!(val, 0xaa); - let eventfd = EventFd::new().unwrap(); + let eventfd = Event::new().unwrap(); eventfd.write(0xaa).unwrap(); let poll_ex = FdExecutor::new().unwrap(); let val = poll_ex diff --git a/cros_async/src/sys/unix/fd_executor.rs b/cros_async/src/sys/unix/fd_executor.rs index a5b4e2bbe1..27e881fe32 100644 --- a/cros_async/src/sys/unix/fd_executor.rs +++ b/cros_async/src/sys/unix/fd_executor.rs @@ -23,10 +23,7 @@ use std::{ }; use async_task::Task; -use base::{ - add_fd_flags, warn, AsRawDescriptor, EpollContext, EpollEvents, Event as EventFd, - WatchingEvents, -}; +use base::{add_fd_flags, warn, AsRawDescriptor, EpollContext, EpollEvents, Event, WatchingEvents}; use futures::task::noop_waker; use pin_utils::pin_mut; use remain::sorted; @@ -43,12 +40,12 @@ use crate::{ #[sorted] #[derive(Debug, ThisError)] pub enum Error { - /// Failed to clone the EventFd for waking the executor. - #[error("Failed to clone the EventFd for waking the executor: {0}")] - CloneEventFd(base::Error), - /// Failed to create the EventFd for waking the executor. - #[error("Failed to create the EventFd for waking the executor: {0}")] - CreateEventFd(base::Error), + /// Failed to clone the Event for waking the executor. + #[error("Failed to clone the Event for waking the executor: {0}")] + CloneEvent(base::Error), + /// Failed to create the Event for waking the executor. + #[error("Failed to create the Event for waking the executor: {0}")] + CreateEvent(base::Error), /// Creating a context to wait on FDs failed. #[error("An error creating the fd waiting context: {0}")] CreatingContext(base::Error), @@ -77,8 +74,8 @@ impl From for io::Error { fn from(e: Error) -> Self { use Error::*; match e { - CloneEventFd(e) => e.into(), - CreateEventFd(e) => e.into(), + CloneEvent(e) => e.into(), + CreateEvent(e) => e.into(), DuplicatingFd(e) => e.into(), ExecutorGone => io::Error::new(io::ErrorKind::Other, e), CreatingContext(e) => e.into(), @@ -217,15 +214,15 @@ impl Drop for PendingOperation { // * The executor then polls the non-epoll future that became ready, any epoll futures that // completed, and the notify_task function, which then queues up another read on the eventfd and // the process can repeat. -async fn notify_task(notify: EventFd, raw: Weak) { +async fn notify_task(notify: Event, raw: Weak) { add_fd_flags(notify.as_raw_descriptor(), libc::O_NONBLOCK) - .expect("Failed to set notify EventFd as non-blocking"); + .expect("Failed to set notify Event as non-blocking"); loop { match notify.read() { Ok(_) => {} Err(e) if e.errno() == libc::EWOULDBLOCK => {} - Err(e) => panic!("Unexpected error while reading notify EventFd: {}", e), + Err(e) => panic!("Unexpected error while reading notify Event: {}", e), } if let Some(ex) = raw.upgrade() { @@ -234,7 +231,7 @@ async fn notify_task(notify: EventFd, raw: Weak) { notify.as_raw_descriptor(), WatchingEvents::empty().set_read(), ) - .expect("Failed to add notify EventFd to PollCtx"); + .expect("Failed to add notify Event to PollCtx"); // We don't want to hold an active reference to the executor in the .await below. mem::drop(ex); @@ -247,7 +244,7 @@ async fn notify_task(notify: EventFd, raw: Weak) { match op.await { Ok(()) => {} Err(Error::ExecutorGone) => break, - Err(e) => panic!("Unexpected error while waiting for notify EventFd: {}", e), + Err(e) => panic!("Unexpected error while waiting for notify Event: {}", e), } } else { // The executor is gone so we should also exit. @@ -257,7 +254,7 @@ async fn notify_task(notify: EventFd, raw: Weak) { } // Indicates that the executor is either within or about to make a PollContext::wait() call. When a -// waker sees this value, it will write to the notify EventFd, which will cause the +// waker sees this value, it will write to the notify Event, which will cause the // PollContext::wait() call to return. const WAITING: i32 = 0x1d5b_c019u32 as i32; @@ -273,11 +270,11 @@ struct RawExecutor { ops: Mutex>, blocking_pool: BlockingPool, state: AtomicI32, - notify: EventFd, + notify: Event, } impl RawExecutor { - fn new(notify: EventFd) -> Result { + fn new(notify: Event) -> Result { Ok(RawExecutor { queue: RunnableQueue::new(), poll_ctx: EpollContext::new().map_err(Error::CreatingContext)?, @@ -496,10 +493,10 @@ pub struct FdExecutor { impl FdExecutor { pub fn new() -> Result { - let notify = EventFd::new().map_err(Error::CreateEventFd)?; + let notify = Event::new().map_err(Error::CreateEvent)?; let raw = notify .try_clone() - .map_err(Error::CloneEventFd) + .map_err(Error::CloneEvent) .and_then(RawExecutor::new) .map(Arc::new)?; diff --git a/cros_async/src/sys/unix/uring_source.rs b/cros_async/src/sys/unix/uring_source.rs index 99ae33b8d3..6186c2fef4 100644 --- a/cros_async/src/sys/unix/uring_source.rs +++ b/cros_async/src/sys/unix/uring_source.rs @@ -343,9 +343,9 @@ mod tests { return; } - use base::Event as EventFd; + use base::Event; - async fn write_event(ev: EventFd, wait: EventFd, ex: &URingExecutor) { + async fn write_event(ev: Event, wait: Event, ex: &URingExecutor) { let wait = UringSource::new(wait, ex).unwrap(); ev.write(55).unwrap(); read_u64(&wait).await; @@ -355,7 +355,7 @@ mod tests { read_u64(&wait).await; } - async fn read_events(ev: EventFd, signal: EventFd, ex: &URingExecutor) { + async fn read_events(ev: Event, signal: Event, ex: &URingExecutor) { let source = UringSource::new(ev, ex).unwrap(); assert_eq!(read_u64(&source).await, 55); signal.write(1).unwrap(); @@ -365,8 +365,8 @@ mod tests { signal.write(1).unwrap(); } - let event = EventFd::new().unwrap(); - let signal_wait = EventFd::new().unwrap(); + let event = Event::new().unwrap(); + let signal_wait = Event::new().unwrap(); let ex = URingExecutor::new().unwrap(); let write_task = write_event( event.try_clone().unwrap(), diff --git a/devices/src/bat.rs b/devices/src/bat.rs index 63add059ad..adf2704fb5 100644 --- a/devices/src/bat.rs +++ b/devices/src/bat.rs @@ -354,7 +354,7 @@ impl GoldfishBattery { Ok(v) => v, Err(e) => { error!( - "{}: failed to create kill EventFd pair: {}", + "{}: failed to create kill Event pair: {}", self.debug_label(), e );