runtime-rs: refactor increase_attach_count with do_increase_count

Try to reduce duplicated code in increase_attach_count with public
new function do_increase_count.

Fixes: #8738

Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
Alex Lyn 2024-04-01 11:11:52 +08:00
parent fff64f1c3e
commit 4f0fab938d
3 changed files with 6 additions and 37 deletions

View File

@ -20,6 +20,7 @@ use crate::{
device::{
pci_path::PciPath,
topology::{do_add_pcie_endpoint, PCIeTopology},
util::do_increase_count,
Device, DeviceType, PCIeDevice,
},
register_pcie_device, unregister_pcie_device, update_pcie_device, Hypervisor as hypervisor,
@ -522,18 +523,7 @@ impl Device for VfioDevice {
}
async fn increase_attach_count(&mut self) -> Result<bool> {
match self.attach_count {
0 => {
// do real attach
self.attach_count += 1;
Ok(false)
}
std::u64::MAX => Err(anyhow!("device was attached too many times")),
_ => {
self.attach_count += 1;
Ok(true)
}
}
do_increase_count(&mut self.attach_count)
}
async fn decrease_attach_count(&mut self) -> Result<bool> {

View File

@ -9,7 +9,7 @@ use async_trait::async_trait;
use super::VhostUserConfig;
use crate::{
device::{topology::PCIeTopology, Device, DeviceType},
device::{topology::PCIeTopology, util::do_increase_count, Device, DeviceType},
Hypervisor as hypervisor,
};
@ -104,18 +104,7 @@ impl Device for VhostUserBlkDevice {
}
async fn increase_attach_count(&mut self) -> Result<bool> {
match self.attach_count {
0 => {
// do real attach
self.attach_count += 1;
Ok(false)
}
std::u64::MAX => Err(anyhow!("device was attached too many times")),
_ => {
self.attach_count += 1;
Ok(true)
}
}
do_increase_count(&mut self.attach_count)
}
async fn decrease_attach_count(&mut self) -> Result<bool> {

View File

@ -6,6 +6,7 @@
use crate::device::pci_path::PciPath;
use crate::device::topology::PCIeTopology;
use crate::device::util::do_increase_count;
use crate::device::Device;
use crate::device::DeviceType;
use crate::Hypervisor as hypervisor;
@ -135,18 +136,7 @@ impl Device for BlockDevice {
}
async fn increase_attach_count(&mut self) -> Result<bool> {
match self.attach_count {
0 => {
// do real attach
self.attach_count += 1;
Ok(false)
}
std::u64::MAX => Err(anyhow!("device was attached too many times")),
_ => {
self.attach_count += 1;
Ok(true)
}
}
do_increase_count(&mut self.attach_count)
}
async fn decrease_attach_count(&mut self) -> Result<bool> {