sound: use thread trait

This commit is contained in:
Stefano Garzarella 2023-03-30 20:56:42 +02:00
parent a1b49a76a7
commit 3ac6c160c1

View File

@ -89,18 +89,30 @@ struct VirtioSoundHeader {
// reading its content from byte array.
unsafe impl ByteValued for VirtioSoundHeader {}
struct VhostUserSoundThread {
trait VhostUserSoundThread {
fn queue_mask(&self) -> u64;
fn set_event_idx(&mut self, enabled: bool);
fn update_memory(&mut self, mem: GuestMemoryAtomic<GuestMemoryMmap>) -> IoResult<()>;
fn handle_event(&self, device_event: u16, vrings: &[VringRwLock]) -> IoResult<bool>;
}
struct VhostUserSoundSingleThread {
mem: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
event_idx: bool,
}
impl VhostUserSoundThread {
impl VhostUserSoundSingleThread {
pub fn new() -> Result<Self> {
Ok(VhostUserSoundThread {
Ok(VhostUserSoundSingleThread {
event_idx: false,
mem: None,
})
}
}
impl VhostUserSoundThread for VhostUserSoundSingleThread {
fn queue_mask(&self) -> u64 {
0xffff_ffff
}
fn set_event_idx(&mut self, enabled: bool) {
self.event_idx = enabled;
@ -127,7 +139,7 @@ impl VhostUserSoundThread {
}
pub(crate) struct VhostUserSoundBackend {
thread: RwLock<VhostUserSoundThread>,
thread: RwLock<Box<dyn VhostUserSoundThread + Sync + Send>>,
config: VirtioSoundConfig,
queues_per_thread: Vec<u64>,
pub(crate) exit_event: EventFd,
@ -136,10 +148,10 @@ pub(crate) struct VhostUserSoundBackend {
impl VhostUserSoundBackend {
pub fn new(_config: SoundConfig) -> Result<Self> {
let queues_per_thread = vec![0b1111];
let thread = RwLock::new(VhostUserSoundThread::new()?);
let thread = Box::new(VhostUserSoundSingleThread::new()?);
Ok(Self {
thread,
thread: RwLock::new(thread),
config: VirtioSoundConfig {
jacks: 0.into(),
streams: 1.into(),