#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
use std::cell::RefCell;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::result;
use std::sync::{Arc, Mutex};
use vmm_sys_util::errno::Error as Errno;
pub use vmm_sys_util::epoll::EventSet;
mod epoll;
mod events;
mod manager;
mod subscribers;
#[doc(hidden)]
#[cfg(feature = "test_utilities")]
pub mod utilities;
pub use events::{EventOps, Events};
pub use manager::{EventManager, MAX_READY_EVENTS_CAPACITY};
#[cfg(feature = "remote_endpoint")]
mod endpoint;
#[cfg(feature = "remote_endpoint")]
pub use endpoint::RemoteEndpoint;
#[derive(Debug, Eq, PartialEq)]
pub enum Error {
#[cfg(feature = "remote_endpoint")]
ChannelSend,
#[cfg(feature = "remote_endpoint")]
ChannelRecv,
#[cfg(feature = "remote_endpoint")]
EventFd(Errno),
Epoll(Errno),
FdAlreadyRegistered,
InvalidId,
InvalidCapacity,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
#[cfg(feature = "remote_endpoint")]
Error::ChannelSend => write!(
f,
"event_manager: failed to send message to remote endpoint"
),
#[cfg(feature = "remote_endpoint")]
Error::ChannelRecv => write!(
f,
"event_manager: failed to receive message from remote endpoint"
),
#[cfg(feature = "remote_endpoint")]
Error::EventFd(e) => write!(
f,
"event_manager: failed to manage EventFd file descriptor: {e}"
),
Error::Epoll(e) => write!(
f,
"event_manager: failed to manage epoll file descriptor: {e}"
),
Error::FdAlreadyRegistered => write!(
f,
"event_manager: file descriptor has already been registered"
),
Error::InvalidId => write!(f, "event_manager: invalid subscriber Id"),
Error::InvalidCapacity => write!(f, "event_manager: invalid ready_list capacity"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
#[cfg(feature = "remote_endpoint")]
Error::ChannelSend => None,
#[cfg(feature = "remote_endpoint")]
Error::ChannelRecv => None,
#[cfg(feature = "remote_endpoint")]
Error::EventFd(e) => Some(e),
Error::Epoll(e) => Some(e),
Error::FdAlreadyRegistered => None,
Error::InvalidId => None,
Error::InvalidCapacity => None,
}
}
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct SubscriberId(u64);
pub trait EventSubscriber {
fn process(&self, events: Events, ops: &mut EventOps);
fn init(&self, ops: &mut EventOps);
}
pub trait MutEventSubscriber {
fn process(&mut self, events: Events, ops: &mut EventOps);
fn init(&mut self, ops: &mut EventOps);
}
pub trait SubscriberOps {
type Subscriber: MutEventSubscriber;
fn add_subscriber(&mut self, subscriber: Self::Subscriber) -> SubscriberId;
fn remove_subscriber(&mut self, subscriber_id: SubscriberId) -> Result<Self::Subscriber>;
fn subscriber_mut(&mut self, subscriber_id: SubscriberId) -> Result<&mut Self::Subscriber>;
fn event_ops(&mut self, subscriber_id: SubscriberId) -> Result<EventOps>;
}
impl<T: EventSubscriber + ?Sized> EventSubscriber for Arc<T> {
fn process(&self, events: Events, ops: &mut EventOps) {
self.deref().process(events, ops);
}
fn init(&self, ops: &mut EventOps) {
self.deref().init(ops);
}
}
impl<T: EventSubscriber + ?Sized> MutEventSubscriber for Arc<T> {
fn process(&mut self, events: Events, ops: &mut EventOps) {
self.deref().process(events, ops);
}
fn init(&mut self, ops: &mut EventOps) {
self.deref().init(ops);
}
}
impl<T: EventSubscriber + ?Sized> EventSubscriber for Rc<T> {
fn process(&self, events: Events, ops: &mut EventOps) {
self.deref().process(events, ops);
}
fn init(&self, ops: &mut EventOps) {
self.deref().init(ops);
}
}
impl<T: EventSubscriber + ?Sized> MutEventSubscriber for Rc<T> {
fn process(&mut self, events: Events, ops: &mut EventOps) {
self.deref().process(events, ops);
}
fn init(&mut self, ops: &mut EventOps) {
self.deref().init(ops);
}
}
impl<T: MutEventSubscriber + ?Sized> EventSubscriber for RefCell<T> {
fn process(&self, events: Events, ops: &mut EventOps) {
self.borrow_mut().process(events, ops);
}
fn init(&self, ops: &mut EventOps) {
self.borrow_mut().init(ops);
}
}
impl<T: MutEventSubscriber + ?Sized> MutEventSubscriber for RefCell<T> {
fn process(&mut self, events: Events, ops: &mut EventOps) {
self.borrow_mut().process(events, ops);
}
fn init(&mut self, ops: &mut EventOps) {
self.borrow_mut().init(ops);
}
}
impl<T: MutEventSubscriber + ?Sized> EventSubscriber for Mutex<T> {
fn process(&self, events: Events, ops: &mut EventOps) {
self.lock().unwrap().process(events, ops);
}
fn init(&self, ops: &mut EventOps) {
self.lock().unwrap().init(ops);
}
}
impl<T: MutEventSubscriber + ?Sized> MutEventSubscriber for Mutex<T> {
fn process(&mut self, events: Events, ops: &mut EventOps) {
self.get_mut().unwrap().process(events, ops);
}
fn init(&mut self, ops: &mut EventOps) {
self.get_mut().unwrap().init(ops);
}
}
impl<T: EventSubscriber + ?Sized> EventSubscriber for Box<T> {
fn process(&self, events: Events, ops: &mut EventOps) {
self.deref().process(events, ops);
}
fn init(&self, ops: &mut EventOps) {
self.deref().init(ops);
}
}
impl<T: MutEventSubscriber + ?Sized> MutEventSubscriber for Box<T> {
fn process(&mut self, events: Events, ops: &mut EventOps) {
self.deref_mut().process(events, ops);
}
fn init(&mut self, ops: &mut EventOps) {
self.deref_mut().init(ops);
}
}