Skip to content

Commit 02347b1

Browse files
authored
Merge pull request #36 from hw0lff/fix-epoll
wait: Fix broken epoll fd setup, previously causing 100% CPU usage
2 parents 7cb3080 + 8fb3e1c commit 02347b1

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

src/wait.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,39 @@ where
6969
/// Wrapper for `libc::epoll_wait`
7070
#[cfg(target_os = "linux")]
7171
fn wait_file_changes(fd: RawFd, timeout: i32) -> bool {
72-
let mut buf: [libc::epoll_event; 10] = [libc::epoll_event { events: 0, u64: 0 }; 10];
72+
let epfd = unsafe { libc::epoll_create(1) as i32 };
73+
let mut ep_event = libc::epoll_event {
74+
events: 0,
75+
u64: fd as u64,
76+
};
7377

7478
let result = unsafe {
75-
libc::epoll_wait(
79+
libc::epoll_ctl(
80+
epfd,
81+
libc::EPOLL_CTL_ADD,
7682
fd,
77-
buf.as_mut_ptr() as *mut libc::epoll_event,
78-
buf.len() as i32,
79-
timeout,
83+
&mut ep_event as *mut libc::epoll_event,
8084
) as i32
8185
};
8286

87+
if result == -1 {
88+
// cannot register fd as epoll fd
89+
// just wait for 100ms
90+
std::thread::sleep(Duration::from_millis(100));
91+
return false;
92+
}
93+
94+
let mut buf: [libc::epoll_event; 1] = [ep_event];
95+
96+
// number of file descriptors ready for the requested I/O operation
97+
let result =
98+
unsafe { libc::epoll_wait(epfd, buf.as_mut_ptr(), buf.len() as i32, timeout) as i32 };
99+
100+
let err = unsafe { libc::close(epfd) as i32 };
101+
if err == -1 {
102+
// epfd cannot be closed, at least we tried
103+
}
104+
83105
result > 0
84106
}
85107

0 commit comments

Comments
 (0)