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

via libc-commits libc-commits at lists.llvm.org
Tue Sep 22 04:23:22 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Jeff Bailey (kaladron)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/225365.diff


13 Files Affected:

- (modified) libc/config/linux/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/linux/arm/entrypoints.txt (+1) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1) 
- (modified) libc/include/unistd.yaml (+6) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+13) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/pause.h (+33) 
- (modified) libc/src/unistd/CMakeLists.txt (+7) 
- (modified) libc/src/unistd/linux/CMakeLists.txt (+13) 
- (added) libc/src/unistd/linux/pause.cpp (+32) 
- (added) libc/src/unistd/pause.h (+25) 
- (modified) libc/test/src/unistd/CMakeLists.txt (+21) 
- (added) libc/test/src/unistd/pause_test.cpp (+65) 


``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f55ac4b16cfd2..88521f41a4cab 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 1602255c6559c..8d020a9ef40d4 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 921de1c649fb4..9f21c4c271ef2 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 165484987f16d..1087e710542ec 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 890a4ae21f191..1f3f064c50dd5 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 f5f28f51f49f6..e4f34682abe58 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 0000000000000..c5dcb01bb86ea
--- /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 97bae9a643f34..236488c8170f4 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 d4e03819612b9..75049bf2bd7c0 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 0000000000000..8ad6b121f5729
--- /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 0000000000000..23b1fc49fbebd
--- /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 17960278bd3f7..2e44a1c2ddc90 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 0000000000000..b1466a0b5c72f
--- /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);
+}

``````````

</details>


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


More information about the libc-commits mailing list