[libc-commits] [libc] [libc] Implement pause (PR #225365)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Tue Sep 22 04:09:30 PDT 2026


https://github.com/kaladron created https://github.com/llvm/llvm-project/pull/225365

Add the linux_syscalls::pause wrapper using SYS_ppoll, along with the unistd pause entrypoint and unit test on Linux.

Assisted-by: Automated tooling, human reviewed.

>From 37e48c78c765edfc905d3061dbaf63abf5b7bb9b Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Tue, 22 Sep 2026 12:07:22 +0100
Subject: [PATCH] [libc] Implement pause

Add the linux_syscalls::pause wrapper using SYS_ppoll, along with the
unistd pause entrypoint and unit test on Linux.

Assisted-by: Automated tooling, human reviewed.
---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/arm/entrypoints.txt         |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/include/unistd.yaml                      |  6 ++
 .../linux/syscall_wrappers/CMakeLists.txt     | 13 ++++
 .../OSUtil/linux/syscall_wrappers/pause.h     | 33 ++++++++++
 libc/src/unistd/CMakeLists.txt                |  7 ++
 libc/src/unistd/linux/CMakeLists.txt          | 13 ++++
 libc/src/unistd/linux/pause.cpp               | 32 +++++++++
 libc/src/unistd/pause.h                       | 25 +++++++
 libc/test/src/unistd/CMakeLists.txt           | 21 ++++++
 libc/test/src/unistd/pause_test.cpp           | 65 +++++++++++++++++++
 13 files changed, 219 insertions(+)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/pause.h
 create mode 100644 libc/src/unistd/linux/pause.cpp
 create mode 100644 libc/src/unistd/pause.h
 create mode 100644 libc/test/src/unistd/pause_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f55ac4b16cfd26..88521f41a4cabe 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -456,6 +456,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.lseek
     libc.src.unistd.nice
     libc.src.unistd.pathconf
+    libc.src.unistd.pause
     libc.src.unistd.pipe
     libc.src.unistd.pipe2
     libc.src.unistd.pread
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 1602255c6559cc..8d020a9ef40d42 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -270,6 +270,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.linkat
     libc.src.unistd.lseek
     libc.src.unistd.nice
+    libc.src.unistd.pause
     libc.src.unistd.pipe
     libc.src.unistd.pipe2
     libc.src.unistd.read
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 921de1c649fb42..9f21c4c271ef21 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -481,6 +481,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.lseek
     libc.src.unistd.nice
     libc.src.unistd.pathconf
+    libc.src.unistd.pause
     libc.src.unistd.pipe
     libc.src.unistd.pipe2
     libc.src.unistd.pread
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 165484987f16d7..1087e710542ec0 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -495,6 +495,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.lseek
     libc.src.unistd.nice
     libc.src.unistd.pathconf
+    libc.src.unistd.pause
     libc.src.unistd.pipe
     libc.src.unistd.pipe2
     libc.src.unistd.pread
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 890a4ae21f1919..1f3f064c50dd5c 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -417,6 +417,12 @@ functions:
     arguments:
       - type: const char *
       - type: int
+  - name: pause
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: void
   - name: pipe
     standards:
       - posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index f5f28f51f49f60..e4f34682abe583 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1356,3 +1356,16 @@ add_header_library(
     libc.src.__support.CPP.bit
     libc.src.__support.error_or
 )
+
+add_header_library(
+  pause
+  HDRS
+    pause.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.src.__support.OSUtil.osutil
+)
+
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/pause.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/pause.h
new file mode 100644
index 00000000000000..c5dcb01bb86ea8
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/pause.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for pause.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_PAUSE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_PAUSE_H
+
+#include "src/__support/OSUtil/linux/syscall.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> pause() {
+  return syscall_checked<int>(SYS_ppoll, nullptr, 0, nullptr, nullptr, 0);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_PAUSE_H
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 97bae9a643f345..236488c8170f46 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -301,6 +301,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.pathconf
 )
 
