Skip to content

Commit

Permalink
Implement pause. (#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode authored Nov 29, 2023
1 parent 7242fdc commit 797a457
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/backend/libc/event/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,11 @@ pub(crate) fn port_send(
) -> io::Result<()> {
unsafe { ret(c::port_send(borrowed_fd(port), events, userdata)) }
}

#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
pub(crate) fn pause() {
let r = unsafe { libc::pause() };
let errno = libc_errno::errno().0;
debug_assert_eq!(r, -1);
debug_assert_eq!(errno, libc::EINTR);
}
21 changes: 20 additions & 1 deletion src/backend/linux_raw/event/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::backend::c;
#[cfg(feature = "alloc")]
use crate::backend::conv::pass_usize;
use crate::backend::conv::{
by_ref, c_int, c_uint, raw_fd, ret, ret_owned_fd, ret_usize, slice_mut, zero,
by_ref, c_int, c_uint, raw_fd, ret, ret_error, ret_owned_fd, ret_usize, slice_mut, zero,
};
use crate::event::{epoll, EventfdFlags, PollFd};
use crate::fd::{BorrowedFd, OwnedFd};
Expand Down Expand Up @@ -131,3 +131,22 @@ pub(crate) fn epoll_wait(
pub(crate) fn eventfd(initval: u32, flags: EventfdFlags) -> io::Result<OwnedFd> {
unsafe { ret_owned_fd(syscall_readonly!(__NR_eventfd2, c_uint(initval), flags)) }
}

#[inline]
pub(crate) fn pause() {
unsafe {
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
let error = ret_error(syscall_readonly!(
__NR_ppoll,
zero(),
zero(),
zero(),
zero()
));

#[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]
let error = ret_error(syscall_readonly!(__NR_pause));

debug_assert_eq!(error, io::Errno::INTR);
}
}
4 changes: 4 additions & 0 deletions src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
mod eventfd;
#[cfg(all(feature = "alloc", bsd))]
pub mod kqueue;
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
mod pause;
mod poll;
#[cfg(solarish)]
pub mod port;
Expand All @@ -22,4 +24,6 @@ pub use crate::backend::event::epoll;
target_os = "espidf"
))]
pub use eventfd::{eventfd, EventfdFlags};
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
pub use pause::*;
pub use poll::{poll, PollFd, PollFlags};
31 changes: 31 additions & 0 deletions src/event/pause.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::backend;

/// `pause()`
///
/// The POSIX `pause` interface returns an error code, but the only thing
/// `pause` does is sleep until interrupted by a signal, so it always
/// returns the same thing with the same error code, so in rustix, the
/// return value is omitted.
///
/// # References
/// - [POSIX]
/// - [Linux]
/// - [Apple]
/// - [FreeBSD]
/// - [NetBSD]
/// - [OpenBSD]
/// - [DragonFly BSD]
/// - [illumos]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html
/// [Linux]: https://man7.org/linux/man-pages/man2/pause.2.html
/// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/pause.3.html
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=pause&sektion=3
/// [NetBSD]: https://man.netbsd.org/pause.3
/// [OpenBSD]: https://man.openbsd.org/pause.3
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=pause&section=3
/// [illumos]: https://illumos.org/man/2/pause
#[inline]
pub fn pause() {
backend::event::syscalls::pause()
}

0 comments on commit 797a457

Please sign in to comment.