Skip to content

Commit 7538306

Browse files
committed
epoll: confirm that timerfd wakes up even on write of 0; add nicer libc utils
1 parent 339caa8 commit 7538306

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

tests/pass-dep/libc/libc-epoll-no-blocking.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,8 @@ fn test_epoll_eventfd() {
262262
let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC;
263263
let fd = unsafe { libc::eventfd(0, flags) };
264264

265-
// Write to the eventfd instance.
266-
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes();
267-
let res = unsafe { libc_utils::write_all(fd, sized_8_data.as_ptr() as *const libc::c_void, 8) };
268-
assert_eq!(res, 8);
265+
// Write 1 to the eventfd instance.
266+
libc_utils::write_all_from_slice(fd, &1_u64.to_ne_bytes()).unwrap();
269267

270268
// Create an epoll instance.
271269
let epfd = unsafe { libc::epoll_create1(0) };
@@ -281,18 +279,15 @@ fn test_epoll_eventfd() {
281279
let expected_value = u64::try_from(fd).unwrap();
282280
check_epoll_wait::<8>(epfd, &[(expected_event, expected_value)]);
283281

284-
// Write to the eventfd again.
285-
let res = unsafe { libc_utils::write_all(fd, sized_8_data.as_ptr() as *const libc::c_void, 8) };
286-
assert_eq!(res, 8);
282+
// Write 0 to the eventfd.
283+
libc_utils::write_all_from_slice(fd, &0_u64.to_ne_bytes()).unwrap();
287284

288285
// This does not change the status, so we should get no event.
289286
// However, Linux performs a spurious wakeup.
290287
check_epoll_wait::<8>(epfd, &[(expected_event, expected_value)]);
291288

292289
// Read from the eventfd.
293-
let mut buf = [0u8; 8];
294-
let res = unsafe { libc_utils::read_all(fd, buf.as_mut_ptr().cast(), 8) };
295-
assert_eq!(res, 8);
290+
libc_utils::read_all_into_array::<8>(fd).unwrap();
296291

297292
// This consumes the event, so the read status is gone. However, deactivation
298293
// does not trigger an event.

tests/utils/libc.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ pub unsafe fn read_all(
2222
return read_so_far as libc::ssize_t;
2323
}
2424

25+
#[track_caller]
26+
pub fn read_all_into_array<const N: usize>(fd: libc::c_int) -> Result<[u8; N], libc::ssize_t> {
27+
let mut buf = [0; N];
28+
let res = unsafe { read_all(fd, buf.as_mut_ptr().cast(), buf.len()) };
29+
if res >= 0 {
30+
assert_eq!(res as usize, buf.len());
31+
Ok(buf)
32+
} else {
33+
Err(res)
34+
}
35+
}
36+
2537
pub unsafe fn write_all(
2638
fd: libc::c_int,
2739
buf: *const libc::c_void,
@@ -39,3 +51,14 @@ pub unsafe fn write_all(
3951
}
4052
return written_so_far as libc::ssize_t;
4153
}
54+
55+
#[track_caller]
56+
pub fn write_all_from_slice(fd: libc::c_int, buf: &[u8]) -> Result<(), libc::ssize_t> {
57+
let res = unsafe { write_all(fd, buf.as_ptr().cast(), buf.len()) };
58+
if res >= 0 {
59+
assert_eq!(res as usize, buf.len());
60+
Ok(())
61+
} else {
62+
Err(res)
63+
}
64+
}

0 commit comments

Comments
 (0)