+add_entrypoint_object(
+  pause
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.pause
+)
+
 add_entrypoint_object(
   pipe
   ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index d4e03819612b92..75049bf2bd7c05 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -510,6 +510,19 @@ add_object_library(
     libc.hdr.types.struct_statfs
 )
 
+add_entrypoint_object(
+  pause
+  SRCS
+    pause.cpp
+  HDRS
+    ../pause.h
+  DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.pause
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   pipe
   SRCS
diff --git a/libc/src/unistd/linux/pause.cpp b/libc/src/unistd/linux/pause.cpp
new file mode 100644
index 00000000000000..8ad6b121f57294
--- /dev/null
+++ b/libc/src/unistd/linux/pause.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of pause.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/pause.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/pause.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, pause, ()) {
+  ErrorOr<int> ret = linux_syscalls::pause();
+  if (!ret) {
+    libc_errno = ret.error();
+    return -1;
+  }
+  return ret.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/pause.h b/libc/src/unistd/pause.h
new file mode 100644
index 00000000000000..23b1fc49fbebdd
--- /dev/null
+++ b/libc/src/unistd/pause.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for pause.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_PAUSE_H
+#define LLVM_LIBC_SRC_UNISTD_PAUSE_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pause();
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_PAUSE_H
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 17960278bd3f75..2e44a1c2ddc90d 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -873,6 +873,27 @@ add_libc_test(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
+add_libc_test(
+  pause_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    pause_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.signal_macros
+    libc.hdr.sys_time_macros
+    libc.hdr.types.struct_itimerval
+    libc.hdr.types.struct_sigaction
+    libc.src.__support.CPP.scope
+    libc.src.signal.sigaction
+    libc.src.signal.sigemptyset
+    libc.src.sys.time.setitimer
+    libc.src.unistd.pause
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
 add_libc_test(
   getopt_test
   SUITE
diff --git a/libc/test/src/unistd/pause_test.cpp b/libc/test/src/unistd/pause_test.cpp
new file mode 100644
index 00000000000000..b1466a0b5c72fc
--- /dev/null
+++ b/libc/test/src/unistd/pause_test.cpp
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for pause.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/signal_macros.h"
+#include "hdr/sys_time_macros.h"
+#include "hdr/types/struct_itimerval.h"
+#include "hdr/types/struct_sigaction.h"
+#include "src/__support/CPP/scope.h"
+#include "src/signal/sigaction.h"
+#include "src/signal/sigemptyset.h"
+#include "src/sys/time/setitimer.h"
+#include "src/unistd/pause.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcPauseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+static volatile bool alarm_fired = false;
+
+extern "C" void pause_sigalrm_handler(int) { alarm_fired = true; }
+
+TEST_F(LlvmLibcPauseTest, InterruptedBySignal) {
+  alarm_fired = false;
+
+  struct sigaction sa = {};
+  sa.sa_handler = pause_sigalrm_handler;
+  ASSERT_THAT(LIBC_NAMESPACE::sigemptyset(&sa.sa_mask), Succeeds(0));
+  sa.sa_flags = 0;
+
+  struct sigaction old_sa = {};
+  ASSERT_THAT(LIBC_NAMESPACE::sigaction(SIGALRM, &sa, &old_sa), Succeeds(0));
+  auto restore_sigaction = LIBC_NAMESPACE::cpp::scope_exit(
+      [&] { LIBC_NAMESPACE::sigaction(SIGALRM, &old_sa, nullptr); });
+
+  // Use a short repeating interval timer so that even if a signal arrives
+  // before pause() blocks, the subsequent tick wakes pause() immediately.
+  struct itimerval timer = {};
+  timer.it_value.tv_sec = 0;
+  timer.it_value.tv_usec = 10000;
+  timer.it_interval.tv_sec = 0;
+  timer.it_interval.tv_usec = 10000;
+
+  struct itimerval old_timer = {};
+  ASSERT_THAT(LIBC_NAMESPACE::setitimer(ITIMER_REAL, &timer, &old_timer),
+              Succeeds(0));
+  auto restore_timer = LIBC_NAMESPACE::cpp::scope_exit(
+      [&] { LIBC_NAMESPACE::setitimer(ITIMER_REAL, &old_timer, nullptr); });
+
+  EXPECT_THAT(LIBC_NAMESPACE::pause(), Fails(EINTR));
+  EXPECT_TRUE(alarm_fired);
+}



More information about the libc-commits mailing list