vm-device: generalize BusDevice to use a shared reference

BusDevice trait functions currently holds a mutable reference to self,
and exclusive access is guaranteed by taking a Mutex when dispatched by
the Bus object. However, this prevents individual devices from serving
accesses that do not require an mutable reference or is better served
with different synchronization primitives. We switch Bus to dispatch via
BusDeviceSync, which holds a shared reference, and delegate locking to
the BusDeviceSync trait implementation for Mutex<BusDevice>.

Other changes are made to make use of the dyn BusDeviceSync
trait object.

Signed-off-by: Yuanchu Xie <yuanchu@google.com>
This commit is contained in:
Yuanchu Xie
2024-03-19 18:03:56 -07:00
committed by Liu Wei
parent 026e2c6aa8
commit 954f3dd057
5 changed files with 69 additions and 50 deletions

View File

@@ -14,7 +14,7 @@ use std::any::Any;
use std::collections::HashMap;
use std::ops::DerefMut;
use std::sync::{Arc, Barrier, Mutex};
use vm_device::{Bus, BusDevice};
use vm_device::{Bus, BusDevice, BusDeviceSync};
const VENDOR_ID_INTEL: u16 = 0x8086;
const DEVICE_ID_INTEL_VIRT_PCIE_HOST: u16 = 0x0d57;
@@ -122,7 +122,7 @@ impl PciBus {
pub fn register_mapping(
&self,
dev: Arc<Mutex<dyn BusDevice>>,
dev: Arc<dyn BusDeviceSync>,
#[cfg(target_arch = "x86_64")] io_bus: &Bus,
mmio_bus: &Bus,
bars: Vec<PciBarConfiguration>,