#[cfg(target_os = "linux")]
use libc;
use std::os::unix::io::RawFd;
use std::time::{Duration, Instant};
pub fn wait<F>(fd: RawFd, cond: F, timeout: Option<Duration>) -> bool
where
F: Fn() -> bool,
{
if cond() {
return true;
}
let start = Instant::now();
let mut t = timeout;
loop {
let wait_timeout = match t {
Some(duration) => duration.as_millis() as i32,
None => -1,
};
wait_file_changes(fd, wait_timeout);
if let Some(duration) = timeout {
let elapsed = start.elapsed();
if elapsed >= duration {
return false;
}
t = Some(duration - elapsed);
}
if cond() {
return true;
}
}
}
#[cfg(target_os = "linux")]
fn wait_file_changes(fd: RawFd, timeout: i32) -> bool {
let epfd = unsafe { libc::epoll_create(1) as i32 };
let mut ep_event = libc::epoll_event {
events: 0,
u64: fd as u64,
};
let result = unsafe {
libc::epoll_ctl(
epfd,
libc::EPOLL_CTL_ADD,
fd,
&mut ep_event as *mut libc::epoll_event,
) as i32
};
if result == -1 {
std::thread::sleep(Duration::from_millis(100));
return false;
}
let mut buf: [libc::epoll_event; 1] = [ep_event];
let result =
unsafe { libc::epoll_wait(epfd, buf.as_mut_ptr(), buf.len() as i32, timeout) as i32 };
let err = unsafe { libc::close(epfd) as i32 };
if err == -1 {
}
result > 0
}
#[cfg(not(target_os = "linux"))]
fn wait_file_changes(_fd: RawFd, _timeout: i32) -> bool {
std::thread::sleep(Duration::from_millis(100));
false
}