Skip to content

Commit 797a457

Browse files
authored
Implement pause. (#948)
1 parent 7242fdc commit 797a457

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/backend/libc/event/syscalls.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,11 @@ pub(crate) fn port_send(
181181
) -> io::Result<()> {
182182
unsafe { ret(c::port_send(borrowed_fd(port), events, userdata)) }
183183
}
184+
185+
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
186+
pub(crate) fn pause() {
187+
let r = unsafe { libc::pause() };
188+
let errno = libc_errno::errno().0;
189+
debug_assert_eq!(r, -1);
190+
debug_assert_eq!(errno, libc::EINTR);
191+
}

src/backend/linux_raw/event/syscalls.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::backend::c;
99
#[cfg(feature = "alloc")]
1010
use crate::backend::conv::pass_usize;
1111
use crate::backend::conv::{
12-
by_ref, c_int, c_uint, raw_fd, ret, ret_owned_fd, ret_usize, slice_mut, zero,
12+
by_ref, c_int, c_uint, raw_fd, ret, ret_error, ret_owned_fd, ret_usize, slice_mut, zero,
1313
};
1414
use crate::event::{epoll, EventfdFlags, PollFd};
1515
use crate::fd::{BorrowedFd, OwnedFd};
@@ -131,3 +131,22 @@ pub(crate) fn epoll_wait(
131131
pub(crate) fn eventfd(initval: u32, flags: EventfdFlags) -> io::Result<OwnedFd> {
132132
unsafe { ret_owned_fd(syscall_readonly!(__NR_eventfd2, c_uint(initval), flags)) }
133133
}
134+
135+
#[inline]
136+
pub(crate) fn pause() {
137+
unsafe {
138+
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
139+
let error = ret_error(syscall_readonly!(
140+
__NR_ppoll,
141+
zero(),
142+
zero(),
143+
zero(),
144+
zero()
145+
));
146+
147+
#[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]
148+
let error = ret_error(syscall_readonly!(__NR_pause));
149+
150+
debug_assert_eq!(error, io::Errno::INTR);
151+
}
152+
}

src/event/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
mod eventfd;
1010
#[cfg(all(feature = "alloc", bsd))]
1111
pub mod kqueue;
12+
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
13+
mod pause;
1214
mod poll;
1315
#[cfg(solarish)]
1416
pub mod port;
@@ -22,4 +24,6 @@ pub use crate::backend::event::epoll;
2224
target_os = "espidf"
2325
))]
2426
pub use eventfd::{eventfd, EventfdFlags};
27+
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
28+
pub use pause::*;
2529
pub use poll::{poll, PollFd, PollFlags};

src/event/pause.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::backend;
2+
3+
/// `pause()`
4+
///
5+
/// The POSIX `pause` interface returns an error code, but the only thing
6+
/// `pause` does is sleep until interrupted by a signal, so it always
7+
/// returns the same thing with the same error code, so in rustix, the
8+
/// return value is omitted.
9+
///
10+
/// # References
11+
/// - [POSIX]
12+
/// - [Linux]
13+
/// - [Apple]
14+
/// - [FreeBSD]
15+
/// - [NetBSD]
16+
/// - [OpenBSD]
17+
/// - [DragonFly BSD]
18+
/// - [illumos]
19+
///
20+
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html
21+
/// [Linux]: https://man7.org/linux/man-pages/man2/pause.2.html
22+
/// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/pause.3.html
23+
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=pause&sektion=3
24+
/// [NetBSD]: https://man.netbsd.org/pause.3
25+
/// [OpenBSD]: https://man.openbsd.org/pause.3
26+
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=pause&section=3
27+
/// [illumos]: https://illumos.org/man/2/pause
28+
#[inline]
29+
pub fn pause() {
30+
backend::event::syscalls::pause()
31+
}

0 commit comments

Comments
 (0)