Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ async-tokio = ["futures", "tokio", "mio-evented"]

[dependencies]
futures = { version = "0.3", optional = true }
nix = "0.26"
mio = { version = "1", optional = true, features = ["os-ext"]}
nix = { version = "0.31", features = ["event"] }
mio = { version = "1", optional = true, features = ["os-ext"] }
tokio = { version = "1", optional = true, features = ["net"] }

[dev-dependencies]
Expand Down
55 changes: 23 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ use std::io;
use std::io::prelude::*;
#[cfg(any(target_os = "linux", target_os = "android", feature = "async-tokio"))]
use std::io::SeekFrom;
#[cfg(not(target_os = "wasi"))]
use std::os::unix::prelude::*;
#[cfg(any(feature = "async-tokio", feature = "mio-evented"))]
use std::os::fd::{AsRawFd, RawFd};
use std::path::Path;
use std::{fs, fs::File};

Expand All @@ -65,9 +65,7 @@ use mio::event::Source;
#[cfg(feature = "mio-evented")]
use mio::unix::SourceFd;
#[cfg(any(target_os = "linux", target_os = "android"))]
use nix::sys::epoll::*;
#[cfg(not(target_os = "wasi"))]
use nix::unistd::close;
use nix::sys::epoll::{Epoll, EpollCreateFlags, EpollEvent, EpollFlags, EpollTimeout};
#[cfg(feature = "async-tokio")]
use std::task::Poll;
#[cfg(feature = "async-tokio")]
Expand Down Expand Up @@ -514,7 +512,9 @@ fn extract_pin_fom_path_test() {
#[derive(Debug)]
pub struct PinPoller {
pin_num: u64,
epoll_fd: RawFd,
#[cfg(any(target_os = "linux", target_os = "android"))]
epoll: Epoll,
#[cfg(any(target_os = "linux", target_os = "android"))]
devfile: File,
}
#[cfg(not(target_os = "wasi"))]
Expand All @@ -531,21 +531,16 @@ impl PinPoller {
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn new(pin_num: u64) -> Result<PinPoller> {
let devfile: File = File::open(format!("/sys/class/gpio/gpio{}/value", pin_num))?;
let devfile_fd = devfile.as_raw_fd();
let epoll_fd = epoll_create()?;
let mut event = EpollEvent::new(EpollFlags::EPOLLPRI | EpollFlags::EPOLLET, 0u64);

match epoll_ctl(epoll_fd, EpollOp::EpollCtlAdd, devfile_fd, &mut event) {
Ok(_) => Ok(PinPoller {
pin_num,
devfile,
epoll_fd,
}),
Err(err) => {
let _ = close(epoll_fd); // cleanup
Err(::std::convert::From::from(err))
}
}
let epoll = Epoll::new(EpollCreateFlags::empty())?;
epoll.add(
&devfile,
EpollEvent::new(EpollFlags::EPOLLPRI | EpollFlags::EPOLLET, 0u64),
)?;
Ok(PinPoller {
pin_num,
devfile,
epoll,
})
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
Expand Down Expand Up @@ -575,7 +570,13 @@ impl PinPoller {
flush_input_from_file(&mut self.devfile, 255)?;
let dummy_event = EpollEvent::new(EpollFlags::EPOLLPRI | EpollFlags::EPOLLET, 0u64);
let mut events: [EpollEvent; 1] = [dummy_event];
let cnt = epoll_wait(self.epoll_fd, &mut events, timeout_ms)?;
// LOGIC: Previous versions essentially passed `timeout_ms as c_int` to the
// kernel without validation. We now validate that the value is in range
// -1..=i32::MAX. A future major version of this library should update
// the `poll` signature.
let timeout = EpollTimeout::try_from(timeout_ms as i128)
.map_err(|err| Error::Io(io::Error::other(err)))?;
let cnt = self.epoll.wait(&mut events, timeout)?;
Ok(match cnt {
0 => None, // timeout
_ => Some(get_value_from_file(&mut self.devfile)?),
Expand All @@ -588,16 +589,6 @@ impl PinPoller {
}
}

#[cfg(not(target_os = "wasi"))]
impl Drop for PinPoller {
fn drop(&mut self) {
// we implement drop to close the underlying epoll fd as
// it does not implement drop itself. This is similar to
// how mio works
close(self.epoll_fd).unwrap(); // panic! if close files
}
}

#[cfg(feature = "mio-evented")]
#[derive(Debug)]
pub struct AsyncPinPoller {
Expand Down
Loading