1 // Copyright 2021 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 //! Virtio device async helper functions. 6 7 use anyhow::Context; 8 use anyhow::Result; 9 use base::Event; 10 use cros_async::EventAsync; 11 use cros_async::Executor; 12 13 use super::Interrupt; 14 use super::SignalableInterrupt; 15 16 /// Async task that waits for a signal from `event`. Once this event is readable, exit. Exiting 17 /// this future will cause the main loop to break and the worker thread to exit. await_and_exit(ex: &Executor, event: Event) -> Result<()>18pub async fn await_and_exit(ex: &Executor, event: Event) -> Result<()> { 19 let event_async = EventAsync::new(event, ex).context("failed to create EventAsync")?; 20 let _ = event_async.next_val().await; 21 Ok(()) 22 } 23 24 /// Async task that resamples the status of the interrupt when the guest sends a request by 25 /// signalling the resample event associated with the interrupt. handle_irq_resample(ex: &Executor, interrupt: Interrupt) -> Result<()>26pub async fn handle_irq_resample(ex: &Executor, interrupt: Interrupt) -> Result<()> { 27 // Clone resample_evt if interrupt has one. 28 let resample_evt = if let Some(resample_evt) = interrupt.get_resample_evt() { 29 let resample_evt = resample_evt 30 .try_clone() 31 .context("resample_evt.try_clone() failed")?; 32 Some(EventAsync::new(resample_evt, ex).context("failed to create async resample event")?) 33 } else { 34 None 35 }; 36 37 if let Some(resample_evt) = resample_evt { 38 loop { 39 let _ = resample_evt 40 .next_val() 41 .await 42 .context("failed to read resample event")?; 43 interrupt.do_interrupt_resample(); 44 } 45 } else { 46 // No resample event; park the future. 47 futures::future::pending::<()>().await; 48 } 49 Ok(()) 50 } 51