qdl: make interfaces Sync

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2021-08-14 00:44:55 +04:00
parent 998a8e7ddd
commit fb517a13db
6 changed files with 18 additions and 16 deletions

View File

@ -3,7 +3,7 @@ use std::default::Default;
use std::os::unix::net::UnixStream;
use std::str::FromStr;
use std::sync::mpsc::{self, Receiver, SendError};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::{os::unix::io::AsRawFd, thread};
use zbus::{dbus_interface, dbus_proxy, zvariant::Fd};
@ -294,7 +294,7 @@ impl Audio {
.build()
.unwrap();
let mut s = zbus::ObjectServer::new(&c);
let listener = AudioOutListener::new(tx);
let listener = AudioOutListener::new(Mutex::new(tx));
let err = listener.err();
s.at("/org/qemu/Display1/AudioOutListener", listener)
.unwrap();
@ -326,7 +326,7 @@ impl Audio {
.build()
.unwrap();
let mut s = zbus::ObjectServer::new(&c);
let listener = AudioInListener::new(tx);
let listener = AudioInListener::new(Mutex::new(tx));
let err = listener.err();
s.at("/org/qemu/Display1/AudioInListener", listener)
.unwrap();

View File

@ -2,7 +2,7 @@ use once_cell::sync::OnceCell;
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::convert::TryFrom;
use std::sync::mpsc::{channel, Sender};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use zbus::{dbus_interface, dbus_proxy, zvariant::ObjectPath};
use zvariant::derive::Type;
@ -53,7 +53,7 @@ pub enum ClipboardEvent {
Request {
selection: ClipboardSelection,
mimes: Vec<String>,
tx: Sender<Result<(String, Vec<u8>)>>,
tx: Mutex<Sender<Result<(String, Vec<u8>)>>>,
},
}
@ -94,7 +94,7 @@ impl<E: 'static + EventSender<Event = ClipboardEvent>> ClipboardListener<E> {
self.send(ClipboardEvent::Request {
selection,
mimes,
tx,
tx: Mutex::new(tx),
});
rx.recv()
.map_err(|e| zbus::fdo::Error::Failed(format!("Request recv failed: {}", e)))?

View File

@ -1,6 +1,7 @@
use std::convert::TryFrom;
use std::os::unix::net::UnixStream;
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::Mutex;
use std::{os::unix::io::AsRawFd, thread};
use zbus::{
@ -122,7 +123,7 @@ impl Console {
.build()
.unwrap();
let mut s = zbus::ObjectServer::new(&c);
let listener = ConsoleListener::new(tx, wait_rx);
let listener = ConsoleListener::new(Mutex::new(tx), wait_rx);
let err = listener.err();
s.at("/org/qemu/Display1/Listener", listener).unwrap();
loop {

View File

@ -3,7 +3,7 @@ use std::ops::Drop;
use std::os::unix::io::IntoRawFd;
use std::os::unix::io::{AsRawFd, RawFd};
use std::sync::mpsc::{Receiver, RecvError, SendError};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use derivative::Derivative;
use zbus::{dbus_interface, zvariant::Fd};
@ -94,7 +94,7 @@ pub enum ConsoleEvent {
#[derive(Debug)]
pub(crate) struct ConsoleListener<E: EventSender<Event = ConsoleEvent>> {
tx: E,
wait_rx: Receiver<()>,
wait_rx: Mutex<Receiver<()>>,
err: Arc<OnceCell<SendError<ConsoleEvent>>>,
}
@ -188,7 +188,7 @@ impl<E: EventSender<Event = ConsoleEvent>> ConsoleListener<E> {
pub(crate) fn new(tx: E, wait_rx: Receiver<()>) -> Self {
ConsoleListener {
tx,
wait_rx,
wait_rx: Mutex::new(wait_rx),
err: Default::default(),
}
}
@ -200,7 +200,7 @@ impl<E: EventSender<Event = ConsoleEvent>> ConsoleListener<E> {
}
fn wait(&mut self) -> Result<(), RecvError> {
self.wait_rx.recv()
self.wait_rx.lock().unwrap().recv()
}
pub fn err(&self) -> Arc<OnceCell<SendError<ConsoleEvent>>> {

View File

@ -1,21 +1,22 @@
use std::sync::mpsc::{SendError, Sender};
use std::sync::Mutex;
pub(crate) trait EventSender: Send {
pub(crate) trait EventSender: Send + Sync {
type Event;
fn send_event(&self, t: Self::Event) -> Result<(), SendError<Self::Event>>;
}
impl<T: Send> EventSender for Sender<T> {
impl<T: Send + Sync> EventSender for Mutex<Sender<T>> {
type Event = T;
fn send_event(&self, t: Self::Event) -> Result<(), SendError<Self::Event>> {
self.send(t)
self.lock().unwrap().send(t)
}
}
#[cfg(feature = "glib")]
impl<T: Send> EventSender for glib::Sender<T> {
impl<T: Send + Sync> EventSender for glib::Sender<T> {
type Event = T;
fn send_event(&self, t: Self::Event) -> Result<(), SendError<Self::Event>> {

View File

@ -107,7 +107,7 @@ impl Handler {
Err(qdl::Error::Failed(format!("{}", e)))
}
};
let _ = tx.send(reply);
let _ = tx.lock().unwrap().send(reply);
});
}
}