mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-12-22 13:45:20 +00:00
event_monitor: make it possible to subscribe
to Monitor
Signed-off-by: Omer Faruk Bayram <omer.faruk@sartura.hr>
This commit is contained in:
parent
654ac49bd6
commit
e02efe9ba0
@ -9,6 +9,7 @@ use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
static mut MONITOR: Option<MonitorHandle> = None;
|
||||
@ -24,6 +25,23 @@ struct Event<'a> {
|
||||
pub struct Monitor {
|
||||
pub rx: flume::Receiver<String>,
|
||||
pub file: File,
|
||||
pub broadcast: Vec<flume::Sender<Arc<String>>>,
|
||||
}
|
||||
|
||||
impl Monitor {
|
||||
pub fn new(rx: flume::Receiver<String>, file: File) -> Self {
|
||||
Self {
|
||||
rx,
|
||||
file,
|
||||
broadcast: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscribe(&mut self) -> flume::Receiver<Arc<String>> {
|
||||
let (tx, rx) = flume::unbounded();
|
||||
self.broadcast.push(tx);
|
||||
rx
|
||||
}
|
||||
}
|
||||
|
||||
struct MonitorHandle {
|
||||
@ -56,7 +74,7 @@ pub fn set_monitor(file: File) -> io::Result<Monitor> {
|
||||
|
||||
set_file_nonblocking(&file)?;
|
||||
let (tx, rx) = flume::unbounded();
|
||||
let monitor = Monitor { rx, file };
|
||||
let monitor = Monitor::new(rx, file);
|
||||
|
||||
// SAFETY: MONITOR is None. Nobody else can hold a reference to it.
|
||||
unsafe {
|
||||
|
@ -319,8 +319,14 @@ pub fn start_event_monitor_thread(
|
||||
|
||||
std::panic::catch_unwind(AssertUnwindSafe(move || {
|
||||
while let Ok(event) = monitor.rx.recv() {
|
||||
let event = Arc::new(event);
|
||||
|
||||
monitor.file.write_all(event.as_bytes().as_ref()).ok();
|
||||
monitor.file.write_all(b"\n\n").ok();
|
||||
|
||||
for tx in monitor.broadcast.iter() {
|
||||
tx.send(event.clone()).ok();
|
||||
}
|
||||
}
|
||||
}))
|
||||
.map_err(|_| {
|
||||
|
Loading…
Reference in New Issue
Block a user