switch to using the poll stuff from filedescriptor 0.3
This commit is contained in:
1 parent
c18dd96472
commit
97daa8b246
6 files changed
+23
-157
No files matched your search
+1
-1
@@ -19,7 +19,7 @@ crossbeam-channel = "0.3"
|
||||
dirs = "1.0"
|
||||
downcast-rs = "1.0"
|
||||
euclid = "0.19"
|
||||
filedescriptor = "0.2"
|
||||
filedescriptor = "0.3"
|
||||
pretty_env_logger = "0.3"
|
||||
failure = "0.1"
|
||||
failure_derive = "0.1"
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
use crate::server::UnixStream;
|
||||
use crossbeam_channel::{unbounded as channel, Receiver, Sender, TryRecvError};
|
||||
use failure::{format_err, Fallible};
|
||||
use filedescriptor::{AsRawFileDescriptor, FileDescriptor, RawFileDescriptor};
|
||||
#[cfg(unix)]
|
||||
use libc::{poll, pollfd, POLLERR, POLLIN};
|
||||
use filedescriptor::*;
|
||||
use std::cell::RefCell;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpStream;
|
||||
#[cfg(windows)]
|
||||
use winapi::um::winsock2::{WSAPoll as poll, POLLERR, POLLIN, WSAPOLLFD as pollfd};
|
||||
|
||||
pub trait ReadAndWrite: std::io::Read + std::io::Write + Send + AsPollFd {
|
||||
fn set_non_blocking(&self, non_blocking: bool) -> Fallible<()>;
|
||||
@@ -127,5 +123,5 @@ impl AsPollFd for UnixStream {
|
||||
}
|
||||
|
||||
pub fn poll_for_read(pfd: &mut [pollfd]) {
|
||||
unsafe { poll(pfd.as_mut_ptr(), pfd.len() as _, -1 as _) };
|
||||
poll(pfd, None).ok();
|
||||
}
|
||||
+1
-1
@@ -16,7 +16,7 @@ bitflags = "1.0"
|
||||
cassowary = "0.3"
|
||||
derive_builder = "0.7"
|
||||
failure = "0.1"
|
||||
filedescriptor = "0.2"
|
||||
filedescriptor = "0.3"
|
||||
fnv = "1.0"
|
||||
image = "0.21"
|
||||
libc = "0.2"
|
||||
|
||||
@@ -8,8 +8,6 @@ use num::{self, NumCast};
|
||||
use std::fmt::Display;
|
||||
use std::time::Duration;
|
||||
|
||||
#[cfg(unix)]
|
||||
pub mod poll;
|
||||
#[cfg(unix)]
|
||||
pub mod unix;
|
||||
#[cfg(windows)]
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
// macOS has a broken poll(2) implementation, so we introduce a layer to deal with that here
|
||||
|
||||
use libc::pollfd;
|
||||
use std::time::Duration;
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
pub fn poll(pfd: &mut [pollfd], duration: Option<Duration>) -> Result<usize, std::io::Error> {
|
||||
let poll_result = unsafe {
|
||||
libc::poll(
|
||||
pfd.as_mut_ptr(),
|
||||
pfd.len() as _,
|
||||
duration
|
||||
.map(|wait| wait.as_millis() as libc::c_int)
|
||||
.unwrap_or(-1),
|
||||
)
|
||||
};
|
||||
if poll_result < 0 {
|
||||
Err(std::io::Error::last_os_error())
|
||||
} else {
|
||||
Ok(poll_result as usize)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
mod macos {
|
||||
use super::*;
|
||||
use libc::{fd_set, timeval, FD_ISSET, FD_SET, FD_SETSIZE, FD_ZERO, POLLERR, POLLIN, POLLOUT};
|
||||
use std::os::unix::io::RawFd;
|
||||
|
||||
struct FdSet {
|
||||
set: fd_set,
|
||||
}
|
||||
|
||||
fn check_fd(fd: RawFd) {
|
||||
assert!(fd >= 0);
|
||||
assert!((fd as usize) < FD_SETSIZE);
|
||||
}
|
||||
|
||||
impl FdSet {
|
||||
pub fn new() -> Self {
|
||||
unsafe {
|
||||
let mut set = std::mem::uninitialized();
|
||||
FD_ZERO(&mut set);
|
||||
Self { set }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, fd: RawFd) {
|
||||
check_fd(fd);
|
||||
unsafe {
|
||||
FD_SET(fd, &mut self.set);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn contains(&mut self, fd: RawFd) -> bool {
|
||||
check_fd(fd);
|
||||
unsafe { FD_ISSET(fd, &mut self.set) }
|
||||
}
|
||||
}
|
||||
|
||||
fn materialize(set: &mut Option<FdSet>) -> &mut FdSet {
|
||||
set.get_or_insert_with(FdSet::new)
|
||||
}
|
||||
|
||||
fn set_ptr(set: &mut Option<FdSet>) -> *mut fd_set {
|
||||
set.as_mut()
|
||||
.map(|s| &mut s.set as *mut _)
|
||||
.unwrap_or_else(std::ptr::null_mut)
|
||||
}
|
||||
|
||||
fn is_set(set: &mut Option<FdSet>, fd: RawFd) -> bool {
|
||||
set.as_mut().map(|s| s.contains(fd)).unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn poll(pfd: &mut [pollfd], duration: Option<Duration>) -> Result<usize, std::io::Error> {
|
||||
let mut read_set = None;
|
||||
let mut write_set = None;
|
||||
let mut exception_set = None;
|
||||
let mut nfds = 0;
|
||||
|
||||
for item in pfd.iter_mut() {
|
||||
item.revents = 0;
|
||||
|
||||
nfds = nfds.max(item.fd);
|
||||
|
||||
if item.events & POLLIN != 0 {
|
||||
materialize(&mut read_set).add(item.fd);
|
||||
}
|
||||
if item.events & POLLOUT != 0 {
|
||||
materialize(&mut write_set).add(item.fd);
|
||||
}
|
||||
materialize(&mut exception_set).add(item.fd);
|
||||
}
|
||||
|
||||
let mut timeout = duration.map(|d| timeval {
|
||||
tv_sec: d.as_secs() as _,
|
||||
tv_usec: d.as_micros() as _,
|
||||
});
|
||||
|
||||
let res = unsafe {
|
||||
libc::select(
|
||||
nfds + 1,
|
||||
set_ptr(&mut read_set),
|
||||
set_ptr(&mut write_set),
|
||||
set_ptr(&mut exception_set),
|
||||
timeout
|
||||
.as_mut()
|
||||
.map(|t| t as *mut _)
|
||||
.unwrap_or_else(std::ptr::null_mut),
|
||||
)
|
||||
};
|
||||
|
||||
if res < 0 {
|
||||
Err(std::io::Error::last_os_error())
|
||||
} else {
|
||||
for item in pfd.iter_mut() {
|
||||
if is_set(&mut read_set, item.fd) {
|
||||
item.revents |= POLLIN;
|
||||
}
|
||||
if is_set(&mut write_set, item.fd) {
|
||||
item.revents |= POLLOUT;
|
||||
}
|
||||
if is_set(&mut exception_set, item.fd) {
|
||||
item.revents |= POLLERR;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(res as usize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub use macos::poll;
|
||||
@@ -1,6 +1,6 @@
|
||||
use failure::{bail, format_err, Error, Fallible};
|
||||
use filedescriptor::FileDescriptor;
|
||||
use libc::{self, pollfd, winsize, POLLIN};
|
||||
use filedescriptor::{poll, pollfd, FileDescriptor, POLLIN};
|
||||
use libc::{self, winsize};
|
||||
use signal_hook::{self, SigId};
|
||||
use std::collections::VecDeque;
|
||||
use std::fs::OpenOptions;
|
||||
@@ -410,18 +410,24 @@ impl Terminal for UnixTerminal {
|
||||
},
|
||||
];
|
||||
|
||||
if let Err(err) = super::poll::poll(&mut pfd, wait) {
|
||||
if err.kind() == ErrorKind::Interrupted {
|
||||
// SIGWINCH may have been the source of the interrupt.
|
||||
// Check for that now so that we reduce the latency of
|
||||
// processing the resize
|
||||
if let Some(resize) = self.caught_sigwinch()? {
|
||||
return Ok(Some(resize));
|
||||
if let Err(err) = poll(&mut pfd, wait) {
|
||||
return match err.downcast::<std::io::Error>() {
|
||||
Ok(err) => {
|
||||
if err.kind() == ErrorKind::Interrupted {
|
||||
// SIGWINCH may have been the source of the interrupt.
|
||||
// Check for that now so that we reduce the latency of
|
||||
// processing the resize
|
||||
if let Some(resize) = self.caught_sigwinch()? {
|
||||
Ok(Some(resize))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Err(format_err!("poll(2) error: {}", err))
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(None);
|
||||
}
|
||||
return Err(format_err!("poll(2) error: {}", err));
|
||||
Err(err) => Err(format_err!("poll(2) error: {}", err)),
|
||||
};
|
||||
};
|
||||
|
||||
if pfd[0].revents != 0 {
|
||||
|
||||
Reference in new issue
Block a user