base: event: add Event::reset() API

This allows resetting an Event without waiting on it. Windows already
had this functionality in its EventExt, and we can provide an equivalent
implementation for eventfd on Linux, so promote this function to the
cross-platform Event type.

BUG=b:231344063
TEST=tools/presubmit --all

Change-Id: I6dba3cb2e0b2d702e8a3f0dacf5c25c1dd044a13
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3966480
Reviewed-by: Frederick Mayle <fmayle@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Noah Gold <nkgold@google.com>
This commit is contained in:
Daniel Verkamp 2022-10-19 14:56:13 -07:00 committed by crosvm LUCI
parent 129f66ec51
commit 28d0f67858
4 changed files with 17 additions and 6 deletions

View file

@ -69,6 +69,13 @@ impl Event {
self.0.wait_timeout(timeout)
}
/// Clears the event without blocking.
///
/// If the event is not signaled, this has no effect and returns immediately.
pub fn reset(&self) -> Result<()> {
self.0.reset()
}
/// Clones the event. The event's state is shared between cloned instances.
///
/// The documented caveats for `Event` also apply to a set of cloned instances, e.g., it is

View file

@ -145,6 +145,16 @@ impl PlatformEvent {
Ok(EventWaitResult::Signaled)
}
/// See `Event::reset`.
pub fn reset(&self) -> Result<()> {
// If the eventfd is currently signaled (counter > 0), `wait_timeout()` will `read()` it to
// reset the count. Otherwise (if the eventfd is not signaled), `wait_timeout()` will return
// immediately since we pass a zero duration. We don't care about the EventWaitResult; we
// just want a non-blocking read to reset the counter.
let _: EventWaitResult = self.wait_timeout(Duration::ZERO)?;
Ok(())
}
/// Clones this eventfd, internally creating a new file descriptor. The new eventfd will share
/// the same underlying count within the kernel.
pub fn try_clone(&self) -> Result<PlatformEvent> {

View file

@ -50,7 +50,6 @@ pub(crate) struct PlatformEvent {
}
pub trait EventExt {
fn reset(&self) -> Result<()>;
fn new_with_manual_reset(manual_reset: bool) -> Result<Event>;
fn new_auto_reset() -> Result<Event>;
fn open(name: &str) -> Result<Event>;
@ -58,10 +57,6 @@ pub trait EventExt {
}
impl EventExt for Event {
fn reset(&self) -> Result<()> {
self.0.reset()
}
fn new_with_manual_reset(manual_reset: bool) -> Result<Event> {
PlatformEvent::new_with_manual_reset(manual_reset).map(Event)
}

View file

@ -6,7 +6,6 @@ use std::mem::ManuallyDrop;
use base::AsRawDescriptor;
use base::Event;
use base::EventExt;
use base::FromRawDescriptor;
use crate::AsyncError;