Make the IoEvents type to more complete and robust

This commit is contained in:
Tate, Hongliang Tian 2020-11-08 12:00:18 +08:00 committed by Zongmin.Gu
parent 4260a8defc
commit 7133315f46
3 changed files with 31 additions and 7 deletions

@ -2,13 +2,37 @@ use crate::events::{Event, EventFilter, Notifier, Observer};
use crate::prelude::*; use crate::prelude::*;
bitflags! { bitflags! {
/// I/O Events
#[rustfmt::skip]
pub struct IoEvents: u32 { pub struct IoEvents: u32 {
const IN = 0x001; // = POLLIN const IN = 0x0001; // = POLLIN
const OUT = 0x004; // = POLLOUT const PRI = 0x0002; // = POLLPRI
const PRI = 0x002; // = POLLPRI const OUT = 0x0004; // = POLLOUT
const ERR = 0x008; // = POLLERR const ERR = 0x0008; // = POLLERR
const HUP = 0x0010; // = POLLHUP
const NVAL = 0x0020; // = POLLNVAL
const RDHUP = 0x2000; // = POLLRDHUP const RDHUP = 0x2000; // = POLLRDHUP
const HUP = 0x010; // = POLLHUP }
}
impl IoEvents {
pub fn from_raw(raw: u32) -> Self {
if Self::contains_unrecognizable_bits(raw) {
warn!("contain unknow flags: {:#x}", raw);
}
Self::from_bits_truncate(raw)
}
fn contains_unrecognizable_bits(raw: u32) -> bool {
// Help to detect four valid but mostly useless flags that we do not
// handle, yet: POLLRDNORM, POLLRDBAND, POLLWRNORM, annd POLLWRBAND.
let all_raw = Self::all().to_raw();
(raw & !all_raw) != 0
}
pub fn to_raw(&self) -> u32 {
self.bits()
} }
} }

@ -156,7 +156,7 @@ impl HostFileEpoller {
let mut host_files = self.host_files.lock().unwrap(); let mut host_files = self.host_files.lock().unwrap();
for raw_event in &raw_events[..count] { for raw_event in &raw_events[..count] {
let raw_event = unsafe { raw_event.assume_init() }; let raw_event = unsafe { raw_event.assume_init() };
let io_events = IoEvents::from_bits_truncate(raw_event.events as u32); let io_events = IoEvents::from_raw(raw_event.events as u32);
let host_fd = raw_event.u64 as u32; let host_fd = raw_event.u64 as u32;
let host_file = match host_files.get(&host_fd) { let host_file = match host_files.get(&host_fd) {

@ -55,7 +55,7 @@ impl EpollEvent {
} }
pub fn from_c(c_event: &libc::epoll_event) -> Self { pub fn from_c(c_event: &libc::epoll_event) -> Self {
let mask = IoEvents::from_bits_truncate(c_event.events as u32); let mask = IoEvents::from_raw(c_event.events as u32);
let user_data = c_event.u64; let user_data = c_event.u64;
Self { mask, user_data } Self { mask, user_data }
} }