[libc-commits] [libc] [libc] Implement sigwait (Part of #236167) (PR #237572)
via libc-commits
libc-commits at lists.llvm.org
Sat Oct 31 07:56:20 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Anshitha Yeruva (anshithayeruva)
<details>
<summary>Changes</summary>
This patch implements the POSIX `sigwait` function in LLVM libc, along with unit tests.
Part of #<!-- -->236167.
---
Full diff: https://github.com/llvm/llvm-project/pull/237572.diff
12 Files Affected:
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1-1)
- (modified) libc/config/linux/riscv/entrypoints.txt (+1)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
- (modified) libc/include/signal.yaml (+7)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+14-1)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigtimedwait.h (+38)
- (modified) libc/src/signal/CMakeLists.txt (+6-1)
- (modified) libc/src/signal/linux/CMakeLists.txt (+14)
- (added) libc/src/signal/linux/sigwait.cpp (+35)
- (added) libc/src/signal/sigwait.h (+21)
- (modified) libc/test/src/signal/CMakeLists.txt (+16)
- (added) libc/test/src/signal/sigwait_test.cpp (+39)
``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 09b1b9080e0874..cc228e4227d675 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1395,7 +1395,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigismember
libc.src.signal.signal
libc.src.signal.sigprocmask
-
+ libc.src.signal.sigwait
# spawn.h entrypoints
libc.src.spawn.posix_spawn
libc.src.spawn.posix_spawn_file_actions_addclose
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 69189b37ddddd4..1c73eaf0bbe357 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1581,6 +1581,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigismember
libc.src.signal.signal
libc.src.signal.sigprocmask
+ libc.src.signal.sigwait
# spawn.h entrypoints
libc.src.spawn.posix_spawn
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 746e712ec160e6..b7f200fbf9cb26 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1607,6 +1607,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigismember
libc.src.signal.signal
libc.src.signal.sigprocmask
+ libc.src.signal.sigwait
# spawn.h entrypoints
libc.src.spawn.posix_spawn
diff --git a/libc/include/signal.yaml b/libc/include/signal.yaml
index b763da7eeb6025..6424dbe515e90e 100644
--- a/libc/include/signal.yaml
+++ b/libc/include/signal.yaml
@@ -127,6 +127,13 @@ functions:
- type: int
- type: const sigset_t *__restrict
- type: sigset_t *__restrict
+ - name: sigwait
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: const sigset_t *__restrict
+ - type: int *__restrict
- name: pthread_sigmask
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 05eb6f0fa6f776..40b3081f07afc2 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -647,7 +647,20 @@ add_header_library(
libc.hdr.types.sigset_t
libc.include.sys_syscall
)
-
+add_header_library(
+ rt_sigtimedwait
+ HDRS
+ rt_sigtimedwait.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.hdr.types.siginfo_t
+ libc.hdr.types.sigset_t
+ libc.hdr.types.struct_timespec
+ libc.include.sys_syscall
+)
add_header_library(
raise
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigtimedwait.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigtimedwait.h
new file mode 100644
index 00000000000000..e0b3cd7e398f99
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigtimedwait.h
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 rt_sigtimedwait.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGTIMEDWAIT_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGTIMEDWAIT_H
+
+#include "hdr/types/siginfo_t.h"
+#include "hdr/types/sigset_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> rt_sigtimedwait(const sigset_t *set, siginfo_t *info,
+ const struct timespec *timeout) {
+ return syscall_checked<int>(SYS_rt_sigtimedwait, set, info, timeout,
+ sizeof(sigset_t));
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGTIMEDWAIT_H
\ No newline at end of file
diff --git a/libc/src/signal/CMakeLists.txt b/libc/src/signal/CMakeLists.txt
index 8da94d1eea85fd..40140effc9c625 100644
--- a/libc/src/signal/CMakeLists.txt
+++ b/libc/src/signal/CMakeLists.txt
@@ -36,7 +36,12 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.sigprocmask
)
-
+add_entrypoint_object(
+ sigwait
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.sigwait
+)
add_entrypoint_object(
pthread_sigmask
ALIAS
diff --git a/libc/src/signal/linux/CMakeLists.txt b/libc/src/signal/linux/CMakeLists.txt
index 77898cf9aab636..1108f3e8e28dc6 100644
--- a/libc/src/signal/linux/CMakeLists.txt
+++ b/libc/src/signal/linux/CMakeLists.txt
@@ -94,6 +94,20 @@ add_entrypoint_object(
libc.src.__support.OSUtil.linux.syscall_wrappers.rt_sigprocmask
libc.src.errno.errno
)
+add_entrypoint_object(
+ sigwait
+ SRCS
+ sigwait.cpp
+ HDRS
+ ../sigwait.h
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.types.siginfo_t
+ libc.hdr.types.sigset_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.rt_sigtimedwait
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
add_entrypoint_object(
pthread_sigmask
diff --git a/libc/src/signal/linux/sigwait.cpp b/libc/src/signal/linux/sigwait.cpp
new file mode 100644
index 00000000000000..79989003f9e065
--- /dev/null
+++ b/libc/src/signal/linux/sigwait.cpp
@@ -0,0 +1,35 @@
+//===-- Linux implementation of sigwait -----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/signal/sigwait.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/siginfo_t.h"
+#include "hdr/types/sigset_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/rt_sigtimedwait.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, sigwait,
+ (const sigset_t *__restrict set, int *__restrict sig)) {
+ siginfo_t info;
+ ErrorOr<int> result = 0;
+ do {
+ result = linux_syscalls::rt_sigtimedwait(set, &info, nullptr);
+ } while (!result.has_value() && result.error() == EINTR);
+
+ if (!result.has_value())
+ return result.error();
+
+ *sig = info.si_signo;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
\ No newline at end of file
diff --git a/libc/src/signal/sigwait.h b/libc/src/signal/sigwait.h
new file mode 100644
index 00000000000000..77cbe5308039cb
--- /dev/null
+++ b/libc/src/signal/sigwait.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sigwait function --------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SIGNAL_SIGWAIT_H
+#define LLVM_LIBC_SRC_SIGNAL_SIGWAIT_H
+
+#include "hdr/types/sigset_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int sigwait(const sigset_t *__restrict set, int *__restrict sig);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SIGNAL_SIGWAIT_H
\ No newline at end of file
diff --git a/libc/test/src/signal/CMakeLists.txt b/libc/test/src/signal/CMakeLists.txt
index 287977e2762236..8617245a11cad4 100644
--- a/libc/test/src/signal/CMakeLists.txt
+++ b/libc/test/src/signal/CMakeLists.txt
@@ -53,6 +53,22 @@ add_libc_test(
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_test(
+ sigwait_test
+ SUITE
+ libc-signal-tests
+ SRCS
+ sigwait_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.signal_macros
+ libc.hdr.types.sigset_t
+ libc.src.signal.raise
+ libc.src.signal.sigaddset
+ libc.src.signal.sigemptyset
+ libc.src.signal.sigprocmask
+ libc.src.signal.sigwait
+)
add_libc_test(
pthread_sigmask_test
diff --git a/libc/test/src/signal/sigwait_test.cpp b/libc/test/src/signal/sigwait_test.cpp
new file mode 100644
index 00000000000000..593433e4e1bea6
--- /dev/null
+++ b/libc/test/src/signal/sigwait_test.cpp
@@ -0,0 +1,39 @@
+//===-- Unittests for sigwait ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/signal/sigwait.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/signal_macros.h"
+#include "hdr/types/sigset_t.h"
+#include "src/signal/raise.h"
+#include "src/signal/sigaddset.h"
+#include "src/signal/sigemptyset.h"
+#include "src/signal/sigprocmask.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcSigwaitTest, WaitPendingSignal) {
+ // Block SIGUSR1 so raising it leaves it pending without terminating.
+ sigset_t mask;
+ ASSERT_EQ(LIBC_NAMESPACE::sigemptyset(&mask), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&mask, SIGUSR1), 0);
+
+ sigset_t oldset;
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_BLOCK, &mask, &oldset), 0);
+
+ // Raise SIGUSR1. It is now queued/pending for this thread.
+ ASSERT_EQ(LIBC_NAMESPACE::raise(SIGUSR1), 0);
+
+ // sigwait should immediately catch and dequeue SIGUSR1.
+ int sig = 0;
+ ASSERT_EQ(LIBC_NAMESPACE::sigwait(&mask, &sig), 0);
+ ASSERT_EQ(sig, SIGUSR1);
+
+ // Restore the original signal mask.
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, &oldset, nullptr), 0);
+}
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/237572
More information about the libc-commits
mailing list